From c397ab3a0cd184044ae4f73540549b30a396821c Mon Sep 17 00:00:00 2001 From: David Luecke Date: Tue, 18 Jul 2023 20:32:14 -0700 Subject: [PATCH] fix(transport-commons): Handle invalid service paths on socket lookups (#3241) --- packages/transport-commons/src/socket/index.ts | 8 ++------ packages/transport-commons/src/socket/utils.ts | 8 +++++--- .../transport-commons/test/socket/index.test.ts | 16 ++++++++++++++-- 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/packages/transport-commons/src/socket/index.ts b/packages/transport-commons/src/socket/index.ts index 6e49d20237..1c21095fd1 100644 --- a/packages/transport-commons/src/socket/index.ts +++ b/packages/transport-commons/src/socket/index.ts @@ -1,11 +1,8 @@ import { Application, getServiceOptions, Params, RealTimeConnection } from '@feathersjs/feathers' -import { createDebug } from '@feathersjs/commons' import { channels } from '../channels' import { routing } from '../routing' import { getDispatcher, runMethod } from './utils' -const debug = createDebug('@feathersjs/transport-commons') - export interface SocketOptions { done: Promise emit: string @@ -51,10 +48,9 @@ export function socket({ done, emit, socketMap, socketKey, getParams }: SocketOp methods.forEach((method) => { if (!result[method]) { result[method] = (...args: any[]) => { - const path = args.shift() + const [path, ...rest] = args - debug(`Got '${method}' call for service '${path}'`) - runMethod(app, getParams(connection), path, method, args) + runMethod(app, getParams(connection), path, method, rest) } } }) diff --git a/packages/transport-commons/src/socket/utils.ts b/packages/transport-commons/src/socket/utils.ts index f61d9a856a..768bba469a 100644 --- a/packages/transport-commons/src/socket/utils.ts +++ b/packages/transport-commons/src/socket/utils.ts @@ -69,10 +69,12 @@ export function getDispatcher(emit: string, socketMap: WeakMap { }) }) - it('.get with invalid service name and arguments', (done) => { + it('method with invalid service name and arguments', (done) => { const socket = new EventEmitter() provider.emit('connection', socket) socket.emit('get', null, (error: any) => { assert.strictEqual(error.name, 'NotFound') - assert.strictEqual(error.message, "Service 'null' not found") + assert.strictEqual(error.message, 'Invalid service path') + done() + }) + }) + + it('method with implicit toString errors properly', (done) => { + const socket = new EventEmitter() + + provider.emit('connection', socket) + + socket.emit('get', { toString: '' }, (error: any) => { + assert.strictEqual(error.name, 'NotFound') + assert.strictEqual(error.message, 'Invalid service path') done() }) })