Skip to content

Commit c279fc3

Browse files
authoredMar 29, 2022
feat: Add support for "Private Network Access" (#83)
closes #81
1 parent 97d9220 commit c279fc3

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed
 

‎index.js

+5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const vary = require('vary');
1414
* - {Boolean|Function(ctx)} credentials `Access-Control-Allow-Credentials`
1515
* - {Boolean} keepHeadersOnError Add set headers to `err.header` if an error is thrown
1616
* - {Boolean} secureContext `Cross-Origin-Opener-Policy` & `Cross-Origin-Embedder-Policy` headers.', default is false
17+
* - {Boolean} privateNetworkAccess handle `Access-Control-Request-Private-Network` request by return `Access-Control-Allow-Private-Network`, default to false
1718
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer/Planned_changes
1819
* @return {Function} cors middleware
1920
* @public
@@ -137,6 +138,10 @@ module.exports = function(options) {
137138
ctx.set('Access-Control-Max-Age', options.maxAge);
138139
}
139140

141+
if (options.privateNetworkAccess && ctx.get('Access-Control-Request-Private-Network')) {
142+
ctx.set('Access-Control-Allow-Private-Network', 'true');
143+
}
144+
140145
if (options.allowMethods) {
141146
ctx.set('Access-Control-Allow-Methods', options.allowMethods);
142147
}

‎test/cors.test.js

+89
Original file line numberDiff line numberDiff line change
@@ -698,4 +698,93 @@ describe('cors.test.js', function() {
698698
.expect(500, done);
699699
});
700700
});
701+
702+
describe('options.privateNetworkAccess=false', function() {
703+
const app = new Koa();
704+
app.use(cors({
705+
privateNetworkAccess: false,
706+
}));
707+
708+
app.use(function(ctx) {
709+
ctx.body = { foo: 'bar' };
710+
});
711+
712+
it('should not set `Access-Control-Allow-Private-Network` on not OPTIONS', function(done) {
713+
request(app.listen())
714+
.get('/')
715+
.set('Origin', 'http://koajs.com')
716+
.set('Access-Control-Request-Method', 'PUT')
717+
.expect(res => {
718+
assert(!('Access-Control-Allow-Private-Network' in res.headers));
719+
})
720+
.expect(200, done);
721+
});
722+
723+
it('should not set `Access-Control-Allow-Private-Network` if `Access-Control-Request-Private-Network` not exist on OPTIONS', function(done) {
724+
request(app.listen())
725+
.options('/')
726+
.set('Origin', 'http://koajs.com')
727+
.set('Access-Control-Request-Method', 'PUT')
728+
.expect(res => {
729+
assert(!('Access-Control-Allow-Private-Network' in res.headers));
730+
})
731+
.expect(204, done);
732+
});
733+
734+
it('should not set `Access-Control-Allow-Private-Network` if `Access-Control-Request-Private-Network` exist on OPTIONS', function(done) {
735+
request(app.listen())
736+
.options('/')
737+
.set('Origin', 'http://koajs.com')
738+
.set('Access-Control-Request-Method', 'PUT')
739+
.set('Access-Control-Request-Private-Network', 'true')
740+
.expect(res => {
741+
assert(!('Access-Control-Allow-Private-Network' in res.headers));
742+
})
743+
.expect(204, done);
744+
});
745+
});
746+
747+
describe('options.privateNetworkAccess=true', function() {
748+
const app = new Koa();
749+
app.use(cors({
750+
privateNetworkAccess: true,
751+
}));
752+
753+
app.use(function(ctx) {
754+
ctx.body = { foo: 'bar' };
755+
});
756+
757+
it('should not set `Access-Control-Allow-Private-Network` on not OPTIONS', function(done) {
758+
request(app.listen())
759+
.get('/')
760+
.set('Origin', 'http://koajs.com')
761+
.set('Access-Control-Request-Method', 'PUT')
762+
.expect(res => {
763+
assert(!('Access-Control-Allow-Private-Network' in res.headers));
764+
})
765+
.expect(200, done);
766+
});
767+
768+
it('should not set `Access-Control-Allow-Private-Network` if `Access-Control-Request-Private-Network` not exist on OPTIONS', function(done) {
769+
request(app.listen())
770+
.options('/')
771+
.set('Origin', 'http://koajs.com')
772+
.set('Access-Control-Request-Method', 'PUT')
773+
.expect(res => {
774+
assert(!('Access-Control-Allow-Private-Network' in res.headers));
775+
})
776+
.expect(204, done);
777+
});
778+
779+
it('should always set `Access-Control-Allow-Private-Network` if `Access-Control-Request-Private-Network` exist on OPTIONS', function(done) {
780+
request(app.listen())
781+
.options('/')
782+
.set('Origin', 'http://koajs.com')
783+
.set('Access-Control-Request-Method', 'PUT')
784+
.set('Access-Control-Request-Private-Network', 'true')
785+
.expect('Access-Control-Allow-Private-Network', 'true')
786+
.expect(204, done);
787+
});
788+
});
789+
701790
});

0 commit comments

Comments
 (0)
Please sign in to comment.