Title: REDIRECTION_TOKENS mutated by alias/macro create+list (4 locations) -- compounding cross-instance state corruption · Issue #1649 · python-cmd2/cmd2 · GitHub
Open Graph Title: REDIRECTION_TOKENS mutated by alias/macro create+list (4 locations) -- compounding cross-instance state corruption · Issue #1649 · python-cmd2/cmd2
X Title: REDIRECTION_TOKENS mutated by alias/macro create+list (4 locations) -- compounding cross-instance state corruption · Issue #1649 · python-cmd2/cmd2
Description: Hi cmd2 team — found a module-level state mutation bug in cmd2/cmd2.py while running adversarial test generation via tailtest. One root cause, four copy-pasted source locations, compounding effect across calls. Bug Four functions assign ...
Open Graph Description: Hi cmd2 team — found a module-level state mutation bug in cmd2/cmd2.py while running adversarial test generation via tailtest. One root cause, four copy-pasted source locations, compounding effect ...
X Description: Hi cmd2 team — found a module-level state mutation bug in cmd2/cmd2.py while running adversarial test generation via tailtest. One root cause, four copy-pasted source locations, compounding effect ...
Opengraph URL: https://github.com/python-cmd2/cmd2/issues/1649
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"REDIRECTION_TOKENS mutated by alias/macro create+list (4 locations) -- compounding cross-instance state corruption","articleBody":"Hi cmd2 team — found a module-level state mutation bug in `cmd2/cmd2.py` while running adversarial test generation via [tailtest](https://tailtest.com). One root cause, four copy-pasted source locations, compounding effect across calls.\n\n### Bug\n\nFour functions assign `constants.REDIRECTION_TOKENS` by reference then `.extend()` it with terminators:\n\n| Function | Line |\n|---|---|\n| `_alias_create` | 3821 |\n| `_alias_list` | 3901 |\n| `_macro_create` | 4068 |\n| `_macro_list` | 4191 |\n\nEach is shaped like:\n\n```python\ntokens_to_unquote = constants.REDIRECTION_TOKENS # reference, not copy\ntokens_to_unquote.extend(self.statement_parser.terminators)\n```\n\nEvery call to these four functions permanently appends terminators to the module-level `constants.REDIRECTION_TOKENS`. Over multiple calls the list grows without bound:\n\n```\nAfter 0 calls: ['|', '\u003e', '\u003e\u003e']\nAfter 1 call: ['|', '\u003e', '\u003e\u003e', ';']\nAfter 5 calls: ['|', '\u003e', '\u003e\u003e', ';', ';', ';', ';', ';', ';', ';', ';']\n```\n\n### Impact\n\n1. **Compounding mutation:** Each alias/macro create/list call pollutes the global list.\n2. **Cross-instance corruption:** Two `Cmd()` instances in the same process share the corrupted state. Instance A's alias operations affect instance B's redirection parsing.\n3. **Unbounded growth in long-running sessions:** Each invocation adds duplicates.\n\n### Fix\n\nIn all 4 locations, change:\n\n```python\ntokens_to_unquote = constants.REDIRECTION_TOKENS\n```\n\nto:\n\n```python\ntokens_to_unquote = list(constants.REDIRECTION_TOKENS)\n```\n\n### Reproduction\n\n```python\nimport pytest\nfrom cmd2 import Cmd\nfrom cmd2 import constants\n\n\ndef test_alias_create_does_not_mutate_redirection_tokens():\n \"\"\"alias create should not mutate the module-level REDIRECTION_TOKENS list.\"\"\"\n snapshot = list(constants.REDIRECTION_TOKENS)\n app = Cmd()\n app.onecmd_plus_hooks(\"alias create myalias echo hello\")\n assert constants.REDIRECTION_TOKENS == snapshot, (\n f\"BUG: REDIRECTION_TOKENS mutated by alias create. \"\n f\"Was {snapshot!r}, now {constants.REDIRECTION_TOKENS!r}\"\n )\n\n\ndef test_repeated_alias_creates_compound_mutation():\n \"\"\"Compound effect: 5 alias creates should not grow REDIRECTION_TOKENS.\"\"\"\n snapshot = list(constants.REDIRECTION_TOKENS)\n app = Cmd()\n for i in range(5):\n app.onecmd_plus_hooks(f\"alias create alias_{i} echo {i}\")\n assert constants.REDIRECTION_TOKENS == snapshot, (\n f\"BUG: REDIRECTION_TOKENS grew over 5 alias creates: \"\n f\"{snapshot!r} -\u003e {constants.REDIRECTION_TOKENS!r}\"\n )\n\n\ndef test_two_instances_alias_create_independence():\n \"\"\"Two Cmd instances should not share corrupted REDIRECTION_TOKENS state.\"\"\"\n snapshot = list(constants.REDIRECTION_TOKENS)\n a = Cmd()\n b = Cmd()\n a.onecmd_plus_hooks(\"alias create x echo x\")\n state_after_a = list(constants.REDIRECTION_TOKENS)\n assert state_after_a == snapshot, (\n f\"BUG: instance A mutated module-level REDIRECTION_TOKENS: \"\n f\"{snapshot!r} -\u003e {state_after_a!r}; instance B sees the corruption\"\n )\n```\n\nHappy to open a PR — it's a 4-line change. Found via [tailtest](https://tailtest.com) adversarial test generation.\n","author":{"url":"https://github.com/pramodavansaber","@type":"Person","name":"pramodavansaber"},"datePublished":"2026-04-24T03:24:37.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":2},"url":"https://github.com/1649/cmd2/issues/1649"}
| 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:a09824f1-d106-d1e5-f08b-18855a8daba4 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | CCE2:60C9C:47A6C:5F9DE:6A4D8BD2 |
| html-safe-nonce | 02c50bc187a231f8942eec6e71c239f6cccd5efd23f9f24c262891e265120c86 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDQ0UyOjYwQzlDOjQ3QTZDOjVGOURFOjZBNEQ4QkQyIiwidmlzaXRvcl9pZCI6Ijc2NDg3MDQ4NTM0NzI0MDkxNCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | a0a5d3e634ff32af442eed1f3e92c3487f7d347a4174439505a937ee30e5fe3b |
| hovercard-subject-tag | issue:4320292050 |
| 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/python-cmd2/cmd2/1649/issue_layout |
| twitter:image | https://opengraph.githubassets.com/3f8ea6c5ec42ffde5ad78af7b14005c9bbd0089db9c402340617f0775096580a/python-cmd2/cmd2/issues/1649 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/3f8ea6c5ec42ffde5ad78af7b14005c9bbd0089db9c402340617f0775096580a/python-cmd2/cmd2/issues/1649 |
| og:image:alt | Hi cmd2 team — found a module-level state mutation bug in cmd2/cmd2.py while running adversarial test generation via tailtest. One root cause, four copy-pasted source locations, compounding effect ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | pramodavansaber |
| hostname | github.com |
| expected-hostname | github.com |
| None | 9f8758a3953dfe943439713a6fa4f90d542a3431f10e861ca03dd7f39009f320 |
| turbo-cache-control | no-preview |
| go-import | github.com/python-cmd2/cmd2 git https://github.com/python-cmd2/cmd2.git |
| octolytics-dimension-user_id | 17161025 |
| octolytics-dimension-user_login | python-cmd2 |
| octolytics-dimension-repository_id | 51442999 |
| octolytics-dimension-repository_nwo | python-cmd2/cmd2 |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 51442999 |
| octolytics-dimension-repository_network_root_nwo | python-cmd2/cmd2 |
| 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 | bffd5484f01713a661b03469b77678f72b6574ed |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width