-
Couldn't load subscription status.
- Fork 254
Add support for W3C Reporting API #556
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tmaier
wants to merge
1
commit into
github:main
Choose a base branch
from
tmaier:claude/add-w3c-reporting-api-011CUQGcw4QBUpw2btyDZpba
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| # frozen_string_literal: true | ||
| module SecureHeaders | ||
| class ReportingEndpointsConfigError < StandardError; end | ||
|
|
||
| class ReportingEndpoints | ||
| HEADER_NAME = "reporting-endpoints".freeze | ||
| INVALID_CONFIGURATION_ERROR = "config must be a hash.".freeze | ||
| INVALID_ENDPOINT_NAME_ERROR = "endpoint names must be strings or symbols.".freeze | ||
| INVALID_ENDPOINT_URL_ERROR = "endpoint URLs must be strings.".freeze | ||
|
|
||
| class << self | ||
| # Public: Generate a Reporting-Endpoints header. | ||
| # | ||
| # Returns nil if not configured, returns header name and value if | ||
| # configured. | ||
| def make_header(config, user_agent = nil) | ||
| return if config.nil? || config == OPT_OUT | ||
|
|
||
| header = new(config) | ||
| [HEADER_NAME, header.value] | ||
| end | ||
|
|
||
| def validate_config!(config) | ||
| return if config.nil? || config == OPT_OUT | ||
| raise ReportingEndpointsConfigError.new(INVALID_CONFIGURATION_ERROR) unless config.is_a?(Hash) | ||
|
|
||
| config.each do |name, url| | ||
| unless name.is_a?(String) || name.is_a?(Symbol) | ||
| raise ReportingEndpointsConfigError.new(INVALID_ENDPOINT_NAME_ERROR) | ||
| end | ||
|
|
||
| unless url.is_a?(String) | ||
| raise ReportingEndpointsConfigError.new(INVALID_ENDPOINT_URL_ERROR) | ||
| end | ||
| end | ||
| end | ||
| end | ||
|
|
||
| def initialize(config) | ||
| @endpoints = config | ||
| end | ||
|
|
||
| def value | ||
| @endpoints.map { |name, url| "#{name}=\"#{url}\"" }.join(", ") | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
spec/lib/secure_headers/headers/reporting_endpoints_spec.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| # frozen_string_literal: true | ||
| require "spec_helper" | ||
|
|
||
| module SecureHeaders | ||
| describe ReportingEndpoints do | ||
| specify { expect(ReportingEndpoints.new(default: "https://example.com/reports").value).to eq('default="https://example.com/reports"') } | ||
| specify do | ||
| config = { default: "https://example.com/reports", csp: "https://example.com/csp" } | ||
| header_value = 'default="https://example.com/reports", csp="https://example.com/csp"' | ||
| expect(ReportingEndpoints.new(config).value).to eq(header_value) | ||
| end | ||
| specify do | ||
| config = { endpoint1: "https://example.com/1", endpoint2: "https://example.com/2", endpoint3: "https://example.com/3" } | ||
| header_value = 'endpoint1="https://example.com/1", endpoint2="https://example.com/2", endpoint3="https://example.com/3"' | ||
| expect(ReportingEndpoints.new(config).value).to eq(header_value) | ||
| end | ||
|
|
||
| context "with an invalid configuration" do | ||
| it "raises an exception when configuration isn't a hash" do | ||
| expect do | ||
| ReportingEndpoints.validate_config!(%w(a)) | ||
| end.to raise_error(ReportingEndpointsConfigError) | ||
| end | ||
|
|
||
| it "raises an exception when configuration is a string" do | ||
| expect do | ||
| ReportingEndpoints.validate_config!("https://example.com") | ||
| end.to raise_error(ReportingEndpointsConfigError) | ||
| end | ||
|
|
||
| it "raises an exception when endpoint name is not a string or symbol" do | ||
| expect do | ||
| ReportingEndpoints.validate_config!(123 => "https://example.com") | ||
| end.to raise_error(ReportingEndpointsConfigError) | ||
| end | ||
|
|
||
| it "raises an exception when endpoint URL is not a string" do | ||
| expect do | ||
| ReportingEndpoints.validate_config!(default: 123) | ||
| end.to raise_error(ReportingEndpointsConfigError) | ||
| end | ||
| end | ||
|
|
||
| context "with OPT_OUT" do | ||
| it "does not produce a header" do | ||
| expect(ReportingEndpoints.make_header(OPT_OUT)).to be_nil | ||
| end | ||
| end | ||
|
|
||
| context "with nil config" do | ||
| it "does not produce a header" do | ||
| expect(ReportingEndpoints.make_header(nil)).to be_nil | ||
| end | ||
| end | ||
| end | ||
| end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.