-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.ts
221 lines (195 loc) · 5.52 KB
/
mod.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import {
createKey,
createPlugin,
DenzoReply,
DenzoRequest,
Vary,
} from "./deps.ts";
export type OriginOption = string | string[] | RegExp | boolean;
export type OriginFn = (
origin: string | null,
) => OriginOption | Promise<OriginOption>;
export interface CorsConfig {
origin: OriginOption | OriginFn;
methods: string[];
preflightContinue: boolean;
optionsSuccessStatus: number;
credentials: boolean;
exposedHeaders?: string | string[];
allowedHeaders?: string | string[];
maxAge?: number;
preflight: boolean;
strictPreflight: boolean;
}
const defaultConfig: CorsConfig = {
origin: "*",
methods: ["GET", "HEAD", "PUT", "PATCH", "POST", "DELETE"],
preflightContinue: false,
optionsSuccessStatus: 204,
credentials: false,
preflight: true,
strictPreflight: true,
};
export const corsPreflightEnabledKey = createKey<boolean>(
"corsPreflightEnabled",
);
export const cors = createPlugin(
"denzo-cors",
(denzo, _config: Partial<CorsConfig>) => {
const config = Object.assign({}, defaultConfig, _config);
denzo.addHook("onRequest", (req, reply) => onRequest(config, req, reply), {
scope: "parent",
});
denzo.route({
method: "OPTIONS",
url: "*",
handler(request, reply) {
if (!request.find(corsPreflightEnabledKey)) {
return reply.status(404).send();
}
},
});
},
);
function resolveOriginWrapper(origin: OriginFn) {
return function (request: DenzoRequest) {
return origin(request.headers.get("origin"));
};
}
function vary(reply: DenzoReply, field: string) {
let value = reply.headers.get("Vary") || "";
value = Vary.append(value, field);
if (value) {
reply.header("Vary", value);
}
}
function addCorsHeaders(
req: DenzoRequest,
reply: DenzoReply,
originOption: OriginOption,
config: CorsConfig,
) {
const origin = getAccessControlAllowOriginHeader(
req.headers.get("origin")!,
originOption,
);
// In the case of origin not allowed the header is not
// written in the response.
// https://github.com/fastify/fastify-cors/issues/127
if (origin) {
reply.header("Access-Control-Allow-Origin", origin);
}
if (config.credentials) {
reply.header("Access-Control-Allow-Credentials", "true");
}
if (config.exposedHeaders) {
reply.header(
"Access-Control-Expose-Headers",
Array.isArray(config.exposedHeaders)
? config.exposedHeaders.join(", ")
: config.exposedHeaders,
);
}
}
function addPreflightHeaders(
req: DenzoRequest,
reply: DenzoReply,
config: CorsConfig,
) {
reply.header(
"Access-Control-Allow-Methods",
Array.isArray(config.methods) ? config.methods.join(", ") : config.methods,
);
if (!config.allowedHeaders) {
vary(reply, "Access-Control-Request-Headers");
const reqAllowedHeaders = req.headers.get("access-control-request-headers");
if (reqAllowedHeaders) {
reply.header("Access-Control-Allow-Headers", reqAllowedHeaders);
}
} else {
reply.header(
"Access-Control-Allow-Headers",
Array.isArray(config.allowedHeaders)
? config.allowedHeaders.join(", ")
: config.allowedHeaders!,
);
}
if (config.maxAge !== undefined) {
reply.header("Access-Control-Max-Age", String(config.maxAge));
}
}
function getAccessControlAllowOriginHeader(
reqOrigin: string,
originOption: OriginOption,
) {
if (originOption === "*") {
// allow any origin
return "*";
}
if (typeof originOption === "string") {
// fixed origin
return originOption;
}
// reflect origin
return isRequestOriginAllowed(reqOrigin, originOption) ? reqOrigin : false;
}
function isRequestOriginAllowed(
reqOrigin: string,
allowedOrigin: OriginOption,
) {
if (Array.isArray(allowedOrigin)) {
for (let i = 0; i < allowedOrigin.length; ++i) {
if (isRequestOriginAllowed(reqOrigin, allowedOrigin[i])) {
return true;
}
}
return false;
} else if (typeof allowedOrigin === "string") {
return reqOrigin === allowedOrigin;
} else if (allowedOrigin instanceof RegExp) {
return allowedOrigin.test(reqOrigin);
} else {
return !!allowedOrigin;
}
}
async function onRequest(
options: CorsConfig,
req: DenzoRequest,
reply: DenzoReply,
) {
// Always set Vary header
// https://github.com/rs/cors/issues/10
vary(reply, "Origin");
const resolveOriginOption = typeof options.origin === "function"
? resolveOriginWrapper(options.origin)
: (_: DenzoRequest) => (options.origin as string)!;
const resolvedOriginOption = await resolveOriginOption(req);
if (resolvedOriginOption === false) return;
if (!resolvedOriginOption) {
throw new Error("Invalid CORS origin option");
}
addCorsHeaders(req, reply, resolvedOriginOption, options);
if (req.raw.method === "OPTIONS" && options.preflight === true) {
// Strict mode enforces the required headers for preflight
if (
options.strictPreflight === true &&
(!req.headers.get("origin") ||
!req.headers.get("access-control-request-method"))
) {
reply.status(400).send("Invalid Preflight Request");
return;
}
req.set(corsPreflightEnabledKey, true);
addPreflightHeaders(req, reply, options);
if (!options.preflightContinue) {
// Do not call the hook callback and terminate the request
// Safari (and potentially other browsers) need content-length 0,
// for 204 or they just hang waiting for a body
reply
.status(options.optionsSuccessStatus)
.header("Content-Length", "0")
.send();
return;
}
}
}