|
51 | 51 | #include <cstdint> |
52 | 52 | #include <functional> |
53 | 53 | #include <memory> |
| 54 | +#include <stdexcept> |
54 | 55 | #include <string> |
55 | 56 | #include <type_traits> |
56 | 57 | #include <utility> |
@@ -301,6 +302,121 @@ LT_BEGIN_AUTO_TEST(http_response_factories_suite, |
301 | 302 | LT_CHECK_EQ(r.get_response_code(), 401); |
302 | 303 | LT_END_AUTO_TEST(unauthorized_with_explicit_body) |
303 | 304 |
|
| 305 | +// ----------------------------------------------------------------------- |
| 306 | +// unauthorized() — header injection validation (security-reviewer-iter1-1, |
| 307 | +// security-reviewer-iter1-2). CRLF sequences in scheme or realm must be |
| 308 | +// rejected (std::invalid_argument) to prevent header injection (CWE-113). |
| 309 | +// Double-quotes embedded in realm must be escaped per RFC 7235 §2.1 |
| 310 | +// (backslash-escape) so the quoted-string is syntactically valid. |
| 311 | +// ----------------------------------------------------------------------- |
| 312 | +LT_BEGIN_AUTO_TEST(http_response_factories_suite, |
| 313 | + unauthorized_crlf_in_scheme_throws) |
| 314 | + // CRLF in scheme must throw — caller error, not a runtime failure. |
| 315 | + bool caught = false; |
| 316 | + try { |
| 317 | + auto r = http_response::unauthorized("Basic\r\nX-Injected: hdr", |
| 318 | + "myrealm"); |
| 319 | + (void)r; |
| 320 | + } catch (const std::invalid_argument&) { |
| 321 | + caught = true; |
| 322 | + } |
| 323 | + LT_CHECK_EQ(caught, true); |
| 324 | +LT_END_AUTO_TEST(unauthorized_crlf_in_scheme_throws) |
| 325 | + |
| 326 | +LT_BEGIN_AUTO_TEST(http_response_factories_suite, |
| 327 | + unauthorized_lf_in_scheme_throws) |
| 328 | + bool caught = false; |
| 329 | + try { |
| 330 | + auto r = http_response::unauthorized("Basic\nEvil: hdr", "myrealm"); |
| 331 | + (void)r; |
| 332 | + } catch (const std::invalid_argument&) { |
| 333 | + caught = true; |
| 334 | + } |
| 335 | + LT_CHECK_EQ(caught, true); |
| 336 | +LT_END_AUTO_TEST(unauthorized_lf_in_scheme_throws) |
| 337 | + |
| 338 | +LT_BEGIN_AUTO_TEST(http_response_factories_suite, |
| 339 | + unauthorized_cr_in_scheme_throws) |
| 340 | + bool caught = false; |
| 341 | + try { |
| 342 | + auto r = http_response::unauthorized("Basic\r", "myrealm"); |
| 343 | + (void)r; |
| 344 | + } catch (const std::invalid_argument&) { |
| 345 | + caught = true; |
| 346 | + } |
| 347 | + LT_CHECK_EQ(caught, true); |
| 348 | +LT_END_AUTO_TEST(unauthorized_cr_in_scheme_throws) |
| 349 | + |
| 350 | +LT_BEGIN_AUTO_TEST(http_response_factories_suite, |
| 351 | + unauthorized_nul_in_scheme_throws) |
| 352 | + // NUL in scheme is equally dangerous — reject it. |
| 353 | + bool caught = false; |
| 354 | + try { |
| 355 | + std::string s("Basic"); |
| 356 | + s.push_back('\0'); |
| 357 | + s += "evil"; |
| 358 | + auto r = http_response::unauthorized(std::string_view(s.data(), |
| 359 | + s.size()), |
| 360 | + "myrealm"); |
| 361 | + (void)r; |
| 362 | + } catch (const std::invalid_argument&) { |
| 363 | + caught = true; |
| 364 | + } |
| 365 | + LT_CHECK_EQ(caught, true); |
| 366 | +LT_END_AUTO_TEST(unauthorized_nul_in_scheme_throws) |
| 367 | + |
| 368 | +LT_BEGIN_AUTO_TEST(http_response_factories_suite, |
| 369 | + unauthorized_crlf_in_realm_throws) |
| 370 | + bool caught = false; |
| 371 | + try { |
| 372 | + auto r = http_response::unauthorized( |
| 373 | + "Basic", "evil\r\nX-Injected: hdr"); |
| 374 | + (void)r; |
| 375 | + } catch (const std::invalid_argument&) { |
| 376 | + caught = true; |
| 377 | + } |
| 378 | + LT_CHECK_EQ(caught, true); |
| 379 | +LT_END_AUTO_TEST(unauthorized_crlf_in_realm_throws) |
| 380 | + |
| 381 | +LT_BEGIN_AUTO_TEST(http_response_factories_suite, |
| 382 | + unauthorized_lf_in_realm_throws) |
| 383 | + bool caught = false; |
| 384 | + try { |
| 385 | + auto r = http_response::unauthorized("Basic", "evil\nMore: hdr"); |
| 386 | + (void)r; |
| 387 | + } catch (const std::invalid_argument&) { |
| 388 | + caught = true; |
| 389 | + } |
| 390 | + LT_CHECK_EQ(caught, true); |
| 391 | +LT_END_AUTO_TEST(unauthorized_lf_in_realm_throws) |
| 392 | + |
| 393 | +LT_BEGIN_AUTO_TEST(http_response_factories_suite, |
| 394 | + unauthorized_nul_in_realm_throws) |
| 395 | + bool caught = false; |
| 396 | + try { |
| 397 | + std::string realm("my"); |
| 398 | + realm.push_back('\0'); |
| 399 | + realm += "realm"; |
| 400 | + auto r = http_response::unauthorized( |
| 401 | + "Basic", std::string_view(realm.data(), realm.size())); |
| 402 | + (void)r; |
| 403 | + } catch (const std::invalid_argument&) { |
| 404 | + caught = true; |
| 405 | + } |
| 406 | + LT_CHECK_EQ(caught, true); |
| 407 | +LT_END_AUTO_TEST(unauthorized_nul_in_realm_throws) |
| 408 | + |
| 409 | +LT_BEGIN_AUTO_TEST(http_response_factories_suite, |
| 410 | + unauthorized_double_quote_in_realm_is_escaped) |
| 411 | + // RFC 7235 §2.1: double-quotes inside a quoted-string must be |
| 412 | + // backslash-escaped. realm=foo"bar must produce |
| 413 | + // WWW-Authenticate: Basic realm="foo\"bar" |
| 414 | + auto r = http_response::unauthorized("Basic", R"(foo"bar)"); |
| 415 | + LT_CHECK_EQ( |
| 416 | + r.get_header(httpserver::http::http_utils::http_header_www_authenticate), |
| 417 | + std::string(R"(Basic realm="foo\"bar")")); |
| 418 | +LT_END_AUTO_TEST(unauthorized_double_quote_in_realm_is_escaped) |
| 419 | + |
304 | 420 | // ----------------------------------------------------------------------- |
305 | 421 | // Move smoke: factory results survive being returned from a function. |
306 | 422 | // Protects against a future regression of the noexcept move ctor. |
|
0 commit comments