#18952: fix: sanitize schtasks env vars to prevent CRLF command injection
gateway
size: S
trusted-contributor
Cluster:
Cross-Platform Fixes
## Fix Summary
- sanitize CR/LF characters in schtasks batch-script environment keys and values before emitting `set KEY=VALUE`
- prevent newline-based command breakout in generated `.cmd` files
- add regression tests for CRLF and LF payloads in env values and CRLF in env keys
## Issue Linkage
Fixes #18943
## Security Snapshot
- CVSS v3.1: 7.8 (High)
- CVSS v4.0: 8.5 (High)
## Implementation Details
### Files Changed
- `src/daemon/schtasks.test.ts` (+45/-1)
- `src/daemon/schtasks.ts` (+4/-3)
### Technical Analysis
- sanitize CR/LF characters in schtasks batch-script environment keys and values before emitting `set KEY=VALUE`
## Validation Evidence
- Command: `N/A`
- Status: pass/fail state not explicitly provided in original body
## Risk and Compatibility
non-breaking; compatibility impact was not explicitly documented in the original PR body.
## AI-Assisted Disclosure
- AI-assisted: yes
- Model: Unknown
<!-- greptile_comment -->
<h3>Greptile Summary</h3>
This PR fixes a CRLF command injection vulnerability in the Windows scheduled task batch script generator (`buildTaskScript` in `src/daemon/schtasks.ts`). Previously, env var keys and values were interpolated into `set` lines without sanitization, allowing a crafted value containing carriage return/line feed to break out of the `set` statement and inject arbitrary commands into the generated `.cmd` file.
- Strips CR and LF characters from both env var keys and values before emitting `set` statements
- Exports `buildTaskScript` to enable direct unit testing
- Adds three regression tests covering CRLF in values, LF-only in values, and CRLF in keys
- Minor import reorder for formatting compliance
The fix is targeted and correct for the primary attack vector (env vars from config files via `collectConfigEnvVars`). A minor defense-in-depth suggestion was left for applying the same sanitization to `description` and `workingDirectory` parameters, which are also interpolated into the script.
<h3>Confidence Score: 4/5</h3>
- This PR is safe to merge — it correctly addresses the CRLF injection vulnerability with a minimal, focused change.
- The fix is straightforward and correct: stripping CR/LF from env var keys and values prevents command injection in batch scripts. Tests cover the key scenarios (CRLF, LF-only, key injection). The only minor gap is that other parameters interpolated into the script (description, workingDirectory) are not similarly sanitized, though those vectors are lower risk. No regressions expected from this change.
- No files require special attention — `src/daemon/schtasks.ts` has a minor defense-in-depth suggestion but no blocking issues.
<sub>Last reviewed commit: 8ee677e</sub>
<!-- greptile_other_comments_section -->
<!-- /greptile_comment -->
Most Similar PRs
#19016: fix(daemon): sanitize CRLF in schtasks batch script to prevent comm...
by moxunjinmu · 2026-02-17
91.9%
#5496: Fix: Windows path separators stripped in Gateway scheduled task
by giuliozelante · 2026-01-31
78.1%
#19504: fix(gateway): avoid stale running status from Windows Scheduled Task
by Fologan · 2026-02-17
75.7%
#18143: fix(windows): wrap shell builtins with cmd.exe /c for proper execution
by brandonwise · 2026-02-16
74.6%
#16525: fix(shell): stop rejecting newlines in double-quoted args (#16470)
by yinghaosang · 2026-02-14
73.5%
#5046: schtasks: add helpful hint for 'stub received bad data' error
by shayan919293 · 2026-01-31
73.3%
#9200: Fix: Strip dangerous env vars from baseEnv in host execution
by vishaltandale00 · 2026-02-05
73.0%
#18954: fix(security): secure cron, browser, settings dirs in doctor --fix
by BinHPdev · 2026-02-17
72.5%
#8161: fix(sandbox): block dangerous environment variables from Docker con...
by yubrew · 2026-02-03
72.4%
#8600: fix(update): add --ignore-scripts to prevent supply chain attacks
by yubrew · 2026-02-04
72.2%