исправления
This commit is contained in:
@@ -28,7 +28,6 @@ import (
|
||||
const (
|
||||
maxAttachmentBytes = 10 << 20
|
||||
maxPixels = 40_000_000
|
||||
fullDecodePixels = 4_000_000 // полное декодирование только для небольших изображений
|
||||
)
|
||||
|
||||
func validFilename(s string) bool {
|
||||
@@ -139,11 +138,9 @@ func (a *api) handleUploadAttachment(w http.ResponseWriter, r *http.Request, u m
|
||||
writeError(w, http.StatusRequestEntityTooLarge, requestID, codeTooLarge, "image has too many pixels")
|
||||
return
|
||||
}
|
||||
if pixels <= fullDecodePixels {
|
||||
if _, _, err := image.Decode(io.NewSectionReader(tmp, 0, n)); err != nil {
|
||||
writeError(w, http.StatusUnsupportedMediaType, requestID, codeUnsupportedMedia, "corrupted image")
|
||||
return
|
||||
}
|
||||
if _, _, err := image.Decode(io.NewSectionReader(tmp, 0, n)); err != nil {
|
||||
writeError(w, http.StatusUnsupportedMediaType, requestID, codeUnsupportedMedia, "corrupted image")
|
||||
return
|
||||
}
|
||||
|
||||
id := idgen.GenerateUUIDv4()
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
package httpapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
|
||||
"golang.org/x/sync/singleflight"
|
||||
@@ -50,9 +52,12 @@ func (a *api) authMiddleware(next func(w http.ResponseWriter, r *http.Request, u
|
||||
return
|
||||
}
|
||||
|
||||
u, ok, err := a.store.GetUserByID(r.Context(), userID)
|
||||
dbCtx, dbCancel := context.WithTimeout(r.Context(), 3*time.Second)
|
||||
defer dbCancel()
|
||||
|
||||
u, ok, err := a.store.GetUserByID(dbCtx, userID)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusServiceUnavailable, requestID, codeInternal, "database unavailable")
|
||||
writeError(w, http.StatusServiceUnavailable, requestID, codeUnavailable, "database unavailable")
|
||||
return
|
||||
}
|
||||
if !ok {
|
||||
|
||||
+31
-17
@@ -385,14 +385,20 @@ func (s *Store) CreateMessage(ctx context.Context, chatID, senderID, idemKey, te
|
||||
if tag.RowsAffected() == 0 {
|
||||
var storedHash string
|
||||
var m model.Message
|
||||
if err := tx.QueryRow(ctx,
|
||||
`SELECT k.request_hash, m.id, m.chat_id, m.sender_id, m.text, m.created_at
|
||||
FROM idempotency_keys k JOIN messages m ON m.id = k.message_id
|
||||
WHERE k.user_id=$1 AND k.chat_id=$2 AND k.key=$3`,
|
||||
senderID, chatID, idemKey,
|
||||
).Scan(&storedHash, &m.ID, &m.ChatID, &m.SenderID, &m.Text, &m.CreatedAt); err != nil {
|
||||
var textBytes []byte
|
||||
|
||||
if err := tx.QueryRow(ctx, `
|
||||
SELECT k.request_hash, m.id, m.chat_id, m.sender_id, m.text, m.created_at
|
||||
FROM idempotency_keys k
|
||||
JOIN messages m ON m.id = k.message_id
|
||||
WHERE k.user_id=$1 AND k.chat_id=$2 AND k.key=$3
|
||||
`, senderID, chatID, idemKey).Scan(
|
||||
&storedHash, &m.ID, &m.ChatID, &m.SenderID, &textBytes, &m.CreatedAt,
|
||||
); err != nil {
|
||||
return model.Message{}, err
|
||||
}
|
||||
|
||||
m.Text = string(textBytes)
|
||||
if storedHash != hash {
|
||||
return model.Message{}, ErrIdempotencyConflict
|
||||
}
|
||||
@@ -403,18 +409,22 @@ func (s *Store) CreateMessage(ctx context.Context, chatID, senderID, idemKey, te
|
||||
}
|
||||
|
||||
var m model.Message
|
||||
err = tx.QueryRow(ctx,
|
||||
`INSERT INTO messages (id, chat_id, sender_id, text, created_at)
|
||||
VALUES ($1,$2,$3,$4, clock_timestamp())
|
||||
RETURNING id, chat_id, sender_id, text, created_at`,
|
||||
msgID, chatID, senderID, text,
|
||||
).Scan(&m.ID, &m.ChatID, &m.SenderID, &m.Text, &m.CreatedAt)
|
||||
var textBytes []byte
|
||||
|
||||
err = tx.QueryRow(ctx, `
|
||||
INSERT INTO messages (id, chat_id, sender_id, text, created_at)
|
||||
VALUES ($1,$2,$3,$4, clock_timestamp())
|
||||
RETURNING id, chat_id, sender_id, text, created_at
|
||||
`, msgID, chatID, senderID, []byte(text)).Scan(
|
||||
&m.ID, &m.ChatID, &m.SenderID, &textBytes, &m.CreatedAt,
|
||||
)
|
||||
if err != nil {
|
||||
if isCheckViolation(err) {
|
||||
return model.Message{}, ErrValidation
|
||||
}
|
||||
return model.Message{}, err
|
||||
}
|
||||
m.Text = string(textBytes)
|
||||
for i, id := range attIDs {
|
||||
if _, err := tx.Exec(ctx,
|
||||
`INSERT INTO message_attachments (message_id, attachment_id, position) VALUES ($1,$2,$3)`,
|
||||
@@ -473,9 +483,15 @@ func (s *Store) ListMessages(ctx context.Context, chatID, userID, cursorStr stri
|
||||
var page []model.Message
|
||||
for rows.Next() {
|
||||
var m model.Message
|
||||
if err := rows.Scan(&m.ID, &m.ChatID, &m.SenderID, &m.Text, &m.CreatedAt); err != nil {
|
||||
var textBytes []byte
|
||||
|
||||
if err := rows.Scan(
|
||||
&m.ID, &m.ChatID, &m.SenderID, &textBytes, &m.CreatedAt,
|
||||
); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
m.Text = string(textBytes)
|
||||
page = append(page, m)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
@@ -511,14 +527,12 @@ func isCheckViolation(err error) bool {
|
||||
}
|
||||
|
||||
func ValidMessageBody(s string, hasAttachments bool) bool {
|
||||
if utf8.RuneCountInString(s) > 4000 {
|
||||
if !utf8.ValidString(s) || utf8.RuneCountInString(s) > 4000 {
|
||||
return false
|
||||
}
|
||||
|
||||
hasContent := false
|
||||
for _, r := range s {
|
||||
if r == 0 {
|
||||
return false
|
||||
}
|
||||
if !unicode.IsSpace(r) {
|
||||
hasContent = true
|
||||
}
|
||||
|
||||
@@ -26,16 +26,27 @@ CREATE TABLE chat_members (
|
||||
PRIMARY KEY (chat_id, user_id)
|
||||
);
|
||||
|
||||
CREATE OR REPLACE FUNCTION bytea_utf8_char_length(value BYTEA)
|
||||
RETURNS INTEGER
|
||||
LANGUAGE SQL IMMUTABLE STRICT PARALLEL SAFE
|
||||
AS $$
|
||||
SELECT COUNT(*)::INTEGER
|
||||
FROM generate_series(0, octet_length(value) - 1) AS positions(i)
|
||||
WHERE get_byte(value, i) < 128
|
||||
OR get_byte(value, i) >= 192;
|
||||
$$;
|
||||
|
||||
CREATE TABLE messages (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
chat_id UUID NOT NULL REFERENCES chats(id) ON DELETE CASCADE,
|
||||
sender_id UUID NOT NULL REFERENCES users(id),
|
||||
text TEXT NOT NULL,
|
||||
text BYTEA NOT NULL,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
CONSTRAINT text_length CHECK (char_length(text) BETWEEN 1 AND 4000)
|
||||
CONSTRAINT text_length CHECK (
|
||||
bytea_utf8_char_length(text) BETWEEN 1 AND 4000
|
||||
)
|
||||
);
|
||||
|
||||
|
||||
CREATE INDEX idx_messages_chat_created_id ON messages (chat_id, created_at, id);
|
||||
|
||||
CREATE TABLE sessions (
|
||||
|
||||
@@ -22,4 +22,6 @@ CREATE TABLE message_attachments (
|
||||
CREATE INDEX idx_message_attachments_attachment ON message_attachments (attachment_id);
|
||||
|
||||
ALTER TABLE messages DROP CONSTRAINT text_length;
|
||||
ALTER TABLE messages ADD CONSTRAINT text_length CHECK (char_length(text) <= 4000);
|
||||
ALTER TABLE messages ADD CONSTRAINT text_length CHECK (
|
||||
bytea_utf8_char_length(text) BETWEEN 0 AND 4000
|
||||
);
|
||||
Reference in New Issue
Block a user