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

[Add] Added upgrade-insecure-requests directive in CSP Headers #58

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
20 changes: 19 additions & 1 deletion src/rules/content-security-policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,19 @@ type ReportingDirective = {
reportTo: string;
"report-to": string;
};
type OtherDirective = {
upgradeInsecureRequests: boolean;
"upgrade-insecure-requests": boolean;
};

export type ContentSecurityPolicyOption =
| false
| {
directives: Partial<FetchDirective> &
Partial<DocumentDirective> &
Partial<NavigationDirective> &
Partial<ReportingDirective>;
Partial<ReportingDirective> &
Partial<OtherDirective>;
reportOnly?: boolean;
};

Expand Down Expand Up @@ -203,12 +208,24 @@ export const convertReportingDirectiveToString = (directive?: Partial<ReportingD
return strings.join(directiveValueSepartor);
};

export const convertOtherDirectiveToString = (directive?: Partial<OtherDirective>) => {
if (directive == undefined) return "";

const strings: string[] = [];

const upgradeInsecureRequests = directive.upgradeInsecureRequests ?? directive["upgrade-insecure-requests"];
if (upgradeInsecureRequests) strings.push(createDirectiveValue("upgrade-insecure-requests", []));

return strings.join(directiveValueSepartor);
};

export const createContentSecurityPolicyOptionHeaderValue = (
option?: ContentSecurityPolicyOption,
fetchDirectiveToStringConverter = convertFetchDirectiveToString,
documentDirectiveToStringConverter = convertDocumentDirectiveToString,
navigationDirectiveToStringConverter = convertNavigationDirectiveToString,
reportingDirectiveToStringConverter = convertReportingDirectiveToString,
othersDirectiveToStringConverter = convertOtherDirectiveToString,
): string | undefined => {
if (option == undefined) return;
if (option === false) return;
Expand All @@ -218,6 +235,7 @@ export const createContentSecurityPolicyOptionHeaderValue = (
documentDirectiveToStringConverter(option.directives),
navigationDirectiveToStringConverter(option.directives),
reportingDirectiveToStringConverter(option.directives),
othersDirectiveToStringConverter(option.directives),
]
.filter((string) => string.length > 0)
.join(directiveValueSepartor);
Expand Down