Skip to content

Bug: Fn::Sub interprets backslashes in resolved values as regex replacement escapes #9396

Closed

Description

@vinsymoyya

Description

Local template resolution does not preserve backslashes in values substituted by Fn::Sub. For a parameter such as C:\project\data, the local provider leaves the environment variable as an unresolved intrinsic expression. Some values, such as a literal \n, are silently changed instead.

Steps to reproduce

From a development checkout at 2fd518eeb5ede1ff85522e2d3c39999782322759, run this Python snippet. It exercises the local template provider without AWS credentials or a container runtime:

from samcli.lib.providers.sam_base_provider import SamBaseProvider

template = {
    "Transform": "AWS::Serverless-2016-10-31",
    "Parameters": {"DataRoot": {"Type": "String", "Default": r"C:\project\data"}},
    "Resources": {
        "ExampleFunction": {
            "Type": "AWS::Serverless::Function",
            "Properties": {
                "Runtime": "python3.12",
                "Handler": "index.handler",
                "InlineCode": 'def handler(event, context): return "ok"',
                "Environment": {"Variables": {"DATA_PATH": {"Fn::Sub": r"${DataRoot}\cache"}}},
            },
        }
    },
}
resolved = SamBaseProvider.get_template(template, {})
value = resolved["Resources"]["ExampleFunction"]["Properties"]["Environment"]["Variables"]["DATA_PATH"]
print(repr(value))
assert value == r"C:\project\data\cache"

Observed result

The assertion fails. The value remains:

{'Fn::Sub': '${DataRoot}\\cache'}

The underlying handle_fn_sub passes the resolved value to re.sub as its replacement string, which raises re.error: bad escape \p at position 2. The local provider's permissive resolution catches the exception and preserves the unresolved expression. A replacement containing \1 raises an invalid-group-reference error; \n and doubled backslashes can be silently transformed.

Expected result

The value is the string C:\project\data\cache, with its backslashes preserved. Resolved parameter and variable-map values should be inserted literally, consistent with the Fn::Sub substitution contract.

Additional environment details

  • Windows, Python 3.12.13; development SAM CLI version 1.171.0 at the commit above.
  • No region-specific behavior or AWS API calls are involved.
  • CLI debug output also reports Unable to resolve property DATA_PATH ... Leaving as is; a complete local invocation was not run because no container runtime was available.

Activity

  1. vinsymoyya commented on Nov 3, 2026

    @vinsymoyya
    ContributorAuthor

    Opened #9397 with a fix that inserts resolved Fn::Sub values literally, preserving backslashes. All six new regressions fail against the original code and pass with the fix, including the real local template provider case. The full make pr run passed with 9,758 tests and 94.09% coverage.

  2. valerena commented on Nov 4, 2026

    @valerena
    Contributor

    Thanks for the report and the contribution. I just approved the CI run in the PR and we'll take a look at it soon.

  3. github-actions commented on Nov 5, 2026

    @github-actions
    Contributor

    ⚠️COMMENT VISIBILITY WARNING⚠️

    Comments on closed issues are hard for our team to see.
    If you need more assistance, please either tag a team member or open a new issue that references this one.
    If you wish to keep having a conversation with other community members under this issue feel free to do so.

  4. reedham-aws commented on Nov 5, 2026

    @reedham-aws
    Contributor

    This will be released in v1.172.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions