forked from mtusi2026/mtusi2026lab4
feat(app) implemented lab 4
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func TestValidateFilename(t *testing.T) {
|
||||
valid := []string{"photo.png", "фото 1.jpeg", "a", strings.Repeat("a", 255)}
|
||||
for _, name := range valid {
|
||||
if err := ValidateFilename(name); err != nil {
|
||||
t.Errorf("ValidateFilename(%q) = %v, want nil", name, err)
|
||||
}
|
||||
}
|
||||
|
||||
invalid := []string{
|
||||
"", " ", ".", "..", "dir/photo.png", `dir\photo.png`,
|
||||
"nul\x00byte", "line\nbreak", strings.Repeat("a", 256),
|
||||
}
|
||||
for _, name := range invalid {
|
||||
if err := ValidateFilename(name); err == nil {
|
||||
t.Errorf("ValidateFilename(%q) = nil, want error", name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateAttachmentIDs(t *testing.T) {
|
||||
ids := make([]uuid.UUID, 10)
|
||||
for index := range ids {
|
||||
ids[index] = uuid.New()
|
||||
}
|
||||
if err := ValidateAttachmentIDs(ids); err != nil {
|
||||
t.Fatalf("ten unique ids rejected: %v", err)
|
||||
}
|
||||
if err := ValidateAttachmentIDs(append(ids, uuid.New())); err == nil {
|
||||
t.Fatal("eleven ids accepted")
|
||||
}
|
||||
if err := ValidateAttachmentIDs([]uuid.UUID{ids[0], ids[0]}); err == nil {
|
||||
t.Fatal("duplicate ids accepted")
|
||||
}
|
||||
if err := ValidateAttachmentIDs(nil); err != nil {
|
||||
t.Fatalf("empty list rejected: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateMessageText(t *testing.T) {
|
||||
if err := ValidateMessageText("", 1); err != nil {
|
||||
t.Fatalf("empty text with attachment rejected: %v", err)
|
||||
}
|
||||
if err := ValidateMessageText(" ", 1); err != nil {
|
||||
t.Fatalf("blank text with attachment rejected: %v", err)
|
||||
}
|
||||
if err := ValidateMessageText("", 0); err == nil {
|
||||
t.Fatal("empty text without attachment accepted")
|
||||
}
|
||||
if err := ValidateMessageText(strings.Repeat("ж", 4000), 0); err != nil {
|
||||
t.Fatalf("4000 code points rejected: %v", err)
|
||||
}
|
||||
if err := ValidateMessageText(strings.Repeat("ж", 4001), 1); err == nil {
|
||||
t.Fatal("4001 code points accepted")
|
||||
}
|
||||
}
|
||||
@@ -9,12 +9,40 @@ import (
|
||||
"github.com/gofiber/fiber/v3"
|
||||
)
|
||||
|
||||
func BindJSON(c fiber.Ctx, dst any) error {
|
||||
// ReadBodyLimited reads at most limit bytes from the request body. The caller
|
||||
// must use it instead of c.Body() when the endpoint has a smaller budget than
|
||||
// the process-wide Fiber.BodyLimit. The limit itself belongs to the caller
|
||||
// (config.MaxJSONBodyBytes for JSON endpoints).
|
||||
//
|
||||
// with StreamRequestBody enabled fasthttp exposes the network body as a
|
||||
// stream, so io.ReadAll over a LimitReader allocates only the bytes the
|
||||
// endpoint actually accepts instead of the whole upload limit.
|
||||
func ReadBodyLimited(c fiber.Ctx, limit int64) ([]byte, error) {
|
||||
var reader io.Reader = c.Request().BodyStream()
|
||||
if reader == nil {
|
||||
reader = bytes.NewReader(c.Body())
|
||||
}
|
||||
|
||||
data, err := io.ReadAll(io.LimitReader(reader, limit+1))
|
||||
if err != nil {
|
||||
return nil, BadRequest("incomplete request body")
|
||||
}
|
||||
if int64(len(data)) > limit {
|
||||
return nil, PayloadTooLarge("request body is too large")
|
||||
}
|
||||
|
||||
return data, nil
|
||||
}
|
||||
|
||||
func BindJSON(c fiber.Ctx, dst any, limit int64) error {
|
||||
if contentType := c.Get(fiber.HeaderContentType); !strings.HasPrefix(contentType, "application/json") {
|
||||
return UnsupportedMedia("invalid JSON body")
|
||||
}
|
||||
|
||||
body := c.Body()
|
||||
body, err := ReadBodyLimited(c, limit)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(body) == 0 {
|
||||
return BadRequest("request body is empty")
|
||||
}
|
||||
|
||||
@@ -79,6 +79,10 @@ func UnsupportedMedia(message string) *AppError {
|
||||
return NewAppError(http.StatusUnsupportedMediaType, ErrorCodeUnsupportedMedia, message)
|
||||
}
|
||||
|
||||
func PayloadTooLarge(message string) *AppError {
|
||||
return NewAppError(http.StatusRequestEntityTooLarge, ErrorCodePayloadTooLarge, message)
|
||||
}
|
||||
|
||||
func Internal() *AppError {
|
||||
return NewAppError(http.StatusInternalServerError, ErrorCodeInternal, "Internal server error")
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package common
|
||||
import (
|
||||
"regexp"
|
||||
"strings"
|
||||
"unicode"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/google/uuid"
|
||||
@@ -62,13 +63,49 @@ func ValidateChatTitle(title string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func ValidateMessageText(text string) error {
|
||||
if blank(text) {
|
||||
func ValidateMessageText(text string, attachmentCount int) error {
|
||||
if attachmentCount == 0 && blank(text) {
|
||||
return BadRequest("text must not be empty or only whitespace")
|
||||
}
|
||||
|
||||
if !strLenBetween(text, 1, 4000) {
|
||||
return BadRequest("text must be between 1 and 4000 Unicode code points")
|
||||
if !strLenBetween(text, 0, 4000) {
|
||||
return BadRequest("text must be between 0 and 4000 Unicode code points")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func ValidateAttachmentIDs(ids []uuid.UUID) error {
|
||||
if len(ids) > 10 {
|
||||
return BadRequest("attachment_ids must contain at most 10 items")
|
||||
}
|
||||
|
||||
seen := make(map[uuid.UUID]bool, len(ids))
|
||||
for _, id := range ids {
|
||||
if seen[id] {
|
||||
return BadRequest("attachment_ids must not contain duplicates")
|
||||
}
|
||||
seen[id] = true
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func ValidateFilename(filename string) error {
|
||||
if !strLenBetween(filename, 1, 255) {
|
||||
return BadRequest("filename must be between 1 and 255 Unicode code points")
|
||||
}
|
||||
if blank(filename) {
|
||||
return BadRequest("filename must not be empty or only whitespace")
|
||||
}
|
||||
if filename == "." || filename == ".." {
|
||||
return BadRequest("filename is not allowed")
|
||||
}
|
||||
|
||||
for _, r := range filename {
|
||||
if r == '/' || r == '\\' || r == 0 || unicode.IsControl(r) {
|
||||
return BadRequest("filename contains a forbidden character")
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user