Skip to content

Commit 4a2687e

Browse files
authored
fix(remoteplugin): validate registered function names #521
1 parent ddaaa67 commit 4a2687e

4 files changed

Lines changed: 21 additions & 0 deletions

File tree

packages/decorators/src/plugin/function.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ import { FunctionOptions } from './types';
33

44
export function Function(name: string, options: FunctionOptions = {}) {
55
return function (cls: any, methodName: string | null) {
6+
// Validate that function name starts with a capital letter
7+
if (!/^[A-Z]/.test(name)) {
8+
throw new Error(`Function: function name '${name}' must start with a capital letter`);
9+
}
610
const sync = options && !!options.sync;
711
const isMethod = typeof methodName === 'string';
812
const f = isMethod ? cls[methodName] : cls;

packages/neovim/src/host/NvimPlugin.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ describe('NvimPlugin', () => {
6868
expect(plugin.functions.MyFunction).toEqual({ fn, spec });
6969
});
7070

71+
it('should not register functions with lowercase names', () => {
72+
const plugin = new NvimPlugin('/tmp/filename', () => {}, getFakeNvimClient());
73+
plugin.registerFunction('myLowercaseFunction', () => {}, {});
74+
expect(Object.keys(plugin.functions)).toHaveLength(0);
75+
});
76+
7177
it('should not add autocmds with no pattern option', () => {
7278
const plugin = new NvimPlugin('/tmp/filename', () => {}, getFakeNvimClient());
7379
plugin.registerAutocmd('BufWritePre', () => {}, { pattern: '' });

packages/neovim/src/host/NvimPlugin.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,13 @@ export class NvimPlugin {
165165
registerFunction(name: string, fn: [any, Function], options?: NvimFunctionOptions): void;
166166

167167
registerFunction(name: string, fn: any, options?: NvimFunctionOptions): void {
168+
if (!/^[A-Z]/.test(name)) {
169+
this.nvim.logger.error(
170+
`registerFunction: function name '${name}' must start with a capital letter`
171+
);
172+
return;
173+
}
174+
168175
const spec: Spec = {
169176
type: 'function',
170177
name,

packages/neovim/src/plugin/function.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ export function nvimFunction(name: string, options: NvimFunctionOptions = {}) {
1313
// sync,
1414
// ...opts,
1515
// } = options;
16+
// Validate that function name starts with a capital letter
17+
if (!/^[A-Z]/.test(name)) {
18+
throw new Error(`nvimFunction: function name '${name}' must start with a capital letter`);
19+
}
1620
const sync = options && !!options.sync;
1721
const isMethod = typeof methodName === 'string';
1822
const f = isMethod ? cls[methodName] : cls;

0 commit comments

Comments
 (0)