mirror of
https://github.com/trezor/trezor-suite.git
synced 2026-03-06 15:29:44 +01:00
- Define security headers for local testing of production web build. - Use Vite to run the preview local server.
26 lines
746 B
TypeScript
26 lines
746 B
TypeScript
import { ContentSecurityPolicyRules } from '../types/securityHeaders';
|
|
|
|
const quotedDirectivesValues = new Set(['self', 'none', 'unsafe-inline', 'unsafe-eval']);
|
|
|
|
function quoteDirectiveValue(value: string) {
|
|
return quotedDirectivesValues.has(value) ? `'${value}'` : value;
|
|
}
|
|
|
|
export function formatContentSecurityPolicy<Rules extends Partial<ContentSecurityPolicyRules>>(
|
|
rules: Rules,
|
|
) {
|
|
return Object.entries(rules)
|
|
.map(([key, value]) => {
|
|
if (value === true) {
|
|
return key;
|
|
}
|
|
|
|
if (Array.isArray(value)) {
|
|
return `${key} ${value.map(quoteDirectiveValue).join(' ')}`;
|
|
}
|
|
|
|
return `${key} ${value}`;
|
|
})
|
|
.join('; ');
|
|
}
|