dsh-plugin-explorer 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  /**
2
- * Host half of the plugin finder: serves the community plugin directory to the Web page.
2
+ * Host half of the plugin explorer: serves the community plugin directory to
3
+ * the Web page, plus the pre-install scanner and the installed-plugin inventory.
3
4
  *
4
5
  * The directory is bundled with the package and needs no network. `?refresh=1` re-reads the
5
6
  * upstream CC0 catalog (`dsh-plugin-catalog` on jsDelivr) and replaces the served copy for the
@@ -7,9 +8,13 @@
7
8
  */
8
9
  import { readFileSync } from 'node:fs'
9
10
  import { normalizeCatalog } from './lib/catalog.js'
11
+ import { scanPackage } from './lib/gate-scan.js'
12
+ import { installedWithLatest } from './lib/installed.js'
10
13
 
11
- /** Exact Fetch route below `/api` the browser reads. */
12
- const ROUTE = '/api/plugin-finder/catalog.json'
14
+ /** Exact Fetch routes below `/api` the browser reads. */
15
+ const CATALOG_ROUTE = '/api/plugin-finder/catalog.json'
16
+ const SCAN_ROUTE = '/api/plugin-finder/scan.json'
17
+ const INSTALLED_ROUTE = '/api/plugin-finder/installed.json'
13
18
 
14
19
  /** Upstream catalog document; jsDelivr resolves the tag to the newest published version. */
15
20
  const SOURCE_URL = 'https://cdn.jsdelivr.net/npm/dsh-plugin-catalog@latest/plugins.json'
@@ -17,6 +22,10 @@ const SOURCE_URL = 'https://cdn.jsdelivr.net/npm/dsh-plugin-catalog@latest/plugi
17
22
  /** Deadline for one upstream read, including the body. */
18
23
  const REFRESH_TIMEOUT_MS = 90_000
19
24
 
