safe-formdata 0.0.1 → 0.1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 roottool
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.
package/README.md CHANGED
@@ -1,45 +1,151 @@
1
1
  # safe-formdata
2
2
 
3
- ## ⚠️ IMPORTANT NOTICE ⚠️
3
+ **The strict trust boundary for FormData.**
4
4
 
5
- **This package is created solely for the purpose of setting up OIDC (OpenID Connect) trusted publishing with npm.**
5
+ safe-formdata is a **security-focused** parser that establishes a predictable boundary between untrusted input and application logic.
6
+ It enforces strict rules on keys and forbids structural inference by design.
6
7
 
7
- This is **NOT** a functional package and contains **NO** code or functionality beyond the OIDC setup configuration.
8
+ ---
9
+
10
+ ## Overview
11
+
12
+ FormData is untyped and unstructured by nature.
13
+ Many parsers attempt to infer structure or semantics from key naming conventions.
8
14
 
9
- ## Purpose
15
+ safe-formdata intentionally does not.
10
16
 
11
- This package exists to:
12
- 1. Configure OIDC trusted publishing for the package name `safe-formdata`
13
- 2. Enable secure, token-less publishing from CI/CD workflows
14
- 3. Establish provenance for packages published under this name
17
+ It performs only minimal, security-focused parsing and reports
18
+ all structural issues explicitly, without inferring structure, intent, or meaning.
19
+
20
+ ---
15
21
 
16
- ## What is OIDC Trusted Publishing?
22
+ ## Design principles
17
23
 
18
- OIDC trusted publishing allows package maintainers to publish packages directly from their CI/CD workflows without needing to manage npm access tokens. Instead, it uses OpenID Connect to establish trust between the CI/CD provider (like GitHub Actions) and npm.
24
+ - 🧱 **Keys are opaque**
25
+ Key names are never interpreted as structure.
26
+ - 🚫 **No silent fixes**
27
+ Invalid or conflicting input is reported, not corrected.
28
+ - ⚖️ **Parsing is not validation**
29
+ Schema and business logic belong outside the boundary.
30
+ - 🔒️ **Security over convenience**
31
+ Unsafe input is surfaced early and explicitly.
19
32
 
20
- ## Setup Instructions
33
+ ---
21
34
 
22
- To properly configure OIDC trusted publishing for this package:
35
+ ## Security scope
23
36
 
