Skip to content

Commit

Permalink
unignore /lib
Browse files Browse the repository at this point in the history
  • Loading branch information
atilafassina committed Oct 5, 2024
1 parent 4a3e000 commit 8378376
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
coverage/
lib/
dist/
node_modules/
65 changes: 65 additions & 0 deletions src/lib/csp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import {
getCSP,
nonce,
CSPDirectives,
} from "csp-header";
import { type CSP, type CSPHeaderConfig } from "../types.js";
import { DEV_DEFAULT_CSP, PROD_DEFAULT_CSP } from "../defaults.js";

const cspNonceDirectives = [
"script-src",
"style-src",
"img-src",
"font-src",
"media-src",
"object-src",
"default-src",
] as const;

const DEFAULT_CSP: CSPHeaderConfig = {
prod: {
withNonce: true,
value: PROD_DEFAULT_CSP,
cspBlock: false,
cspReportOnly: true,
},
dev: {
withNonce: true,
value: DEV_DEFAULT_CSP,
cspBlock: true,
cspReportOnly: false,
},
};

export const addNonceToDirectives = (
userDefinedCSP: CSP["value"],
nonceString: string
): CSP["value"] => {
const csp: Partial<CSPDirectives> = {
...DEFAULT_CSP.prod.value,
...userDefinedCSP,
};

cspNonceDirectives.forEach((directive) => {
if (csp[directive] && Array.isArray(csp[directive])) {
csp[directive].push(nonce(nonceString));
}
});

return csp;
};

export function generateCSP(cspOptions: CSP["value"], nonceString?: string) {
const directives = nonceString ? addNonceToDirectives(cspOptions, nonceString) : cspOptions;

if (Object.prototype.hasOwnProperty.call(directives,"report-uri")) {
const reportUri = directives["report-uri"];
delete directives["report-uri"];

return getCSP({ directives, reportUri }) as string;
} else {
return getCSP({
directives,
}) as string;
}
}
15 changes: 15 additions & 0 deletions src/lib/hsts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
interface Params {
includeSubDomains?: boolean;
preload?: boolean;
maxAge: number;
}

export function hsts({
includeSubDomains = true,
preload = true,
maxAge = 31536000 /* 1year */,
}: Params) {
return `max-age=${String(maxAge)};${includeSubDomains ? " includeSubDomains;" : ""}${
preload ? " preload" : ""
}`;
}
20 changes: 20 additions & 0 deletions src/lib/permissions-policy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
interface HardwarePermissions {
camera?: string;
microphone?: string;
geolocation?: string;
payment?: string;
}

export function permissionsPolicy(perms: HardwarePermissions) {
const headerValue: string[] = [];

for (const [key, value] of Object.entries(perms)) {
if (typeof value === "string") {
headerValue.push(`${key}=${value}`);
}
}

// headerValue = ["camera=()", "microphone=()"]
return headerValue.join(", ");
// "camera=(), microfone=()"
}

0 comments on commit 8378376

Please sign in to comment.