Отвечать 400 и 401 вместо 500 на NUL и невалидный UTF-8 во входных данных
This commit is contained in:
@@ -46,6 +46,10 @@ func NewChat(id uuid.UUID, title string) (Chat, error) {
|
||||
if utf8.RuneCountInString(title) > MaxTitleCodePoints {
|
||||
return Chat{}, apperr.Invalid(fmt.Sprintf("title must be at most %d characters", MaxTitleCodePoints))
|
||||
}
|
||||
// PostgreSQL text cannot hold U+0000.
|
||||
if strings.ContainsRune(title, 0) {
|
||||
return Chat{}, apperr.Invalid("title must not contain the NUL character")
|
||||
}
|
||||
return Chat{ID: id, Title: title, CreatedAt: ident.TimeOf(id)}, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -23,7 +23,9 @@ func TestNewChatDerivesCreatedAtFromID(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestNewChatValidatesTitle(t *testing.T) {
|
||||
for _, title := range []string{"", " ", strings.Repeat("я", 101)} {
|
||||
// PostgreSQL text cannot hold U+0000, so a title with it is invalid input,
|
||||
// not a storage failure.
|
||||
for _, title := range []string{"", " ", strings.Repeat("я", 101), "nul\x00inside"} {
|
||||
_, err := chat.NewChat(ident.New(), title)
|
||||
if apperr.KindOf(err) != apperr.KindInvalid {
|
||||
t.Errorf("title %q must be rejected as invalid, got %v", title, err)
|
||||
|
||||
@@ -30,6 +30,10 @@ func NewMessage(id, chatID, senderID uuid.UUID, text string) (Message, error) {
|
||||
if utf8.RuneCountInString(text) > MaxTextCodePoints {
|
||||
return Message{}, apperr.Invalid(fmt.Sprintf("text must be at most %d characters", MaxTextCodePoints))
|
||||
}
|
||||
// PostgreSQL text cannot hold U+0000.
|
||||
if strings.ContainsRune(text, 0) {
|
||||
return Message{}, apperr.Invalid("text must not contain the NUL character")
|
||||
}
|
||||
return Message{
|
||||
ID: id,
|
||||
ChatID: chatID,
|
||||
|
||||
@@ -28,7 +28,7 @@ func TestNewMessageValidatesTextByCodePoints(t *testing.T) {
|
||||
if _, err := chat.NewMessage(ident.New(), chatID, alice, strings.Repeat("я", 4000)); err != nil {
|
||||
t.Errorf("4000 code points must be accepted: %v", err)
|
||||
}
|
||||
for _, text := range []string{"", " ", "\n\t ", strings.Repeat("я", 4001)} {
|
||||
for _, text := range []string{"", " ", "\n\t ", strings.Repeat("я", 4001), "nul\x00inside"} {
|
||||
_, err := chat.NewMessage(ident.New(), chatID, alice, text)
|
||||
if apperr.KindOf(err) != apperr.KindInvalid {
|
||||
t.Errorf("text %q must be rejected as invalid, got %v", text, err)
|
||||
|
||||
Reference in New Issue
Block a user