Skip to content

Commit

Permalink
fix(transport-commons): Ensure socket queries are always plain objects (
Browse files Browse the repository at this point in the history
  • Loading branch information
daffl authored Apr 13, 2022
1 parent d719f54 commit 97313e1
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 87 deletions.
145 changes: 62 additions & 83 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/transport-commons/src/socket/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export async function runMethod (app: Application, connection: RealTimeConnectio
}

const position = paramsPositions[method] !== undefined ? paramsPositions[method] : DEFAULT_PARAMS_POSITION;
const query = methodArgs[position] || {};
const query = Object.assign({}, methodArgs[position]);
// `params` have to be re-mapped to the query and added with the route
const params = Object.assign({ query, route, connection }, connection);

Expand Down
26 changes: 23 additions & 3 deletions packages/transport-commons/test/socket/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import assert from 'assert';
import { EventEmitter } from 'events';
import { feathers, Application, Params } from '@feathersjs/feathers';
import { NotAuthenticated } from '@feathersjs/errors';
import { isPlainObject } from 'lodash';

import { routing } from '../../src/routing';
import {
Expand Down Expand Up @@ -189,11 +190,15 @@ describe('socket commons utils', () => {
beforeEach(() => {
app = feathers().configure(routing());
app.use('/myservice', {
get (id: number|string, params: Params) {
async get (id: number|string, params: Params) {
if (params.query.error) {
return Promise.reject(new NotAuthenticated('None shall pass'));
throw new NotAuthenticated('None shall pass');
}
return Promise.resolve({ id });
if (!isPlainObject(params.query)) {
throw new Error('Query is not a plain object');
}

return { id };
}
});
});
Expand All @@ -212,6 +217,21 @@ describe('socket commons utils', () => {
runMethod(app, {}, 'myservice', 'get', [ 10, {}, callback ]);
});

it('queries are always plain objects', done => {
const callback = (error: any, result: any) => {
if (error) {
return done(error);
}

assert.deepStrictEqual(result, { id: 10 });
done();
};

runMethod(app, {}, 'myservice', 'get', [ 10, {
__proto__: []
}, callback ]);
});

it('merges params with connection and passes connection', done => {
const connection = {
testing: true
Expand Down

0 comments on commit 97313e1

Please sign in to comment.