Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

Check npm package names before the manifest request (#984)

Nguyen Xuan An6c1f5aefbd33
diff --git a/src/marketplace-npm.js b/src/marketplace-npm.js
new file mode 100644
index 00000000..d7660d2b
--- /dev/null
+++ b/src/marketplace-npm.js
@@ -0,0 +1,8 @@
+// npm's own rule for package names, so a name from the search result cannot steer the manifest request elsewhere.
+const NPM_PACKAGE_NAME = /^(?:@[a-z0-9-~][a-z0-9-._~]*\/)?[a-z0-9-~][a-z0-9-._~]*$/;
+
+/** The registry path of a package: every character encoded, then the scope's @ restored, as the registry expects. */
+export function npmManifestPath(name) {
+ if (typeof name !== 'string' || name.length > 214 || !NPM_PACKAGE_NAME.test(name)) return null;
+ return `${encodeURIComponent(name).replace(/^%40/, '@')}/latest`;
+}
diff --git a/src/marketplace.js b/src/marketplace.js
index 06442873..af783095 100644
--- a/src/marketplace.js
+++ b/src/marketplace.js
@@ -1,6 +1,7 @@
import './site.css';
import './brand.js';
import './marketplace.css';
+import { npmManifestPath } from './marketplace-npm.js';
const NPM_SEARCH_URL = 'https://registry.npmjs.org/-/v1/search?text=keywords:67js-addon&size=50';
const NPM_MANIFEST_URL = 'https://registry.npmjs.org/';
@@ -155,8 +156,10 @@ function addonCard(addon) {
}
async function fetchLatestRange(name) {
+ const path = npmManifestPath(name);
+ if (!path) return null;
try {
- const response = await fetch(`${NPM_MANIFEST_URL}${name.replace('/', '%2F')}/latest`);
+ const response = await fetch(`${NPM_MANIFEST_URL}${path}`);
if (!response.ok) return null;
const manifest = await response.json();
return typeof manifest['67js']?.range === 'string' ? manifest['67js'].range : null;
diff --git a/tests/marketplace-npm.test.js b/tests/marketplace-npm.test.js
new file mode 100644
index 00000000..487c117b
--- /dev/null
+++ b/tests/marketplace-npm.test.js
@@ -0,0 +1,14 @@
+import test from 'node:test';
+import assert from 'node:assert/strict';
+import { npmManifestPath } from '../src/marketplace-npm.js';
+
+test('a plain or scoped npm name becomes its registry manifest path', () => {
+ assert.equal(npmManifestPath('hover-controller'), 'hover-controller/latest');
+ assert.equal(npmManifestPath('@studio/67js-hover'), '@studio%2F67js-hover/latest');
+});
+
+test('a name that npm would not publish is refused instead of reaching the request', () => {
+ for (const name of ['../../-/user', '@a/b/c', 'a/b', 'UPPER', 'a?x=1', 'a#b', '', ' a', 'a'.repeat(215), null, 42]) {
+ assert.equal(npmManifestPath(name), null, `refuses ${String(name).slice(0, 20)}`);
+ }
+});