Skip to content

Commit eba7021

Browse files
committed
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>
1 parent 1084ad1 commit eba7021

7 files changed

Lines changed: 232 additions & 19 deletions

File tree

‎rust-bindings/src/lib.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
mod expr;
55
mod model;
66
mod pickle_helpers;
7+
mod py_repr;
78
mod sessions;
89

910
use pyo3::prelude::*;

‎rust-bindings/src/py_repr.rs‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
//! Rendering values into `__repr__` output that Python can parse.
5+
//!
6+
//! `format!("{:?}", s)` is not a Python literal writer. Rust's `Debug` for
7+
//! `str` happens to agree with Python on the quote, the backslash and the
8+
//! C0 controls, but it renders anything else non-printable as `\u{a0}`,
9+
//! and CPython wants exactly four hex digits after `\u`. A repr carrying
10+
//! such a character does not parse at all, so a value that arrives from
11+
//! outside -- captured process output, a template-supplied name -- can
12+
//! corrupt the repr of the object holding it.
13+
//!
14+
//! Delegating to CPython's own `repr()` retires the bug class instead of
15+
//! reimplementing its escaping table: the output is by construction
16+
//! whatever the running interpreter produces, including its per-string
17+
//! choice of quote character.
18+
19+
use pyo3::prelude::*;
20+
use pyo3::types::PyString;
21+
22+
/// CPython's `repr()` of `value`, ready to embed in a `__repr__`.
23+
pub(crate) fn py_str(py: Python<'_>, value: &str) -> PyResult<String> {
24+
Ok(PyString::new(py, value).repr()?.to_string())
25+
}
26+
27+
/// An optional `int` as Python spells it. `Debug` would emit `Some(0)`,
28+
/// which evaluates to a `NameError`.
29+
pub(crate) fn py_opt_int(value: Option<i32>) -> String {
30+
match value {
31+
Some(v) => v.to_string(),
32+
None => "None".to_string(),
33+
}
34+
}

‎rust-bindings/src/sessions/session.rs‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -691,8 +691,12 @@ impl PySession {
691691
}
692692
}
693693

694-
fn __repr__(&self) -> String {
694+
fn __repr__(&self, py: Python<'_>) -> PyResult<String> {
695695
let snap = lock_recover(&self.snapshot);
696-
format!("Session(id={:?}, state={:?})", snap.session_id, snap.state)
696+
Ok(format!(
697+
"Session(id={}, state=SessionState.{})",
698+
crate::py_repr::py_str(py, &snap.session_id)?,
699+
crate::sessions::types::PySessionState::from(snap.state).name()
700+
))
697701
}
698702
}

‎rust-bindings/src/sessions/session_user.rs‎

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,12 @@ impl PyPosixSessionUser {
6363
self.inner.is_process_user()
6464
}
6565

