Skip to content

fix: strip leading dots and hyphens from raw tags#646

Open
zry98 wants to merge 3 commits into
docker:masterfrom
zry98:fix/raw-tag-sanitizing
Open

fix: strip leading dots and hyphens from raw tags#646
zry98 wants to merge 3 commits into
docker:masterfrom
zry98:fix/raw-tag-sanitizing

Conversation

@zry98
Copy link
Copy Markdown

@zry98 zry98 commented Apr 22, 2026

According to the Docker image tag reference, the format is /[\w][\w.-]{0,127}/, a dot (.) or hyphen (-) is not allowed as a leading character.

sanitizeTag() was already replacing illegal characters from the middle of tag names, but not for the leading characters. A given raw tag like .foo\bar would be sanitized to .foo-bar, instead of foo-bar.

This PR added a second regex replacement pass .replace(/^[^a-zA-Z0-9_]+/, '') to strip any leading dots or hyphens.

Also added 3 related tests.

Signed-off-by: zry98 <dev@zry.io>
@zry98 zry98 force-pushed the fix/raw-tag-sanitizing branch from c13d8ec to bd9134e Compare April 22, 2026 16:41
@zry98
Copy link
Copy Markdown
Author

zry98 commented Apr 27, 2026

Hello @crazy-max , I see the CI failed with the yarn lockfile, is there something I need to update?

Copy link
Copy Markdown
Member

@crazy-max crazy-max left a comment

Choose a reason for hiding this comment

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

This is the right direction. Docker tags can contain . and -, but not as the first character, so stripping leading invalid characters matches the existing sanitizer behavior better than rejecting the tag.

One edge case needs tightening before merge: this can now sanitize a non-empty value into an empty tag, for example ..., ---, or /. setVersion() currently checks for empty before calling sanitizeTag(), so an empty sanitized value could become version.main and prevent a later valid tag from becoming the main version.

Could you move the empty check after sanitization?

val = Meta.sanitizeTag(val);
if (val.length == 0) {
  return version;
}

Please also add a focused test for that case, for example a higher-priority raw value that sanitizes to empty followed by a valid raw value, expecting the valid one to become main.

is there something I need to update?

See https://github.com/docker/metadata-action/blob/master/.github/CONTRIBUTING.md#submitting-a-pull-request

@zry98 zry98 force-pushed the fix/raw-tag-sanitizing branch 2 times, most recently from 973c3dd to e70b4d7 Compare May 22, 2026 17:36
Signed-off-by: zry98 <dev@zry.io>
@zry98 zry98 force-pushed the fix/raw-tag-sanitizing branch from e70b4d7 to 060b56a Compare May 22, 2026 17:45
@zry98
Copy link
Copy Markdown
Author

zry98 commented May 22, 2026

this can now sanitize a non-empty value into an empty tag, for example ..., ---, or /.

@crazy-max thank you for the nice catch! I've updated it and added some more related tests, please check.

@zry98 zry98 requested a review from crazy-max May 22, 2026 17:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants