Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/loopzero/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,14 @@ def _remote_workflow(worktree: Path, branch: str, previous: Config | None) -> tu
return previous, revision
shown = _proc.run(
["git", "show", f"{revision}:workflow.toml"],
cwd=worktree, env_allowlist=worktree_mod.GIT_ENV, timeout=60,
cwd=worktree, env_allowlist=worktree_mod.GIT_ENV, timeout=60, preserve_newlines=True, encoding="utf-8",
)
if shown.exit_code != 0:
raise ConfigError(f"cannot read {source}. {hint}")
try:
data = tomllib.loads(shown.stdout)
data = tomllib.loads(shown.stdout.encode("utf-8", errors="surrogateescape").decode("utf-8"))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Surrogateescape round-trip is roundabout

With preserve_newlines=True, _proc.run decodes stdout with errors="surrogateescape" (_proc.py:170). This change re-encodes that output to get the raw bytes back, then decodes it strictly. The result is correct: invalid bytes now raise instead of being silently replaced with U+FFFD as in the old text mode. But it depends on an internal detail of _proc.run's error handler, so it can break silently if that detail changes. The encoding is also passed in two places that must stay in sync. A simpler fix is to let _proc.run decode strictly or return raw bytes, then pass the bytes to tomllib.loads(raw.decode("utf-8")).

except UnicodeDecodeError as exc:
raise ConfigError(f"{source}: {exc}") from exc

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Trusted-Git UTF-8 error omits the recovery hint

Every other error from _remote_workflow adds . {hint}, which tells the user to fetch the trusted base or supply --config. The new UnicodeDecodeError branch leaves it out, so a malformed trusted blob is the only trusted-source error without recovery guidance. Use raise ConfigError(f"{source}: {exc}. {hint}") from None to match the TOML branch next to it.

except tomllib.TOMLDecodeError as exc:
raise ConfigError(f"{source}: invalid TOML: {exc}. {hint}") from None
try:
Expand Down
18 changes: 13 additions & 5 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -695,15 +695,23 @@ def test_config_flag_overrides_root(wt: Path, tmp_path: Path, capsys, bootstrap)
assert out.startswith("checks are unpinned because --config was provided\n")


def test_invalid_workflow_utf8_names_file(wt, capsys):
filename = "broken-workflow.toml"
(wt / filename).write_bytes(bytes([255]))
args = ["--config", filename, "status"]
@pytest.mark.parametrize("trusted", [False, True])
def test_invalid_workflow_utf8_names_file(wt, capsys, trusted):
data = WORKFLOW.encode().replace(b"echo ok", b"echo \xff")
filename = "origin/main:workflow.toml" if trusted else "broken-workflow.toml"
if trusted:
(wt / "workflow.toml").write_bytes(data)
git(wt, "add", "workflow.toml")
git(wt, "commit", "-qm", "invalid workflow bytes")
git(wt, "update-ref", "refs/remotes/origin/main", "HEAD")
else:
(wt / filename).write_bytes(data)
args = ["status"] if trusted else ["--config", filename, "status"]

code, _, err = run(capsys, *args)

assert code == 1
assert filename in err and "utf-8" in err.lower() and "position 0" in err
assert filename in err and "utf-8" in err.lower() and "position " in err
assert "Traceback" not in err


Expand Down
Loading