24
- 1. Go to [npmjs.com](https://www.npmjs.com/) and navigate to your package settings
25
- 2. Configure the trusted publisher (e.g., GitHub Actions)
26
- 3. Specify the repository and workflow that should be allowed to publish
27
- 4. Use the configured workflow to publish your actual package
37
+ ### In scope
28
38
 
29
- ## DO NOT USE THIS PACKAGE
39
+ - Forbidden keys (e.g. prototype pollution)
40
+ - Duplicate keys
41
+ - Structurally invalid keys
42
+ - Explicit reporting of all issues
30
43
 
31
- This package is a placeholder for OIDC configuration only. It:
32
- - Contains no executable code
33
- - Provides no functionality
34
- - Should not be installed as a dependency
35
- - Exists only for administrative purposes
44
+ ### Out of scope
36
45
 
37
- ## More Information
46
+ - Value validation or coercion
47
+ - Authentication or authorization
48
+ - Denial-of-service protection
49
+ - Framework-specific behavior
38
50
 
39
- For more details about npm's trusted publishing feature, see:
40
- - [npm Trusted Publishing Documentation](https://docs.npmjs.com/generating-provenance-statements)
41
- - [GitHub Actions OIDC Documentation](https://docs.github.com/en/actions/deployment/security-hardening-your-deployments/about-security-hardening-with-openid-connect)
51
+ safe-formdata reports issues instead of throwing,
52
+ to preserve the integrity of the FormData boundary.
42
53
 
43
54
  ---
44
55
 
45
- **Maintained for OIDC setup purposes only**
56
+ ## API
57
+
58
+ ### parse(formData): ParseResult
59
+
60
+ ```ts
61
+ import { parse } from "safe-formdata";
62
+
63
+ const { data, issues } = parse(formData);
64
+ ```
65
+
66
+ - `data` is `null` if any boundary violations are detected
67
+ - `issues` contains all detected structural issues
68
+ - Partial success is not allowed
69
+
70
+ ### Result
71
+
72
+ ```ts
73
+ export interface ParseResult {
74
+ data: Record<string, string | File> | null;
75
+ issues: ParseIssue[];
76
+ }
77
+ ```
78
+
79
+ - `data` is non-null only when no boundary violations are detected
80
+ - `data` is always a flat object; no structural inference is performed
81
+ - `issues` must always be checked by the caller
82
+
83
+ ### Issues
84
+
85
+ ```ts
86
+ export interface ParseIssue {
87
+ code: "invalid_key" | "forbidden_key" | "duplicate_key";
88
+ path: string[];
89
+ key?: unknown;
90
+ }
91
+ ```
92
+
93
+ - `path` is always empty and exists only for compatibility
94
+ - Issues are informational and are never thrown
95
+
96
+ ## Design decisions (Why not?)
97
+
98
+ safe-formdata intentionally omits several common features.
99
+
100
+ ### Why no structural inference?
101
+
102
+ Keys such as `a[b][c]`, `user.name`, or `items[]`
103
+ are treated as opaque strings, not paths.
104
+
105
+ ```ts
106
+ {
107
+ "a[b][c]": "value"
108
+ }
109
+ ```
110
+
111
+ Inferring structure introduces ambiguity and security risks.
112
+ safe-formdata validates keys, but never constructs objects from them.
113
+
114
+ ### Why no generic type parameters?
115
+
116
+ safe-formdata does not produce typed structural output.
117
+
118
+ Allowing generic types would imply runtime guarantees
119
+ that the library intentionally does not provide.
120
+
121
+ The output type is intentionally flat:
122
+
123
+ ```ts
124
+ Record<string, string | File>;
125
+ ```
126
+
127
+ ### Why no throwing or `parseOrThrow`?
128
+
129
+ FormData is external input.
130
+ Throwing encourages accidental 500 errors and obscures boundary handling.
131
+
132
+ safe-formdata exposes a single, explicit error-handling model:
133
+ inspect issues and decide what to do.
134
+
135
+ ### What is safe-formdata not?
136
+
137
+ - Not a schema validator
138
+ - Not a typed form parser
139
+ - Not a replacement for Zod, Yup, or similar libraries
140
+
141
+ safe-formdata defines a safe boundary.
142
+ Validation and typing belong beyond it.
143
+
144
+ ## Versioning
145
+
146
+ v0.x focuses exclusively on establishing and clarifying the FormData boundary.
147
+ No inference or convenience features will be added within v0.x.
148
+
149
+ ## License
150
+
151
+ MIT
@@ -0,0 +1,19 @@
1
+ type IssueCode = "invalid_key" | "forbidden_key" | "duplicate_key";
2
+
3
+ interface ParseIssue {
4
+ code: IssueCode;
5
+ path: readonly [];
6
+ key?: unknown;
7
+ }
8
+
9
+ type ParseResult = {
10
+ data: Record<string, string | File>;
11
+ issues: [];
12
+ } | {
13
+ data: null;
14
+ issues: ParseIssue[];
15
+ };
16
+
17
+ declare function parse(formData: FormData): ParseResult;
18
+
19
+ export { type IssueCode, type ParseIssue, type ParseResult, parse };
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ var e=new Set(["__proto__","prototype","constructor"]);function t(e,t={}){return{code:e,path:[],...t}}function n(n){const o=Object.create(null),s=[],r=new Set;for(const[u,a]of n.entries())"string"==typeof u&&0!==u.length?e.has(u)?s.push(t("forbidden_key",{key:u})):r.has(u)?s.push(t("duplicate_key",{key:u})):(r.add(u),o[u]=a):s.push(t("invalid_key",{key:u}));return s.length>0?{data:null,issues:s}:{data:o,issues:[]}}export{n as parse};//# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/issues/forbiddenKeys.ts","../src/issues/createIssue.ts","../src/parse.ts"],"sourcesContent":["/**\n * Keys explicitly forbidden to prevent prototype pollution attacks.\n *\n * These keys are reserved properties on `Object.prototype` and must never\n * be allowed in parsed FormData, regardless of their values or context.\n *\n * The forbidden keys are:\n * - `__proto__`: Legacy prototype accessor\n * - `prototype`: Function prototype property\n * - `constructor`: Object constructor reference\n *\n * Any FormData entry containing these keys will trigger a `forbidden_key` issue,\n * causing the parse operation to fail with `data: null`.\n *\n * @see {@link https://github.com/roottool/safe-formdata/blob/main/AGENTS.md#prototype-safety AGENTS.md > Security rules > Prototype safety}\n */\nexport const FORBIDDEN_KEYS = new Set<string>([\n\t\"__proto__\",\n\t\"prototype\",\n\t\"constructor\",\n] as const satisfies readonly string[]);\n","import type { ParseIssue } from \"#types/ParseIssue\";\nimport type { IssueCode } from \"#types/IssueCode\";\n\n/**\n * Payload for creating a ParseIssue.\n *\n * @property {unknown} key - The problematic key that triggered the issue (for debugging)\n */\ninterface IssuePayload {\n\tkey?: unknown;\n}\n\n/**\n * Creates a ParseIssue with the specified code and optional payload.\n *\n * This is an internal utility function for generating structured issue reports\n * during FormData parsing. All issues have an empty `path` array, as this parser\n * does not infer structural relationships.\n *\n * @param code - The type of issue (invalid_key, forbidden_key, or duplicate_key)\n * @param payload - Optional payload containing the problematic key\n * @returns A ParseIssue object ready to be added to the issues array\n *\n * @internal\n */\nexport function createIssue(code: IssueCode, payload: IssuePayload = {}): ParseIssue {\n\treturn {\n\t\tcode,\n\t\tpath: [],\n\t\t...payload,\n\t};\n}\n","import { FORBIDDEN_KEYS } from \"#issues/forbiddenKeys\";\nimport { createIssue } from \"#issues/createIssue\";\nimport type { ParseResult } from \"#types/ParseResult\";\n\n/**\n * Parses FormData into a flat JavaScript object with strict boundary enforcement.\n *\n * This function establishes a security-focused boundary between untrusted FormData input\n * and application logic by:\n * - Detecting duplicate, forbidden, and invalid keys\n * - Treating keys as opaque strings (no structural inference)\n * - Returning null data if any issues are detected (no partial success)\n *\n * @param formData - The FormData instance to parse\n * @returns ParseResult containing either parsed data or issues (never both)\n *\n * @example\n * ```ts\n * const fd = new FormData()\n * fd.append('name', 'alice')\n * const result = parse(fd)\n *\n * if (result.data) {\n * // Success: result.data is { name: 'alice' }\n * } else {\n * // Failure: result.issues contains detected problems\n * }\n * ```\n *\n * @see {@link https://github.com/roottool/safe-formdata/blob/main/AGENTS.md AGENTS.md} for design rules\n */\nexport function parse(formData: FormData): ParseResult {\n\tconst data = Object.create(null) as Record<string, string | File>;\n\tconst issues = [];\n\tconst seenKeys = new Set<string>();\n\n\tfor (const [key, value] of formData.entries()) {\n\t\tif (typeof key !== \"string\" || key.length === 0) {\n\t\t\tissues.push(createIssue(\"invalid_key\", { key }));\n\t\t\tcontinue;\n\t\t}\n\n\t\tif (FORBIDDEN_KEYS.has(key)) {\n\t\t\tissues.push(createIssue(\"forbidden_key\", { key }));\n\t\t\tcontinue;\n\t\t}\n\n\t\tif (seenKeys.has(key)) {\n\t\t\tissues.push(createIssue(\"duplicate_key\", { key }));\n\t\t\tcontinue;\n\t\t}\n\n\t\tseenKeys.add(key);\n\t\tdata[key] = value;\n\t}\n\n\tif (issues.length > 0) {\n\t\treturn {\n\t\t\tdata: null,\n\t\t\tissues,\n\t\t};\n\t}\n\n\treturn {\n\t\tdata,\n\t\tissues: [],\n\t};\n}\n"],"mappings":";AAgBO,IAAM,iBAAiB,oBAAI,IAAY;AAAA,EAC7C;AAAA,EACA;AAAA,EACA;AACD,CAAsC;;;ACK/B,SAAS,YAAY,MAAiB,UAAwB,CAAC,GAAe;AACpF,SAAO;AAAA,IACN;AAAA,IACA,MAAM,CAAC;AAAA,IACP,GAAG;AAAA,EACJ;AACD;;;ACAO,SAAS,MAAM,UAAiC;AACtD,QAAM,OAAO,uBAAO,OAAO,IAAI;AAC/B,QAAM,SAAS,CAAC;AAChB,QAAM,WAAW,oBAAI,IAAY;AAEjC,aAAW,CAAC,KAAK,KAAK,KAAK,SAAS,QAAQ,GAAG;AAC9C,QAAI,OAAO,QAAQ,YAAY,IAAI,WAAW,GAAG;AAChD,aAAO,KAAK,YAAY,eAAe,EAAE,IAAI,CAAC,CAAC;AAC/C;AAAA,IACD;AAEA,QAAI,eAAe,IAAI,GAAG,GAAG;AAC5B,aAAO,KAAK,YAAY,iBAAiB,EAAE,IAAI,CAAC,CAAC;AACjD;AAAA,IACD;AAEA,QAAI,SAAS,IAAI,GAAG,GAAG;AACtB,aAAO,KAAK,YAAY,iBAAiB,EAAE,IAAI,CAAC,CAAC;AACjD;AAAA,IACD;AAEA,aAAS,IAAI,GAAG;AAChB,SAAK,GAAG,IAAI;AAAA,EACb;AAEA,MAAI,OAAO,SAAS,GAAG;AACtB,WAAO;AAAA,MACN,MAAM;AAAA,MACN;AAAA,IACD;AAAA,EACD;AAEA,SAAO;AAAA,IACN;AAAA,IACA,QAAQ,CAAC;AAAA,EACV;AACD;","names":[]}
package/package.json CHANGED
@@ -1,10 +1,82 @@
1
1
  {
2
- "name": "safe-formdata",
3
- "version": "0.0.1",
4
- "description": "OIDC trusted publishing setup package for safe-formdata",
5
- "keywords": [
6
- "oidc",
7
- "trusted-publishing",
8
- "setup"
9
- ]
2
+ "name": "safe-formdata",
3
+ "version": "0.1.0",
4
+ "description": "Boundary-focused FormData parser with strict security guarantees",
5
+ "author": "roottool",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/roottool/safe-formdata.git"
9
+ },
10
+ "bugs": {
11
+ "url": "https://github.com/roottool/safe-formdata/issues"
12
+ },
13
+ "homepage": "https://github.com/roottool/safe-formdata#readme",
14
+ "imports": {
15
+ "#*": {
16
+ "types": "./src/*.ts",
17
+ "import": "./src/*.ts",
18
+ "default": "./src/*.ts"
19
+ }
20
+ },
21
+ "type": "module",
22
+ "main": "./dist/index.js",
23
+ "module": "./dist/index.js",
24
+ "types": "./dist/index.d.ts",
25
+ "exports": {
26
+ ".": {
27
+ "types": "./dist/index.d.ts",
28
+ "import": "./dist/index.js",
29
+ "default": "./dist/index.js"
30
+ }
31
+ },
32
+ "files": [
33
+ "dist"
34
+ ],
35
+ "scripts": {
36
+ "dev": "tsup --watch",
37
+ "lint": "oxlint . --deny-warnings",
38
+ "lint:fix": "oxlint . --fix-suggestions",
39
+ "check:type": "tsc --noEmit",
40
+ "format:biome": "biome format --write",
41
+ "format:prettier": "prettier --cache --write \"**/*.{md,yml,yaml}\"",
42
+ "format": "npm-run-all2 format:biome format:prettier",
43
+ "fix": "npm-run-all2 lint:fix format",
44
+ "check:biome": "biome format .",
45
+ "check:prettier": "prettier --cache --check \"**/*.{md,yml,yaml}\"",
46
+ "check:format": "npm-run-all2 check:biome check:prettier",
47
+ "check:source": "npm-run-all2 lint check:format",
48
+ "check": "bun run check:source",
49
+ "test": "vitest run",
50
+ "test:watch": "vitest",
51
+ "test:coverage": "vitest run --coverage",
52
+ "build": "tsup",
53
+ "check:package": "publint && attw --pack . --ignore-rules cjs-resolves-to-esm",
54
+ "prepublishOnly": "bun run check:type && bun run test:coverage && bun run build && bun run check:package"
55
+ },
56
+ "engines": {
57
+ "node": "^20.19.0 || >= 22.12.0"
58
+ },
59
+ "devDependencies": {
60
+ "@arethetypeswrong/cli": "0.18.2",
61
+ "@biomejs/biome": "2.3.10",
62
+ "@types/node": "25.0.3",
63
+ "@vitest/coverage-v8": "4.0.16",
64
+ "happy-dom": "20.0.11",
65
+ "npm-run-all2": "8.0.4",
66
+ "oxlint": "1.34.0",
67
+ "prettier": "3.7.4",
68
+ "publint": "0.3.16",
69
+ "terser": "5.44.1",
70
+ "tsup": "8.5.1",
71
+ "typescript": "5.9.3",
72
+ "vitest": "4.0.16"
73
+ },
74
+ "license": "MIT",
75
+ "keywords": [
76
+ "formdata",
77
+ "parser",
78
+ "security",
79
+ "boundary",
80
+ "prototype-pollution"
81
+ ]
10
82
  }