25
+ /** npm names we are willing to look up (scoped and unscoped). */
26
+ const PACKAGE_NAME = /^(?:@[a-z0-9][\w.-]*\/)?[a-z0-9][\w.-]{0,213}$/i
27
+ const VERSION = /^[0-9A-Za-z][0-9A-Za-z.+_-]{0,63}$/
28
+
20
29
  function respond(body, status = 200) {
21
30
  return new Response(body, {
22
31
  status,
@@ -28,13 +37,17 @@ function respond(body, status = 200) {
28
37
  })
29
38
  }
30
39
 
40
+ function fail(message, status) {
41
+ return respond(JSON.stringify({ error: message }), status)
42
+ }
43
+
31
44
  /** The catalog bundled with this package. */
32
45
  function readBundled() {
33
46
  return readFileSync(new URL('./data/plugins.json', import.meta.url), 'utf8')
34
47
  }
35
48
 
36
49
  /**
37
- * Contribute the catalog route.
50
+ * Contribute the catalog, scan, and installed-plugin routes.
38
51
  * @param ctx - the Host plugin context.
39
52
  */
40
53
  export function apply(ctx) {
@@ -59,9 +72,9 @@ export function apply(ctx) {
59
72
  return pending
60
73
  }
61
74
 
62
- ctx.inject(['connection'], (scope) => {
63
- scope.effect(() => scope.connection.fetch.register({
64
- path: ROUTE,
75
+ const routes = [
76
+ {
77
+ path: CATALOG_ROUTE,
65
78
  methods: ['GET'],
66
79
  requestBody: 'buffered',
67
80
  fetch: async (request) => {
@@ -70,11 +83,53 @@ export function apply(ctx) {
70
83
  await refresh()
71
84
  } catch (error) {
72
85
  const message = error instanceof Error ? error.message : String(error)
73
- return respond(JSON.stringify({ error: message }), 502)
86
+ return fail(message, 502)
74
87
  }
75
88
  }
76
89
  return respond(current())
77
90
  },
78
- }), `plugin-finder: ${ROUTE}`)
91
+ },
92
+ {
93
+ path: SCAN_ROUTE,
94
+ methods: ['GET'],
95
+ requestBody: 'buffered',
96
+ fetch: async (request) => {
97
+ const url = new URL(request.url)
98
+ const name = (url.searchParams.get('name') ?? '').trim()
99
+ const version = (url.searchParams.get('version') ?? '').trim()
100
+ if (!PACKAGE_NAME.test(name) || name.includes('..')) {
101
+ return fail('invalid package name', 400)
102
+ }
103
+ if (version !== '' && !VERSION.test(version)) {
104
+ return fail('invalid version', 400)
105
+ }
106
+ try {
107
+ const result = await scanPackage(name, version === '' ? undefined : version)
108
+ return respond(JSON.stringify(result))
109
+ } catch (error) {
110
+ const message = error instanceof Error ? error.message : String(error)
111
+ return fail(message, 502)
112
+ }
113
+ },
114
+ },
115
+ {
116
+ path: INSTALLED_ROUTE,
117
+ methods: ['GET'],
118
+ requestBody: 'buffered',
119
+ fetch: async () => {
120
+ try {
121
+ return respond(JSON.stringify(await installedWithLatest()))
122
+ } catch (error) {
123
+ const message = error instanceof Error ? error.message : String(error)
124
+ return fail(message, 502)
125
+ }
126
+ },
127
+ },
128
+ ]
129
+
130
+ ctx.inject(['connection'], (scope) => {
131
+ for (const route of routes) {
132
+ scope.effect(() => scope.connection.fetch.register(route), `plugin-finder: ${route.path}`)
133
+ }
79
134
  })
80
135
  }
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 863683348
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,116 @@
1
+ /**
2
+ * dsh-plugin-gate — OSV supply-chain check.
3
+ *
4
+ * Pure module (network helpers take an injected fetch). Queries Google OSV
5
+ * (api.osv.dev) for known vulnerabilities against EXACT dependency versions
6
+ * only — ranges, wildcards, git/http/file specs and the unversioned main
7
+ * package are skipped (mirroring dsh-plugin-vet's policy, avoiding stale
8
+ * whole-history noise). Checks a capped number of direct dependencies,
9
+ * skipping @deepseek-ai/* official packages. Network failures degrade
10
+ * silently to a note; the gate never blocks on OSV availability.
11
+ */
12
+
13
+ import { CATEGORY } from "./rules.js";
14
+
15
+ /** True when a spec is a bare exact version (e.g. "1.2.3", "1.2.3-alpha.1"). */
16
+ export function exactVersion(spec) {
17
+ if (typeof spec !== "string") return null;
18
+ const s = spec.trim();
19
+ if (/^(git|file:|workspace:|link:|npm:|https?:)/i.test(s)) return null;
20
+ if (/[\s*^~><|=]/.test(s)) return null;
21
+ if (!/^\d+(\.\d+){0,2}([-+][0-9A-Za-z.-]+)?$/.test(s)) return null;
22
+ return s;
23
+ }
24
+
25
+ /**
26
+ * Extract exact-version direct dependencies for OSV checking.
27
+ * @param {Record<string,string>} depMap - merged {name: spec} from the package fields.
28
+ * @param {{max?:number, skipScope?:string}} [opts]
29
+ * @returns {Array<{name:string, version:string}>}
30
+ */
31
+ export function exactVersionDeps(depMap, opts = {}) {
32
+ const { max = 8, skipScope = "@deepseek-ai" } = opts;
33
+ const out = [];
34
+ const seen = new Set();
35
+ for (const [name, spec] of Object.entries(depMap || {})) {
36
+ if (out.length >= max) break;
37
+ if (skipScope && name.startsWith(skipScope)) continue;
38
+ if (seen.has(name)) continue;
39
+ const version = exactVersion(spec);
40
+ if (!version) continue;
41
+ seen.add(name);
42
+ out.push({ name, version });
43
+ }
44
+ return out;
45
+ }
46
+
47
+ /**
48
+ * Query OSV for one exact package version.
49
+ * @param {Function} fetchImpl
50
+ * @param {string} name - npm package name.
51
+ * @param {string} version - exact version.
52
+ * @param {{timeoutMs?:number}} [opts]
53
+ * @returns {Promise<{ok:boolean, vulns?:Array<{id:string, summary?:string, aliases?:string[], modified?:string}>, error?:string}>}
54
+ */
55
+ export async function queryOsv(fetchImpl, name, version, opts = {}) {
56
+ const { timeoutMs = 10000 } = opts;
57
+ try {
58
+ const res = await fetchImpl("https://api.osv.dev/v1/query", {
59
+ method: "POST",
60
+ headers: { "Content-Type": "application/json" },
61
+ body: JSON.stringify({ package: { name, ecosystem: "npm" }, version }),
62
+ signal: AbortSignal.timeout(timeoutMs),
63
+ });
64
+ if (!res.ok) throw new Error("osv " + res.status);
65
+ const data = await res.json();
66
+ const vulns = (data.vulns || []).map((v) => ({
67
+ id: v.id,
68
+ summary: String(v.summary || "").slice(0, 200),
69
+ aliases: v.aliases || [],
70
+ modified: v.modified,
71
+ }));
72
+ return { ok: true, vulns };
73
+ } catch (e) {
74
+ return { ok: false, error: String(e && e.message || e) };
75
+ }
76
+ }
77
+
78
+ /**
79
+ * Check a dependency list against OSV and build gate hits/notes.
80
+ * @param {Function} fetchImpl
81
+ * @param {Array<{name:string, version:string}>} deps
82
+ * @param {{timeoutMs?:number}} [opts]
83
+ * @returns {Promise<{hits:Array, notes:Array, checked:number, vulnerable:number}>}
84
+ */
85
+ export async function checkOsvDeps(fetchImpl, deps, opts = {}) {
86
+ const { timeoutMs = 10000 } = opts;
87
+ const hits = [];
88
+ const notes = [];
89
+ let checked = 0;
90
+ let vulnerable = 0;
91
+ for (const dep of deps) {
92
+ const r = await queryOsv(fetchImpl, dep.name, dep.version, { timeoutMs });
93
+ if (!r.ok) {
94
+ notes.push({ file: "package.json", kind: "osv", detail: dep.name + "@" + dep.version + ": " + r.error });
95
+ continue;
96
+ }
97
+ checked += 1;
98
+ for (const v of r.vulns) {
99
+ vulnerable += 1;
100
+ hits.push({
101
+ rule: "osv_vulnerability",
102
+ category: CATEGORY.SCRIPTS,
103
+ severity: "medium",
104
+ file: "package.json",
105
+ line: 1,
106
+ count: 1,
107
+ evidence: dep.name + "@" + dep.version + " affected by " + v.id + (v.summary ? " — " + v.summary : ""),
108
+ hint: "Known vulnerability from Google OSV for this exact dependency version; upgrade to a fixed version if one exists.",
109
+ });
110
+ }
111
+ }
112
+ if (deps.length > 0 && checked === 0) {
113
+ notes.push({ file: "package.json", kind: "osv", detail: "OSV queries unavailable; skipped " + deps.length + " exact-version deps" });
114
+ }
115
+ return { hits, notes, checked, vulnerable };
116
+ }