cybersecurity · intermediate · ~15 min · safe pentest lab

Write a safe allowlist validator

Cap branching on user input to a known-good set.

Challenge

Accept untrusted input only when it exactly matches a known-good entry — the building block for safe command dispatch off a fixed menu.

Task

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.

Input

  • 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.

Output

Returns int: 1 if s matches an allowed entry exactly, else 0.

Example

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)

Edge cases

  • The empty string returns 0 (unless it is itself an allowed entry).
  • Matching is exact and case-sensitive.
  • A NULL s or allowed returns 0.

Input format

A candidate string s, an array allowed of n strings, and the count n.

Output format

An int: 1 if s exactly matches an allowed entry, else 0.

Constraints

Exact, case-sensitive matching.

Starter code

#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.