@socketsecurity/lib 5.20.1 → 5.23.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.
Files changed (52) hide show
  1. package/CHANGELOG.md +116 -95
  2. package/README.md +24 -181
  3. package/dist/archives.js +13 -0
  4. package/dist/cacache.js +6 -8
  5. package/dist/cache-with-ttl.js +1 -1
  6. package/dist/constants/socket.js +1 -1
  7. package/dist/dlx/detect.js +25 -8
  8. package/dist/dlx/manifest.js +8 -19
  9. package/dist/dlx/package.js +16 -2
  10. package/dist/env/socket-cli.d.ts +4 -3
  11. package/dist/env/socket-cli.js +1 -1
  12. package/dist/errors.d.ts +96 -2
  13. package/dist/errors.js +55 -0
  14. package/dist/external/pony-cause.js +12 -11
  15. package/dist/fs.js +8 -2
  16. package/dist/github.js +3 -2
  17. package/dist/globs.js +5 -1
  18. package/dist/ipc.js +2 -2
  19. package/dist/json/edit.js +3 -2
  20. package/dist/json/parse.d.ts +47 -2
  21. package/dist/json/parse.js +40 -2
  22. package/dist/json/types.d.ts +49 -0
  23. package/dist/memoization.d.ts +4 -23
  24. package/dist/memoization.js +14 -54
  25. package/dist/packages/isolation.js +4 -4
  26. package/dist/packages/specs.js +9 -2
  27. package/dist/performance.js +3 -2
  28. package/dist/process-lock.js +4 -12
  29. package/dist/promise-queue.d.ts +9 -4
  30. package/dist/promise-queue.js +9 -7
  31. package/dist/promises.d.ts +41 -0
  32. package/dist/promises.js +19 -2
  33. package/dist/regexps.d.ts +4 -13
  34. package/dist/regexps.js +60 -3
  35. package/dist/releases/github.js +3 -2
  36. package/dist/releases/socket-btm.d.ts +61 -5
  37. package/dist/releases/socket-btm.js +2 -2
  38. package/dist/schema/parse.d.ts +26 -0
  39. package/dist/schema/parse.js +38 -0
  40. package/dist/schema/types.d.ts +121 -0
  41. package/dist/schema/validate.d.ts +35 -0
  42. package/dist/{validation/validate-schema.js → schema/validate.js} +4 -14
  43. package/dist/suppress-warnings.js +0 -2
  44. package/dist/url.js +5 -1
  45. package/dist/versions.js +2 -2
  46. package/dist/words.js +4 -7
  47. package/package.json +15 -15
  48. package/dist/validation/json-parser.d.ts +0 -58
  49. package/dist/validation/json-parser.js +0 -63
  50. package/dist/validation/types.d.ts +0 -118
  51. package/dist/validation/validate-schema.d.ts +0 -124
  52. /package/dist/{validation → schema}/types.js +0 -0
