-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support custom pathToRegexpModule
- Loading branch information
Showing
7 changed files
with
183 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
test/fixtures/apps/iframe-with-pathToRegexpModule/app/router.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
module.exports = function(app) { | ||
app.get('/', controller); | ||
app.get('/foo', controller); | ||
app.get('/hello', controller); | ||
app.get('/hello/other/world', controller); | ||
app.get('/world/12', controller); | ||
|
||
app.get('/options', options, controller); | ||
|
||
async function controller() { | ||
this.body = 'body'; | ||
} | ||
|
||
async function options(ctx, next) { | ||
ctx.securityOptions.xframe = { | ||
value: 'ALLOW-FROM http://www.domain.com', | ||
}; | ||
return next(); | ||
} | ||
}; |
8 changes: 8 additions & 0 deletions
8
test/fixtures/apps/iframe-with-pathToRegexpModule/config/config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
exports.keys = 'test key'; | ||
|
||
exports.security = { | ||
defaultMiddleware: 'xframe', | ||
xframe: { | ||
ignore: ['/hello', '/world/:id'], | ||
}, | ||
}; |
3 changes: 3 additions & 0 deletions
3
test/fixtures/apps/iframe-with-pathToRegexpModule/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"name": "iframe-with-pathToRegexpModule" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
const { strict: assert } = require('node:assert'); | ||
const mm = require('egg-mock'); | ||
|
||
describe('test/xframe-with-pathToRegexpModule.test.js', () => { | ||
let app; | ||
let app2; | ||
let app3; | ||
let app4; | ||
before(async () => { | ||
app = mm.app({ | ||
baseDir: 'apps/iframe-with-pathToRegexpModule', | ||
plugin: 'security', | ||
pathToRegexpModule: require.resolve('path-to-regexp-v8'), | ||
}); | ||
await app.ready(); | ||
|
||
app2 = mm.app({ | ||
baseDir: 'apps/iframe-novalue', | ||
plugin: 'security', | ||
pathToRegexpModule: require.resolve('path-to-regexp-v8'), | ||
}); | ||
await app2.ready(); | ||
|
||
app3 = mm.app({ | ||
baseDir: 'apps/iframe-allowfrom', | ||
plugin: 'security', | ||
pathToRegexpModule: require.resolve('path-to-regexp-v8'), | ||
}); | ||
await app3.ready(); | ||
|
||
app4 = mm.app({ | ||
baseDir: 'apps/iframe-black-urls', | ||
plugin: 'security', | ||
pathToRegexpModule: require.resolve('path-to-regexp-v8'), | ||
}); | ||
await app4.ready(); | ||
}); | ||
|
||
afterEach(mm.restore); | ||
|
||
it('should contain X-Frame-Options: SAMEORIGIN', async () => { | ||
await app.httpRequest() | ||
.get('/') | ||
.set('accept', 'text/html') | ||
.expect('x-frame-options', 'SAMEORIGIN'); | ||
|
||
await app.httpRequest() | ||
.get('/foo') | ||
.set('accept', 'text/html') | ||
.expect('x-frame-options', 'SAMEORIGIN'); | ||
}); | ||
|
||
it('should contain X-Frame-Options: ALLOW-FROM http://www.domain.com by this.securityOptions', async () => { | ||
const res = await app.httpRequest() | ||
.get('/options') | ||
.set('accept', 'text/html'); | ||
assert.equal(res.status, 200); | ||
assert.equal(res.headers['x-frame-options'], 'ALLOW-FROM http://www.domain.com'); | ||
}); | ||
|
||
it('should contain X-Frame-Options: SAMEORIGIN when dont set value option', function(done) { | ||
app2.httpRequest() | ||
.get('/foo') | ||
.set('accept', 'text/html') | ||
.expect('x-frame-options', 'SAMEORIGIN', done); | ||
}); | ||
|
||
it('should contain X-Frame-Options: ALLOW-FROM with page when set ALLOW-FROM and page option', function(done) { | ||
app3.httpRequest() | ||
.get('/foo') | ||
.set('accept', 'text/html') | ||
.expect('x-frame-options', 'ALLOW-FROM http://www.domain.com', done); | ||
}); | ||
|
||
it('should not contain X-Frame-Options: SAMEORIGIN when use ignore', async () => { | ||
let res = await app.httpRequest() | ||
.get('/hello') | ||
.set('accept', 'text/html') | ||
.expect(200); | ||
assert.equal(res.headers['x-frame-options'], undefined); | ||
|
||
// '/hello' won't match '/hello/other/world' on path-to-regexp@8 | ||
res = await app.httpRequest() | ||
.get('/hello/other/world') | ||
.set('accept', 'text/html') | ||
.expect(200); | ||
assert.equal(res.headers['x-frame-options'], 'SAMEORIGIN'); | ||
|
||
res = await app4.httpRequest() | ||
.get('/hello') | ||
.set('accept', 'text/html') | ||
.expect(200); | ||
assert.equal(res.headers['x-frame-options'], undefined); | ||
|
||
res = await app.httpRequest() | ||
.get('/world/12') | ||
.set('accept', 'text/html') | ||
.expect(200); | ||
assert.equal(res.headers['x-frame-options'], undefined); | ||
|
||
res = await app.httpRequest() | ||
.get('/world/12?xx=xx') | ||
.set('accept', 'text/html') | ||
.expect(200); | ||
assert.equal(res.headers['x-frame-options'], undefined); | ||
|
||
res = await app2.httpRequest() | ||
.get('/hello') | ||
.set('accept', 'text/html') | ||
.expect(200); | ||
assert.equal(res.headers['x-frame-options'], undefined); | ||
|
||
res = await app2.httpRequest() | ||
.get('/world/12') | ||
.set('accept', 'text/html') | ||
.expect(200); | ||
assert.equal(res.headers['x-frame-options'], undefined); | ||
|
||
res = await app2.httpRequest() | ||
.get('/world/12?xx=xx') | ||
.set('accept', 'text/html') | ||
.expect(200); | ||
assert.equal(res.headers['x-frame-options'], undefined); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters