package domain import ( "encoding/hex" "fmt" "strings" "time" "unicode" "unicode/utf8" ) const ( MaxAttachmentFilenameCodePoints = 255 MaxAttachmentSizeBytes = 10 * 1024 * 1024 MaxAttachmentPixels = 40_000_000 ) type AttachmentVariant struct { Name string ContentType string SizeBytes int64 Width int Height int SHA256 string ObjectKey string PipelineVersion int } type AttachmentStatus string const ( AttachmentStatusQueued AttachmentStatus = "queued" AttachmentStatusProcessing AttachmentStatus = "processing" AttachmentStatusReady AttachmentStatus = "ready" AttachmentStatusFailed AttachmentStatus = "failed" // Source-compatible alias for the lab4 reconciliation code. AttachmentStatusPending = AttachmentStatusQueued ) // Attachment contains metadata only. ObjectKey points to bytes in the // private object store and must never be derived from Filename. type Attachment struct { ID string ChatID string OwnerID string Filename string ObjectKey string ContentType string SizeBytes int64 SHA256 string Width int Height int StagingKey string PipelineVersion int Status AttachmentStatus Variants []AttachmentVariant ErrorCode *string CreatedAt time.Time UpdatedAt time.Time } func ValidateAttachmentFilename(filename string) error { if !utf8.ValidString(filename) { return fmt.Errorf("filename must be valid UTF-8") } filenameLength := utf8.RuneCountInString(filename) if filenameLength < 1 || filenameLength > MaxAttachmentFilenameCodePoints { return fmt.Errorf("filename length must be between 1 and %d code points", MaxAttachmentFilenameCodePoints) } if strings.TrimSpace(filename) == "" || filename == "." || filename == ".." { return fmt.Errorf("filename is unsafe") } if strings.ContainsAny(filename, `/\\`) { return fmt.Errorf("filename must not contain path separators") } for _, character := range filename { if unicode.IsControl(character) { return fmt.Errorf("filename must not contain control characters") } } return nil } func ValidateAttachmentContentType(contentType string) error { if contentType != "image/png" && contentType != "image/jpeg" { return fmt.Errorf("content type must be image/png or image/jpeg") } return nil } // ValidateReadyAttachmentMetadata checks the bounded metadata produced after // the upload stream has been identified, decoded and hashed. It deliberately // does not trust an HTTP Content-Type header. func ValidateReadyAttachmentMetadata( filename string, contentType string, sizeBytes int64, sha256Hex string, width int, height int, ) error { if err := ValidateAttachmentFilename(filename); err != nil { return err } if err := ValidateAttachmentContentType(contentType); err != nil { return err } if sizeBytes < 1 || sizeBytes > MaxAttachmentSizeBytes { return fmt.Errorf("attachment size must be between 1 and %d bytes", MaxAttachmentSizeBytes) } if width < 1 || height < 1 { return fmt.Errorf("image dimensions must be positive and contain at most %d pixels", MaxAttachmentPixels) } if int64(width) > int64(MaxAttachmentPixels)/int64(height) { return fmt.Errorf("image dimensions must be positive and contain at most %d pixels", MaxAttachmentPixels) } if len(sha256Hex) != 64 || sha256Hex != strings.ToLower(sha256Hex) { return fmt.Errorf("sha256 must contain 64 lowercase hexadecimal characters") } decodedHash, err := hex.DecodeString(sha256Hex) if err != nil || len(decodedHash) != 32 { return fmt.Errorf("sha256 must contain 64 lowercase hexadecimal characters") } return nil }