Skip to content
This repository was archived by the owner on Aug 10, 2023. It is now read-only.

Commit 3c29a62

Browse files
committed
adding cors as a base diff. also fix bad ref for query parser plugin in
test files.
1 parent 161a9bb commit 3c29a62

2 files changed

Lines changed: 139 additions & 1 deletion

File tree

lib/plugins/cors.js

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
// Copyright 2013 Mark Cavage, Inc. All rights reserved.
2+
3+
'use strict';
4+
5+
var assert = require('assert-plus');
6+
7+
8+
///--- Globals
9+
10+
var ALLOW_HEADERS = [
11+
'accept',
12+
'accept-version',
13+
'content-type',
14+
'request-id',
15+
'origin',
16+
'x-api-version',
17+
'x-request-id'
18+
];
19+
20+
var EXPOSE_HEADERS = [
21+
'api-version',
22+
'content-length',
23+
'content-md5',
24+
'content-type',
25+
'date',
26+
'request-id',
27+
'response-time'
28+
];
29+
30+
// Normal
31+
var AC_ALLOW_ORIGIN = 'Access-Control-Allow-Origin';
32+
var AC_ALLOW_CREDS = 'Access-Control-Allow-Credentials';
33+
var AC_EXPOSE_HEADERS = 'Access-Control-Expose-Headers';
34+
35+
36+
///--- Internal Functions
37+
38+
function matchOrigin(req, origins) {
39+
var origin = req.headers.origin;
40+
41+
function belongs(o) {
42+
if (origin === o || o === '*') {
43+
origin = o;
44+
return (true);
45+
}
46+
47+
return (false);
48+
}
49+
50+
return ((origin && origins.some(belongs)) ? origin : false);
51+
}
52+
53+
54+
///--- API
55+
56+
/**
57+
* From http://www.w3.org/TR/cors/#resource-processing-model
58+
*
59+
* If "simple" request (paraphrased):
60+
*
61+
* 1. If the Origin header is not set, or if the value of Origin is not a
62+
* case-sensitive match to any values listed in `opts.origins`, do not
63+
* send any CORS headers
64+
*
65+
* 2. If the resource supports credentials add a single
66+
* 'Access-Control-Allow-Credentials' header with the value as "true", and
67+
* ensure 'AC-Allow-Origin' is not '*', but is the request header value,
68+
* otherwise add a single Access-Control-Allow-Origin header, with either the
69+
* value of the Origin header or the string "*" as value
70+
*
71+
* 3. Add Access-Control-Expose-Headers as appropriate
72+
*
73+
* Pre-flight requests are handled by the router internally
74+
*
75+
* @public
76+
* @function cors
77+
* @param {Object} options an options object
78+
* @returns {Function}
79+
*/
80+
function cors(options) {
81+
assert.optionalObject(options, 'options');
82+
var opts = options || {};
83+
assert.optionalArrayOfString(opts.origins, 'options.origins');
84+
assert.optionalBool(opts.credentials, 'options.credentials');
85+
assert.optionalArrayOfString(opts.headers, 'options.headers');
86+
87+
cors.credentials = opts.credentials;
88+
cors.origins = opts.origins || ['*'];
89+
90+
var headers = (opts.headers || []).slice(0);
91+
var origins = opts.origins || ['*'];
92+
93+
EXPOSE_HEADERS.forEach(function (h) {
94+
if (headers.indexOf(h) === -1) {
95+
headers.push(h);
96+
}
97+
});
98+
99+
// Handler for simple requests
100+
function restifyCORSSimple(req, res, next) {
101+
var origin;
102+
103+
if (!(origin = matchOrigin(req, origins))) {
104+
next();
105+
return;
106+
}
107+
108+
function corsOnHeader() {
109+
origin = req.headers.origin;
110+
111+
if (opts.credentials) {
112+
res.setHeader(AC_ALLOW_ORIGIN, origin);
113+
res.setHeader(AC_ALLOW_CREDS, 'true');
114+
} else {
115+
res.setHeader(AC_ALLOW_ORIGIN, origin);
116+
}
117+
118+
res.setHeader(AC_EXPOSE_HEADERS, headers.join(', '));
119+
}
120+
121+
res.once('header', corsOnHeader);
122+
next();
123+
}
124+
125+
return (restifyCORSSimple);
126+
}
127+
128+
129+
///--- Exports
130+
131+
module.exports = cors;
132+
133+
// All of these are needed for the pre-flight code over in lib/router.js
134+
cors.ALLOW_HEADERS = ALLOW_HEADERS;
135+
cors.EXPOSE_HEADERS = EXPOSE_HEADERS;
136+
cors.credentials = false;
137+
cors.origins = [];
138+
cors.matchOrigin = matchOrigin;

test/audit.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ describe('audit logger', function () {
207207
done();
208208
});
209209
SERVER.get('/audit', [
210-
restify.queryParser(),
210+
plugins.queryParser(),
211211
function (req, res, next) {
212212
res.send();
213213
next();

0 commit comments

Comments
 (0)