-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
165 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,65 @@ | ||
require "./spec_helper" | ||
|
||
describe Marten::Middleware::ReferrerPolicy do | ||
describe "#call" do | ||
it "returns the default Referrer-Policy header if not modified early" do | ||
request = Marten::HTTP::Request.new( | ||
::HTTP::Request.new( | ||
method: "GET", | ||
resource: "/test/xyz", | ||
headers: HTTP::Headers{"Host" => "example.com"}, | ||
) | ||
) | ||
|
||
middleware = Marten::Middleware::ReferrerPolicy.new | ||
response = middleware.call( | ||
request, ->{ Marten::HTTP::Response.new("It works!", content_type: "text/plain", status: 200) } | ||
) | ||
|
||
response.headers[:"Referrer-Policy"].should eq "strict-origin-when-cross-origin" | ||
end | ||
|
||
it "returns the response early if it already contains the Referrer-Policy header" do | ||
request = Marten::HTTP::Request.new( | ||
::HTTP::Request.new( | ||
method: "GET", | ||
resource: "/test/xyz", | ||
headers: HTTP::Headers{"Host" => "example.com"}, | ||
) | ||
) | ||
|
||
middleware = Marten::Middleware::ReferrerPolicy.new | ||
response = middleware.call( | ||
request, | ||
->{ | ||
r = Marten::HTTP::Response.new("It works!", content_type: "text/plain", status: 200) | ||
r[:"Referrer-Policy"] = "origin" | ||
r | ||
} | ||
) | ||
|
||
response.headers[:"Referrer-Policy"].should eq "origin" | ||
end | ||
|
||
it "inserts the right Referrer-Policy header value based on the related setting" do | ||
request = Marten::HTTP::Request.new( | ||
::HTTP::Request.new( | ||
method: "GET", | ||
resource: "/test/xyz", | ||
headers: HTTP::Headers{"Host" => "example.com"}, | ||
) | ||
) | ||
|
||
middleware = Marten::Middleware::ReferrerPolicy.new | ||
|
||
with_overridden_setting("referrer_policy", "origin") do | ||
response = middleware.call( | ||
request, | ||
->{ Marten::HTTP::Response.new("It works!", content_type: "text/plain", status: 200) } | ||
) | ||
|
||
response.headers[:"Referrer-Policy"].should eq "origin" | ||
end | ||
end | ||
end | ||
end |
This file contains 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 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,39 @@ | ||
module Marten | ||
abstract class Middleware | ||
# Sets the Referrer-Policy header in the response if it wasn't already set. | ||
# | ||
# When this middleware is used, a Referrer-Policy header will be inserted into the HTTP response. The value for this | ||
# header is configurable in the `referrer_policy` setting. This header controls how much referrer information should | ||
# be included with requests made from your website to other origins. By setting this header, you can enhance the | ||
# privacy and security of your users by limiting the amount of information that is sent with outbound requests. | ||
# | ||
# The possible values for the Referrer-Policy header include: | ||
# - no-referrer: The Referer header will be omitted entirely. No referrer information is sent with requests. | ||
# - no-referrer-when-downgrade: The Referer header will not be sent to less secure destinations | ||
# (e.g., from HTTPS to HTTP), but will be sent to same or more secure destinations. | ||
# - origin: Only the origin of the document is sent as the referrer. | ||
# - origin-when-cross-origin: The full URL is sent as the referrer when performing a same-origin request, | ||
# but only the origin is sent for cross-origin requests. | ||
# - same-origin: The Referer header is sent with same-origin requests, but not with cross-origin requests. | ||
# - strict-origin: Only the origin is sent as the referrer, and only for same-origin requests. | ||
# - strict-origin-when-cross-origin: The full URL is sent as the referrer when performing a same-origin request, | ||
# but only the origin is sent for cross-origin requests. | ||
# No referrer information is sent to less secure destinations. | ||
# - unsafe-url: The full URL is always sent as the referrer, regardless of the request's security. | ||
# | ||
# You can configure the desired policy in the `referrer_policy` setting in your application's configuration. | ||
class ReferrerPolicy < Middleware | ||
def call(request : Marten::HTTP::Request, get_response : Proc(Marten::HTTP::Response)) : Marten::HTTP::Response | ||
response = get_response.call | ||
|
||
# Don't change the Referrer-Policy if it is already set | ||
return response if response.headers[:"Referrer-Policy"]? | ||
|
||
# Set the Referrer-Policy according to settings | ||
response.headers[:"Referrer-Policy"] = Marten.settings.referrer_policy | ||
|
||
response | ||
end | ||
end | ||
end | ||
end |