diff --git a/ahriman-core/src/ahriman/core/alpm/pkgbuild_parser.py b/ahriman-core/src/ahriman/core/alpm/pkgbuild_parser.py index 4b2cf6b1..6e5ff512 100644 --- a/ahriman-core/src/ahriman/core/alpm/pkgbuild_parser.py +++ b/ahriman-core/src/ahriman/core/alpm/pkgbuild_parser.py @@ -164,23 +164,37 @@ class PkgbuildParser(Iterable[PkgbuildPatch]): Returns: str: extracted string value """ + def parts(source: str) -> Iterator[str]: + for child in node.named_children: + # retain gaps (such as newlines) between children + prefix, _, source = source.partition(PkgbuildParser.text(child)) + yield prefix + yield PkgbuildParser.value(child) + yield source + def unescape(source: str) -> str: for original, replacement in PkgbuildParser._ESCAPED_CHARACTERS.items(): source = source.replace(original, replacement) return source + # raw processing to avoid converting to text if not required if node.type == PkgbuildNodeType.CONCATENATION: return "".join(PkgbuildParser.value(child) for child in node.named_children) value = PkgbuildParser.text(node) match node.type: - case PkgbuildNodeType.STRING | PkgbuildNodeType.STRING_CONTENT: - if node.type == PkgbuildNodeType.STRING: - value = PkgbuildParser.unquote(value) - # protect escaped backslashes from being interpreted a second time. + case PkgbuildNodeType.STRING: + # this node is always quoted, so that we manually remove first and last characters + return "".join(parts(value[1:-1])) + + case PkgbuildNodeType.STRING_CONTENT: + # unescape literal text only, substitutions are returned as written + value = PkgbuildParser.unquote(f'"{value}"') return "\\".join(unescape(part) for part in value.split("\\\\")) + case PkgbuildNodeType.NUMBER | PkgbuildNodeType.RAW_STRING | PkgbuildNodeType.WORD: return PkgbuildParser.unquote(value) + case _: return value diff --git a/ahriman-core/tests/ahriman/core/alpm/test_pkgbuild_parser.py b/ahriman-core/tests/ahriman/core/alpm/test_pkgbuild_parser.py index 7ad70496..8dba5507 100644 --- a/ahriman-core/tests/ahriman/core/alpm/test_pkgbuild_parser.py +++ b/ahriman-core/tests/ahriman/core/alpm/test_pkgbuild_parser.py @@ -290,4 +290,8 @@ def test_parse(resource_path_root: Path) -> None: PkgbuildPatch("var", "a\\qb"), PkgbuildPatch("var", "\\$name"), PkgbuildPatch("var", "first\nsecond"), + PkgbuildPatch( + "_source64", + """https:$(echo "$_dl_urls" | sed -En 's|^.*rel="(//.*/linuxx64-[^"]*)".*$|\\1|p')""", + ), ] diff --git a/tests/testresources/models/pkgbuild b/tests/testresources/models/pkgbuild index 6fd56809..09ac81e7 100644 --- a/tests/testresources/models/pkgbuild +++ b/tests/testresources/models/pkgbuild @@ -125,3 +125,6 @@ var="a\qb" var="\\\\$name" var="first second" + +# nested quotes inside command substitutions +_source64=https:"$(echo "$_dl_urls" | sed -En 's|^.*rel="(//.*/linuxx64-[^"]*)".*$|\1|p')"