-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Protect Prototype-Poisoning (#225)
<!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Added a new controller with a POST method to demonstrate object poisoning prevention techniques. - Introduced configurations for CSRF protection and handling proto poisoning. - Added configurations for `egg-tracer`, `@eggjs/tegg-plugin`, and `@eggjs/tegg-config` plugins. - **Bug Fixes** - Implemented test cases to verify protection against proto poisoning in the application. - **Chores** - Updated GitHub Actions workflows: - Removed a scheduled job trigger in the CodeQL analysis workflow. - Added Node.js version 22 to the testing matrix. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
- Loading branch information
Showing
7 changed files
with
108 additions
and
3 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
29 changes: 29 additions & 0 deletions
29
plugin/controller/test/fixtures/apps/proto-poisoning/app/controller/HelloController.ts
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,29 @@ | ||
import { | ||
HTTPController, | ||
HTTPMethod, | ||
HTTPMethodEnum, | ||
HTTPBody, | ||
} from '@eggjs/tegg'; | ||
|
||
@HTTPController() | ||
export class HelloController { | ||
@HTTPMethod({ | ||
method: HTTPMethodEnum.POST, | ||
path: '/hello-proto-poisoning', | ||
}) | ||
async get(@HTTPBody() body: any) { | ||
// console.log(body, body.__proto__); | ||
const params1 = Object.assign({}, body); | ||
const params2 = { | ||
...body, | ||
}; | ||
return { | ||
params1, | ||
params2, | ||
body, | ||
'params1.boom': params1.boom, | ||
'params2.boom': params2.boom, | ||
'body.boom': body.boom, | ||
}; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
plugin/controller/test/fixtures/apps/proto-poisoning/config/config.default.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,14 @@ | ||
module.exports = () => { | ||
const config = { | ||
keys: 'test key', | ||
security: { | ||
csrf: { | ||
ignoreJSON: false, | ||
}, | ||
}, | ||
bodyParser: { | ||
onProtoPoisoning: 'remove', | ||
}, | ||
}; | ||
return config; | ||
}; |
14 changes: 14 additions & 0 deletions
14
plugin/controller/test/fixtures/apps/proto-poisoning/config/plugin.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,14 @@ | ||
exports.tracer = { | ||
package: 'egg-tracer', | ||
enable: true, | ||
}; | ||
|
||
exports.tegg = { | ||
package: '@eggjs/tegg-plugin', | ||
enable: true, | ||
}; | ||
|
||
exports.teggConfig = { | ||
package: '@eggjs/tegg-config', | ||
enable: true, | ||
}; |
3 changes: 3 additions & 0 deletions
3
plugin/controller/test/fixtures/apps/proto-poisoning/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": "proto-poisoning" | ||
} |
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,47 @@ | ||
import path from 'node:path'; | ||
import { strict as assert } from 'node:assert'; | ||
import mm from 'egg-mock'; | ||
|
||
describe('plugin/controller/test/http/proto-poisoning.test.ts', () => { | ||
let app; | ||
|
||
beforeEach(() => { | ||
mm(process.env, 'EGG_TYPESCRIPT', true); | ||
}); | ||
|
||
afterEach(() => { | ||
mm.restore(); | ||
}); | ||
|
||
before(async () => { | ||
mm(process.env, 'EGG_TYPESCRIPT', true); | ||
mm(process, 'cwd', () => { | ||
return path.join(__dirname, '../..'); | ||
}); | ||
app = mm.app({ | ||
baseDir: path.join(__dirname, '../fixtures/apps/proto-poisoning'), | ||
framework: require.resolve('egg'), | ||
}); | ||
await app.ready(); | ||
}); | ||
|
||
after(() => { | ||
return app.close(); | ||
}); | ||
|
||
it('should protect proto poisoning', async () => { | ||
app.mockCsrf(); | ||
const res = await app.httpRequest() | ||
.post('/hello-proto-poisoning') | ||
.set('content-type', 'application/json') | ||
.send(`{ | ||
"hello": "world", | ||
"__proto__": { "boom": "💣" } | ||
}`) | ||
.expect(200); | ||
console.log(res.body); | ||
assert.equal(res.body['body.boom'], undefined, 'body.boom'); | ||
assert.equal(res.body['params2.boom'], undefined, 'params2.boom'); | ||
assert.equal(res.body['params1.boom'], undefined, 'params1.boom'); | ||
}); | ||
}); |