1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-01-02 12:47:41 +02:00

Plugin Repo: Disallow invalid plugin version numbers

This commit is contained in:
Laurent Cozic 2021-12-21 10:50:32 +01:00
parent cf661b454b
commit bbe4d09260
5 changed files with 60 additions and 0 deletions

View File

@ -1539,6 +1539,12 @@ packages/lib/services/plugins/utils/validatePluginId.js.map
packages/lib/services/plugins/utils/validatePluginId.test.d.ts
packages/lib/services/plugins/utils/validatePluginId.test.js
packages/lib/services/plugins/utils/validatePluginId.test.js.map
packages/lib/services/plugins/utils/validatePluginVersion.d.ts
packages/lib/services/plugins/utils/validatePluginVersion.js
packages/lib/services/plugins/utils/validatePluginVersion.js.map
packages/lib/services/plugins/utils/validatePluginVersion.test.d.ts
packages/lib/services/plugins/utils/validatePluginVersion.test.js
packages/lib/services/plugins/utils/validatePluginVersion.test.js.map
packages/lib/services/rest/Api.d.ts
packages/lib/services/rest/Api.js
packages/lib/services/rest/Api.js.map

6
.gitignore vendored
View File

@ -1530,6 +1530,12 @@ packages/lib/services/plugins/utils/validatePluginId.js.map
packages/lib/services/plugins/utils/validatePluginId.test.d.ts
packages/lib/services/plugins/utils/validatePluginId.test.js
packages/lib/services/plugins/utils/validatePluginId.test.js.map
packages/lib/services/plugins/utils/validatePluginVersion.d.ts
packages/lib/services/plugins/utils/validatePluginVersion.js
packages/lib/services/plugins/utils/validatePluginVersion.js.map
packages/lib/services/plugins/utils/validatePluginVersion.test.d.ts
packages/lib/services/plugins/utils/validatePluginVersion.test.js
packages/lib/services/plugins/utils/validatePluginVersion.test.js.map
packages/lib/services/rest/Api.d.ts
packages/lib/services/rest/Api.js
packages/lib/services/rest/Api.js.map

View File

@ -0,0 +1,32 @@
import validatePluginVersion from './validatePluginVersion';
describe('validatePluginVersion', () => {
test('should validate a version number', () => {
const okCases = [
'1.2.3',
'3',
'1.3-beta',
'10-re',
'4-ALPHA',
];
const errorCases = [
'',
'3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196442881097566593344612847564823378678316527120190914564856692346034861045432664821339360726024914127372458700660631558817',
'0.14.1 OHNO',
'-',
'-1.5',
'1.6-',
];
for (const t of okCases) {
expect(() => validatePluginVersion(t)).not.toThrow();
}
for (const t of errorCases) {
expect(() => validatePluginVersion(t)).toThrow();
}
});
});

View File

@ -0,0 +1,14 @@
export default function(version: string): void {
if (!version || !version.length) throw new Error('Version number cannot be empty');
const maxLength = 64;
if (version.length > 64) throw new Error(`Version number cannot be longer than ${maxLength} characters`);
const patternStart = '^[0-9a-zA-Z].*$';
const patternEnd = '^.*[0-9a-zA-Z]$';
const pattern = '^[0-9a-zA-Z-.]+$';
if (!version.match(new RegExp(patternStart))) throw new Error(`Version number must match pattern /${patternStart}/`);
if (!version.match(new RegExp(patternEnd))) throw new Error(`Version number must match pattern /${patternEnd}/`);
if (!version.match(new RegExp(pattern))) throw new Error(`Version number must match pattern /${pattern}/`);
}

View File

@ -4,6 +4,7 @@ import * as fs from 'fs-extra';
import * as path from 'path';
import * as process from 'process';
import validatePluginId from '@joplin/lib/services/plugins/utils/validatePluginId';
import validatePluginVersion from '@joplin/lib/services/plugins/utils/validatePluginVersion';
import { execCommand2, resolveRelativePathWithinDir, gitPullTry, gitRepoCleanTry, gitRepoClean } from '@joplin/tools/tool-utils.js';
import checkIfPluginCanBeAdded from './lib/checkIfPluginCanBeAdded';
import updateReadme from './lib/updateReadme';
@ -61,6 +62,7 @@ async function extractPluginFilesFromPackage(existingManifests: any, workDir: st
// manifest properties are checked when the plugin is loaded into the app.
const manifest = await readJsonFile(manifestFilePath);
validatePluginId(manifest.id);
validatePluginVersion(manifest.version);
manifest._npm_package_name = packageName;