|
| 1 | +/* |
| 2 | + This file is part of libhttpserver |
| 3 | + Copyright (C) 2011-2026 Sebastiano Merlino |
| 4 | +
|
| 5 | + This library is free software; you can redistribute it and/or |
| 6 | + modify it under the terms of the GNU Lesser General Public |
| 7 | + License as published by the Free Software Foundation; either |
| 8 | + version 2.1 of the License, or (at your option) any later version. |
| 9 | +
|
| 10 | + This library is distributed in the hope that it will be useful, |
| 11 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | + Lesser General Public License for more details. |
| 14 | +
|
| 15 | + You should have received a copy of the GNU Lesser General Public |
| 16 | + License along with this library; if not, write to the Free Software |
| 17 | + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
| 18 | + USA |
| 19 | +*/ |
| 20 | + |
| 21 | +// TASK-058 step 2: pin that auth_skip_paths entries are normalized at |
| 22 | +// webserver construction time, so non-canonical inputs ({"/public/", |
| 23 | +// "/a/../b", "/x/./y"}) match canonical request paths ({"/public", |
| 24 | +// "/b", "/x/y"}). |
| 25 | +// |
| 26 | +// Pre-TASK-058: should_skip_auth normalized the *request* path but |
| 27 | +// compared it verbatim against the skip list, so a non-canonical skip- |
| 28 | +// list entry never matched. This was a latent bug -- callers who |
| 29 | +// passed pretty-but-non-canonical skip paths (e.g. trailing slashes |
| 30 | +// from copy-paste, "/foo/./bar" from path-builder code) silently saw |
| 31 | +// their auth-skip preference ignored. |
| 32 | +// |
| 33 | +// After step 2 the skip list is pre-normalized at construction; this |
| 34 | +// test pins the new behaviour and also covers the empty-list early- |
| 35 | +// out path (no skip paths configured -> should_skip_auth returns |
| 36 | +// false without touching normalize_path). |
| 37 | + |
| 38 | +#include <memory> |
| 39 | +#include <string> |
| 40 | +#include <vector> |
| 41 | + |
| 42 | +#include "./httpserver.hpp" |
| 43 | +#include "./httpserver/detail/webserver_impl.hpp" |
| 44 | +#include "./littletest.hpp" |
| 45 | + |
| 46 | +namespace ht = httpserver; |
| 47 | + |
| 48 | +namespace { |
| 49 | + |
| 50 | +ht::detail::webserver_impl& impl_of(ht::webserver& ws) { |
| 51 | + return *ht::webserver_test_access::impl(ws); |
| 52 | +} |
| 53 | + |
| 54 | +} // namespace |
| 55 | + |
| 56 | +LT_BEGIN_SUITE(auth_skip_normalize_suite) |
| 57 | + void set_up() {} |
| 58 | + void tear_down() {} |
| 59 | +LT_END_SUITE(auth_skip_normalize_suite) |
| 60 | + |
| 61 | +// --------------------------------------------------------------------- |
| 62 | +// Non-canonical skip paths with trailing slashes match canonical |
| 63 | +// request paths after construction-time normalization. |
| 64 | +// --------------------------------------------------------------------- |
| 65 | +LT_BEGIN_AUTO_TEST(auth_skip_normalize_suite, |
| 66 | + trailing_slash_skip_path_matches_canonical_request) |
| 67 | + ht::webserver ws{ht::create_webserver(8080) |
| 68 | + .start_method(ht::http::http_utils::INTERNAL_SELECT) |
| 69 | + .auth_skip_paths({"/public/"})}; |
| 70 | + |
| 71 | + // Canonical request path -- pre-normalize on skip-list side makes |
| 72 | + // this match. |
| 73 | + LT_CHECK(impl_of(ws).should_skip_auth("/public")); |
| 74 | +LT_END_AUTO_TEST(trailing_slash_skip_path_matches_canonical_request) |
| 75 | + |
| 76 | +// --------------------------------------------------------------------- |
| 77 | +// ".." segments in skip paths are collapsed at construction time. |
| 78 | +// --------------------------------------------------------------------- |
| 79 | +LT_BEGIN_AUTO_TEST(auth_skip_normalize_suite, |
| 80 | + dotdot_skip_path_matches_canonical_request) |
| 81 | + ht::webserver ws{ht::create_webserver(8080) |
| 82 | + .start_method(ht::http::http_utils::INTERNAL_SELECT) |
| 83 | + .auth_skip_paths({"/a/../b"})}; |
| 84 | + |
| 85 | + LT_CHECK(impl_of(ws).should_skip_auth("/b")); |
| 86 | +LT_END_AUTO_TEST(dotdot_skip_path_matches_canonical_request) |
| 87 | + |
| 88 | +// --------------------------------------------------------------------- |
| 89 | +// "." segments in skip paths are stripped at construction time. |
| 90 | +// --------------------------------------------------------------------- |
| 91 | +LT_BEGIN_AUTO_TEST(auth_skip_normalize_suite, |
| 92 | + dot_skip_path_matches_canonical_request) |
| 93 | + ht::webserver ws{ht::create_webserver(8080) |
| 94 | + .start_method(ht::http::http_utils::INTERNAL_SELECT) |
| 95 | + .auth_skip_paths({"/x/./y"})}; |
| 96 | + |
| 97 | + LT_CHECK(impl_of(ws).should_skip_auth("/x/y")); |
| 98 | +LT_END_AUTO_TEST(dot_skip_path_matches_canonical_request) |
| 99 | + |
| 100 | +// --------------------------------------------------------------------- |
| 101 | +// Already-canonical skip paths continue to work (regression net for |
| 102 | +// the existing happy path). |
| 103 | +// --------------------------------------------------------------------- |
| 104 | +LT_BEGIN_AUTO_TEST(auth_skip_normalize_suite, |
| 105 | + canonical_skip_path_still_matches) |
| 106 | + ht::webserver ws{ht::create_webserver(8080) |
| 107 | + .start_method(ht::http::http_utils::INTERNAL_SELECT) |
| 108 | + .auth_skip_paths({"/public", "/health"})}; |
| 109 | + |
| 110 | + LT_CHECK(impl_of(ws).should_skip_auth("/public")); |
| 111 | + LT_CHECK(impl_of(ws).should_skip_auth("/health")); |
| 112 | + LT_CHECK(!impl_of(ws).should_skip_auth("/api")); |
| 113 | +LT_END_AUTO_TEST(canonical_skip_path_still_matches) |
| 114 | + |
| 115 | +// --------------------------------------------------------------------- |
| 116 | +// Wildcard-suffix skip paths ("/public/*") have their prefix |
| 117 | +// normalized too; the wildcard semantics are preserved. |
| 118 | +// --------------------------------------------------------------------- |
| 119 | +LT_BEGIN_AUTO_TEST(auth_skip_normalize_suite, |
| 120 | + wildcard_suffix_skip_path_normalizes_prefix) |
| 121 | + ht::webserver ws{ht::create_webserver(8080) |
| 122 | + .start_method(ht::http::http_utils::INTERNAL_SELECT) |
| 123 | + .auth_skip_paths({"/public/*"})}; |
| 124 | + |
| 125 | + LT_CHECK(impl_of(ws).should_skip_auth("/public/foo")); |
| 126 | + LT_CHECK(impl_of(ws).should_skip_auth("/public/foo/bar")); |
| 127 | + LT_CHECK(!impl_of(ws).should_skip_auth("/private/foo")); |
| 128 | +LT_END_AUTO_TEST(wildcard_suffix_skip_path_normalizes_prefix) |
| 129 | + |
| 130 | +// --------------------------------------------------------------------- |
| 131 | +// Empty skip list early-out: no auth_skip_paths configured, so |
| 132 | +// should_skip_auth must return false without touching the per-request |
| 133 | +// normalize machinery. Behavior pin only -- the perf win is visible |
| 134 | +// in the bench, not here. |
| 135 | +// --------------------------------------------------------------------- |
| 136 | +LT_BEGIN_AUTO_TEST(auth_skip_normalize_suite, |
| 137 | + empty_skip_list_returns_false_for_any_path) |
| 138 | + ht::webserver ws{ht::create_webserver(8080) |
| 139 | + .start_method(ht::http::http_utils::INTERNAL_SELECT)}; |
| 140 | + |
| 141 | + LT_CHECK(!impl_of(ws).should_skip_auth("/anywhere")); |
| 142 | + LT_CHECK(!impl_of(ws).should_skip_auth("/")); |
| 143 | + LT_CHECK(!impl_of(ws).should_skip_auth("")); |
| 144 | +LT_END_AUTO_TEST(empty_skip_list_returns_false_for_any_path) |
| 145 | + |
| 146 | +LT_BEGIN_AUTO_TEST_ENV() |
| 147 | + AUTORUN_TESTS() |
| 148 | +LT_END_AUTO_TEST_ENV() |
0 commit comments