8000 fix(mongodb): Ensure arbitrary objects can't be passed as MongoDB ids by daffl · Pull Request #3664 · feathersjs/feathers · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/mongodb/src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ export class MongoDbAdapter<
const { $select, $sort, $limit: _limit, $skip = 0, ...query } = (params.query || {}) as AdapterQuery
const $limit = getLimit(_limit, options.paginate)
if (id !== null) {
if (typeof id !== 'string' && typeof id !== 'number' && !(id instanceof ObjectId)) {
throw new BadRequest(`Invalid id '${JSON.stringify(id)}'`)
}
query.$and = (query.$and || []).concat({
[this.id]: this.getObjectId(id)
})
Expand Down
59 changes: 59 additions & 0 deletions packages/mongodb/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -839,6 +839,65 @@ describe('Feathers MongoDB Service', () => {
})
})

describe('NoSQL injection via object id', () => {
let target: Person

beforeEach(async () => {
target = await app.service('people').create({ name: 'Target' })
})

afterEach(async () => {
try {
await app.service('people').remove(target._id)
} catch (e: unknown) {}
})

it('rejects object as id in get', async () => {
await assert.rejects(
() => app.service('people').get({ $ne: null } as any),
{
name: 'BadRequest'
}
)
})

it('rejects object as id in remove', async () => {
await assert.rejects(
() => app.service('people').remove({ $ne: null } as any),
{
name: 'BadRequest'
}
)
})

it('rejects object as id in update', async () => {
await assert.rejects(
() => app.service('people').update({ $ne: null } as any, { name: 'Hacked' }),
{
name: 'BadRequest'
}
)
})

it('rejects object as id in patch', async () => {
await assert.rejects(
() => app.service('people').patch({ $ne: null } as any, { name: 'Hacked' }),
{
name: 'BadRequest'
}
)
})

it('rejects regex operator as id', async () => {
await assert.rejects(
() => app.service('people').get({ $regex: '^' } as any),
{
name: 'BadRequest'
}
)
})
})

testSuite(app, errors, 'people', '_id')
testSuite(app, errors, 'people-customid', 'customid')
})
0