Skip to content

bug(github-pat): a backslash in the token raises re.error mid-rotation; .env quote-escaping is write-only #2017

Closed

Description

@dolho

Summary

Two low-severity robustness findings in the same text transform, both from the /edge-cases pass on #1979. Filed together because they share a root: the .env line is built as a regex replacement string and consumed by a parser that never reverses the escaping the writer applies.

1. A backslash in the token raises re.error mid-rotation

_patch_env_github_pat passes the formatted line to re.sub as the replacement, where \1, \g<…> and unknown letter-escapes are interpreted:

'ghp_a\g<1>b'      -> re.error: invalid group reference 1 at position 20
'ghp_back\slash'   -> re.error: bad escape \s at position 20
'ghp_with\1group'  -> re.error: invalid group reference 1 at position 21

Impact is bounded and honest, not silent: _propagate_to_agent catches only httpx errors, so this escapes to the asyncio.gather(..., return_exceptions=True) in propagate_pat_to_all_agents, which records the agent as failed with the message and continues. The operator sees failures — they just say bad escape \s instead of "that is not a valid token". Real GitHub PATs (ghp_[A-Za-z0-9]{36}, github_pat_[A-Za-z0-9_]{82}) contain no backslashes, so this needs a malformed paste.

Fix: use a function replacement (lambda _: new_line) or re.sub's replacement escaping. Rejecting a token that doesn't match the PAT shape at the settings boundary would be better still.

tests/unit/test_pat_propagation_properties.py::TestKnownGaps::test_a_backslash_in_the_token_does_not_raise (xfail(strict=True), 3 params).

2. .env writer escapes ", reader never unescapes it

_format_pat_line escapes embedded double quotes (pat.replace('"', '\\"')), matching the agent-server's own .env writer. The agent-server's reader does value.strip().strip('"').strip("'") — it strips quote characters but never unescapes \". So any value containing a quote round-trips corrupted:

written : GITHUB_PAT="ghp_a\"b"
agent reads: 'ghp_a\\"b'   (expected 'ghp_a"b')

This is a property of the .env parse contract, not of #1979 — it affects every credential written through that path, not just PATs. I deliberately kept that parser byte-faithful in #1999/#2010 rather than improving it inside a security fix, precisely because the ent#127 "is this credential set" predicate is defined as agreement with it and its source is spliced into an in-container probe. Fixing the asymmetry means changing both sides together, with test_ent127_predicate.py::TestExporterParity as the gate.

tests/unit/test_pat_propagation_properties.py::TestKnownGaps::test_a_quote_in_the_token_round_trips (xfail(strict=True)).

Acceptance criteria

  • A token containing a backslash fails with a comprehensible message (or is rejected at the settings boundary), never as re.error
  • If the .env escaping asymmetry is fixed: writer and reader change together, with the ent#127 parity test updated in the same commit

Related

Activity

  1. added
    priority-p3
    type-bug
    theme-reliability
    on Aug 5, 2026
  2. dolho commented on Aug 5, 2026

    @dolho
    ContributorAuthor

    /claim

  3. added
    status-in-progress
    on Aug 5, 2026
  4. github-actions commented on Aug 5, 2026

    @github-actions

    Claimed by @dolho. Label status-in-progress added.

    Next steps:

    1. Create branch: feature/2017-<short-slug>
    2. Implement the feature
    3. Open a PR with Fixes #2017
  5. added
    status-in-dev
    and removed
    status-in-progress
    on Aug 11, 2026
  6. added a commit that references this issue on Aug 13, 2026
  7. removed
    status-in-dev
    on Aug 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

priority-p3theme-reliabilitytype-bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions