Skip to content

Commit

Permalink
Move path validation tests before charset tests
Browse files Browse the repository at this point in the history
The charset validation tests do exhaustive loops over all possible
codepoints.  This is very useful for ensuring correct behavior on all
inputs, but is slow enough (several seconds) to be annoying during
development.  Moving the path validation tests (which are very fast)
before the charset validation tests allows developers to conclude that
they have passed much sooner, since if they fail the code will crash
quickly.

(cherry picked from commit a656698)
  • Loading branch information
DemiMarie authored and marmarek committed Jun 22, 2024
1 parent d3834f3 commit 6e7c758
Showing 1 changed file with 48 additions and 47 deletions.
95 changes: 48 additions & 47 deletions qrexec-lib/validator-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,54 @@ int main(int argc, char **argv)
(void)argc;
(void)argv;
assert(qubes_pure_validate_file_name((uint8_t *)u8"simple_safe_filename.txt"));

// Directory traversal checks
assert(!qubes_pure_validate_file_name((uint8_t *)".."));
assert(!qubes_pure_validate_file_name((uint8_t *)"../.."));
assert(!qubes_pure_validate_file_name((uint8_t *)"a/.."));
assert(!qubes_pure_validate_file_name((uint8_t *)"a/../b"));
assert(!qubes_pure_validate_file_name((uint8_t *)"/"));
assert(!qubes_pure_validate_file_name((uint8_t *)"//"));
assert(!qubes_pure_validate_file_name((uint8_t *)"///"));
assert(!qubes_pure_validate_file_name((uint8_t *)"/a"));
assert(!qubes_pure_validate_file_name((uint8_t *)"//a"));
assert(!qubes_pure_validate_file_name((uint8_t *)"///a"));

// No repeated slashes
assert(!qubes_pure_validate_file_name((uint8_t *)"a//b"));

// No "." as a path component
assert(!qubes_pure_validate_file_name((uint8_t *)"."));
assert(!qubes_pure_validate_file_name((uint8_t *)"a/."));
assert(!qubes_pure_validate_file_name((uint8_t *)"./a"));
assert(!qubes_pure_validate_file_name((uint8_t *)"a/./a"));

// No ".." as a path component
assert(!qubes_pure_validate_file_name((uint8_t *)".."));
assert(!qubes_pure_validate_file_name((uint8_t *)"a/.."));
assert(!qubes_pure_validate_file_name((uint8_t *)"../a"));
assert(!qubes_pure_validate_file_name((uint8_t *)"a/../a"));

// Looks like "." or ".." but is not
assert(qubes_pure_validate_file_name((const uint8_t *)".a"));
assert(qubes_pure_validate_file_name((const uint8_t *)"..a"));

// Symbolic links
// Top level cannot be symlink
assert(!qubes_pure_validate_symbolic_link((const uint8_t *)"a", (const uint8_t *)"b"));
// Symbolic links cannot escape
assert(!qubes_pure_validate_symbolic_link((const uint8_t *)"a/b", (const uint8_t *)"../a"));
assert(!qubes_pure_validate_symbolic_link((const uint8_t *)"a/b", (const uint8_t *)"../a/b/c"));
assert(!qubes_pure_validate_symbolic_link((const uint8_t *)"a/b/c", (const uint8_t *)"../../a"));
assert(qubes_pure_validate_symbolic_link((const uint8_t *)"a/b", (const uint8_t *)"a"));
assert(qubes_pure_validate_symbolic_link((const uint8_t *)"a/b/c", (const uint8_t *)"../a"));
// Absolute symlinks are rejected
assert(!qubes_pure_validate_symbolic_link((const uint8_t *)"a/b/c", (const uint8_t *)"/a"));
// Symlinks may end in "..".
assert(qubes_pure_validate_symbolic_link((const uint8_t *)"a/b/c", (const uint8_t *)".."));
// Symlinks may end in "/".
assert(qubes_pure_validate_symbolic_link((const uint8_t *)"a/b/c", (const uint8_t *)"a/"));

// Greek letters are safe
assert(qubes_pure_validate_file_name((uint8_t *)u8"\u03b2.txt"));
assert(qubes_pure_validate_file_name((uint8_t *)u8"\u03b1.txt"));
Expand Down Expand Up @@ -174,51 +222,4 @@ int main(int argc, char **argv)
assert(j < 0x10FFFFE);
}
}

// Directory traversal checks
assert(!qubes_pure_validate_file_name((uint8_t *)".."));
assert(!qubes_pure_validate_file_name((uint8_t *)"../.."));
assert(!qubes_pure_validate_file_name((uint8_t *)"a/.."));
assert(!qubes_pure_validate_file_name((uint8_t *)"a/../b"));
assert(!qubes_pure_validate_file_name((uint8_t *)"/"));
assert(!qubes_pure_validate_file_name((uint8_t *)"//"));
assert(!qubes_pure_validate_file_name((uint8_t *)"///"));
assert(!qubes_pure_validate_file_name((uint8_t *)"/a"));
assert(!qubes_pure_validate_file_name((uint8_t *)"//a"));
assert(!qubes_pure_validate_file_name((uint8_t *)"///a"));

// No repeated slashes
assert(!qubes_pure_validate_file_name((uint8_t *)"a//b"));

// No "." as a path component
assert(!qubes_pure_validate_file_name((uint8_t *)"."));
assert(!qubes_pure_validate_file_name((uint8_t *)"a/."));
assert(!qubes_pure_validate_file_name((uint8_t *)"./a"));
assert(!qubes_pure_validate_file_name((uint8_t *)"a/./a"));

// No ".." as a path component
assert(!qubes_pure_validate_file_name((uint8_t *)".."));
assert(!qubes_pure_validate_file_name((uint8_t *)"a/.."));
assert(!qubes_pure_validate_file_name((uint8_t *)"../a"));
assert(!qubes_pure_validate_file_name((uint8_t *)"a/../a"));

// Looks like "." or ".." but is not
assert(qubes_pure_validate_file_name((const uint8_t *)".a"));
assert(qubes_pure_validate_file_name((const uint8_t *)"..a"));

// Symbolic links
// Top level cannot be symlink
assert(!qubes_pure_validate_symbolic_link((const uint8_t *)"a", (const uint8_t *)"b"));
// Symbolic links cannot escape
assert(!qubes_pure_validate_symbolic_link((const uint8_t *)"a/b", (const uint8_t *)"../a"));
assert(!qubes_pure_validate_symbolic_link((const uint8_t *)"a/b", (const uint8_t *)"../a/b/c"));
assert(!qubes_pure_validate_symbolic_link((const uint8_t *)"a/b/c", (const uint8_t *)"../../a"));
assert(qubes_pure_validate_symbolic_link((const uint8_t *)"a/b", (const uint8_t *)"a"));
assert(qubes_pure_validate_symbolic_link((const uint8_t *)"a/b/c", (const uint8_t *)"../a"));
// Absolute symlinks are rejected
assert(!qubes_pure_validate_symbolic_link((const uint8_t *)"a/b/c", (const uint8_t *)"/a"));
// Symlinks may end in "..".
assert(qubes_pure_validate_symbolic_link((const uint8_t *)"a/b/c", (const uint8_t *)".."));
// Symlinks may end in "/".
assert(qubes_pure_validate_symbolic_link((const uint8_t *)"a/b/c", (const uint8_t *)"a/"));
}

0 comments on commit 6e7c758

Please sign in to comment.