-
-
Notifications
You must be signed in to change notification settings - Fork 191
Expand file tree
/
Copy pathauth_skip_normalize_test.cpp
More file actions
180 lines (153 loc) · 7.93 KB
/
Copy pathauth_skip_normalize_test.cpp
File metadata and controls
180 lines (153 loc) · 7.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
This file is part of libhttpserver
Copyright (C) 2011-2026 Sebastiano Merlino
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
USA
*/
// Pin that auth_skip_paths entries are normalized at
// webserver construction time, so non-canonical inputs ({"/public/",
// "/a/../b", "/x/./y"}) match canonical request paths ({"/public",
// "/b", "/x/y"}).
//
// Previously: should_skip_auth normalized the *request* path but
// compared it verbatim against the skip list, so a non-canonical skip-
// list entry never matched. This was a latent bug -- callers who
// passed pretty-but-non-canonical skip paths (e.g. trailing slashes
// from copy-paste, "/foo/./bar" from path-builder code) silently saw
// their auth-skip preference ignored.
//
// Now the skip list is pre-normalized at construction; this
// test pins the new behaviour and also covers the empty-list early-
// out path (no skip paths configured -> should_skip_auth returns
// false without touching normalize_path).
#include <memory>
#include <string>
#include <vector>
#include "./httpserver.hpp"
#include "./httpserver/detail/webserver_impl.hpp"
#include "./littletest.hpp"
namespace ht = httpserver;
namespace {
ht::detail::webserver_impl& impl_of(ht::webserver& ws) {
return *ht::webserver_test_access::impl(ws);
}
} // namespace
LT_BEGIN_SUITE(auth_skip_normalize_suite)
void set_up() {}
void tear_down() {}
LT_END_SUITE(auth_skip_normalize_suite)
// ---------------------------------------------------------------------
// Non-canonical skip paths with trailing slashes match canonical
// request paths after construction-time normalization.
// ---------------------------------------------------------------------
LT_BEGIN_AUTO_TEST(auth_skip_normalize_suite,
trailing_slash_skip_path_matches_canonical_request)
ht::webserver ws{ht::create_webserver(8080)
.start_method(ht::http::http_utils::INTERNAL_SELECT)
.auth_skip_paths({"/public/"})};
// Canonical request path -- pre-normalize on skip-list side makes
// this match.
LT_CHECK(impl_of(ws).should_skip_auth("/public"));
LT_END_AUTO_TEST(trailing_slash_skip_path_matches_canonical_request)
// ---------------------------------------------------------------------
// ".." segments in skip paths are collapsed at construction time.
// ---------------------------------------------------------------------
LT_BEGIN_AUTO_TEST(auth_skip_normalize_suite,
dotdot_skip_path_matches_canonical_request)
ht::webserver ws{ht::create_webserver(8080)
.start_method(ht::http::http_utils::INTERNAL_SELECT)
.auth_skip_paths({"/a/../b"})};
LT_CHECK(impl_of(ws).should_skip_auth("/b"));
LT_END_AUTO_TEST(dotdot_skip_path_matches_canonical_request)
// ---------------------------------------------------------------------
// "." segments in skip paths are stripped at construction time.
// ---------------------------------------------------------------------
LT_BEGIN_AUTO_TEST(auth_skip_normalize_suite,
dot_skip_path_matches_canonical_request)
ht::webserver ws{ht::create_webserver(8080)
.start_method(ht::http::http_utils::INTERNAL_SELECT)
.auth_skip_paths({"/x/./y"})};
LT_CHECK(impl_of(ws).should_skip_auth("/x/y"));
LT_END_AUTO_TEST(dot_skip_path_matches_canonical_request)
// ---------------------------------------------------------------------
// Already-canonical skip paths continue to work (regression net for
// the existing happy path).
// ---------------------------------------------------------------------
LT_BEGIN_AUTO_TEST(auth_skip_normalize_suite,
canonical_skip_path_still_matches)
ht::webserver ws{ht::create_webserver(8080)
.start_method(ht::http::http_utils::INTERNAL_SELECT)
.auth_skip_paths({"/public", "/health"})};
LT_CHECK(impl_of(ws).should_skip_auth("/public"));
LT_CHECK(impl_of(ws).should_skip_auth("/health"));
LT_CHECK(!impl_of(ws).should_skip_auth("/api"));
LT_END_AUTO_TEST(canonical_skip_path_still_matches)
// ---------------------------------------------------------------------
// Wildcard-suffix skip paths ("/public/*") have their prefix
// normalized too; the wildcard semantics are preserved.
// ---------------------------------------------------------------------
LT_BEGIN_AUTO_TEST(auth_skip_normalize_suite,
wildcard_suffix_skip_path_normalizes_prefix)
ht::webserver ws{ht::create_webserver(8080)
.start_method(ht::http::http_utils::INTERNAL_SELECT)
.auth_skip_paths({"/public/*"})};
LT_CHECK(impl_of(ws).should_skip_auth("/public/foo"));
LT_CHECK(impl_of(ws).should_skip_auth("/public/foo/bar"));
LT_CHECK(!impl_of(ws).should_skip_auth("/private/foo"));
LT_END_AUTO_TEST(wildcard_suffix_skip_path_normalizes_prefix)
// ---------------------------------------------------------------------
// Empty skip list early-out: no auth_skip_paths configured, so
// should_skip_auth must return false without touching the per-request
// normalize machinery. Behavior pin only -- the perf win is visible
// in the bench, not here.
// ---------------------------------------------------------------------
LT_BEGIN_AUTO_TEST(auth_skip_normalize_suite,
empty_skip_list_returns_false_for_any_path)
ht::webserver ws{ht::create_webserver(8080)
.start_method(ht::http::http_utils::INTERNAL_SELECT)};
LT_CHECK(!impl_of(ws).should_skip_auth("/anywhere"));
LT_CHECK(!impl_of(ws).should_skip_auth("/"));
LT_CHECK(!impl_of(ws).should_skip_auth(""));
LT_END_AUTO_TEST(empty_skip_list_returns_false_for_any_path)
// ---------------------------------------------------------------------
// Global wildcard "/*" must skip
// auth for every path. Pre-fix the size() > 2 guard in should_skip_auth
// excluded "/*" (size == 2), so it fell through to an exact-equality
// check that never matched, silently doing nothing.
// ---------------------------------------------------------------------
LT_BEGIN_AUTO_TEST(auth_skip_normalize_suite,
global_wildcard_skip_path_matches_any_path)
ht::webserver ws{ht::create_webserver(8080)
.start_method(ht::http::http_utils::INTERNAL_SELECT)
.auth_skip_paths({"/*"})};
LT_CHECK(impl_of(ws).should_skip_auth("/"));
LT_CHECK(impl_of(ws).should_skip_auth("/anything"));
LT_CHECK(impl_of(ws).should_skip_auth("/foo/bar/baz"));
LT_END_AUTO_TEST(global_wildcard_skip_path_matches_any_path)
// ---------------------------------------------------------------------
// Percent-encoded entries in
// auth_skip_paths must be rejected at construction time. Passing a
// '%'-containing entry is a misconfiguration (the entry would silently
// never match a decoded request path). The expectation is that
// normalize_auth_skip_paths throws std::invalid_argument.
// ---------------------------------------------------------------------
LT_BEGIN_AUTO_TEST(auth_skip_normalize_suite,
percent_encoded_skip_path_throws_invalid_argument)
LT_CHECK_THROW(
ht::webserver{ht::create_webserver(8080)
.start_method(ht::http::http_utils::INTERNAL_SELECT)
.auth_skip_paths({"/public%2Ftest"})});
LT_END_AUTO_TEST(percent_encoded_skip_path_throws_invalid_argument)
LT_BEGIN_AUTO_TEST_ENV()
AUTORUN_TESTS()
LT_END_AUTO_TEST_ENV()