Title: GitConfigParser does not decode backslash escapes in unquoted config values · Issue #2296 · gitpython-developers/GitPython · GitHub
Open Graph Title: GitConfigParser does not decode backslash escapes in unquoted config values · Issue #2296 · gitpython-developers/GitPython
X Title: GitConfigParser does not decode backslash escapes in unquoted config values · Issue #2296 · gitpython-developers/GitPython
Description: This was found and prepared with AI assistance (drafted by Codex, posted through the account owner's account); the reproduction and analysis below are agent-authored. Description GitConfigParser leaves backslash escapes literal in unquot...
Open Graph Description: This was found and prepared with AI assistance (drafted by Codex, posted through the account owner's account); the reproduction and analysis below are agent-authored. Description GitConfigParser le...
X Description: This was found and prepared with AI assistance (drafted by Codex, posted through the account owner's account); the reproduction and analysis below are agent-authored. Description GitConfigParse...
Opengraph URL: https://github.com/gitpython-developers/GitPython/issues/2296
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"GitConfigParser does not decode backslash escapes in unquoted config values","articleBody":"This was found and prepared with AI assistance (drafted by Codex, posted through the account owner's account); the reproduction and analysis below are agent-authored.\n\n## Description\n\n`GitConfigParser` leaves backslash escapes literal in **unquoted** config values, so it disagrees with Git on the same file. Quoted values are handled correctly today; the unquoted path only decoded escapes when the value continued onto the next line, so a single-line unquoted value kept its escapes verbatim.\n\n| config | Git | GitPython (before) |\n| --- | --- | --- |\n| `b = first\\nsecond` | `first\u003cLF>second` | `first\\nsecond` |\n| `b = first\\tsecond` | `first\u003cTAB>second` | `first\\tsecond` |\n| `b = foo\\\\bar` | `foo\\bar` | `foo\\\\bar` |\n| `b = ab\\\"cd` | `ab\"cd` | `ab\\\"cd` |\n\nThe same value also decoded differently depending only on whether it was wrapped, which is the inconsistency that points at the cause:\n\n| config | Git | GitPython (before) |\n| --- | --- | --- |\n| `b = first\\nsecond` | `first\u003cLF>second` | `first\\nsecond` |\n| `b = first\\nsecond \\`\u003cbr>` z` | `first\u003cLF>second z` | `first\u003cLF>second z` |\n\nBeyond reading the wrong value, this makes rewriting unsafe. Because the parsed value contained no control character, the writer's CR/LF/NUL guard did not trigger, so saving an unrelated key emitted a value Git then read differently than before:\n\n```\noriginal: [a]\\n\\tb = x\\\\ny\\n\ngit reads: x\u003cLF>y\nGitPython: x\\ny # literal backslash, no control char\nrewritten: [a]\\n\\tb = \"x\\\\\\\\ny\\\\\\n\"\\n\ngit reads: x\\\\ny # changed meaning, file was corrupted by a read-modify-write\n```\n\n## Reproduction\n\n```python\nfrom git import GitConfigParser\n\nwith open(\"/tmp/config\", \"w\") as f:\n f.write(\"[a]\\n\\tb = first\\\\nsecond\\n\")\n\nprint(repr(GitConfigParser(\"/tmp/config\", read_only=True).get_value(\"a\", \"b\")))\n```\n\nBefore: `'first\\\\nsecond'`. Expected, and what git prints: `'first\\nsecond'`.\n\nCompare against real git:\n\n```console\n$ GIT_CONFIG_GLOBAL=/tmp/config GIT_CONFIG_SYSTEM=/dev/null git config --get a.b | od -c\n0000000 f i r s t \\n s e c o n d \\n\n```\n\n## Root cause\n\n`git/config.py:624` in `GitConfigParser._read` — `parse_value()` was invoked only when `continued` was set:\n\n```python\n continued = False\n while True:\n if not is_line_continuation(optval):\n break\n ...\n continued = True\n if continued:\n optval = parse_value(optval)\n```\n\n`parse_value()` is the routine that decodes escapes, strips unquoted comments and trims whitespace. Guarding it on `continued` meant a single-line unquoted value skipped it entirely, while a value that happened to continue got the full decode. The quoted branches a few lines below already decoded unconditionally.\n\n## A caveat that shapes the fix\n\nGit decodes the standard escape sequences (`\\n`, `\\t`, `\\b`, `\\\\`, `\\\"`) in unquoted values as well, but it is **not** accurate to say it treats unquoted values exactly as it treats quoted ones: for an escape it does not recognise, git rejects the line outright.\n\n```\n$ printf '[a]\\n\\tb = C:\\\\Users\\n' > c\n$ GIT_CONFIG_GLOBAL=c GIT_CONFIG_SYSTEM=/dev/null git config --get a.b\nfatal: bad config line 2 in file c\n```\n\nSo `path = C:\\Users\\me` is not something git can read at all. GitPython is deliberately more tolerant here — it keeps such backslashes literal, which is what lets a Windows path survive a round-trip — and several existing tests depend on that tolerance.\n\nDecoding unconditionally would therefore regress it: in `C:\\Users\\...\\Temp\\test_foo`, the `\\t` of `\\Temp` and `\\test` would become tabs while the `\\U` of `\\Users` stayed literal, corrupting the path. That broke four `test_conditional_includes_*` / `test_multiple_include_paths_with_same_key` tests on Windows in a first attempt.\n\n## Suggested fix\n\nCall `parse_value()` when every backslash in the value introduces a known escape, and leave the value untouched otherwise. This keeps the tolerance for unknown escapes intact while fixing the decode:\n\n- `first\\nsecond`, `a\\tb`, `foo\\\\bar` — all backslashes are known escapes, so they are decoded as git does.\n- `C:\\Users\\...\\Temp\\test_foo` — contains the unknown `\\U`, so the whole value stays literal and the path is preserved.\n\nThe escape table moves to a module-level `UNQUOTED_ESCAPES` so the decoder and this guard cannot drift apart.\n\nOne existing expectation encodes the current behaviour and would need to move with it: `test_backslash_line_continuation` asserts `k = val\\\\` followed by a newline reads as `val\\\\`, while git yields `val\\` (the second backslash escapes the first, so there is no continuation).\n\nI can put up the PR with the fix and regression tests.\n","author":{"url":"https://github.com/JingHao-Leon","@type":"Person","name":"JingHao-Leon"},"datePublished":"2026-10-19T10:44:05Z","url":"https://github.com/gitpython-developers/GitPython/issues/2296"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:73f217b2-6f94-c2cb-d027-c8d5eb9d208e |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | BF60:154D88:313AC9:3E6A5D:6B0103D3 |
| html-safe-nonce | 8c842283f506045e46f30539a78a0da3ece351722d22e70a487eef3fc5facacc |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCRjYwOjE1NEQ4ODozMTNBQzk6M0U2QTVEOjZCMDEwM0QzIiwidmlzaXRvcl9pZCI6Ijg5NzE1ODI5Njk2NTEyMDA5NzkiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 66067a97d395bb71cea993653c0ed013a40ea92b6dd2992f7ea2da3bb3ed30b5 |
| hovercard-subject-tag | issue:5922399001 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/gitpython-developers/GitPython/2296/issue_layout |
| twitter:image | https://opengraph.githubassets.com/c966a40decd56763ff6086ebb81a6a36112cb877d9472965c057d98b6cf5b722/gitpython-developers/GitPython/issues/2296 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/c966a40decd56763ff6086ebb81a6a36112cb877d9472965c057d98b6cf5b722/gitpython-developers/GitPython/issues/2296 |
| og:image:alt | This was found and prepared with AI assistance (drafted by Codex, posted through the account owner's account); the reproduction and analysis below are agent-authored. Description GitConfigParser le... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | JingHao-Leon |
| hostname | github.com |
| expected-hostname | github.com |
| None | cb7fa109eca7d4156c889f474306b0e91336055e9adf8fd4684b61e49094d124 |
| turbo-cache-control | no-preview |
| go-import | github.com/gitpython-developers/GitPython git https://github.com/gitpython-developers/GitPython.git |
| octolytics-dimension-user_id | 503709 |
| octolytics-dimension-user_login | gitpython-developers |
| octolytics-dimension-repository_id | 1126087 |
| octolytics-dimension-repository_nwo | gitpython-developers/GitPython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 1126087 |
| octolytics-dimension-repository_network_root_nwo | gitpython-developers/GitPython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 2b59d31a4e44f08b7af67a4b67f8fd0af3befe2a |
| ui-target | canary-2 |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width