Skip to content

fix(windows): grant icacls by SID, not unqualified username - #2479

Open
asizux2 wants to merge 1 commit into
garrytan:mainfrom
asizux2:fix/windows-icacls-sid-principal
Open

fix(windows): grant icacls by SID, not unqualified username#2479
asizux2 wants to merge 1 commit into
garrytan:mainfrom
asizux2:fix/windows-icacls-sid-principal

Conversation

@asizux2

@asizux2 asizux2 commented Aug 8, 2026

Copy link
Copy Markdown

Fixes #2478.

The bug

restrictFilePermissions() / restrictDirectoryPermissions() pass an unqualified username to icacls:

const user = os.userInfo().username;
execFileSync('icacls', [dirPath, '/inheritance:r', '/grant:r', `${user}:(OI)(CI)(F)`], { stdio: 'ignore' });

On a machine whose hostname equals the username, the bare name does not resolve to the user account. icacls writes an ACE for the machine SID instead, and because the same call passes /inheritance:r, the directory is left with a single ACE that matches nobody. The process that just created the directory can no longer enumerate or write to it.

Observed on Windows 11, hostname ESLAM, user ESLAM\eslam:

Form Resolves to
eslam (current behaviour) NTAccount.Translate throws "Some or all identity references could not be translated"
ESLAM\eslam S-1-5-21-<redacted>-1001
whoami /user S-1-5-21-<redacted>-1001

Net effect: browse can never start on an affected machine. mkdirSecure() creates .gstack/, hardens it into inaccessibility, and the daemon's next act is writing a lockfile inside. acquireServerLock() returns null on the resulting EACCES, so the user sees:

[browse] Another instance is starting the server, waiting...
[browse] Timed out waiting for another instance to start the server

which points at a concurrency problem that does not exist. icacls reports success and stdio: 'ignore' discards its output, so nothing surfaces the real cause.

The fix

Resolve the current user's SID and hand icacls the literal *<SID> form, which is immune to name-resolution ambiguity. Fall back to the domain-qualified name if the lookup fails.

One subtlety worth flagging for review: the SID lookup pins %SystemRoot%\System32\whoami.exe rather than calling a bare whoami. Under a bash-flavoured PATH (Git Bash / MSYS, extremely common for this tool on Windows) a bare whoami resolves to the MSYS build, which rejects /user. I hit exactly that on the first test run: the lookup silently failed and the code fell through to the qualified-name fallback. The fallback is correct, but without the pin the primary path would almost never fire.

Tests

The existing Windows cases only assert not.toThrow(), which this bug sails straight past, since icacls genuinely succeeds while writing a useless ACE. The added cases assert the contract that actually broke: the directory stays usable by the calling process after hardening. No icacls output parsing, so no locale or version brittleness.

Verified both directions on an affected machine:

Code path Result
With this fix 15 pass, 0 fail
Pre-fix principal 7 pass, 8 fail

Several of those 8 are pre-existing tests (appendSecureFile, writeSecureFile). The suite could always have caught this; it just never ran anywhere the hostname matched the username.

Risk

Behaviour is unchanged on POSIX (the chmod paths are untouched) and unchanged on Windows machines where the bare username already resolved correctly, since *<SID> and the resolved name name the same principal. The fallback preserves today's behaviour if whoami.exe is unavailable.

An unqualified username is ambiguous. On a machine whose hostname equals
the username, it does not resolve to the user account; icacls silently
writes an ACE for the machine SID instead. Combined with /inheritance:r,
the directory is left with a single ACE that matches nobody, so the
process that just created it can no longer enumerate or write to it.

In practice this means browse can never start on an affected machine: it
bricks .gstack/ on first run, then reports "Another instance is starting
the server" because acquireServerLock() returns null on the resulting
EACCES. The real cause is invisible, since icacls reports success and
stdio is ignored.

Resolve the current user's SID and pass icacls the literal *<SID> form,
which is immune to name-resolution ambiguity. Fall back to the
domain-qualified name if the lookup fails.

The SID lookup pins %SystemRoot%\System32\whoami.exe rather than a bare
`whoami`, which under a bash-flavoured PATH resolves to the MSYS build
and rejects /user. Without the pin the primary path would silently fail
on Git Bash, one of the most common Windows setups for this tool.

Tests assert the directory stays usable by the calling process after
hardening. The existing Windows cases only asserted "does not throw",
which this bug sails straight past. On the pre-fix code path 8 of 15
tests fail, several of them pre-existing.

Refs garrytan#2478

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@trunk-io

trunk-io Bot commented Aug 8, 2026

Copy link
Copy Markdown

Merging to main in this repository is managed by Trunk.

  • To merge this pull request, check the box to the left or comment /trunk merge below.

After your PR is submitted to the merge queue, this comment will be automatically updated with its status. If the PR fails, failure details will also be posted here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Windows: icacls grant uses unqualified username, bricks .gstack/ when hostname == username

1 participant