@@ -1,63 +0,0 @@
1
- "use strict";
2
- /* Socket Lib - Built with esbuild */
3
- "use strict";
4
- var __defProp = Object.defineProperty;
5
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
- var __getOwnPropNames = Object.getOwnPropertyNames;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __export = (target, all) => {
9
- for (var name in all)
10
- __defProp(target, name, { get: all[name], enumerable: true });
11
- };
12
- var __copyProps = (to, from, except, desc) => {
13
- if (from && typeof from === "object" || typeof from === "function") {
14
- for (let key of __getOwnPropNames(from))
15
- if (!__hasOwnProp.call(to, key) && key !== except)
16
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
- }
18
- return to;
19
- };
20
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
21
- var json_parser_exports = {};
22
- __export(json_parser_exports, {
23
- safeJsonParse: () => safeJsonParse
24
- });
25
- module.exports = __toCommonJS(json_parser_exports);
26
- const DANGEROUS_KEYS = /* @__PURE__ */ new Set(["__proto__", "constructor", "prototype"]);
27
- function prototypePollutionReviver(key, value) {
28
- if (DANGEROUS_KEYS.has(key)) {
29
- throw new Error(
30
- "JSON contains potentially malicious prototype pollution keys"
31
- );
32
- }
33
- return value;
34
- }
35
- function safeJsonParse(jsonString, schema, options = {}) {
36
- const { allowPrototype = false, maxSize = 10 * 1024 * 1024 } = options;
37
- const byteLength = Buffer.byteLength(jsonString, "utf8");
38
- if (byteLength > maxSize) {
39
- throw new Error(
40
- `JSON string exceeds maximum size limit${maxSize !== 10 * 1024 * 1024 ? ` of ${maxSize} bytes` : ""}`
41
- );
42
- }
43
- let parsed;
44
- try {
45
- parsed = allowPrototype ? JSON.parse(jsonString) : JSON.parse(jsonString, prototypePollutionReviver);
46
- } catch (error) {
47
- throw new Error(`Failed to parse JSON: ${error}`);
48
- }
49
- if (schema) {
50
- const result = schema.safeParse(parsed);
51
- if (!result.success) {
52
- const error = result.error;
53
- const errors = error.issues.map((issue) => `${issue.path.join(".")}: ${issue.message}`).join(", ");
54
- throw new Error(`Validation failed: ${errors}`);
55
- }
56
- return result.data;
57
- }
58
- return parsed;
59
- }
60
- // Annotate the CommonJS export names for ESM import in node:
61
- 0 && (module.exports = {
62
- safeJsonParse
63
- });
@@ -1,118 +0,0 @@
1
- /**
2
- * @fileoverview Validation type definitions.
3
- * Provides core types for schema validation and JSON parsing with security features.
4
- */
5
- /**
6
- * Result of a schema validation operation.
7
- * Contains either successful parsed data or error information.
8
- *
9
- * @template T - The expected type of the parsed data
10
- *
11
- * @example
12
- * ```ts
13
- * const result: ParseResult<User> = schema.safeParse(data)
14
- * if (result.success) {
15
- * console.log(result.data) // User object
16
- * } else {
17
- * console.error(result.error) // Error details
18
- * }
19
- * ```
20
- */
21
- export interface ParseResult<T> {
22
- /** Indicates whether parsing was successful */
23
- success: boolean;
24
- /** Parsed and validated data (only present when `success` is `true`) */
25
- data?: T | undefined;
26
- /** Error information (only present when `success` is `false`) */
27
- error?: unknown;
28
- }
29
- /**
30
- * Base schema interface compatible with Zod and similar validation libraries.
31
- * Provides both safe and throwing parsing methods.
32
- *
33
- * @template T - The expected output type after validation
34
- *
35
- * @example
36
- * ```ts
37
- * import { z } from 'zod'
38
- *
39
- * const userSchema = z.object({
40
- * name: z.string(),
41
- * age: z.number()
42
- * })
43
- *
44
- * // Schema satisfies this interface
45
- * const schema: Schema<User> = userSchema
46
- * const result = schema.safeParse({ name: 'Alice', age: 30 })
47
- * ```
48
- */
49
- export interface Schema<T = unknown> {
50
- /**
51
- * Safely parse data without throwing errors.
52
- * Returns a result object indicating success or failure.
53
- *
54
- * @param data - The data to validate
55
- * @returns Parse result with success flag and data or error
56
- */
57
- safeParse(data: unknown): ParseResult<T>;
58
- /**
59
- * Parse data and throw an error if validation fails.
60
- * Use this when you want to fail fast on invalid data.
61
- *
62
- * @param data - The data to validate
63
- * @returns The validated and parsed data
64
- * @throws {Error} When validation fails
65
- */
66
- parse(data: unknown): T;
67
- /**
68
- * Optional schema name for debugging and error messages.
69
- * Useful for identifying which schema failed in complex validation chains.
70
- */
71
- _name?: string | undefined;
72
- }
73
- /**
74
- * Options for configuring safe JSON parsing with security controls.
75
- * Distinct from `JsonParseOptions` in `@socketsecurity/lib/json/types`
76
- * which is scoped to reviver/error-handling for fs-oriented JSON reads.
77
- *
78
- * @example
79
- * ```ts
80
- * const options: SafeJsonParseOptions = {
81
- * maxSize: 1024 * 1024, // 1MB limit
82
- * allowPrototype: false // Block prototype pollution
83
- * }
84
- * ```
85
- */
86
- export interface SafeJsonParseOptions {
87
- /**
88
- * Allow dangerous prototype pollution keys (`__proto__`, `constructor`, `prototype`).
89
- * Set to `true` only if you trust the JSON source completely.
90
- *
91
- * @default false
92
- *
93
- * @example
94
- * ```ts
95
- * // Will throw error by default
96
- * safeJsonParse('{"__proto__": {"polluted": true}}')
97
- *
98
- * // Allows the parse (dangerous!)
99
- * safeJsonParse('{"__proto__": {"polluted": true}}', undefined, {
100
- * allowPrototype: true
101
- * })
102
- * ```
103
- */
104
- allowPrototype?: boolean | undefined;
105
- /**
106
- * Maximum allowed size of JSON string in bytes.
107
- * Prevents memory exhaustion from extremely large payloads.
108
- *
109
- * @default 10_485_760 (10 MB)
110
- *
111
- * @example
112
- * ```ts
113
- * // Limit to 1KB
114
- * safeJsonParse(jsonString, undefined, { maxSize: 1024 })
115
- * ```
116
- */
117
- maxSize?: number | undefined;
118
- }
@@ -1,124 +0,0 @@
1
- /**
2
- * @fileoverview Universal schema validation for Zod-style schemas (Zod v3,
3
- * v4, and any `safeParse`-shaped duck type).
4
- *
5
- * Accepts a schema and returns a tagged result.
6
- * - `{ ok: true, value }` — validation passed, `value` is typed as the
7
- * schema's inferred output (`z.infer<typeof S>`).
8
- * - `{ ok: false, errors }` — validation failed, `errors` is a normalized
9
- * list of `{ path, message }`.
10
- *
11
- * Zod is detected purely structurally via `.safeParse` — no runtime import of
12
- * the `zod` package is required by socket-lib.
13
- *
14
- * @internal
15
- * Socket-lib additionally recognizes TypeBox schemas for its own internal
16
- * use (e.g. `src/ipc.ts`'s stub-file validation). That path is not a
17
- * supported consumer API — callers should use Zod.
18
- */
19
- import type { Schema } from './types';
20
- /**
21
- * TypeBox's `Kind` symbol. We reference it structurally for schema detection
22
- * rather than importing it from `@sinclair/typebox` — detection scans the
23
- * schema's own-symbol keys for one whose description is `'TypeBox.Kind'`.
24
- * The `Value` runtime is only loaded lazily when a TypeBox schema is seen.
25
- */
26
- type TypeBoxKindSymbol = symbol & {
27
- __typeBoxKindBrand?: never;
28
- };
29
- /**
30
- * Structural minimum of a TypeBox `TSchema`. The phantom `static` field is
31
- * the type TypeBox uses for inference (`Static<T> = T['static']`).
32
- */
33
- interface TypeBoxLikeSchema {
34
- [k: TypeBoxKindSymbol]: string;
35
- static: unknown;
36
- }
37
- /**
38
- * Structural shape of a Zod v4 schema — carries output type on `_zod.output`.
39
- */
40
- interface ZodV4LikeSchema<O = unknown> {
41
- _zod: {
42
- output: O;
43
- };
44
- safeParse(data: unknown): unknown;
45
- }
46
- /**
47
- * Structural shape of a Zod v3 schema — carries output type on `_output`.
48
- */
49
- interface ZodV3LikeSchema<O = unknown> {
50
- _output: O;
51
- safeParse(data: unknown): unknown;
52
- }
53
- /**
54
- * Any schema kind this helper accepts.
55
- */
56
- export type AnySchema = TypeBoxLikeSchema | ZodV4LikeSchema<unknown> | ZodV3LikeSchema<unknown> | Schema<unknown>;
57
- /**
58
- * Infer the validated output type from any supported schema kind.
59
- *
60
- * Order matters: TypeBox schemas also carry a phantom `static` field, so we
61
- * check for TypeBox before falling through to Zod and the duck-type.
62
- */
63
- export type Infer<S> = S extends {
64
- static: infer Static;
65
- } ? Static : S extends {
66
- _zod: {
67
- output: infer O;
68
- };
69
- } ? O : S extends {
70
- _output: infer O;
71
- } ? O : S extends Schema<infer T> ? T : unknown;
72
- /**
73
- * A single normalized validation error.
74
- * - `path` is a dotted or slash-separated identifier locating the bad value.
75
- * - `message` is human-readable.
76
- */
77
- export interface ValidationIssue {
78
- /** Array path into the value (e.g. `['user', 'age']`). */
79
- path: Array<string | number>;
80
- /** Human-readable description of the failure. */
81
- message: string;
82
- }
83
- /**
84
- * Tagged-union result of {@link validateSchema}. Callers narrow on `ok`.
85
- */
86
- export type ValidateResult<T> = {
87
- ok: true;
88
- value: T;
89
- } | {
90
- ok: false;
91
- errors: ValidationIssue[];
92
- };
93
- /**
94
- * Validate `data` against a Zod-style `schema`. Non-throwing.
95
- *
96
- * Accepted schemas:
97
- * - `zod` schemas, v3 and v4 (detected via `.safeParse` on the schema)
98
- * - Any object conforming to {@link Schema} (the socket-lib duck type)
99
- *
100
- * The return type narrows `value` to {@link Infer | `Infer<S>`}, so callers
101
- * get `z.infer<typeof S>` with no casts.
102
- *
103
- * @example
104
- * ```ts
105
- * import { z } from 'zod'
106
- * const U = z.object({ name: z.string() })
107
- * const r = validateSchema(U, data)
108
- * if (r.ok) r.value.name // string
109
- * ```
110
- *
111
- * Errors are normalized to {@link ValidationIssue}: `{ path, message }`.
112
- */
113
- export declare function validateSchema<S>(schema: S, data: unknown): ValidateResult<Infer<S>>;
114
- /**
115
- * Parse `data` against `schema` and return the validated value. Throws if
116
- * validation fails. This is the throwing twin of {@link validateSchema}.
117
- *
118
- * Use when you want fail-fast semantics at a trust boundary. For recoverable
119
- * validation (form input, external configs), prefer {@link validateSchema}.
120
- *
121
- * @throws {Error} When validation fails. The message lists all issues.
122
- */
123
- export declare function parseSchema<S>(schema: S, data: unknown): Infer<S>;
124
- export {};
File without changes