Skip to content

Commit

Permalink
disallow 'xml' prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
yzqzss committed Dec 23, 2024
1 parent 6fd99f9 commit ef5727f
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
9 changes: 7 additions & 2 deletions pkg/metadata/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ var KeyHyphenWithUnderscoreError = errors.New("Key contains -_ or _-, which is n
// Although we can create an item metadata keys starting with underscore via the S3 API,
// it would lead to an corrupted _meta.xml. (like `<_key>value</_key>`) and IA's /editxml/
// tool would also prevent you from editing the metadata before you fix the key.
var KeyInvalidStartError = errors.New("Key must start with a lowercase a-z letter")
// `xml` is a XML 1.1 reserved prefix.
var KeyInvalidStartError = errors.New("Key must start with a lowercase a-z letter and may not start with 'xml'")

// IA's metadata keys are always lowercase.
// If you try to create a metadata key with uppercase letters, IA will normalize it to lowercase.
Expand All @@ -44,7 +45,7 @@ var KeyInllegalCharError = errors.New("Key contains illegal characters")
var KeyTooLongError = errors.New("Key is too long")

// TODO: Item Metadata API option
func isValidKey(k string) error {
func IsValidKey(k string) error {
if len(k) == 0 {
return KeyEmptyError
}
Expand All @@ -62,6 +63,10 @@ func isValidKey(k string) error {
return KeyInvalidStartError
}

if strings.HasPrefix(k, "xml") {
return KeyInvalidStartError
}

for _, c := range k[1:] {
if c >= 'a' && c <= 'z' {
continue
Expand Down
3 changes: 2 additions & 1 deletion pkg/metadata/key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func Test_IsValidKey(t *testing.T) {
{"a-_b", KeyHyphenWithUnderscoreError},
{"a_-b", KeyHyphenWithUnderscoreError},
{"123abc", KeyInvalidStartError},
{"xmlabc", KeyInvalidStartError},
{"ABC123", KeyuUpcaseError},
{"abc___123", nil},
{"avr-123dc_adsd923-sd2.312-123.123_123", nil},
Expand All @@ -25,7 +26,7 @@ func Test_IsValidKey(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.key, func(t *testing.T) {
if err := isValidKey(tt.key); err != tt.errW {
if err := IsValidKey(tt.key); err != tt.errW {
t.Fatalf("want %v, got %v", tt.errW, err)
}
})
Expand Down
2 changes: 1 addition & 1 deletion pkg/metadata/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var ErrEmptyValue = errors.New("empty value")
var ErrNLessThanOne = errors.New("n must be greater than 0")

func toS3HeaderKey(k string, n int) (string, error) {
if err := isValidKey(k); err != nil {
if err := IsValidKey(k); err != nil {
return "", err
}
if n < 1 {
Expand Down

0 comments on commit ef5727f

Please sign in to comment.