66-
fn __repr__(&self) -> String {
67-
format!(
68-
"PosixSessionUser(user={:?}, group={:?})",
69-
self.inner.user(),
70-
self.inner.group()
71-
)
66+
fn __repr__(&self, py: Python<'_>) -> PyResult<String> {
67+
Ok(format!(
68+
"PosixSessionUser(user={}, group={})",
69+
crate::py_repr::py_str(py, self.inner.user())?,
70+
crate::py_repr::py_str(py, self.inner.group())?
71+
))
7272
}
7373

7474
/// Pickle support — round-trips through `__init__(user, *, group=...)`.
@@ -307,8 +307,11 @@ impl PyWindowsSessionUser {
307307
self.inner.is_process_user()
308308
}
309309

310-
fn __repr__(&self) -> String {
311-
format!("WindowsSessionUser(user={:?})", self.inner.user())
310+
fn __repr__(&self, py: Python<'_>) -> PyResult<String> {
311+
Ok(format!(
312+
"WindowsSessionUser(user={})",
313+
crate::py_repr::py_str(py, self.inner.user())?
314+
))
312315
}
313316

314317
/// Pickle support — round-trips through `__init__(user, *,

‎rust-bindings/src/sessions/types.rs‎

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ impl From<SessionState> for PySessionState {
5353
impl PySessionState {
5454
/// Variant name as a string (e.g. `"READY"`).
5555
#[getter]
56-
fn name(&self) -> &'static str {
56+
pub(crate) fn name(&self) -> &'static str {
5757
match self {
5858
Self::READY => "READY",
5959
Self::RUNNING => "RUNNING",
@@ -323,8 +323,9 @@ impl PyActionStatus {
323323

324324
fn __repr__(&self) -> String {
325325
format!(
326-
"ActionStatus(state={:?}, exit_code={:?})",
327-
self.inner.state, self.inner.exit_code
326+
"ActionStatus(state=ActionState.{}, exit_code={})",
327+
self.state().name(),
328+
crate::py_repr::py_opt_int(self.inner.exit_code)
328329
)
329330
}
330331

@@ -525,13 +526,13 @@ impl PyActionResult {
525526
}
526527
}
527528

528-
fn __repr__(&self) -> String {
529-
format!(
530-
"ActionResult(state={}, exit_code={:?}, stdout={:?})",
529+
fn __repr__(&self, py: Python<'_>) -> PyResult<String> {
530+
Ok(format!(
531+
"ActionResult(state=ActionState.{}, exit_code={}, stdout={})",
531532
self.state.name(),
532-
self.exit_code,
533-
self.stdout,
534-
)
533+
crate::py_repr::py_opt_int(self.exit_code),
534+
crate::py_repr::py_str(py, &self.stdout)?,
535+
))
535536
}
536537

537538
fn __eq__(&self, other: &Self) -> bool {

‎test/openjd/sessions/__init__.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0

‎test/openjd/sessions/test_repr.py‎

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
"""``__repr__`` output for the sessions bindings must be parseable Python.
5+
6+
These reprs were built with ``format!("{:?}")``, which is not a Python
7+
literal writer. Rust's ``Debug`` for ``str`` agrees with Python on the
8+
quote, the backslash and the C0 controls, but renders anything else
9+
non-printable as ``\\u{a0}`` -- and CPython wants exactly four hex digits
10+
after ``\\u``, so the literal does not parse. ``Debug`` for ``Option``
11+
likewise emits ``Some(0)``, which is a ``NameError``.
12+
13+
That made a repr corruptible by its own data. ``ActionResult.stdout`` is
14+
captured process output, so a non-ASCII byte in a job's stdout is ordinary
15+
rather than adversarial, and ``PosixSessionUser`` carries a user name that
16+
arrives from outside. Both land in log lines and exception messages.
17+
18+
Every string field now goes through CPython's own ``repr()``, so the
19+
escaping is by construction whatever the running interpreter produces.
20+
"""
21+
22+
from __future__ import annotations
23+
24+
import pytest
25+
26+
from openjd._openjd_rs import (
27+
ActionResult,
28+
ActionState,
29+
ActionStatus,
30+
PosixSessionUser,
31+
)
32+
33+
# The characters Rust's Debug renders as \u{...}: non-printable outside
34+
# ASCII (U+00A0, U+3000, U+0085) plus non-printable ASCII (U+007F). Then
35+
# the ones Debug does escape correctly, kept as controls against a
36+
# hand-rolled replacement getting them wrong, and printable non-ASCII
37+
# that must survive verbatim.
38+
HOSTILE_STRINGS = [
39+
"a\xa0b",
40+
"a\u3000b",
41+
"a\x85b",
42+
"a\x7fb",
43+
"a\u200bb",
44+
"a\U00100000b",
45+
'a"b',
46+
"it's",
47+
"a\\b",
48+
"a\nb",
49+
"a\r\nb",
50+
"a\tb",
51+
"a\x00b",
52+
"café",
53+
"a\U0001f600b",
54+
"",
55+
]
56+
57+
ALL_ACTION_STATES = [
58+
ActionState.RUNNING,
59+
ActionState.SUCCESS,
60+
ActionState.FAILED,
61+
ActionState.CANCELED,
62+
ActionState.TIMEOUT,
63+
]
64+
65+
EVAL_NS = {
66+
"ActionResult": ActionResult,
67+
"ActionState": ActionState,
68+
"ActionStatus": ActionStatus,
69+
"PosixSessionUser": PosixSessionUser,
70+
}
71+
72+
73+
def assert_parses(text: str) -> None:
74+
"""The repr must at least be syntactically valid Python."""
75+
compile(text, "<repr>", "eval")
76+
77+
78+
class TestActionResultRepr:
79+
"""``ActionResult.stdout`` is captured process output."""
80+
81+
@pytest.mark.parametrize("stdout", HOSTILE_STRINGS)
82+
def test_repr_parses(self, stdout: str) -> None:
83+
assert_parses(repr(ActionResult(state=ActionState.SUCCESS, exit_code=0, stdout=stdout)))
84+
85+
@pytest.mark.parametrize("stdout", HOSTILE_STRINGS)
86+
def test_repr_embeds_cpython_repr_of_stdout(self, stdout: str) -> None:
87+
result = ActionResult(state=ActionState.SUCCESS, exit_code=0, stdout=stdout)
88+
assert repr(result) == (
89+
f"ActionResult(state=ActionState.SUCCESS, exit_code=0, stdout={stdout!r})"
90+
)
91+
92+
@pytest.mark.parametrize("stdout", HOSTILE_STRINGS)
93+
def test_repr_round_trips(self, stdout: str) -> None:
94+
result = ActionResult(state=ActionState.SUCCESS, exit_code=0, stdout=stdout)
95+
assert eval(repr(result), dict(EVAL_NS)) == result
96+
97+
@pytest.mark.parametrize(
98+
"exit_code,expected",
99+
[(0, "exit_code=0"), (1, "exit_code=1"), (-9, "exit_code=-9"), (None, "exit_code=None")],
100+
)
101+
def test_repr_renders_exit_code_as_python(self, exit_code: int | None, expected: str) -> None:
102+
# Debug would emit `Some(0)`, which is a NameError.
103+
assert expected in repr(
104+
ActionResult(state=ActionState.SUCCESS, exit_code=exit_code, stdout="")
105+
)
106+
107+
@pytest.mark.parametrize("state", ALL_ACTION_STATES)
108+
def test_repr_names_the_state_as_python(self, state: ActionState) -> None:
109+
result = ActionResult(state=state, exit_code=0, stdout="")
110+
assert f"state={state!r}" in repr(result)
111+
assert eval(repr(result), dict(EVAL_NS)) == result
112+
113+
114+
class TestActionStatusRepr:
115+
"""No string field, but the same ``Option`` and enum defects."""
116+
117+
@pytest.mark.parametrize("exit_code", [0, 1, -9, None])
118+
def test_repr_is_evaluable(self, exit_code: int | None) -> None:
119+
status = ActionStatus(state=ActionState.SUCCESS, exit_code=exit_code)
120+
assert_parses(repr(status))
121+
# Evaluates without NameError; ActionStatus has no __eq__ against a
122+
# rebuilt instance's other fields, so this asserts evaluability only.
123+
eval(repr(status), dict(EVAL_NS))
124+
125+
def test_repr_renders_none_exit_code(self) -> None:
126+
assert repr(ActionStatus(state=ActionState.FAILED, exit_code=None)) == (
127+
"ActionStatus(state=ActionState.FAILED, exit_code=None)"
128+
)
129+
130+
131+
class TestPosixSessionUserRepr:
132+
"""``user`` and ``group`` arrive from outside."""
133+
134+
@pytest.mark.parametrize("value", HOSTILE_STRINGS)
135+
def test_repr_parses_for_user(self, value: str) -> None:
136+
assert_parses(repr(PosixSessionUser(user=value, group="g")))
137+
138+
@pytest.mark.parametrize("value", HOSTILE_STRINGS)
139+
def test_repr_matches_cpython_for_user(self, value: str) -> None:
140+
assert repr(PosixSessionUser(user=value, group="g")) == (
141+
f"PosixSessionUser(user={value!r}, group={'g'!r})"
142+
)
143+
144+
@pytest.mark.parametrize("value", HOSTILE_STRINGS)
145+
def test_repr_matches_cpython_for_group(self, value: str) -> None:
146+
# `group` is a separate argument to the same writer; a fix applied
147+
# to only the first would pass every `user` case above.
148+
assert repr(PosixSessionUser(user="u", group=value)) == (
149+
f"PosixSessionUser(user={'u'!r}, group={value!r})"
150+
)
151+
152+
153+
class TestReprNegativeControls:
154+
"""Text needing no escaping must pass through unaltered."""
155+
156+
def test_plain_action_result(self) -> None:
157+
assert repr(ActionResult(state=ActionState.SUCCESS, exit_code=0, stdout="ok")) == (
158+
"ActionResult(state=ActionState.SUCCESS, exit_code=0, stdout='ok')"
159+
)
160+
161+
def test_plain_posix_session_user(self) -> None:
162+
assert repr(PosixSessionUser(user="alice", group="staff")) == (
163+
"PosixSessionUser(user='alice', group='staff')"
164+
)
165+
166+
def test_state_enum_repr_unchanged(self) -> None:
167+
# The spelling the reprs above embed.
168+
assert repr(ActionState.SUCCESS) == "ActionState.SUCCESS"

0 commit comments

Comments
 (0)