From 6caf49c281ffe29b1ac816c6f3d185ad18cc2fd5 Mon Sep 17 00:00:00 2001 From: Topaz T Date: Wed, 18 Oct 2023 02:04:00 +0300 Subject: [PATCH] feat: add pub(dart) validation according to spec (#51) --- src/package-url.js | 9 +++++++++ test/data/test-suite-data.json | 24 ++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/src/package-url.js b/src/package-url.js index 09a0688..0fe0293 100644 --- a/src/package-url.js +++ b/src/package-url.js @@ -74,6 +74,12 @@ class PackageURL { _handlePyPi() { this.name = this.name.toLowerCase().replace(/_/g, '-'); } + _handlePub() { + this.name = this.name.toLowerCase(); + if (!/^[a-z0-9_]+$/i.test(this.name)) { + throw new Error('Invalid purl: contains an illegal character.'); + } + } toString() { var purl = ['pkg:', encodeURIComponent(this.type), '/']; @@ -81,6 +87,9 @@ class PackageURL { if (this.type === 'pypi') { this._handlePyPi(); } + if (this.type === 'pub') { + this._handlePub(); + } if (this.namespace) { purl.push( diff --git a/test/data/test-suite-data.json b/test/data/test-suite-data.json index 8084d19..df2cc29 100644 --- a/test/data/test-suite-data.json +++ b/test/data/test-suite-data.json @@ -323,6 +323,30 @@ "subpath": null, "is_invalid": true }, + { + "description": "checks for invalid characters", + "purl": "pkg:pub/flutter_downloader@1.0.0", + "canonical_purl": "pkg:pub/flutter_downloader@1.0.0", + "type": "pub", + "namespace": null, + "name": "flutter_downloader", + "version": "1.0.0", + "qualifiers": null, + "subpath": null, + "is_invalid": false + }, + { + "description": "checks for invalid characters", + "purl": "pkg:pub/flutter--downloader@1.0.0", + "canonical_purl": "pkg:pub/flutter--downloader@1.0.0", + "type": "pub", + "namespace": null, + "name": null, + "version": null, + "qualifiers": null, + "subpath": null, + "is_invalid": true + }, { "description": "namespace can contain special characters", "purl": "pkg:npm/%40foo%40%3F%23/bar@1.0.0",