Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expand regex to allow underscores (and periods) #172

Merged
merged 2 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,9 +450,12 @@ func graphURIFromName(name string) (string, error) {
}

// guidRx from https://learn.microsoft.com/en-us/rest/api/defenderforcloud/tasks/get-subscription-level-task
var guidRx = regexp.MustCompile(`^[0-9A-Fa-f]{8}-([0-9A-Fa-f]{4}-){3}[0-9A-Fa-f]{12}$`)
var nameRx = regexp.MustCompile(`^[a-zA-Z][a-zA-Z0-9\-]*$`)
var rgRx = regexp.MustCompile(`^[\-_.\pL\pN]*[\-_\pL\pN]$`)
var guidRx = regexp.MustCompile(`^[0-9A-Fa-f]{8}-([0-9A-Fa-f]{4}-){3}[0-9A-Fa-f]{12}$`) // just a uuid
// nameRx based on https://azure.github.io/PSRule.Rules.Azure/en/rules/Azure.VM.Name/#description
var nameRx = regexp.MustCompile(`^[a-zA-Z]$|^[a-zA-Z][a-zA-Z0-9.\-_]*[a-zA-Z0-9_]$`) // alphanumeric, doesn't start with a number, at least 1 character, doesn't end with a . or -
// https://azure.github.io/PSRule.Rules.Azure/en/rules/Azure.ResourceGroup.Name/ and https://learn.microsoft.com/en-us/azure/azure-resource-manager/management/resource-name-rules
// The latter documentation specifically allows characters in unicode letter/digit categories, which is wider than a-zA-Z0-9.
var rgRx = regexp.MustCompile(`^[\-_.()\pL\pN]*[\-_()\pL\pN]$`)

// verify the field provided matches Azure's requirements
// (see: https://learn.microsoft.com/en-us/azure/azure-resource-manager/management/resource-name-rules).
Expand Down
48 changes: 48 additions & 0 deletions azure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,18 +210,54 @@ func TestValidationRegex(t *testing.T) {
regex: guidRx,
isMatch: false,
},
{
name: "short name",
in: "a",
regex: nameRx,
isMatch: true,
},
{
name: "tricky name",
in: "real/../../secret/top-secret",
regex: nameRx,
isMatch: false,
},
{
name: "tricky but valid name",
in: "real.._..secret_top-secret",
regex: nameRx,
isMatch: true,
},
{
name: "valid name",
in: "this-name-is-good-14",
regex: nameRx,
isMatch: true,
},
{
name: "valid with underscore",
in: "this_name_is_good_15",
regex: nameRx,
isMatch: true,
},
{
name: "invalid start",
in: "15abc",
regex: nameRx,
isMatch: false,
},
{
name: "invalid end, period",
in: "a.",
regex: nameRx,
isMatch: false,
},
{
name: "invalid end, hyphen",
in: "a-",
regex: nameRx,
isMatch: false,
},
{
name: "tricky resource group",
in: "real/../../secret/top-secret",
Expand All @@ -234,6 +270,18 @@ func TestValidationRegex(t *testing.T) {
regex: rgRx,
isMatch: true,
},
{
name: "paren resource group",
in: ".(сыноо)",
regex: rgRx,
isMatch: true,
},
{
name: "resource group, invalid end with period",
in: ".(сыноо-_).",
regex: rgRx,
isMatch: false,
},
}

for _, tc := range cases {
Expand Down
Loading