Skip to content

Commit

Permalink
Added the X-Xss-Protection header
Browse files Browse the repository at this point in the history
  • Loading branch information
marekdedic committed Jun 5, 2024
1 parent fc45832 commit e3e9bc6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/headers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,17 @@ import {
buildXContentTypeOptionsValue,
type XContentTypeOptionsSpec,
} from "./headers/XContentTypeOptions";
import {
buildXXssProtectionValue,
type XXssProtectionSpec,
} from "./headers/XXssProtection";

/* eslint-disable @typescript-eslint/naming-convention -- These are header names */
interface HeaderValueSpecMap {
"Referrer-Policy": ReferrerPolicySpec;
"Strict-Transport-Security": StrictTransportSecuritySpec;
"X-Content-Type-Options": XContentTypeOptionsSpec;
"X-Xss-Protection": XXssProtectionSpec;
}
/* eslint-enable */

Expand Down Expand Up @@ -64,6 +69,8 @@ function buildHeaderValue<
return buildStrictTransportSecurityValue(value);
case "X-Content-Type-Options":
return buildXContentTypeOptionsValue();
case "X-Xss-Protection":
return buildXXssProtectionValue(value);
}
throw new Error('Unknown header type "' + header + '".');
}
Expand Down
27 changes: 27 additions & 0 deletions src/headers/XXssProtection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
export type XXssProtectionSpec =
| {
mode: "block";
}
| {
mode: "disabled";
}
| {
mode: "sanitize";
}
| {
mode: "sanitize+report";
reportUri: string;
};

export function buildXXssProtectionValue(spec: XXssProtectionSpec): string {
switch (spec.mode) {
case "block":
return "1; mode=block";
case "disabled":
return "0";
case "sanitize":
return "1";
case "sanitize+report":
return '"1; report=' + spec.reportUri.replaceAll('"', '\\"') + '"';
}
}

0 comments on commit e3e9bc6

Please sign in to comment.