Skip to content

Commit

Permalink
fix(core): Add list of protected methods that can not be used for cus…
Browse files Browse the repository at this point in the history
…tom methods (#2390)
  • Loading branch information
daffl authored Jun 14, 2021
1 parent 3f4db68 commit 6584a21
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
8 changes: 7 additions & 1 deletion packages/feathers/src/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
} from './dependencies';
import { eventHook, eventMixin } from './events';
import { hookMixin } from './hooks/index';
import { wrapService, getServiceOptions } from './service';
import { wrapService, getServiceOptions, protectedMethods } from './service';
import {
FeathersApplication,
ServiceMixin,
Expand Down Expand Up @@ -95,6 +95,12 @@ export class Feathers<ServiceTypes, AppSettings> extends EventEmitter implements
const protoService = wrapService(location, service, options);
const serviceOptions = getServiceOptions(service, options);

for (const name of protectedMethods) {
if (serviceOptions.methods.includes(name)) {
throw new Error(`'${name}' on service '${location}' is not allowed as a custom method name`);
}
}

debug(`Registering new service at \`${location}\``);

// Add all the mixins
Expand Down
13 changes: 12 additions & 1 deletion packages/feathers/src/service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createSymbol } from './dependencies';
import { createSymbol, EventEmitter } from './dependencies';
import { ServiceOptions } from './declarations';

export const SERVICE = createSymbol('@feathersjs/service');
Expand All @@ -21,6 +21,17 @@ export const defaultEventMap = {
remove: 'removed'
}

export const protectedMethods = Object.keys(Object.prototype)
.concat(Object.keys(EventEmitter.prototype))
.concat([
'before',
'after',
'error',
'hooks',
'setup',
'publish'
]);

export function getHookMethods (service: any, options: ServiceOptions) {
const { methods } = options;

Expand Down
23 changes: 23 additions & 0 deletions packages/feathers/test/application.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,29 @@ describe('Feathers application', () => {
assert.strictEqual(data.message, 'Test message');
});

it('can not register custom methods on a protected methods', async () => {
const dummyService = {
async create (data: any) {
return data;
},
async removeListener (data: any) {
return data;
},
async setup () {}
};

assert.throws(() => feathers().use('/dummy', dummyService, {
methods: ['create', 'removeListener']
}), {
message: '\'removeListener\' on service \'dummy\' is not allowed as a custom method name'
});
assert.throws(() => feathers().use('/dummy', dummyService, {
methods: ['create', 'setup']
}), {
message: '\'setup\' on service \'dummy\' is not allowed as a custom method name'
});
});

it('can use a root level service', async () => {
const app = feathers().use('/', {
async get (id: string) {
Expand Down

0 comments on commit 6584a21

Please # to comment.