From 69685a6d7cfd613908f88d6d056ac41589766380 Mon Sep 17 00:00:00 2001 From: Joao Moreno Date: Tue, 17 Nov 2015 15:38:32 +0100 Subject: [PATCH] validate publisher and extension name fixes #20 --- src/package.ts | 12 ++++------ src/test/validation.test.ts | 48 +++++++++++++++++++++++++++++++++++++ src/validation.ts | 14 ++++++++++- 3 files changed, 65 insertions(+), 9 deletions(-) create mode 100644 src/test/validation.test.ts diff --git a/src/package.ts b/src/package.ts index 193dcb4..c89eaaf 100644 --- a/src/package.ts +++ b/src/package.ts @@ -10,6 +10,7 @@ import * as minimatch from 'minimatch'; import * as denodeify from 'denodeify'; import * as mime from 'mime'; import * as urljoin from 'url-join'; +import { validatePublisher, validateExtensionName } from './validation'; interface IReadFile { (filePath: string): Promise; @@ -226,13 +227,8 @@ class IconProcessor extends BaseProcessor { } export function validateManifest(manifest: Manifest): Manifest { - if (!manifest.publisher) { - throw new Error('Manifest missing field: publisher'); - } - - if (!manifest.name) { - throw new Error('Manifest missing field: name'); - } + validatePublisher(manifest.publisher); + validateExtensionName(manifest.name); if (!manifest.version) { throw new Error('Manifest missing field: version'); @@ -245,7 +241,7 @@ export function validateManifest(manifest: Manifest): Manifest { if (!manifest.engines['vscode']) { throw new Error('Manifest missing field: engines.vscode'); } - + return manifest; } diff --git a/src/test/validation.test.ts b/src/test/validation.test.ts new file mode 100644 index 0000000..7ef196b --- /dev/null +++ b/src/test/validation.test.ts @@ -0,0 +1,48 @@ +import * as assert from 'assert'; +import { validatePublisher, validateExtensionName } from '../validation'; + +describe('validatePublisher', () => { + it('should throw with empty', () => { + assert.throws(() => validatePublisher(null)); + assert.throws(() => validatePublisher(void 0)); + assert.throws(() => validatePublisher('')); + }); + + it('should validate', () => { + validatePublisher('hello'); + validatePublisher('Hello'); + validatePublisher('HelloWorld'); + validatePublisher('Hello-World'); + validatePublisher('Hell0-World'); + + assert.throws(() => validatePublisher('hello.')); + assert.throws(() => validatePublisher('.hello')); + assert.throws(() => validatePublisher('h ello')); + assert.throws(() => validatePublisher('hello world')); + assert.throws(() => validatePublisher('-hello')); + assert.throws(() => validatePublisher('-')); + }); +}); + +describe('validateExtensionName', () => { + it('should throw with empty', () => { + assert.throws(() => validateExtensionName(null)); + assert.throws(() => validateExtensionName(void 0)); + assert.throws(() => validateExtensionName('')); + }); + + it('should validate', () => { + validateExtensionName('hello'); + validateExtensionName('Hello'); + validateExtensionName('HelloWorld'); + validateExtensionName('Hello-World'); + validateExtensionName('Hell0-World'); + + assert.throws(() => validateExtensionName('hello.')); + assert.throws(() => validateExtensionName('.hello')); + assert.throws(() => validateExtensionName('h ello')); + assert.throws(() => validateExtensionName('hello world')); + assert.throws(() => validateExtensionName('-hello')); + assert.throws(() => validateExtensionName('-')); + }); +}); \ No newline at end of file diff --git a/src/validation.ts b/src/validation.ts index b7f0718..1d336a0 100644 --- a/src/validation.ts +++ b/src/validation.ts @@ -1,9 +1,21 @@ +const nameRegex = /^[a-z0-9][a-z0-9\-]*$/i; + export function validatePublisher(publisher: string): void { if (!publisher) { throw new Error(`Missing publisher name`); } - if (!/^[a-z0-9][a-z0-9\-]*$/i.test(publisher)) { + if (!nameRegex.test(publisher)) { throw new Error(`Invalid publisher '${ publisher }'`); } +} + +export function validateExtensionName(name: string): void { + if (!name) { + throw new Error(`Missing extension name`); + } + + if (!nameRegex.test(name)) { + throw new Error(`Invalid extension name '${ name }'`); + } } \ No newline at end of file