Skip to content
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

Create DetectWeakReferrerPolicy.bambda #83

Merged
merged 2 commits into from
Jan 2, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions Filter/Proxy/HTTP/DetectWeakReferrerPolicy.bambda
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Bambda Script to Detect "Weak or Missing Referrer-Policy" Header in HTTP Response
* @author ctflearner
* This script checks if the HTTP response lacks the "Referrer-Policy" header or uses a weak policy,
* such as "no-referrer-when-downgrade" or "unsafe-url".
* It ensures there is a response and scans the headers for either the absence of the Referrer-Policy header
* or the presence of policies that may expose sensitive referrer information.
**/


ctflearner marked this conversation as resolved.
Show resolved Hide resolved
if (!requestResponse.hasResponse()) {
return false;
}

Optional<HttpHeader> referrerPolicyHeader = Optional.ofNullable(
requestResponse.response().header("Referrer-Policy")
);

if (referrerPolicyHeader.isEmpty()) {
return true;
}

String headerValue = referrerPolicyHeader.get().value().toLowerCase(Locale.US).trim();

// Check for weak referrer policies using a stream
boolean hasWeakPolicy = requestResponse.response().headers().stream()
.filter(header -> header.name().equalsIgnoreCase("Referrer-Policy"))
.anyMatch(header -> {
String value = header.value().toLowerCase(Locale.US).trim(); // Include Locale for toLowerCase()
return value.equals("no-referrer-when-downgrade") || value.equals("unsafe-url");
});

return headerValue.equals("no-referrer-when-downgrade") || headerValue.equals("unsafe-url") || hasWeakPolicy;
Loading