cybersecurity · beginner · ~12 min · safe pentest lab
Structural validation of a dotted, character-restricted token.
Check a JWT's shape before any signature verification: exactly three base64url segments joined by dots. Malformed tokens get rejected early.
Implement int jwt_is_wellformed(const char *tok) that returns 1 if tok is structurally a JWT, else 0.
tok is well-formed when:
.;A-Z, a-z, 0-9, -, _ (no +, /, or =).tok: the token string, or NULL. The grader passes fixed strings.Returns int: 1 if well-formed, else 0 (including for NULL).
jwt_is_wellformed("eyJhbGc.eyJzdWI.sIg-_9") -> 1
jwt_is_wellformed("aaa.bbb") -> 0 (only two segments)
jwt_is_wellformed("aaa..ccc") -> 0 (empty middle segment)
jwt_is_wellformed("aa!.bb.cc") -> 0 (illegal char)
jwt_is_wellformed("a.b.c.d") -> 0 (four segments)
jwt_is_wellformed("a.b.") -> 0 (empty final segment)
jwt_is_wellformed(NULL) -> 0
NULL are invalid.+, /, =) are not base64url and reject the token.alg.Before you ever verify a JWT's signature you must confirm its shape: exactly three base64url segments. Malformed tokens should be rejected early.
A token string tok, or NULL.
An int: 1 if tok is exactly three non-empty base64url segments, else 0.
base64url = A-Za-z0-9-_; exactly two dots; no empty segments; NULL is 0.
int jwt_is_wellformed(const char *tok) {
/* TODO */
(void)tok;
return 0;
}
Allowing empty segments. Accepting standard base64 chars (+/=). Not requiring exactly two dots.
Trailing dot (empty last segment). Four parts. Empty string.
O(n).
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.