Repository navigation
feat(service): run Windows control daemon via per-user Scheduled Task - #378
Merged
Merged
Conversation
… logon Symptom: on Windows `control pair`, `connect`, `pool on` and `ask on` ended with "no service manager for this platform yet; run `sessionpipe control run` yourself", so the daemon lived as long as the terminal it was started in (#184). Core's service module gains a third manager beside launchd and systemd: a per-user scheduled task, \sessionpipe\<label>, registered from a definition with a logon trigger that names the user, which needs no administrator. - No window: a task that runs node.exe directly opens a terminal window at every logon (measured on Windows 11), so the task runs it under `conhost.exe --headless`. - Task Scheduler gives an action no environment, keeps none of its output and restarts it on no exit code. The task runs a small launcher script (%LOCALAPPDATA%\sessionpipe\service\<label>.cjs) that starts the service with the environment it was given, appends its output to the log, starts it again at once on 75 and after 5 s on a failure, and ends with it on 0. What it runs is one line of JSON, so nothing is read by cmd.exe; only the node and the launcher path are on the task's command line, and one with a quote or a % is refused with why. - The launcher sets SESSIONPIPE_SERVICE=schtasks:<its pid>, and restartVia answers "schtasks" only for the process whose parent that is, as it does for the supervisor. The daemon exits 75 and the launcher starts the new copy. - `schtasks /End` ends conhost alone, so replacing or removing the task also stops the launcher and what it runs, found by command line and never by a pid written down earlier. - Task Scheduler and the process list are read through one PowerShell script (cmdlets only), which prints a number and writes text to a file as UTF-8. - pair, update and doctor name the task and Task Scheduler; status no longer says updates on Windows run only while `control run` is running; update writes down who is mid-turn before it replaces the task, which ends its processes outright. - A service of a sessionpipe's own (SESSIONPIPE_SERVICE_LABEL) is given APPDATA and LOCALAPPDATA too: on Windows its files are under those, and without them it came up on the account's real ones. Tests that used win32 as the platform with no manager use freebsd. Signed-off-by: connorburgess <connorburgesscodes@gmail.com>
…ing, and teardown
- Escape Windows PowerShell 5.1 curly single quotes (U+2018..U+201B) in psQuote
alongside ASCII single quotes to prevent single-quote breakout injection.
- Reject ASCII control characters (0x00..0x1F) in unsafeWord and double trailing
backslashes before closing quotes in taskArguments for Win32 argv safety.
- Use path.win32.dirname in taskXml <WorkingDirectory> so Windows paths resolve
consistently across platforms.
- Harden the Windows launcher script:
- Exponential crash backoff (5s..60s) on rapid failures and repeated rapid
exit 75s (first update exit 75 still restarts immediately; backoff resets
after 60s healthy uptime).
- Log rotation at 5 MiB to .1 with 0600 permissions, immediate parent log fd
close after spawn (preventing fd leaks and Windows log file locks), and
spawn try/catch safety.
- Case-insensitive Windows env key deduplication (e.g. Path vs PATH, AppData
vs APPDATA) so Task Scheduler's default Path does not shadow service.env.
- Clean SIGTERM/SIGINT teardown clearing restart timers and stopping the child.
- Use case-insensitive USERDOMAIN/USERNAME lookup in installService, skip
removing task XML on dryRun, clean stale XML on uninstallService, and forward
caller env as shell in CLI service helpers (with mixed-case Path support).
Refs #184
Signed-off-by: kombuchapunk <35040017+kombuchapunk@users.noreply.github.com>
…indows scheduled task
- Neutralize line terminators (including U+2028/U+2029) in launcherText() label
comments, escape U+2028/U+2029 in the embedded JSON line for clean round-trip
parsing, and reject U+2028/U+2029 in unsafeWord().
- Make WORLD regex case-insensitive in serviceEnv() so Windows mixed-case
AppData/LocalAppData/UserProfile are preserved for isolated services.
- Harden Windows installService() rollback:
- Keep launcher and XML writes inside the rollback guard and only remove
the XML file in finally if written.
- When /Run fails after /Create /F replaced the task, re-register the
previous task XML alongside restoring the previous launcher .cjs.
- When re-installing an identical launcher after stopping a running task,
attempt schtasks /Run on failure to restore service availability.
- Preserve launcher .cjs and surface errors when schtasks /Delete fails for
reasons other than task-not-found (and pipe stderr in execQuiet so missing
task errors are inspectable).
- Replace /\+$/ in word() with a linear-time trailing-backslash loop (eliminates
CodeQL polynomial ReDoS).
- Prioritize explicit InstallOptions.user over env and preserve USERNAME when
USERDOMAIN is absent.
- Report installed=true in Windows serviceState() when a task is registered
even if its launcher .cjs is missing, enabling automatic repair.
- Resolve conhost.exe via system32(), make taskXmlBytes() CRLF-idempotent, and
stop launcher PIDs before schtasks /End (skipping conhost taskkill unless
/End failed with no known launcher PID) to minimize PID-reuse windows.
Refs #184
Signed-off-by: kombuchapunk <35040017+kombuchapunk@users.noreply.github.com>
5 tasks done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Rebases #317 (preserving @connorburgess's original commit and DCO attribution) onto
main(addingLOCAL_COPY_VIA_SCHTASKS = 4toproto/sessionpipe/v1/local.protoand regenerating schemas) and hardens the Windows per-user Scheduled Task service manager (\sessionpipe\<label>) after multi-model adversarial QA.Closes #184
Supersedes #317
Hardening & Security Fixes
'plus Unicode curly single quotes (U+2018..U+201B) inpsQuote, rejects ASCII control chars (0x00..0x1F) and JS line separators (U+2028/U+2029) inunsafeWord, neutralizes line terminators inlauncherText()comments, and escapesU+2028/U+2029in the embedded JSON config line for lossless round-tripping.word()with a linear-time loop and properly doubles trailing backslashes before closing quotes intaskArguments.5s..60s, reset after 60s healthy uptime; immediate first exit-75 update restart), 5 MiB log rotation to.1(0600) with immediate parent fd close, case-insensitive Windows env key deduplication (Path/PATH,AppData/APPDATA), and cleanSIGTERM/SIGINTteardown.serviceEnvfixes:.cjsand previous task XML whenschtasks /Runfails after/Create /F, and restarts the previous task on identical-launcher failure..cjsand surfaces errors whenschtasks /Deletefails for reasons other than task-not-found (piping stderr inexecQuiet).schtasks /End, resolvesconhost.exeviasystem32(), and makestaskXmlBytes()CRLF-idempotent.WORLDenv filter case-insensitive inserviceEnv()so mixed-caseAppData/LocalAppData/UserProfileare preserved on Windows.InstallOptions.user, preservesUSERNAMEwithoutUSERDOMAIN, and reportsinstalled: truewhen a task exists with a missing launcher so repair works automatically.Verification
npm run lint,npm run typecheck,npm run build,npm run schemas -- --check,npm run conformance, and 100% test coverage passing.