-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathamp-cors.js
86 lines (76 loc) · 2.25 KB
/
amp-cors.js
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
/**
* In practice this would be *.ampproject.org and the publishers
* origin. Please see AMP CORS docs for more details:
* https://goo.gl/F6uCAY
* @type {RegExp}
*/
const ORIGIN_REGEX = new RegExp(
'^https?://localhost:8000|^https?://.+\\.localhost:8000'
);
/**
* @param {*} req require('express').Request
* @param {*} res require('express').Response
* @param {['POST'|'GET']} opt_validMethods
* @param {string[]=} opt_exposeHeaders
*/
function assertCors(req, res, opt_validMethods, opt_exposeHeaders) {
// Allow disable CORS check (iframe fixtures have origin 'about:srcdoc').
if (req.query.cors == '0') {
return;
}
const validMethods = opt_validMethods || ['GET', 'POST', 'OPTIONS'];
const invalidMethod = req.method + ' method is not allowed. Use POST.';
const invalidOrigin = 'Origin header is invalid.';
const unauthorized = 'Unauthorized Request';
let origin;
if (validMethods.indexOf(req.method) == -1) {
res.statusCode = 405;
res.end(JSON.stringify({message: invalidMethod}));
throw invalidMethod;
}
if (req.headers.origin) {
origin = req.headers.origin;
if (!ORIGIN_REGEX.test(req.headers.origin)) {
res.statusCode = 403;
res.end(JSON.stringify({message: invalidOrigin}));
throw invalidOrigin;
}
} else if (req.headers['amp-same-origin'] == 'true') {
origin = getUrlPrefix(req);
} else {
res.statusCode = 403;
res.end(JSON.stringify({message: unauthorized}));
throw unauthorized;
}
enableCors(req, res, origin, opt_exposeHeaders);
}
/**
* @param {*} req require('express').Request
* @param {*} res require('express').Response
* @param {string=} origin
* @param {string[]=} opt_exposeHeaders
*/
function enableCors(req, res, origin, opt_exposeHeaders) {
res.setHeader('Access-Control-Allow-Credentials', 'true');
if (!origin && req.headers.origin) {
origin = req.headers.origin;
}
if (origin) {
res.setHeader('Access-Control-Allow-Origin', origin);
}
res.setHeader(
'Access-Control-Expose-Headers',
(opt_exposeHeaders || []).join(', ')
);
}
/**
* @param {*} req require('express').Request
* @return {string}
*/
function getUrlPrefix(req) {
return req.protocol + '://' + req.headers.host;
}
module.exports = {
enableCors,
assertCors,
};