Skip to content
Open
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
26 changes: 26 additions & 0 deletions validator/subscription.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package validator

import "unicode"

// Check empty strings, control characters (including NUL),
// Check characters that would affect URL parsing such as '/', '?', '#'.
func IsValidSubscriptionID(id string) bool {
if len(id) == 0 {

Check failure on line 8 in validator/subscription.go

View workflow job for this annotation

GitHub Actions / lint (1.25.5)

File is not properly formatted (gci)
return false
}

for _, r := range id {
if r <= 31 || r == 127 { // control characters
return false
}
switch r {
case '/', '?', '#':
return false
}
if unicode.IsSpace(r) {
return false
}
}

return true
}
57 changes: 57 additions & 0 deletions validator/subscription_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package validator

import (
"testing"

Check failure on line 4 in validator/subscription_test.go

View workflow job for this annotation

GitHub Actions / lint (1.25.5)

File is not properly formatted (gci)
)

func TestIsValidSubscriptionID(t *testing.T) {
type Args struct {
id string
}

type testCase struct {
name string
Args Args
Want bool
}

runTests := func(t *testing.T, tests []testCase) {
t.Helper()
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := IsValidSubscriptionID(tt.Args.id); got != tt.Want {
t.Errorf("IsValidSubscriptionID() = %v, Want %v (input: %q)", got, tt.Want, tt.Args.id)
}
})
}
}

t.Run("Valid", func(t *testing.T) {
tests := []testCase{
{"Numeric", Args{"123"}, true},
{"AlphaNumeric", Args{"sub-abc-123"}, true},
{"LongID", Args{"a-very-long-subscription-id-0123456789"}, true},
}
runTests(t, tests)
})

t.Run("Invalid_ControlChars", func(t *testing.T) {
tests := []testCase{
{"Empty", Args{""}, false},
{"NullByte", Args{"abc\x00def"}, false},
{"Ctrl1", Args{"abc\x01"}, false},
{"DEL", Args{"abc\x7f"}, false},
{"ContainsSpace", Args{"sub id"}, false},
}
runTests(t, tests)
})

t.Run("Invalid_URLChars", func(t *testing.T) {
tests := []testCase{
{"Slash", Args{"a/b"}, false},
{"Question", Args{"a?b"}, false},
{"Hash", Args{"a#b"}, false},
}
runTests(t, tests)
})
}
Loading