Repository navigation
Commit a3d4f4e
authored
fix(sessions): Escape strings in the sessions __repr__ output (#361)
* fix(sessions): Escape strings in the sessions __repr__ output
The sessions reprs were built with `format!("{:?}")`, which is not a
Python literal writer. Rust's `Debug` for `str` agrees with Python on the
quote, the backslash and the C0 controls, but renders anything else
non-printable as `\u{a0}`, and CPython wants exactly four hex digits after
`\u`. The literal then does not parse at all:
>>> repr(ActionResult(state=ActionState.SUCCESS, exit_code=0,
... stdout="a\xa0b"))
'ActionResult(state=SUCCESS, exit_code=Some(0), stdout="a\\u{a0}b")'
SyntaxError: truncated \uXXXX escape
That made a repr corruptible by its own data. `ActionResult.stdout` is
captured process output, so a non-ASCII byte in a job's stdout is ordinary
rather than adversarial, and `PosixSessionUser` carries a user name that
arrives from outside. Both reach log lines and exception messages.
Every string field now goes through CPython's own `repr()` via a new
`py_repr` module. Delegating retires the bug class instead of
reimplementing Python's escaping table: the output is by construction
whatever the running interpreter produces, including its per-string choice
of quote character. openjd-rs#374 added an escaping writer upstream for
`ExprValue`, but it is `pub(crate)` in openjd-expr and so unreachable from
this crate; PyO3 exposes the interpreter's `repr()` directly, which needs
no upstream change and is the same oracle the tests assert against.
Two neighbouring defects in the same expressions went with it, because
leaving them would have kept the repr un-evaluable and so left the fix
unverifiable by round-trip:
* `Debug` for `Option` emitted `exit_code=Some(0)`, a `NameError`.
* The state field rendered as a bare `Success` / `SUCCESS`, also a
`NameError`. It now uses the spelling the enum's own repr already
produces, `ActionState.SUCCESS`.
`eval(repr(x)) == x` now holds for `ActionResult` across all 16 test
inputs, which is what pins the escaping rather than a spelling assertion
alone.
Four sites changed: `ActionResult`, `ActionStatus`, `PosixSessionUser`,
`WindowsSessionUser` and `Session`. `SessionState.__repr__` and
`ScriptRunnerState.__repr__` were already correct.
Adds test/openjd/sessions/, which did not exist -- the sessions surface
had no Python tests beyond a module-name check. 113 cases over 16 inputs:
the characters Rust renders as `\u{...}`, the ones it escapes correctly,
printable non-ASCII that must survive verbatim, and negative controls for
text needing no escaping. `group` is asserted separately from `user`
because a fix applied to only the first argument would pass every `user`
case.
Mutation-checked, each behaviour reverted independently against a green
113-case baseline: escaping 67 failed, Option 44 failed, enum spelling 43
failed.
No stub regeneration: a `Python<'_>` token is invisible to Python and
`PyResult<String>` still maps to `str`, so the emitted signature is
unchanged. `SymbolTable.__repr__` already uses this form and its stub
entry is `def __repr__(self) -> builtins.str`.
WindowsSessionUser is unverified -- it cannot be constructed off Windows
(`RuntimeError: Only available on Windows systems`), so its change is
by inspection and shares the helper the other three sites test.
Signed-off-by: David Leong <116610336+leongdl@users.noreply.github.com>
* test(sessions): Gate the PosixSessionUser repr tests on posix
The binding gates construction on `#[cfg(unix)]` and raises
`RuntimeError: Only available on posix systems.` elsewhere, so the four
tests that build one failed on the Windows matrix: 49 failures, exactly
the 16+16+16+1 cases those tests parametrize.
Mirrors the Rust guard with `os.name`. WindowsSessionUser gets no
counterpart: off the process user it demands a password or a logon token,
so it cannot be built with an arbitrary name just to read its repr.
Signed-off-by: David Leong <116610336+leongdl@users.noreply.github.com>
* fix(sessions): Address review findings on the repr escaping
Five defects from review, each measured before fixing.
**py_repr::py_str could panic instead of raising.** `.to_string()` on a
`Bound<PyString>` resolves to PyO3's `Display`, which calls `str()` on the
object and has nowhere to put a `PyErr` -- `ToString` treats the formatter
error as unreachable and panics. That put an unwind across the FFI boundary
in the one function four reprs route through, and reprs are evaluated from
logging and exception formatting where an unwind loses the diagnostic that
prompted it. Now uses `to_cow()?`, which reads the UTF-8 directly,
propagates failure, and skips a redundant interpreter round-trip.
**Session's repr held the snapshot mutex across a CPython call.** `py_str`
allocates, an allocation can trigger a GC pass, and a finalizer run by that
pass may re-enter this Session and re-lock a non-reentrant `Mutex`.
`lock_recover` maps a poisoned lock to `into_inner()`, so a panic there
would not even surface. Reads `session_id` and `state` out from under the
guard and drops it before formatting. Recorded in the module docs as a rule
for the helper rather than a one-off.
**Session's repr used `id=`, not the constructor's `session_id=`.** It
parsed and then raised `TypeError: got an unexpected keyword argument 'id'`
-- arguably worse than the old form, which failed at `compile()`.
**PathMappingRule corrupted Windows paths silently.** Not a `{:?}` at all,
so it was invisible to the search that found the others: it hand-rolled
`'{}'` with no escaping. `C:\temp` rendered as `'C:\temp'`, which Python
reads as `C:` + TAB + `emp` -- it parses and gives back a different string.
An apostrophe in a path closed the literal early. This is the only repr
found that corrupts on its most typical input, a Windows destination path,
which is what path mapping exists to produce. Now routes through `py_str`.
**ESC was missing from the test inputs.** Rust's `Debug` special-cases only
the quote, the backslash, and NUL/tab/CR/LF; every other control falls
through to the unparseable brace form. So ANSI colour sequences in captured
stdout were almost certainly the highest-frequency trigger of this bug in
the field, and no case exercised them. Adds ESC, U+0001 and U+001F, and
narrows the module docs, which previously implied only non-ASCII was
affected. `"a\x00b"` was also mislabelled as representative of C0 when it
passes only because NUL has its own arm.
Also corrects two claims of mine that were wrong:
* A test comment said `ActionStatus` has no `__eq__`. It does
(types.rs:337), comparing seven fields. The real reason its repr cannot
round-trip is that the repr emits two of them, and it cannot be fixed by
adding fields because `started_at`/`ended_at` are not constructor
arguments. The misleading `eval()` assertion is replaced by one that pins
the lossiness explicitly, so a later change to the field list is
deliberate.
* `py_repr`'s docs said delegating "retires the bug class". It retires it
for `sessions/`; `model/` and the rest of `expr/` still carry it. Scoped
the wording and pointed at the tracking note.
Adds coverage for `Session` and `PathMappingRule`, which the first revision
omitted -- both are constructible in a unit test, so the omissions were
oversights rather than constraints. `Session` skips the NUL case: the id
becomes a working-directory path component and the filesystem rejects it
before any repr is taken, which is a constructor constraint, not a repr
gap.
176 tests in the sessions module, up from 113. Two new mutants against a
green baseline: reverting PathMappingRule to hand-rolled quoting fails 21,
reverting Session's keyword fails 19. The pre-existing
`TestPathMappingRuleRepr` in test/openjd/expr/test_path_mapping.py passes
unmodified, which is the evidence that the change is a no-op for
well-behaved paths.
Signed-off-by: David Leong <116610336+leongdl@users.noreply.github.com>
* test(sessions): Bound the Session repr cases by filename portability
Windows CI failed 8 of the new TestSessionRepr cases: Session names its
working directory after the session_id, so the inputs are bounded by what
a filename may contain, not by what the repr can render. ESC, U+0001,
U+001F, tab, newline, CRLF, the double quote and the backslash are all
legal on APFS and ext4 and rejected by Windows -- 'The filename, directory
name, or volume label syntax is incorrect. (os error 123)'.
The previous revision excluded only NUL, which is the one case macOS
caught. That fixed the instance rather than the class.
Now filtered by predicate -- the C0 controls plus the punctuation Windows
reserves -- so a new HOSTILE_STRINGS entry is classified automatically
instead of silently breaking one platform. 10 of 19 cases survive,
including U+00A0 and U+3000, the characters this change exists for.
The 9 excluded are not left unverified: ActionResult and PosixSessionUser
parametrize the full list through the same py_repr::py_str helper and
touch no disk. What Session verifies is that it routes through that helper
at all, and that its repr keyword matches its constructor.
Adds test_the_case_filter_keeps_the_canonical_trigger so the filter cannot
quietly empty out and leave the sweep asserting nothing.
Signed-off-by: David Leong <116610336+leongdl@users.noreply.github.com>
---------
Signed-off-by: David Leong <116610336+leongdl@users.noreply.github.com>1 parent efba36c commit a3d4f4e
8 files changed
Lines changed: 402 additions & 25 deletions
File tree
- rust-bindings/src
- expr
- sessions
- test/openjd/sessions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
122 | 122 | | |
123 | 123 | | |
124 | 124 | | |
125 | | - | |
| 125 | + | |
126 | 126 | | |
127 | 127 | | |
128 | 128 | | |
129 | 129 | | |
130 | 130 | | |
131 | | - | |
132 | | - | |
133 | | - | |
134 | | - | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
135 | 141 | | |
136 | 142 | | |
137 | 143 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| 7 | + | |
7 | 8 | | |
8 | 9 | | |
9 | 10 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
691 | 691 | | |
692 | 692 | | |
693 | 693 | | |
694 | | - | |
695 | | - | |
696 | | - | |
| 694 | + | |
| 695 | + | |
| 696 | + | |
| 697 | + | |
| 698 | + | |
| 699 | + | |
| 700 | + | |
| 701 | + | |
| 702 | + | |
| 703 | + | |
| 704 | + | |
| 705 | + | |
| 706 | + | |
| 707 | + | |
697 | 708 | | |
698 | 709 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
63 | 63 | | |
64 | 64 | | |
65 | 65 | | |
66 | | - | |
67 | | - | |
68 | | - | |
69 | | - | |
70 | | - | |
71 | | - | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
72 | 72 | | |
73 | 73 | | |
74 | 74 | | |
| |||
307 | 307 | | |
308 | 308 | | |
309 | 309 | | |
310 | | - | |
311 | | - | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
312 | 315 | | |
313 | 316 | | |
314 | 317 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
53 | 53 | | |
54 | 54 | | |
55 | 55 | | |
56 | | - | |
| 56 | + | |
57 | 57 | | |
58 | 58 | | |
59 | 59 | | |
| |||
323 | 323 | | |
324 | 324 | | |
325 | 325 | | |
326 | | - | |
327 | | - | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
328 | 329 | | |
329 | 330 | | |
330 | 331 | | |
| |||
525 | 526 | | |
526 | 527 | | |
527 | 528 | | |
528 | | - | |
529 | | - | |
530 | | - | |
| 529 | + | |
| 530 | + | |
| 531 | + | |
531 | 532 | | |
532 | | - | |
533 | | - | |
534 | | - | |
| 533 | + | |
| 534 | + | |
| 535 | + | |
535 | 536 | | |
536 | 537 | | |
537 | 538 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
0 commit comments