cybersecurity · intermediate · ~15 min

Does the path contain ..?

Detect path-traversal components.

Challenge

Flag a path that contains a .. sequence — a basic path-traversal indicator that lets a path escape its intended directory.

Task

Implement int has_dotdot(const char *path) that returns 1 if path contains the substring "..", and 0 otherwise.

Input

  • path: a NUL-terminated path string the grader provides.

Output

Returns 1 if ".." appears anywhere in path, 0 otherwise.

Example

has_dotdot("/var/www/../etc/passwd")   ->   1
has_dotdot("/var/www/index.html")      ->   0

Edge cases

  • This is a coarse substring check; a path with no .. returns 0.

Input format

A NUL-terminated path string path.

Output format

1 if path contains the substring ".."; otherwise 0.

Constraints

Substring test for "..".

Starter code

#include <string.h>

int has_dotdot(const char *path) {
    /* TODO */
    return 0;
}

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.