-
-
Notifications
You must be signed in to change notification settings - Fork 657
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: validate that id is number or we throw our our source code (#6860)
Previously, we were not validating that the ID was a number, which sometimes resulted in returning our database queries (source code) to the frontend. Now, we have validation middleware.
- Loading branch information
Showing
6 changed files
with
72 additions
and
10 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
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,32 @@ | ||
import express from 'express'; | ||
import supertest from 'supertest'; | ||
import idNumberMiddleware from './id-number-middleware'; | ||
|
||
describe('idNumberMiddleware', () => { | ||
it('should pass when id is a valid integer', async () => { | ||
const app = express(); | ||
app.use('/:id', idNumberMiddleware()); | ||
app.get('/:id', (req, res) => { | ||
res.status(200).send('Valid ID'); | ||
}); | ||
|
||
await supertest(app) | ||
.get('/123') | ||
.expect(200) | ||
.expect((res) => { | ||
expect(res.text).toBe('Valid ID'); | ||
}); | ||
}); | ||
it('should throw BadDataError when id is not a valid integer', async () => { | ||
const app = express(); | ||
app.use('/:id', idNumberMiddleware()); | ||
app.get('/:id', (req, res) => { | ||
res.status(200).send('This should not be executed'); | ||
}); | ||
|
||
const { body } = await supertest(app).get('/abc').expect(400); | ||
expect(body).toMatchObject({ | ||
details: [{ message: 'ID should be an integer' }], | ||
}); | ||
}); | ||
}); |
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,16 @@ | ||
import { BadDataError } from '../error'; | ||
|
||
const idNumberMiddleware = (): any => { | ||
return async (req, res, next) => { | ||
const { id } = req.params; | ||
if (!Number.isInteger(Number(id))) { | ||
res.status(400).send( | ||
new BadDataError('ID should be an integer').toJSON(), | ||
); | ||
return; | ||
} | ||
next(); | ||
}; | ||
}; | ||
|
||
export default idNumberMiddleware; |
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