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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740
|
# frozen_string_literal: true
require "spec_helper"
module SecureHeaders
describe SecureHeaders do
before(:each) do
reset_config
end
let(:request) { Rack::Request.new("HTTP_X_FORWARDED_SSL" => "on") }
it "has a HEADER_NAME with no capital letters" do
# Iterate through all headerable attributes
Configuration::HEADERABLE_ATTRIBUTES.each do |attr|
klass = Configuration::CONFIG_ATTRIBUTES_TO_HEADER_CLASSES[attr]
next if [SecureHeaders::ContentSecurityPolicy].include? klass
expect(klass::HEADER_NAME).to eq(klass::HEADER_NAME.downcase)
end
# Now to iterate through the CSP and CSP-Report-Only classes, since they're registered differently
klass = SecureHeaders::ContentSecurityPolicyConfig
expect(klass::HEADER_NAME).to eq(klass::HEADER_NAME.downcase)
klass = SecureHeaders::ContentSecurityPolicyReportOnlyConfig
expect(klass::HEADER_NAME).to eq(klass::HEADER_NAME.downcase)
end
it "raises a NotYetConfiguredError if default has not been set" do
expect do
SecureHeaders.header_hash_for(request)
end.to raise_error(Configuration::NotYetConfiguredError)
end
it "raises a NotYetConfiguredError if trying to opt-out of unconfigured headers" do
expect do
SecureHeaders.opt_out_of_header(request, :csp)
end.to raise_error(Configuration::NotYetConfiguredError)
end
it "raises a AlreadyConfiguredError if trying to configure and default has already been set " do
Configuration.default
expect do
Configuration.default
end.to raise_error(Configuration::AlreadyConfiguredError)
end
it "raises and ArgumentError when referencing an override that has not been set" do
expect do
Configuration.default
SecureHeaders.use_secure_headers_override(request, :missing)
end.to raise_error(ArgumentError)
end
describe "#header_hash_for" do
it "allows you to opt out of individual headers via API" do
Configuration.default do |config|
config.csp = { default_src: %w('self'), script_src: %w('self') }
config.csp_report_only = config.csp
end
SecureHeaders.opt_out_of_header(request, :csp)
SecureHeaders.opt_out_of_header(request, :csp_report_only)
SecureHeaders.opt_out_of_header(request, :x_content_type_options)
hash = SecureHeaders.header_hash_for(request)
expect(hash["content-security-policy-report-only"]).to be_nil
expect(hash["content-security-policy"]).to be_nil
expect(hash["x-content-type-options"]).to be_nil
end
it "Carries options over when using overrides" do
Configuration.default do |config|
config.x_download_options = OPT_OUT
config.x_permitted_cross_domain_policies = OPT_OUT
end
Configuration.override(:api) do |config|
config.x_frame_options = OPT_OUT
end
SecureHeaders.use_secure_headers_override(request, :api)
hash = SecureHeaders.header_hash_for(request)
expect(hash["x-download-options"]).to be_nil
expect(hash["x-permitted-cross-domain-policies"]).to be_nil
expect(hash["x-frame-options"]).to be_nil
end
it "Overrides the current default config if default config changes during request" do
Configuration.default do |config|
config.x_frame_options = OPT_OUT
end
# Dynamically update the default config for this request
SecureHeaders.override_x_frame_options(request, "DENY")
Configuration.override(:dynamic_override) do |config|
config.x_content_type_options = "nosniff"
end
SecureHeaders.use_secure_headers_override(request, :dynamic_override)
hash = SecureHeaders.header_hash_for(request)
expect(hash["x-content-type-options"]).to eq("nosniff")
expect(hash["x-frame-options"]).to eq("DENY")
end
it "allows you to opt out entirely" do
# configure the disabled-by-default headers to ensure they also do not get set
Configuration.default do |config|
config.csp = { default_src: ["example.com"], script_src: %w('self') }
config.csp_report_only = config.csp
end
SecureHeaders.opt_out_of_all_protection(request)
hash = SecureHeaders.header_hash_for(request)
expect(hash.count).to eq(0)
end
it "allows you to disable secure_headers entirely via Configuration.disable!" do
Configuration.disable!
hash = SecureHeaders.header_hash_for(request)
expect(hash.count).to eq(0)
end
it "allows you to override x-frame-options settings" do
Configuration.default
SecureHeaders.override_x_frame_options(request, XFrameOptions::DENY)
hash = SecureHeaders.header_hash_for(request)
expect(hash[XFrameOptions::HEADER_NAME]).to eq(XFrameOptions::DENY)
end
it "allows you to override opting out" do
Configuration.default do |config|
config.x_frame_options = OPT_OUT
config.csp = OPT_OUT
end
SecureHeaders.override_x_frame_options(request, XFrameOptions::SAMEORIGIN)
SecureHeaders.override_content_security_policy_directives(request, default_src: %w(https:), script_src: %w('self'))
hash = SecureHeaders.header_hash_for(request)
expect(hash[ContentSecurityPolicyConfig::HEADER_NAME]).to eq("default-src https:; script-src 'self'")
expect(hash[XFrameOptions::HEADER_NAME]).to eq(XFrameOptions::SAMEORIGIN)
end
it "produces a hash of headers with default config" do
Configuration.default
hash = SecureHeaders.header_hash_for(request)
expect_default_values(hash)
end
it "does not set the HSTS header if request is over HTTP" do
plaintext_request = Rack::Request.new({})
Configuration.default do |config|
config.hsts = "max-age=123456"
end
expect(SecureHeaders.header_hash_for(plaintext_request)[StrictTransportSecurity::HEADER_NAME]).to be_nil
end
context "content security policy" do
let(:chrome_request) {
Rack::Request.new(request.env.merge("HTTP_USER_AGENT" => USER_AGENTS[:chrome]))
}
it "appends a value to csp directive" do
Configuration.default do |config|
config.csp = {
default_src: %w('self'),
script_src: %w(mycdn.com 'unsafe-inline')
}
end
SecureHeaders.append_content_security_policy_directives(request, script_src: %w(anothercdn.com))
hash = SecureHeaders.header_hash_for(request)
expect(hash[ContentSecurityPolicyConfig::HEADER_NAME]).to eq("default-src 'self'; script-src mycdn.com 'unsafe-inline' anothercdn.com")
end
it "supports named appends" do
Configuration.default do |config|
config.csp = {
default_src: %w('self'),
script_src: %w('self')
}
end
Configuration.named_append(:moar_default_sources) do |request|
{ default_src: %w(https:), style_src: %w('self') }
end
Configuration.named_append(:how_about_a_script_src_too) do |request|
{ script_src: %w('unsafe-inline') }
end
SecureHeaders.use_content_security_policy_named_append(request, :moar_default_sources)
SecureHeaders.use_content_security_policy_named_append(request, :how_about_a_script_src_too)
hash = SecureHeaders.header_hash_for(request)
expect(hash[ContentSecurityPolicyConfig::HEADER_NAME]).to eq("default-src 'self' https:; script-src 'self' 'unsafe-inline'; style-src 'self'")
end
it "appends a nonce to a missing script-src value" do
Configuration.default do |config|
config.csp = {
default_src: %w('self'),
script_src: %w('self')
}
end
SecureHeaders.content_security_policy_script_nonce(request) # should add the value to the header
hash = SecureHeaders.header_hash_for(chrome_request)
expect(hash[ContentSecurityPolicyConfig::HEADER_NAME]).to match(/\Adefault-src 'self'; script-src 'self' 'nonce-.*'\z/)
end
it "appends a hash to a missing script-src value" do
Configuration.default do |config|
config.csp = {
default_src: %w('self'),
script_src: %w('self')
}
end
SecureHeaders.append_content_security_policy_directives(request, script_src: %w('sha256-abc123'))
hash = SecureHeaders.header_hash_for(chrome_request)
expect(hash[ContentSecurityPolicyConfig::HEADER_NAME]).to match(/\Adefault-src 'self'; script-src 'self' 'sha256-abc123'\z/)
end
it "overrides individual directives" do
Configuration.default do |config|
config.csp = {
default_src: %w('self'),
script_src: %w('self')
}
end
SecureHeaders.override_content_security_policy_directives(request, default_src: %w('none'))
hash = SecureHeaders.header_hash_for(request)
expect(hash[ContentSecurityPolicyConfig::HEADER_NAME]).to eq("default-src 'none'; script-src 'self'")
end
it "overrides non-existent directives" do
Configuration.default do |config|
config.csp = {
default_src: %w(https:),
script_src: %w('self')
}
end
SecureHeaders.override_content_security_policy_directives(request, img_src: [ContentSecurityPolicy::DATA_PROTOCOL])
hash = SecureHeaders.header_hash_for(request)
expect(hash[ContentSecurityPolicyReportOnlyConfig::HEADER_NAME]).to be_nil
expect(hash[ContentSecurityPolicyConfig::HEADER_NAME]).to eq("default-src https:; img-src data:; script-src 'self'")
end
it "appends a nonce to the script-src when used" do
Configuration.default do |config|
config.csp = {
default_src: %w('self'),
script_src: %w(mycdn.com),
style_src: %w('self')
}
end
nonce = SecureHeaders.content_security_policy_script_nonce(chrome_request)
# simulate the nonce being used multiple times in a request:
SecureHeaders.content_security_policy_script_nonce(chrome_request)
SecureHeaders.content_security_policy_script_nonce(chrome_request)
SecureHeaders.content_security_policy_script_nonce(chrome_request)
hash = SecureHeaders.header_hash_for(chrome_request)
expect(hash["content-security-policy"]).to eq("default-src 'self'; script-src mycdn.com 'nonce-#{nonce}' 'unsafe-inline'; style-src 'self'")
end
it "does not support the deprecated `report_only: true` format" do
expect {
Configuration.default do |config|
config.csp = {
default_src: %w('self'),
report_only: true
}
end
}.to raise_error(ContentSecurityPolicyConfigError)
end
it "Raises an error if csp_report_only is used with `report_only: false`" do
expect do
Configuration.default do |config|
config.csp_report_only = {
default_src: %w('self'),
script_src: %w('self'),
report_only: false
}
end
end.to raise_error(ContentSecurityPolicyConfigError)
end
context "setting two headers" do
before(:each) do
Configuration.default do |config|
config.csp = {
default_src: %w('self'),
script_src: %w('self')
}
config.csp_report_only = config.csp
end
end
it "sets identical values when the configs are the same" do
reset_config
Configuration.default do |config|
config.csp = {
default_src: %w('self'),
script_src: %w('self')
}
config.csp_report_only = {
default_src: %w('self'),
script_src: %w('self')
}
end
hash = SecureHeaders.header_hash_for(request)
expect(hash["content-security-policy"]).to eq("default-src 'self'; script-src 'self'")
expect(hash["content-security-policy-report-only"]).to eq("default-src 'self'; script-src 'self'")
end
it "sets different headers when the configs are different" do
reset_config
Configuration.default do |config|
config.csp = {
default_src: %w('self'),
script_src: %w('self')
}
config.csp_report_only = config.csp.merge({ script_src: %w(foo.com) })
end
hash = SecureHeaders.header_hash_for(request)
expect(hash["content-security-policy"]).to eq("default-src 'self'; script-src 'self'")
expect(hash["content-security-policy-report-only"]).to eq("default-src 'self'; script-src foo.com")
end
it "allows you to opt-out of enforced CSP" do
reset_config
Configuration.default do |config|
config.csp = SecureHeaders::OPT_OUT
config.csp_report_only = {
default_src: %w('self'),
script_src: %w('self')
}
end
hash = SecureHeaders.header_hash_for(request)
expect(hash["content-security-policy"]).to be_nil
expect(hash["content-security-policy-report-only"]).to eq("default-src 'self'; script-src 'self'")
end
it "allows appending to the enforced policy" do
SecureHeaders.append_content_security_policy_directives(request, { script_src: %w(anothercdn.com) }, :enforced)
hash = SecureHeaders.header_hash_for(request)
expect(hash["content-security-policy"]).to eq("default-src 'self'; script-src 'self' anothercdn.com")
expect(hash["content-security-policy-report-only"]).to eq("default-src 'self'; script-src 'self'")
end
it "allows appending to the report only policy" do
SecureHeaders.append_content_security_policy_directives(request, { script_src: %w(anothercdn.com) }, :report_only)
hash = SecureHeaders.header_hash_for(request)
expect(hash["content-security-policy"]).to eq("default-src 'self'; script-src 'self'")
expect(hash["content-security-policy-report-only"]).to eq("default-src 'self'; script-src 'self' anothercdn.com")
end
it "allows appending to both policies" do
SecureHeaders.append_content_security_policy_directives(request, { script_src: %w(anothercdn.com) }, :both)
hash = SecureHeaders.header_hash_for(request)
expect(hash["content-security-policy"]).to eq("default-src 'self'; script-src 'self' anothercdn.com")
expect(hash["content-security-policy-report-only"]).to eq("default-src 'self'; script-src 'self' anothercdn.com")
end
it "allows overriding the enforced policy" do
SecureHeaders.override_content_security_policy_directives(request, { script_src: %w(anothercdn.com) }, :enforced)
hash = SecureHeaders.header_hash_for(request)
expect(hash["content-security-policy"]).to eq("default-src 'self'; script-src anothercdn.com")
expect(hash["content-security-policy-report-only"]).to eq("default-src 'self'; script-src 'self'")
end
it "allows overriding the report only policy" do
SecureHeaders.override_content_security_policy_directives(request, { script_src: %w(anothercdn.com) }, :report_only)
hash = SecureHeaders.header_hash_for(request)
expect(hash["content-security-policy"]).to eq("default-src 'self'; script-src 'self'")
expect(hash["content-security-policy-report-only"]).to eq("default-src 'self'; script-src anothercdn.com")
end
it "allows overriding both policies" do
SecureHeaders.override_content_security_policy_directives(request, { script_src: %w(anothercdn.com) }, :both)
hash = SecureHeaders.header_hash_for(request)
expect(hash["content-security-policy"]).to eq("default-src 'self'; script-src anothercdn.com")
expect(hash["content-security-policy-report-only"]).to eq("default-src 'self'; script-src anothercdn.com")
end
context "when inferring which config to modify" do
it "updates the enforced header when configured" do
reset_config
Configuration.default do |config|
config.csp = {
default_src: %w('self'),
script_src: %w('self')
}
end
SecureHeaders.append_content_security_policy_directives(request, { script_src: %w(anothercdn.com) })
hash = SecureHeaders.header_hash_for(request)
expect(hash["content-security-policy"]).to eq("default-src 'self'; script-src 'self' anothercdn.com")
expect(hash["content-security-policy-report-only"]).to be_nil
end
it "updates the report only header when configured" do
reset_config
Configuration.default do |config|
config.csp = OPT_OUT
config.csp_report_only = {
default_src: %w('self'),
script_src: %w('self')
}
end
SecureHeaders.append_content_security_policy_directives(request, { script_src: %w(anothercdn.com) })
hash = SecureHeaders.header_hash_for(request)
expect(hash["content-security-policy-report-only"]).to eq("default-src 'self'; script-src 'self' anothercdn.com")
expect(hash["content-security-policy"]).to be_nil
end
it "updates both headers if both are configured" do
reset_config
Configuration.default do |config|
config.csp = {
default_src: %w(enforced.com),
script_src: %w('self')
}
config.csp_report_only = {
default_src: %w(reportonly.com),
script_src: %w('self')
}
end
SecureHeaders.append_content_security_policy_directives(request, { script_src: %w(anothercdn.com) })
hash = SecureHeaders.header_hash_for(request)
expect(hash["content-security-policy"]).to eq("default-src enforced.com; script-src 'self' anothercdn.com")
expect(hash["content-security-policy-report-only"]).to eq("default-src reportonly.com; script-src 'self' anothercdn.com")
end
end
end
it "does not set upgrade-insecure-requests if request is over HTTP" do
reset_config
Configuration.default do |config|
config.csp = {
default_src: %w('self'),
script_src: %w('self'),
upgrade_insecure_requests: true
}
end
plaintext_request = Rack::Request.new({})
hash = SecureHeaders.header_hash_for(plaintext_request)
expect(hash[ContentSecurityPolicyConfig::HEADER_NAME]).to eq("default-src 'self'; script-src 'self'")
expect(hash[ContentSecurityPolicyConfig::HEADER_NAME]).not_to include("upgrade-insecure-requests")
end
it "sets upgrade-insecure-requests if request is over HTTPS" do
reset_config
Configuration.default do |config|
config.csp = {
default_src: %w('self'),
script_src: %w('self'),
upgrade_insecure_requests: true
}
end
https_request = Rack::Request.new("HTTPS" => "on")
hash = SecureHeaders.header_hash_for(https_request)
expect(hash[ContentSecurityPolicyConfig::HEADER_NAME]).to eq("default-src 'self'; script-src 'self'; upgrade-insecure-requests")
end
it "does not set upgrade-insecure-requests in report-only mode if request is over HTTP" do
reset_config
Configuration.default do |config|
config.csp_report_only = {
default_src: %w('self'),
script_src: %w('self'),
upgrade_insecure_requests: true
}
end
plaintext_request = Rack::Request.new({})
hash = SecureHeaders.header_hash_for(plaintext_request)
expect(hash[ContentSecurityPolicyReportOnlyConfig::HEADER_NAME]).to eq("default-src 'self'; script-src 'self'")
expect(hash[ContentSecurityPolicyReportOnlyConfig::HEADER_NAME]).not_to include("upgrade-insecure-requests")
end
it "sets upgrade-insecure-requests in report-only mode if request is over HTTPS" do
reset_config
Configuration.default do |config|
config.csp_report_only = {
default_src: %w('self'),
script_src: %w('self'),
upgrade_insecure_requests: true
}
end
https_request = Rack::Request.new("HTTPS" => "on")
hash = SecureHeaders.header_hash_for(https_request)
expect(hash[ContentSecurityPolicyReportOnlyConfig::HEADER_NAME]).to eq("default-src 'self'; script-src 'self'; upgrade-insecure-requests")
end
end
end
context "validation" do
it "validates your hsts config upon configuration" do
expect do
Configuration.default do |config|
config.hsts = "lol"
end
end.to raise_error(STSConfigError)
end
it "validates your csp config upon configuration" do
expect do
Configuration.default do |config|
config.csp = { ContentSecurityPolicy::DEFAULT_SRC => "123456" }
end
end.to raise_error(ContentSecurityPolicyConfigError)
end
it "raises errors for unknown directives" do
expect do
Configuration.default do |config|
config.csp = { made_up_directive: "123456" }
end
end.to raise_error(ContentSecurityPolicyConfigError)
end
it "validates your xfo config upon configuration" do
expect do
Configuration.default do |config|
config.x_frame_options = "NOPE"
end
end.to raise_error(XFOConfigError)
end
it "validates your xcto config upon configuration" do
expect do
Configuration.default do |config|
config.x_content_type_options = "lol"
end
end.to raise_error(XContentTypeOptionsConfigError)
end
it "validates your clear site data config upon configuration" do
expect do
Configuration.default do |config|
config.clear_site_data = 1
end
end.to raise_error(ClearSiteDataConfigError)
end
it "validates your x_xss config upon configuration" do
expect do
Configuration.default do |config|
config.x_xss_protection = "lol"
end
end.to raise_error(XXssProtectionConfigError)
end
it "validates your xdo config upon configuration" do
expect do
Configuration.default do |config|
config.x_download_options = "lol"
end
end.to raise_error(XDOConfigError)
end
it "validates your x_permitted_cross_domain_policies config upon configuration" do
expect do
Configuration.default do |config|
config.x_permitted_cross_domain_policies = "lol"
end
end.to raise_error(XPCDPConfigError)
end
it "validates your referrer_policy config upon configuration" do
expect do
Configuration.default do |config|
config.referrer_policy = "lol"
end
end.to raise_error(ReferrerPolicyConfigError)
end
it "validates your cookies config upon configuration" do
expect do
Configuration.default do |config|
config.cookies = { secure: "lol" }
end
end.to raise_error(CookiesConfigError)
end
it "validates report_to directive on configuration" do
expect do
Configuration.default do |config|
config.csp = {
default_src: %w('self'),
script_src: %w('self'),
report_to: ["not_a_string"]
}
end
end.to raise_error(ContentSecurityPolicyConfigError)
end
it "allows report_to directive with string endpoint" do
expect do
Configuration.default do |config|
config.csp = {
default_src: %w('self'),
script_src: %w('self'),
report_to: "csp-endpoint"
}
end
end.to_not raise_error
end
end
describe "report_to with overrides and appends" do
let(:request) { double("Request", scheme: "https", env: {}) }
it "overrides the report_to directive" do
Configuration.default do |config|
config.csp = {
default_src: %w('self'),
script_src: %w('self'),
report_to: "endpoint-1"
}
end
SecureHeaders.override_content_security_policy_directives(request, report_to: "endpoint-2")
headers = SecureHeaders.header_hash_for(request)
csp_header = headers[ContentSecurityPolicyConfig::HEADER_NAME]
expect(csp_header).to include("report-to endpoint-2")
end
it "includes report_to when appending CSP directives" do
Configuration.default do |config|
config.csp = {
default_src: %w('self'),
script_src: %w('self')
}
end
SecureHeaders.append_content_security_policy_directives(request, report_to: "new-endpoint")
headers = SecureHeaders.header_hash_for(request)
csp_header = headers[ContentSecurityPolicyConfig::HEADER_NAME]
expect(csp_header).to include("report-to new-endpoint")
end
it "handles report_to with report_uri together" do
Configuration.default do |config|
config.csp = {
default_src: %w('self'),
script_src: %w('self'),
report_uri: %w(/csp-report),
report_to: "reporting-endpoint"
}
end
headers = SecureHeaders.header_hash_for(request)
csp_header = headers[ContentSecurityPolicyConfig::HEADER_NAME]
# Both should be present
expect(csp_header).to include("report-to reporting-endpoint")
expect(csp_header).to include("report-uri /csp-report")
# report-to should come before report-uri (alphabetical order)
expect(csp_header.index("report-to")).to be < csp_header.index("report-uri")
end
end
describe "reporting_endpoints header generation" do
let(:request) { double("Request", scheme: "https", env: {}) }
before(:each) do
reset_config
end
it "includes reporting_endpoints header in generated headers" do
Configuration.default do |config|
config.csp = {
default_src: %w('self'),
script_src: %w('self')
}
config.reporting_endpoints = {
"csp-endpoint" => "https://example.com/reports"
}
end
headers = SecureHeaders.header_hash_for(request)
expect(headers["reporting-endpoints"]).to eq('csp-endpoint="https://example.com/reports"')
end
it "includes reporting_endpoints after config.dup() is called" do
# This test specifically validates that reporting_endpoints survives
# the .dup() call made by the middleware
Configuration.default do |config|
config.csp = {
default_src: %w('self'),
script_src: %w('self')
}
config.reporting_endpoints = {
"csp-violations" => "https://api.example.com/reports?enforcement=enforce",
"csp-violations-report-only" => "https://api.example.com/reports?enforcement=report-only"
}
end
# Simulate what the middleware does internally
config = Configuration.dup # ← This calls .dup() which must preserve reporting_endpoints
headers = config.generate_headers
expect(headers["reporting-endpoints"]).to include('csp-violations="https://api.example.com/reports?enforcement=enforce"')
expect(headers["reporting-endpoints"]).to include('csp-violations-report-only="https://api.example.com/reports?enforcement=report-only"')
end
it "does not include reporting_endpoints header when OPT_OUT" do
Configuration.default do |config|
config.csp = { default_src: %w('self'), script_src: %w('self') }
config.reporting_endpoints = OPT_OUT
end
headers = SecureHeaders.header_hash_for(request)
expect(headers["reporting-endpoints"]).to be_nil
end
it "does not include reporting_endpoints header when not configured" do
Configuration.default do |config|
config.csp = { default_src: %w('self'), script_src: %w('self') }
end
headers = SecureHeaders.header_hash_for(request)
expect(headers["reporting-endpoints"]).to be_nil
end
end
end
end
|