-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.ts
141 lines (126 loc) · 4.54 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import type { IncomingMessage, ServerResponse } from "http";
interface FeaturePolicyOptions {
features: Record<string, string[]>;
}
function isPlainObject(value: unknown): value is Record<string, unknown> {
return typeof value === "object" && !Array.isArray(value) && value !== null;
}
function getHeaderValueFromOptions(options: unknown): string {
const FEATURES: Record<string, string> = {
accelerometer: "accelerometer",
ambientLightSensor: "ambient-light-sensor",
autoplay: "autoplay",
battery: "battery",
camera: "camera",
displayCapture: "display-capture",
documentDomain: "document-domain",
documentWrite: "document-write",
encryptedMedia: "encrypted-media",
executionWhileNotRendered: "execution-while-not-rendered",
executionWhileOutOfViewport: "execution-while-out-of-viewport",
fontDisplayLateSwap: "font-display-late-swap",
fullscreen: "fullscreen",
geolocation: "geolocation",
gyroscope: "gyroscope",
layoutAnimations: "layout-animations",
legacyImageFormats: "legacy-image-formats",
loadingFrameDefaultEager: "loading-frame-default-eager",
magnetometer: "magnetometer",
microphone: "microphone",
midi: "midi",
navigationOverride: "navigation-override",
notifications: "notifications",
oversizedImages: "oversized-images",
payment: "payment",
pictureInPicture: "picture-in-picture",
publickeyCredentials: "publickey-credentials",
push: "push",
serial: "serial",
speaker: "speaker",
syncScript: "sync-script",
syncXhr: "sync-xhr",
unoptimizedImages: "unoptimized-images",
unoptimizedLosslessImages: "unoptimized-lossless-images",
unoptimizedLossyImages: "unoptimized-lossy-images",
unsizedMedia: "unsized-media",
usb: "usb",
verticalScroll: "vertical-scroll",
vibrate: "vibrate",
vr: "vr",
wakeLock: "wake-lock",
xr: "xr",
xrSpatialTracking: "xr-spatial-tracking",
};
if (!isPlainObject(options)) {
throw new Error(
"featurePolicy must be called with an object argument. See the documentation.",
);
}
const { features } = options;
if (!isPlainObject(features)) {
throw new Error(
'featurePolicy must have a single key, "features", which is an object of features. See the documentation.',
);
}
const result = Object.entries(features)
.map(([featureKeyCamelCase, featureValue]) => {
if (
!Object.prototype.hasOwnProperty.call(FEATURES, featureKeyCamelCase)
) {
throw new Error(
`featurePolicy does not support the "${featureKeyCamelCase}" feature.`,
);
}
if (!Array.isArray(featureValue) || featureValue.length === 0) {
throw new Error(
`The value of the "${featureKeyCamelCase}" feature must be a non-empty array of strings.`,
);
}
const allowedValuesSeen: Set<string> = new Set();
featureValue.forEach((allowedValue) => {
if (typeof allowedValue !== "string") {
throw new Error(
`The value of the "${featureKeyCamelCase}" feature contains a non-string, which is not supported.`,
);
} else if (allowedValuesSeen.has(allowedValue)) {
throw new Error(
`The value of the "${featureKeyCamelCase}" feature contains duplicates, which it shouldn't.`,
);
} else if (allowedValue === "self") {
throw new Error("'self' must be quoted.");
} else if (allowedValue === "none") {
throw new Error("'none' must be quoted.");
}
allowedValuesSeen.add(allowedValue);
});
if (featureValue.length > 1) {
if (allowedValuesSeen.has("*")) {
throw new Error(
`The value of the "${featureKeyCamelCase}" feature cannot contain * and other values.`,
);
} else if (allowedValuesSeen.has("'none'")) {
throw new Error(
`The value of the "${featureKeyCamelCase}" feature cannot contain 'none' and other values.`,
);
}
}
const featureKeyDashed = FEATURES[featureKeyCamelCase];
return [featureKeyDashed, ...featureValue].join(" ");
})
.join(";");
if (result.length === 0) {
throw new Error("At least one feature is required.");
}
return result;
}
export default function featurePolicy(options: Readonly<FeaturePolicyOptions>) {
const headerValue = getHeaderValueFromOptions(options);
return function featurePolicy(
_req: IncomingMessage,
res: ServerResponse,
next: () => void,
) {
res.setHeader("Feature-Policy", headerValue);
next();
};
}