Skip to content

api/image: escape images[].name before interpolating into regexp - #6208

Open
zanarellidev wants to merge 2 commits into
kubernetes-sigs:masterfrom
zanarellidev:fix/image-name-regex-escape
Open

api/image: escape images[].name before interpolating into regexp#6208
zanarellidev wants to merge 2 commits into
kubernetes-sigs:masterfrom
zanarellidev:fix/image-name-regex-escape

Conversation

@zanarellidev

Copy link
Copy Markdown

Summary

images[].name is documented as an identical/literal image name match:

// Name is a tag-less image name.

but IsImageMatched splices it unescaped into a regexp pattern:

pattern, err := regexp.Compile("^" + t + "(:...)?(@...)?$")

Since . is a regexp metacharacter and appears in virtually every real registry hostname (gcr.io, docker.io, quay.io, ghcr.io, *.azurecr.io), a rule such as:

images:
  - name: gcr.io/my-project/my-app
    newTag: v2

would also match an unrelated image like gcrXio/my-project/my-app if one happened to be present in the same manifest set, contradicting the documented "identical" semantics.

This line has had two recent fixes for adjacent issues (#6184 panic-on-invalid-regexp, #6167 digest-algorithm matching) that didn't touch this particular gap.

Fix

Wrap the interpolated name in regexp.QuoteMeta so it's matched literally, per commit 2.

Test plan

  • Added a failing-first regression test at the unit level (image_test.go) and an end-to-end krusty test (transformersimage_test.go), following this repo's own documented reviewer-guide shape (failing-test commit, then fix commit).
  • Verified via git checkout <test-commit> -- image.go (pre-fix) that both new tests fail on unmodified HEAD.
  • Verified both tests pass after the fix, alongside all existing tests in api/internal/image/... and api/krusty/....

IsImageMatched interpolates images[].name unescaped into a regexp, so a
literal "." in a registry hostname (gcr.io, quay.io, docker.io, etc.)
acts as a wildcard instead of matching only itself, contradicting the
documented "identical" match semantics. These tests fail on current
HEAD; the following commit fixes it.
images[].name is documented as an identical/literal image name match,
but IsImageMatched spliced it unescaped into a regexp pattern. Since
"." is a regexp wildcard and appears in virtually every real registry
hostname, a rule like images: [{name: gcr.io/org/app, newTag: v2}]
could also incorrectly match an unrelated image such as
gcrXio/org/app if one happened to appear in the same manifest set.

Wrap the interpolated name in regexp.QuoteMeta so it is matched
literally, matching the documented behavior.
@kubernetes-prow

Copy link
Copy Markdown
Contributor

This PR has multiple commits, and the default merge method is: merge.
You can request commits to be squashed using the label: tide/merge-method-squash

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@kubernetes-prow kubernetes-prow Bot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Jul 31, 2026
@kubernetes-prow

Copy link
Copy Markdown
Contributor

Welcome @zanarellidev!

It looks like this is your first PR to kubernetes-sigs/kustomize 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes-sigs/kustomize has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@kubernetes-prow

Copy link
Copy Markdown
Contributor

Hi @zanarellidev. Thanks for your PR.

I'm waiting for a kubernetes-sigs member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work.

Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@kubernetes-prow kubernetes-prow Bot added the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label Jul 31, 2026
@kubernetes-prow
kubernetes-prow Bot requested review from koba1t and sarab97 July 31, 2026 00:49
@kubernetes-prow kubernetes-prow Bot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Jul 31, 2026

@saitejabandaru-in saitejabandaru-in left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent catch! Escaping the input with regexp.QuoteMeta is the correct approach to prevent regex injection/wildcard matching for image names. The tests clearly demonstrate the issue and verify the fix.

@saitejabandaru-in saitejabandaru-in left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Excellent catch. Using regexp.QuoteMeta is exactly the right approach here to prevent image registries containing dots from accidentally matching wildcard characters. The added tests look very comprehensive too.

@kubernetes-prow

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: saitejabandaru-in, zanarellidev
Once this PR has been reviewed and has the lgtm label, please assign varshaprasad96 for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

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

Labels

cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants