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