@vltpkg/spec 1.0.1 → 1.0.2

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/dist/browser.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import type { Range } from '@vltpkg/semver';
2
2
  import type { GitSelectorParsed, Scope, SpecLike, SpecOptions, SpecOptionsFilled, SpecType } from './types.ts';
3
3
  export * from './types.ts';
4
+ export * from './valid-name.ts';
4
5
  export declare const kCustomInspect: unique symbol;
5
6
  export declare const defaultRegistryName = "npm";
6
7
  export declare const defaultRegistries: {
package/dist/browser.js CHANGED
@@ -1,6 +1,8 @@
1
1
  import { error, typeError } from '@vltpkg/error-cause';
2
2
  import { parseRange } from '@vltpkg/semver';
3
+ import { assertPathSafeName } from "./valid-name.js";
3
4
  export * from "./types.js";
5
+ export * from "./valid-name.js";
4
6
  export const kCustomInspect = Symbol.for('nodejs.util.inspect.custom');
5
7
  export const defaultRegistryName = 'npm';
6
8
  export const defaultRegistries = {
@@ -152,6 +154,7 @@ export class Spec {
152
154
  parsed.name = parsed.subspec.name;
153
155
  }
154
156
  parsed.spec = `${parsed.name}@${parsed.bareSpec}`;
157
+ assertPathSafeName(parsed.name, parsed.spec);
155
158
  }
156
159
  return parsed;
157
160
  }
@@ -246,6 +249,8 @@ export class Spec {
246
249
  this.#parseScope(spec);
247
250
  this.bareSpec = bareOrOptions;
248
251
  this.spec = `${this.name}@${bareOrOptions}`;
252
+ // the dependency alias becomes a path segment
253
+ assertPathSafeName(this.name, this.spec);
249
254
  }
250
255
  else {
251
256
  this.spec = spec;
@@ -273,6 +278,7 @@ export class Spec {
273
278
  spec += '@';
274
279
  }
275
280
  this.name = spec.substring(0, at);
281
+ assertPathSafeName(this.name, spec);
276
282
  if (hasScope)
277
283
  this.#parseScope(this.name);
278
284
  this.bareSpec = spec.substring(at + 1);
@@ -424,6 +430,8 @@ export class Spec {
424
430
  if (this.subspec && this.name === this.bareSpec) {
425
431
  this.name = this.subspec.name;
426
432
  this.spec = `${this.name}@${this.bareSpec}`;
433
+ // name derived from the bareSpec, eg `npm:../../x@1`
434
+ assertPathSafeName(this.name, this.spec);
427
435
  }
428
436
  this.#guessRegistryTarball();
429
437
  return;
@@ -610,6 +618,8 @@ export class Spec {
610
618
  else {
611
619
  this.name = s.substring(0, s.indexOf('@', 1));
612
620
  }
621
+ // name derived from the bareSpec, eg `jsr:../../x@1`
622
+ assertPathSafeName(this.name, s);
613
623
  }
614
624
  const reg = `${this.namedJsrRegistry}:`;
615
625
  const n = `${reg}${this.name}`;
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Is a given value usable as a filesystem path segment?
3
+ */
4
+ export declare const isPathSafeName: (name: unknown) => boolean;
5
+ /**
6
+ * Throw unless `name` is usable as a filesystem path segment. `from` is
7
+ * the actionable origin of the name: a tarball URL, git spec, or manifest
8
+ * path.
9
+ */
10
+ export declare const assertPathSafeName: (name: unknown, from?: string) => void;
@@ -0,0 +1,30 @@
1
+ import { error } from '@vltpkg/error-cause';
2
+ /**
3
+ * A package name that is safe to use as a filesystem path segment.
4
+ *
5
+ * Rejects: empty, absolute (`/x`, `\x`, `C:x`), any `/` outside a
6
+ * well-formed `@scope/name`, any `\`, control characters and NUL, `.` or
7
+ * `..` in either segment, and the reserved store dirs `node_modules` and
8
+ * `.vlt`. Allows legacy-ugly but harmless names: leading `_`, uppercase,
9
+ * spaces, non-ASCII, `~`.
10
+ */
11
+ const pathSafeName = /^(?!(?:node_modules|\.vlt)$)(?![A-Za-z]:)(?:@(?!\.\.?\/)[^/\\\x00-\x1f\x7f]+\/)?(?!\.\.?$)[^/\\\x00-\x1f\x7f]+$/i;
12
+ /**
13
+ * Is a given value usable as a filesystem path segment?
14
+ */
15
+ export const isPathSafeName = (name) => typeof name === 'string' && pathSafeName.test(name);
16
+ /**
17
+ * Throw unless `name` is usable as a filesystem path segment. `from` is
18
+ * the actionable origin of the name: a tarball URL, git spec, or manifest
19
+ * path.
20
+ */
21
+ export const assertPathSafeName = (name, from) => {
22
+ if (isPathSafeName(name))
23
+ return;
24
+ throw error('Invalid package name: not usable as a path segment', {
25
+ code: 'EINVALIDNAME',
26
+ found: name,
27
+ from,
28
+ wanted: 'a single path segment, or a well-formed @scope/name',
29
+ }, assertPathSafeName);
30
+ };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@vltpkg/spec",
3
3
  "description": "Package specifier library",
4
- "version": "1.0.1",
4
+ "version": "1.0.2",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "git+https://github.com/vltpkg/vltpkg.git",
@@ -13,8 +13,8 @@
13
13
  "url": "http://vlt.sh"
14
14
  },
15
15
  "dependencies": {
16
- "@vltpkg/error-cause": "1.0.1",
17
- "@vltpkg/semver": "1.0.1"
16
+ "@vltpkg/error-cause": "1.0.2",
17
+ "@vltpkg/semver": "1.0.2"
18
18
  },
19
19
  "devDependencies": {
20
20
  "@eslint/js": "^9.39.1",