8000 fix(transport-commons): Handle invalid service paths on socket lookups by daffl · Pull Request #3241 · 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
8 changes: 2 additions & 6 deletions packages/transport-commons/src/socket/index.ts
Original file line number Diff line number Diff line change
@@ -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<any>
emit: string
Expand Down Expand Up @@ -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)
}
}
})
Expand Down
8 changes: 5 additions & 3 deletions packages/transport-commons/src/socket/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,12 @@ export function getDispatcher(emit: string, socketMap: WeakMap<RealTimeConnectio
export async function runMethod(
app: Application,
connection: RealTimeConnection,
path: string,
method: string,
_path: string,
_method: string,
args: any[]
) {
const path = typeof _path === 'string' ? _path : null
const method = typeof _method === 'string' ? _method : null
const trace = `method '${method}' on service '${path}'`
const methodArgs = args.slice(0)
const callback =
Expand All @@ -91,7 +93,7 @@ export async function runMethod(

// No valid service was found throw a NotFound error
if (lookup === null) {
throw new NotFound(`Service '${path}' not found`)
throw new NotFound(path === null ? `Invalid service path` : `Service '${path}' not found`)
}

const { service, params: route = {} } = lookup
Expand Down
16 changes: 14 additions & 2 deletions packages/transport-commons/test/socket/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,26 @@ describe('@feathersjs/transport-commons', () => {
})
})

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()
})
})
Expand Down
0