T09 · Insecure Skill Coding Practices
Error
- Location
- scripts/apply-patches.sh:145
- Finding
- JavaScript Source Injection Through Unescaped Environment Variables<![CDATA[ ## Vulnerability Details **File Location**: `scripts/apply-patches.sh`, lines 39-40 and 145-146 **Vulnerability Type**: JavaScript code injection through unsafe source-code generation **Risk Level**: High ### Vulnerable Code ```javascript const BOT_OPEN_ID = process.env.BOT_OPEN_ID || 'YOUR_BOT_OPEN_ID'; const BOT_NAME = process.env.BOT_NAME || 'YOUR_BOT_NAME'; ``` The environment-derived values are subsequently interpolated directly into JavaScript source code: ```javascript const BOT_OPEN_ID = "${BOT_OPEN_ID}"; const BOT_NAME = "${BOT_NAME}"; ``` ### Technical Analysis The script reads `BOT_OPEN_ID` and `BOT_NAME` from the invoking process environment and embeds them into replacement text that is written to the installed `monitor.js` file. The values are inserted inside JavaScript string literals without escaping or serialization. An attacker-controlled value containing a double quote, newline, backslash, or JavaScript syntax can terminate the intended string literal and inject additional statements into `monitor.js`. The shell's quoted heredoc prevents shell expansion, but it does not protect the later JavaScript template-literal interpolation performed by the Node.js patching program. This is a persistent source-injection issue rather than only a malformed-configuration issue: the generated code is saved to the installed claw-lark plugin and is subsequently loaded when the gateway restarts. ### Attack Path 1. An attacker gains influence over the environment used to invoke the patch script, such as through a wrapper script, compromised deployment configuration, CI variable, shell initialization file, or misleading setup instructions. 2. The attacker supplies a crafted `BOT_OPEN_ID` or `BOT_NAME` value containing a closing quote followed by JavaScript statements. 3. A user executes: ```bash bash scripts/apply-patches.sh ``` 4. The Node.js patching logic interpolates the crafted value into the replacement source without escaping i ...[truncated 1262 chars]
- Remediation
- <![CDATA[ ## Remediation Suggestions Serialize all environment-derived values before embedding them in generated JavaScript. For example: ```javascript const serializedBotOpenId = JSON.stringify(BOT_OPEN_ID); const serializedBotName = JSON.stringify(BOT_NAME); ``` Then construct the replacement source using those serialized values without adding another pair of quotes: ```javascript const replacement = `const BOT_OPEN_ID = ${serializedBotOpenId}; const BOT_NAME = ${serializedBotName};`; ``` Additional hardening should include: 1. Validate `BOT_OPEN_ID` against the expected Lark identifier format before patching, such as a strict `ou_`-prefixed allowlist pattern. 2. Apply a reasonable maximum length to both variables. 3. Reject control characters and unexpected line breaks. 4. Prefer reading bot identity from runtime configuration rather than generating source code from environment input. 5. Write the patched file atomically and validate its JavaScript syntax before replacing the installed file. 6. Abort the operation when validation or an expected patch fails instead of leaving a partially patched plugin. 7. Add tests covering quotes, backslashes, template-literal markers, Unicode, and newline characters in both configuration values. ]]>
