From e2492959476291009ab99a8787b57756c8102b4c Mon Sep 17 00:00:00 2001 From: Luces Huayhuaca <21225410+uturunku1@users.noreply.github.com> Date: Tue, 15 Apr 2025 15:23:31 -0700 Subject: [PATCH] poc for rfc --- provider.go | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/provider.go b/provider.go index 7fb252e..d1bf648 100644 --- a/provider.go +++ b/provider.go @@ -51,6 +51,7 @@ const UnknownProviderNamespace = "?" // FQN. This may be produced by Terraform 0.13. const LegacyProviderNamespace = "-" +var allowedPattern = regexp.MustCompile("^[0-9A-Za-z](?:[0-9A-Za-z-_]{0,62}[0-9A-Za-z])?$") // String returns an FQN string, indended for use in machine-readable output. func (pt Provider) String() string { if pt.IsZero() { @@ -423,13 +424,24 @@ func ParseProviderPart(given string) (string, error) { // This also, as a side-effect, prevents the use of the "punycode" // indicator prefix "xn--" that would cause the IDNA library to interpret // the given name as punycode, because that would be weird and unexpected. - if strings.Contains(given, "--") { - return "", fmt.Errorf("cannot use multiple consecutive dashes") + + // if strings.Contains(given, "--") { + // return "", fmt.Errorf("cannot use multiple consecutive dashes") + // } //we could remove this check because ValidateLabels(true) will not allow double dashes + + // Custom validation for allowed characters + if !allowedPattern.MatchString(given) { + return "", fmt.Errorf("must contain only letters, digits, dashes, and underscores, and may not use leading or trailing dashes or underscores") } - result, err := idna.Lookup.ToUnicode(given) + opts := idna.New(idna.MapForLookup(), + idna.StrictDomainName(false), + idna.ValidateLabels(true), + idna.Transitional(false)) + + result, err := opts.ToUnicode(given) if err != nil { - return "", fmt.Errorf("must contain only letters, digits, and dashes, and may not use leading or trailing dashes") + return result, fmt.Errorf("must contain only letters, digits, dashes, and underscores, and may not use leading or trailing dashes or underscores") // even when the conversion fails, it result will return the original string } return result, nil