cybersecurity · intermediate · ~15 min · safe pentest lab
Cap branching on user input to a known-good set.
Accept untrusted input only when it exactly matches a known-good entry — the building block for safe command dispatch off a fixed menu.
Implement int in_allowlist(const char *s, const char *const *allowed, size_t n) that returns 1 if s is exactly equal (case-sensitive) to one of the n strings in allowed, else 0.
s: the NUL-terminated candidate string.allowed: an array of n NUL-terminated allowed strings (the harness passes a fixed list such as {"list", "show", "describe"}).n: the number of entries in allowed.Returns int: 1 if s matches an allowed entry exactly, else 0.
allowed = {"list", "show", "describe"}
in_allowlist("list", allowed, 3) -> 1
in_allowlist("describe", allowed, 3) -> 1
in_allowlist("delete", allowed, 3) -> 0
in_allowlist("List", allowed, 3) -> 0 (case-sensitive)
s or allowed returns 0.A candidate string s, an array allowed of n strings, and the count n.
An int: 1 if s exactly matches an allowed entry, else 0.
Exact, case-sensitive matching.
#include <stdio.h>
#include <string.h>
#include <stddef.h>
int in_allowlist(const char *s, const char *const *allowed, size_t n) {
/* TODO */
return 0;
}
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.