Skip to content

Commit b3e0828

Browse files
committed
f
1 parent 34b539f commit b3e0828

8 files changed

Lines changed: 92 additions & 4 deletions

File tree

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
test/fixtures
22
coverage
3+
__snapshots__

__snapshots__/context.test.ts.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
exports['test/context.test.ts context.isSafeDomain should return false when domains are not safe 1'] = {
2+
"domainWhiteList": [
3+
".domain.com",
4+
"http://www.baidu.com",
5+
"192.*.0.*",
6+
"*.alibaba.com"
7+
],
8+
"protocolWhiteList": [],
9+
"defaultMiddleware": "xframe",
10+
"csrf": {
11+
"enable": true,
12+
"type": "ctoken",
13+
"ignoreJSON": false,
14+
"cookieName": "csrfToken",
15+
"sessionName": "csrfToken",
16+
"headerName": "x-csrf-token",
17+
"bodyName": "_csrf",
18+
"queryName": "_csrf",
19+
"rotateWhenInvalid": false,
20+
"useSession": false,
21+
"supportedRequests": [
22+
{
23+
"path": {},
24+
"methods": [
25+
"POST",
26+
"PATCH",
27+
"DELETE",
28+
"PUT",
29+
"CONNECT"
30+
]
31+
}
32+
],
33+
"refererWhiteList": [],
34+
"cookieOptions": {
35+
"signed": false,
36+
"httpOnly": false,
37+
"overwrite": true
38+
}
39+
},
40+
"xframe": {
41+
"enable": true,
42+
"value": "SAMEORIGIN"
43+
},
44+
"hsts": {
45+
"enable": false,
46+
"maxAge": 31536000,
47+
"includeSubdomains": false
48+
},
49+
"methodnoallow": {
50+
"enable": true
51+
},
52+
"noopen": {
53+
"enable": true
54+
},
55+
"nosniff": {
56+
"enable": true
57+
},
58+
"xssProtection": {
59+
"enable": true,
60+
"value": "1; mode=block"
61+
},
62+
"csp": {
63+
"enable": false,
64+
"policy": {}
65+
},
66+
"referrerPolicy": {
67+
"enable": false,
68+
"value": "no-referrer-when-downgrade"
69+
},
70+
"dta": {
71+
"enable": true
72+
},
73+
"ssrf": {}
74+
}

__snapshots__/csp.test.ts.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,10 @@ exports['test/csp.test.ts should ignore path 1'] = {
5555
"value": "1; mode=block"
5656
},
5757
"csp": {
58-
"ignore": "/api/",
58+
"ignore": [
59+
"/api/",
60+
{}
61+
],
5962
"enable": true,
6063
"policy": {
6164
"script-src": [

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
"lint": "eslint --cache src test --ext .ts",
7979
"pretest": "npm run clean && npm run lint -- --fix",
8080
"test": "egg-bin test",
81+
"test:snapshot:update": "SNAPSHOT_UPDATE=1 egg-bin test",
8182
"preci": "npm run clean && npm run lint",
8283
"ci": "egg-bin cov",
8384
"postci": "npm run prepublishOnly && npm run clean",

test/context.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { strict as assert } from 'node:assert';
22
import { mm, MockApplication } from '@eggjs/mock';
3+
import snapshot from 'snap-shot-it';
34

45
describe('test/context.test.ts', () => {
56
afterEach(mm.restore);
@@ -16,6 +17,7 @@ describe('test/context.test.ts', () => {
1617
after(() => app.close());
1718

1819
it('should return false when domains are not safe', async () => {
20+
snapshot(app.config.security);
1921
const res = await app.httpRequest()
2022
.get('/unsafe')
2123
.set('accept', 'text/html')

test/csp.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,12 @@ describe('test/csp.test.ts', () => {
109109
assert.equal(res.headers['x-csp-nonce'], undefined);
110110
});
111111

112+
it('should ignore path by regex rule', async () => {
113+
const res = await app2.httpRequest()
114+
.get('/ignore/update')
115+
.expect(200);
116+
assert.equal(res.headers['x-csp-nonce'], undefined);
117+
});
112118

113119
it('should not ignore path when do not match', async () => {
114120
const res = await app2.httpRequest()

test/fixtures/apps/csp-ignore/app/router.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,7 @@ module.exports = function(app) {
55
app.get('/api/update', async function() {
66
this.body = 456;
77
});
8+
app.get('/ignore/update', async function() {
9+
this.body = 456;
10+
});
811
};

test/fixtures/apps/csp-ignore/config/config.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
'use strict';
2-
31
exports.keys = 'test key';
42

53
exports.security = {
64
defaultMiddleware: 'csp',
75
csp:{
86
enable: true,
9-
ignore: '/api/',
7+
ignore: [ '/api/', /^\/ignore\// ],
108
policy:{
119
'script-src': [
1210
'\'self\'',

0 commit comments

Comments
 (0)