Skip to content
Closed
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
4 changes: 2 additions & 2 deletions id.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,8 @@ func (id *ID) UnmarshalJSON(b []byte) error {
*id = nilID
return nil
}
// Check the slice length to prevent panic on passing it to UnmarshalText()
if len(b) < 2 {
// Only JSON strings can contain an encoded ID.
if len(b) < 2 || b[0] != '"' || b[len(b)-1] != '"' {
return ErrInvalidID
}
return id.UnmarshalText(b[1 : len(b)-1])
Expand Down
22 changes: 21 additions & 1 deletion id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,6 @@ func TestID_IsZero(t *testing.T) {
}
}


func TestNilID(t *testing.T) {
got := ID{}
if want := NilID(); !reflect.DeepEqual(got, want) {
Expand Down Expand Up @@ -622,3 +621,24 @@ func TestSort(t *testing.T) {
t.Fail()
}
}

func TestIDJSONRejectsNonStringValues(t *testing.T) {
for _, input := range []string{
`1111111111111111111101`,
`[11111111111111111110]`,
} {
t.Run(input, func(t *testing.T) {
if !json.Valid([]byte(input)) {
t.Fatal("test input must be valid JSON")
}
original := IDs[0].id
got := original
if err := json.Unmarshal([]byte(input), &got); err != ErrInvalidID {
t.Errorf("got error %v, want %v", err, ErrInvalidID)
}
if got != original {
t.Errorf("invalid JSON type changed ID: got %v, want %v", got, original)
}
})
}
}
Loading