Repository navigation
Commit b45bb2d
authored
fix: take the named pipe user SID from the process token (#286)
* fix: take the named pipe user SID from the process token
NamedPipeHelper.create_security_attributes() resolved the DACL's user SID
with LookupAccountName("", getpass.getuser()). getpass.getuser() only reads
the LOGNAME, USER, LNAME and USERNAME environment variables, and the name
lookup goes through LSA, which can fail with ERROR_NONE_MAPPED (1332). A
Windows CI host running as SYSTEM hit 1332 on both the server path
(create_named_pipe_server) and the client path
(establish_named_pipe_connection), and the 3-attempt retry from #262 ran
out.
- Read the SID from the process token: OpenProcessToken(GetCurrentProcess(),
TOKEN_QUERY) and GetTokenInformation(token, TokenUser)[0], closing the
token in a finally. No name lookup, so 1332 cannot occur. The #262 retry
and the getpass import are removed.
- The client no longer builds a DACL. CreateFile ignores the security
descriptor when opening an existing pipe and reads only bInheritHandle. It
passes SECURITY_ATTRIBUTES with SECURITY_DESCRIPTOR = None and
bInheritHandle = True. pywin32's SECURITY_ATTRIBUTES() defaults
bInheritHandle to TRUE (PySECURITY_ATTRIBUTES.cpp), so the client handle
stays inheritable, as before. Passing None would have made it
non-inheritable.
- The server DACL is unchanged: GENERIC_READ|GENERIC_WRITE allowed to the
user SID, then GENERIC_ALL denied to S-1-5-2 (NETWORK).
Tests:
- Unit: the token SID ends up in the DACL; with the env user name set to
an unmapped name, LookupAccountName is not called; the token is closed
when GetTokenInformation raises; the client passes no descriptor, an
inheritable handle and OPEN_EXISTING, and does not call
create_security_attributes. The three LookupAccountName/retry tests
pinned the removed behaviour and are replaced.
- Integ: test_pipe_security_does_not_depend_on_user_name runs a real pipe
round-trip with LOGNAME, USER, LNAME and USERNAME set to
no-such-user-1332, once for server and client and once for the client
only, plus an ambient-environment control. It asserts the precondition
that LookupAccountName("", "no-such-user-1332") raises 1332, and reads the
DACL back with GetSecurityInfo. The old code fails this test
deterministically.
- CI: code_quality.yml runs windows-latest x Python 3.9-3.14 through
OpenJobDescription/.github reusable_python_build.yml, whose last step
(line 72) is `hatch run test`, which is `pytest ... test -vv`. That
collects test/openjd/adaptor_runtime/integ/_named_pipe/. No marker gates
the new test.
Ran on macOS (Python 3.13, hatch 1.14.1):
- hatch run lint, before and after: ruff "All checks passed!", black
"146 files would be left unchanged", mypy "no issues found in 146
source files".
- hatch run mypy --platform win32 src test, before and after: no issues
in 146 source files.
- hatch run test: before (mainline d9044ea) 482 passed, 26 skipped,
coverage 93.39%. After: 482 passed, 29 skipped, coverage 93.39%. The 3
new skips are the 3 new integ cases. The Windows-only unit module is
one module-level skip before and after.
- Windows tests skip on macOS. To run them anyway, a fake pywin32 plugin
(not committed) models the APIs these tests touch, using the pywin32 C
sources. Fidelity control: mainline code with mainline tests passes on
it (11 passed). Branch: 15 passed, 1 skipped (the GITHUB_ACTIONS-only
different-user test).
Mutation check, EXECUTED on the fake (sha256 restore, __pycache__
cleared between mutants), all caught:
- M1 restore LookupAccountName(getpass.getuser()) plus retry: fails
test_create_security_attributes_uses_process_token_user,
..._does_not_look_up_user_name, ..._closes_token_when_query_fails and
integ [unmapped-user-name-for-server-and-client].
- M2 restore the client DACL: fails
test_establish_named_pipe_connection_builds_no_dacl.
- M3 original code (M1+M2): also fails integ
[unmapped-user-name-for-client].
- M4 no finally, M5 no close, M6 client not inheritable, M7 client None,
M8 client keeps blank descriptor, M9 allow Everyone, M10 drop network
deny: each caught. M4-M8 by the token-close or client unit test, M9
and M10 also by all three integ cases.
ARGUED for real Windows, not run: with the old code, LookupAccountName
raises 1332 on every attempt (the test asserts this as a precondition),
so both unmapped integ cases fail. M2 alone is equivalent at the OS level,
because CreateFile ignores the descriptor. Only the unit test catches it.
Auditors (three, independent; each returned PASS):
- Pin/mutation: 27 of their own mutants, 26 killed. The survivor removes
the bInheritHandle = True line, which is equivalent because pywin32
defaults it to TRUE. Kept the explicit line for the reader. The
CloseHandle-call assertion rejects an equivalent token.Close(). Kept,
since it matches the repo's mock style.
- Gap analysis: 9 mutants, all killed. Pre-existing:
test_fail_to_connect_to_named_pipe_with_different_user has its assert
inside pytest.raises, so the assert never runs. Not in this diff, and
tightening it to winerror 5 would gate CI on a claim nobody has run.
Recorded as a follow-up. _secure_open.py has the same LookupAccountName
pattern; follow-up.
- Craft/cross-platform: lint, win32 mypy and collection green; pywin32
API shapes checked against source. Fixed: the client-only case now sets
the unmapped name inside the try, so a failed precondition still closes
the server handle. Declined: cross-checking the SID with an LSA name
lookup. It would bring back the lookup this fix removes. The test
helper hard-codes TokenUser independently, and a TokenOwner mutant is
killed.
Follow-up, out of scope: openjd-sessions-for-python _get_process_user_sid()
uses the same name-lookup pattern.
Signed-off-by: David Leong <116610336+leongdl@users.noreply.github.com>
* test: expect the deny-first ACE order on the named pipe DACL
Windows CI showed the pipe DACL reads back with the NETWORK deny ACE
before the user allow ACE, both in the in-memory security attributes
and on the created pipe. That is canonical order, and the order that
makes the deny apply to the user's own network logons. The new tests
asserted allow-first and failed on all six Windows jobs; production
code was not involved, since mainline builds the same DACL.
Assert the canonical order exactly, and find the allowed SID by ACE
type rather than by index.
Signed-off-by: David Leong <116610336+leongdl@users.noreply.github.com>
---------
Signed-off-by: David Leong <116610336+leongdl@users.noreply.github.com>1 parent 9c8d54e commit b45bb2d
3 files changed
Lines changed: 266 additions & 57 deletions
File tree
- src/openjd/adaptor_runtime_client/named_pipe
- test/openjd/adaptor_runtime
- integ/_named_pipe
- unit/named_pipe
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| 7 | + | |
7 | 8 | | |
8 | 9 | | |
9 | 10 | | |
10 | 11 | | |
11 | | - | |
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
| |||
116 | 116 | | |
117 | 117 | | |
118 | 118 | | |
119 | | - | |
| 119 | + | |
120 | 120 | | |
121 | 121 | | |
122 | 122 | | |
| |||
125 | 125 | | |
126 | 126 | | |
127 | 127 | | |
128 | | - | |
129 | | - | |
130 | | - | |
131 | | - | |
132 | | - | |
133 | | - | |
134 | | - | |
135 | | - | |
136 | | - | |
137 | | - | |
138 | | - | |
139 | | - | |
140 | | - | |
141 | | - | |
142 | | - | |
143 | | - | |
144 | | - | |
145 | | - | |
146 | | - | |
147 | | - | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
148 | 136 | | |
149 | 137 | | |
150 | 138 | | |
| |||
320 | 308 | | |
321 | 309 | | |
322 | 310 | | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
323 | 318 | | |
324 | 319 | | |
325 | 320 | | |
| |||
330 | 325 | | |
331 | 326 | | |
332 | 327 | | |
333 | | - | |
| 328 | + | |
334 | 329 | | |
335 | 330 | | |
336 | 331 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
5 | 6 | | |
6 | 7 | | |
7 | 8 | | |
8 | 9 | | |
| 10 | + | |
| 11 | + | |
9 | 12 | | |
10 | 13 | | |
| 14 | + | |
11 | 15 | | |
| 16 | + | |
12 | 17 | | |
13 | 18 | | |
14 | 19 | | |
15 | 20 | | |
| 21 | + | |
16 | 22 | | |
17 | 23 | | |
18 | 24 | | |
| |||
21 | 27 | | |
22 | 28 | | |
23 | 29 | | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
24 | 36 | | |
25 | 37 | | |
26 | 38 | | |
| |||
52 | 64 | | |
53 | 65 | | |
54 | 66 | | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
55 | 125 | | |
56 | 126 | | |
57 | 127 | | |
| |||
129 | 199 | | |
130 | 200 | | |
131 | 201 | | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
0 commit comments