cybersecurity · beginner · ~15 min
Strict allow-list + length validation in a single pass.
Accept a token only if its length is in range and every character is on a strict allowlist — the cheap pre-check that stops malformed tokens before they reach anything else.
Implement int valid_token(const char *s, size_t min_len, size_t max_len) that returns 1 when all of these hold, and 0 otherwise:
s is non-NULL,strlen(s) is within [min_len, max_len] (inclusive), ands is in the allowlist [A-Za-z0-9_-].s: a NUL-terminated token the grader provides (may be NULL).min_len, max_len: the inclusive length window.Returns 1 if s is valid, 0 otherwise.
valid_token("abc123_XYZ-99", 8, 32) -> 1
valid_token("short", 8, 32) -> 0 (too short)
valid_token("has space here", 1, 64) -> 0 (space not allowed)
valid_token("ok!", 1, 64) -> 0 ('!' not allowed)
valid_token("", 0, 64) -> 1 (empty allowed when min_len == 0)
valid_token(NULL, 0, 64) -> 0
min_len == 0.NULL pointer: return 0.API keys, session tokens, and CSRF cookies look random to humans but follow strict rules (e.g. [A-Za-z0-9_-], 32-64 bytes). Validating that a token only contains allow-listed characters before passing it on to anything else is the cheapest defence against injection.
A NUL-terminated token s (may be NULL) and an inclusive length window min_len..max_len.
1 if s is in range and all characters are [A-Za-z0-9_-]; otherwise 0.
Allowlist [A-Za-z0-9_-]; single pass; no allocations; guard NULL.
#include <stddef.h>
int valid_token(const char *s, size_t min_len, size_t max_len) { /* TODO */ return 0; }
Deny-listing (if (c == ';') return 0;) — infinite holes. Forgetting NULL guard.
Empty input with min==0; min > max; NULL pointer.
O(strlen).
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.