Skip to content

Commit c397ab3

Browse files
authored
fix(transport-commons): Handle invalid service paths on socket lookups (#3241)
1 parent 043298b commit c397ab3

File tree

3 files changed

+21
-11
lines changed

3 files changed

+21
-11
lines changed

packages/transport-commons/src/socket/index.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
import { Application, getServiceOptions, Params, RealTimeConnection } from '@feathersjs/feathers'
2-
import { createDebug } from '@feathersjs/commons'
32
import { channels } from '../channels'
43
import { routing } from '../routing'
54
import { getDispatcher, runMethod } from './utils'
65

7-
const debug = createDebug('@feathersjs/transport-commons')
8-
96
export interface SocketOptions {
107
done: Promise<any>
118
emit: string
@@ -51,10 +48,9 @@ export function socket({ done, emit, socketMap, socketKey, getParams }: SocketOp
5148
methods.forEach((method) => {
5249
if (!result[method]) {
5350
result[method] = (...args: any[]) => {
54-
const path = args.shift()
51+
const [path, ...rest] = args
5552

56-
debug(`Got '${method}' call for service '${path}'`)
57-
runMethod(app, getParams(connection), path, method, args)
53+
runMethod(app, getParams(connection), path, method, rest)
5854
}
5955
}
6056
})

packages/transport-commons/src/socket/utils.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,12 @@ export function getDispatcher(emit: string, socketMap: WeakMap<RealTimeConnectio
6969
export async function runMethod(
7070
app: Application,
7171
connection: RealTimeConnection,
72-
path: string,
73-
method: string,
72+
_path: string,
73+
_method: string,
7474
args: any[]
7575
) {
76+
const path = typeof _path === 'string' ? _path : null
77+
const method = typeof _method === 'string' ? _method : null
7678
const trace = `method '${method}' on service '${path}'`
7779
const methodArgs = args.slice(0)
7880
const callback =
@@ -91,7 +93,7 @@ export async function runMethod(
9193

9294
// No valid service was found throw a NotFound error
9395
if (lookup === null) {
94-
throw new NotFound(`Service '${path}' not found`)
96+
throw new NotFound(path === null ? `Invalid service path` : `Service '${path}' not found`)
9597
}
9698

9799
const { service, params: route = {} } = lookup

packages/transport-commons/test/socket/index.test.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,26 @@ describe('@feathersjs/transport-commons', () => {
9090
})
9191
})
9292

93-
it('.get with invalid service name and arguments', (done) => {
93+
it('method with invalid service name and arguments', (done) => {
9494
const socket = new EventEmitter()
9595

9696
provider.emit('connection', socket)
9797

9898
socket.emit('get', null, (error: any) => {
9999
assert.strictEqual(error.name, 'NotFound')
100-
assert.strictEqual(error.message, "Service 'null' not found")
100+
assert.strictEqual(error.message, 'Invalid service path')
101+
done()
102+
})
103+
})
104+
105+
it('method with implicit toString errors properly', (done) => {
106+
const socket = new EventEmitter()
107+
108+
provider.emit('connection', socket)
109+
110+
socket.emit('get', { toString: '' }, (error: any) => {
111+
assert.strictEqual(error.name, 'NotFound')
112+
assert.strictEqual(error.message, 'Invalid service path')
101113
done()
102114
})
103115
})

0 commit comments

Comments
 (0)