Skip to content

Commit 10a8fa9

Browse files
committed
fix: use exact cncf regex and re2 library
1 parent 79a0338 commit 10a8fa9

File tree

3 files changed

+921
-23
lines changed

3 files changed

+921
-23
lines changed

lib/image-reference.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,21 @@
33
* Uses OCI distribution-style regexes to parse name, registry, tag, and digest.
44
*/
55

6+
import RE2 = require("re2");
7+
68
// Full reference: optional registry, repository path, optional tag, optional digest.
79
// Capture groups: 1 = name (repo path including optional registry), 2 = tag, 3 = digest.
8-
const imageReferencePattern = String.raw`^((?:(?:(?:[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9])(?:\.(?:[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9]))*|\[(?:[a-fA-F0-9:]+)\])(?::[0-9]+)?/)?[a-z0-9]+(?:(?:[._]|__|[-]+)[a-z0-9]+)*(?:/[a-z0-9]+(?:(?:[._]|__|[-]+)[a-z0-9]+)*)*)(?::([a-zA-Z0-9_][a-zA-Z0-9._-]{0,127}))?(?:@([A-Za-z][A-Za-z0-9]*(?:[-_+.][A-Za-z][A-Za-z0-9]*)*[:][a-fA-F0-9]{32,}))?$`;
9-
const imageReferenceRegex = new RegExp(imageReferencePattern);
10+
const imageReferencePattern = String.raw`^((?:(?:(?:[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9])(?:\.(?:[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9]))*|\[(?:[a-fA-F0-9:]+)\])(?::[0-9]+)?/)?[a-z0-9]+(?:(?:[._]|__|[-]+)[a-z0-9]+)*(?:/[a-z0-9]+(?:(?:[._]|__|[-]+)[a-z0-9]+)*)*)(?::([\w][\w.-]{0,127}))?(?:@([A-Za-z][A-Za-z0-9]*(?:[-_+.][A-Za-z][A-Za-z0-9]*)*[:][[:xdigit:]]{32,}))?$`;
11+
const imageReferenceRegex = new RE2(imageReferencePattern);
1012

1113
// Registry prefix only. Requires '.' or localhost to distinguish registry from repository.
1214
// Capture group 1 = registry hostname (no trailing '/' or '@').
1315
const imageRegistryPattern = String.raw`^((?:(?:[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9])(?:\.(?:[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9]))+|\[(?:[a-fA-F0-9:]+)\]|localhost)(?::[0-9]+)?)(?:/|@)`;
14-
const imageRegistryRegex = new RegExp(imageRegistryPattern);
16+
const imageRegistryRegex = new RE2(imageRegistryPattern);
1517

1618
// Digest regex. Anchored to the start and end of the string.
1719
const digestPattern = String.raw`^[A-Za-z][A-Za-z0-9]*(?:[-_+.][A-Za-z][A-Za-z0-9]*)*[:][a-fA-F0-9]{32,}$`;
18-
const digestRegex = new RegExp(digestPattern);
20+
const digestRegex = new RE2(digestPattern);
1921

2022
export class ParsedImageReference {
2123
/** Repository path (e.g. nginx, library/nginx) */

0 commit comments

Comments
 (0)