[Detail Bug] Dynamic UI evaluation crashes or mangles labels when TextArg values contain backslashes
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 4
- Forks
- 0
- Avg merge
- 15h 38m
- Merged PRs (30d)
- 37
Description
Detail Bug Report
Introduced in 09f490434fc1712370be798abbd5984272ff19cd by @kewde on Jun 6, 2026
Summary
- Context:
apply_text_argssubstitutes{{ArgName}}/{{ArgName:fmt}}placeholders in module name/text templates using the per-instance module text-args before the names of communication objects, parameters, separators, channels, etc. are rendered. These placeholders carry short display labels for module instances (channel numbers, parameter suffixes, com-object names) per the docs (packages/product/docs/knxprod/4-application.mdx). - Bug: The text-arg
valueis passed directly as thereplstring tore.sub, so any backslash in a text-arg value is interpreted by CPython'sreengine as a replacement escape / backreference instead of a literal character. - Actual vs. expected: Actual: backslash sequences in
<TextArg Value="...">(e.g.C:\Users\me,\1,C:\temp) are processed as regex replacement escapes/backrefs during dynamic UI rendering, causing eitherre.errorexceptions or silently altered display strings. Expected: text-arg values should be substituted literally into UI labels. - Impact:
- Crash class: a
Valuecontaining\+ an unknown escape letter (e.g.\U,\d) or a backreference (\1,\g<name>) raisesre.errorfrom insideDynamicUI.ui(). This blocks every consumer that callsui()first —encode_to_memory,encode_to_properties,*_param_map,get_module_instances— for the affectedApplication. - Cosmetic class: a
Valuecontaining\t/\n/\ris silently mangled in the display label only (e.g.C:\temprenders asC: emp). Encoded parameter/property bytes are not affected.
- Crash class: a
Code with Bug
packages/product/src/xknxmono/product/parser_v2/_name.py
def apply_text_args(text: str, text_args: dict[str, str]) -> str:
"""Substitute {{ArgName}} and {{ArgName:fmt}} placeholders from module text args."""
for name, value in text_args.items():
text = re.sub(r"\{\{" + re.escape(name) + r"(?::[^}]*)?\}\}", value, text) # <-- BUG 🔴 `value` is a string repl; backslashes become escapes/backrefs and can raise re.error
return text
Explanation
apply_text_argsusesre.sub(..., repl=value, ...)wherevaluecomes directly from parsed<TextArg Value="...">(no validation/escaping).- In Python
re.sub, a string replacement processes backslash escapes and backreferences. Therefore:- Values like
C:\Users\metriggerre.error: bad escape \U ...duringDynamicUI.ui(). - Values like
\1triggerre.error: invalid group reference 1 ...(the pattern has no capture groups). - Values like
C:\tempinterpret\tas a tab; downstream whitespace normalization collapses it, producing a wrong label.
- Values like
- The issue is lazy:
loader.load(...)succeeds becauseapply_text_argsis only invoked during dynamic-tree evaluation (app.dynamic_ui().ui()and entrypoints that callui()first).
Recommended Fix
Use a callable replacement so the value is inserted literally:
for name, value in text_args.items():
text = re.sub(
r"\{\{" + re.escape(name) + r"(?::[^}]*)?\}\}",
lambda _m, v=value: v,
text,
)
return text
History
This bug was introduced in commit 09f4904. The change "Render Grid/Table parameter blocks and resolve channel names" rewrote the static substitute_template helper (which substituted {{ArgName}} via safe result.replace(f"{{{{{arg_name}}}}}", arg_value) calls) into a new apply_text_args function that needed to match {{ArgName:fmt}} format-suffix placeholders, so the author switched from str.replace to re.sub and passed the arg value as a string repl — which the re module then interprets for backslash escapes and backreferences, introducing the bug.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in packages/product/src/xknxmono/product/parser_v2/_name.py at apply_text_args, then trace the app.dynamic_ui().ui() path where text arguments are rendered. Reproduce the issue with values such as C:\temp or \1 and verify that labels retain backslashes literally without raising re.error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Active
- Clarity
- Clearly specified
- Newbie friendliness
- 86/100