feat: lab4 - S3 attachments, image validation, failover

This commit is contained in:
2026-11-11 21:42:08 +03:00
parent 312131c344
commit 9fc37b0d8a
18 changed files with 1050 additions and 59 deletions
+1
View File
@@ -29,3 +29,4 @@ coverage/
.idea/
.vscode/
.aider*
+1 -1
View File
@@ -25,4 +25,4 @@ USER appuser
EXPOSE 8080
ENTRYPOINT ["./server"]
ENTRYPOINT ["./server"]
+253 -9
View File
@@ -1,23 +1,36 @@
package main
import (
"bytes"
"context"
"crypto/rand"
"crypto/sha256"
"database/sql"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"image"
_ "image/jpeg"
_ "image/png"
"io"
"log"
"net/http"
"os"
"os/signal"
"path/filepath"
"strconv"
"strings"
"syscall"
"time"
_ "github.com/lib/pq"
"github.com/minio/minio-go/v7"
"mtusi2026lab3/internal/config"
"mtusi2026lab3/internal/ident"
"mtusi2026lab3/internal/password"
"mtusi2026lab3/internal/s3"
)
const (
@@ -28,11 +41,15 @@ const (
instanceHeader = "X-Instance-Id"
idempotencyHeader = "Idempotency-Key"
maxIdempotencyKey = 128
maxUploadSize = 10 * 1024 * 1024
maxPixels = 40000000
)
type server struct {
db *sql.DB
instance string
s3Client *minio.Client
s3Bucket string
}
type credentials struct {
@@ -41,16 +58,18 @@ type credentials struct {
}
type newMessage struct {
Recipient string `json:"recipient"`
Body string `json:"body"`
Recipient string `json:"recipient"`
Body string `json:"body"`
AttachmentIDs []int64 `json:"attachment_ids"`
}
type message struct {
ID int64 `json:"id"`
SenderID int64 `json:"sender_id"`
Recipient string `json:"recipient"`
Body string `json:"body"`
CreatedAt time.Time `json:"created_at"`
ID int64 `json:"id"`
SenderID int64 `json:"sender_id"`
Recipient string `json:"recipient"`
Body string `json:"body"`
CreatedAt time.Time `json:"created_at"`
AttachmentIDs []int64 `json:"attachment_ids"`
}
type session struct {
@@ -76,7 +95,17 @@ func main() {
log.Fatalf("ping database: %v", err)
}
srv := &server{db: db, instance: cfg.InstanceID}
s3Client, s3Bucket, err := s3.NewClient()
if err != nil {
log.Fatalf("s3 client: %v", err)
}
ctx := context.Background()
if err := s3.EnsureBucket(ctx, s3Client, s3Bucket); err != nil {
log.Fatalf("ensure bucket: %v", err)
}
srv := &server{db: db, instance: cfg.InstanceID, s3Client: s3Client, s3Bucket: s3Bucket}
httpServer := &http.Server{
Addr: ":" + cfg.Port,
@@ -111,6 +140,8 @@ func (s *server) routes() http.Handler {
mux.HandleFunc("POST /api/auth/logout", s.handleLogout)
mux.HandleFunc("GET /api/messages", s.handleListMessages)
mux.HandleFunc("POST /api/messages", s.handleCreateMessage)
mux.HandleFunc("POST /api/attachments", s.handleUploadAttachment)
mux.HandleFunc("GET /api/attachments/", s.handleDownloadAttachment)
return s.addInstanceID(mux)
}
@@ -212,6 +243,7 @@ func (s *server) handleListMessages(w http.ResponseWriter, r *http.Request) {
writeError(w, http.StatusInternalServerError, "internal_error", "scan failed")
return
}
m.AttachmentIDs = s.getAttachmentIDs(r.Context(), m.ID)
items = append(items, m)
}
if err := rows.Err(); err != nil {
@@ -222,6 +254,24 @@ func (s *server) handleListMessages(w http.ResponseWriter, r *http.Request) {
writeJSON(w, http.StatusOK, items)
}
func (s *server) getAttachmentIDs(ctx context.Context, messageID int64) []int64 {
rows, err := s.db.QueryContext(ctx,
`SELECT id FROM attachments WHERE message_id = $1 ORDER BY id`, messageID)
if err != nil {
return []int64{}
}
defer rows.Close()
ids := make([]int64, 0)
for rows.Next() {
var id int64
if err := rows.Scan(&id); err == nil {
ids = append(ids, id)
}
}
return ids
}
func (s *server) handleCreateMessage(w http.ResponseWriter, r *http.Request) {
sess, err := s.authenticate(r)
if err != nil {
@@ -282,6 +332,20 @@ func (s *server) handleCreateMessage(w http.ResponseWriter, r *http.Request) {
return
}
for _, attID := range in.AttachmentIDs {
_, err := tx.ExecContext(ctx,
`UPDATE attachments SET message_id = $1 WHERE id = $2 AND message_id IS NULL`,
msg.ID, attID)
if err != nil {
writeError(w, http.StatusInternalServerError, "internal_error", "attachment link failed")
return
}
}
msg.AttachmentIDs = in.AttachmentIDs
if msg.AttachmentIDs == nil {
msg.AttachmentIDs = []int64{}
}
payload, err := json.Marshal(msg)
if err != nil {
writeError(w, http.StatusInternalServerError, "internal_error", "response encode failed")
@@ -290,7 +354,6 @@ func (s *server) handleCreateMessage(w http.ResponseWriter, r *http.Request) {
if _, err := tx.ExecContext(ctx,
`INSERT INTO idempotency_keys (key, response) VALUES ($1, $2::jsonb)`, key, payload); err != nil {
// A concurrent request on another replica won the race for this key.
tx.Rollback()
var existing []byte
if readErr := s.db.QueryRowContext(ctx,
@@ -310,6 +373,187 @@ func (s *server) handleCreateMessage(w http.ResponseWriter, r *http.Request) {
writeRawJSON(w, http.StatusCreated, payload)
}
func (s *server) handleUploadAttachment(w http.ResponseWriter, r *http.Request) {
sess, err := s.authenticate(r)
if err != nil {
writeError(w, http.StatusUnauthorized, "unauthorized", "invalid or expired session")
return
}
_ = sess
r.Body = http.MaxBytesReader(w, r.Body, maxUploadSize+1024)
if err := r.ParseMultipartForm(maxUploadSize); err != nil {
writeError(w, http.StatusBadRequest, "invalid_request", "file too large or invalid multipart")
return
}
file, header, err := r.FormFile("file")
if err != nil {
writeError(w, http.StatusBadRequest, "invalid_request", "file field is required")
return
}
defer file.Close()
// Проверка опасного имени ДО filepath.Base
rawName := header.Filename
if strings.Contains(rawName, "..") || strings.ContainsAny(rawName, "/\\") || strings.HasPrefix(filepath.Base(rawName), ".") {
writeError(w, http.StatusBadRequest, "invalid_request", "dangerous filename rejected")
return
}
filename := filepath.Base(rawName)
data, err := io.ReadAll(io.LimitReader(file, maxUploadSize+1))
if err != nil {
writeError(w, http.StatusInternalServerError, "internal_error", "read failed")
return
}
if int64(len(data)) > maxUploadSize {
writeError(w, http.StatusBadRequest, "invalid_request", "file exceeds 10 MiB limit")
return
}
contentType := detectMIME(data)
if contentType != "image/png" && contentType != "image/jpeg" {
writeError(w, http.StatusBadRequest, "invalid_request", "only PNG and JPEG allowed")
return
}
if contentType == "image/jpeg" || contentType == "image/png" {
cfg, _, err := image.DecodeConfig(bytes.NewReader(data))
if err != nil {
writeError(w, http.StatusBadRequest, "invalid_request", "cannot decode image dimensions")
return
}
if int64(cfg.Width)*int64(cfg.Height) > maxPixels {
writeError(w, http.StatusBadRequest, "invalid_request", "image exceeds 40 million pixels")
return
}
}
keyBytes := make([]byte, 16)
rand.Read(keyBytes)
s3Key := hex.EncodeToString(keyBytes)
hash := sha256.Sum256(data)
checksum := hex.EncodeToString(hash[:])
// Таймаут 5 секунд для S3 — чтобы быстро вернуть 503, а не ждать 504 от HAProxy
s3Ctx, s3Cancel := context.WithTimeout(r.Context(), 5*time.Second)
defer s3Cancel()
err = s3.UploadFile(s3Ctx, s.s3Client, s.s3Bucket, s3Key, bytes.NewReader(data), int64(len(data)), contentType)
if err != nil {
writeError(w, http.StatusServiceUnavailable, "s3_unavailable", "S3 storage unavailable")
return
}
// ИСПРАВЛЕНО: используем r.Context() вместо несуществующего ctx
var attID int64
err = s.db.QueryRowContext(r.Context(),
`INSERT INTO attachments (s3_key, filename, content_type, size, checksum)
VALUES ($1, $2, $3, $4, $5) RETURNING id`,
s3Key, filename, contentType, len(data), checksum).Scan(&attID)
if err != nil {
writeError(w, http.StatusInternalServerError, "internal_error", "metadata save failed")
return
}
writeJSON(w, http.StatusCreated, map[string]any{
"id": attID,
"filename": filename,
"content_type": contentType,
"size": len(data),
"checksum": checksum,
"status": "ready",
})
}
func (s *server) handleDownloadAttachment(w http.ResponseWriter, r *http.Request) {
sess, err := s.authenticate(r)
if err != nil {
writeError(w, http.StatusUnauthorized, "unauthorized", "invalid or expired session")
return
}
idStr := strings.TrimPrefix(r.URL.Path, "/api/attachments/")
attID, err := strconv.ParseInt(idStr, 10, 64)
if err != nil {
writeError(w, http.StatusBadRequest, "invalid_request", "invalid attachment id")
return
}
var s3Key, filename, contentType string
var size int64
var messageID sql.NullInt64
var uploaderUserID sql.NullInt64
err = s.db.QueryRowContext(r.Context(),
`SELECT a.s3_key, a.filename, a.content_type, a.size, a.message_id,
(SELECT sender_id FROM messages WHERE id = a.message_id)
FROM attachments a WHERE a.id = $1`,
attID).Scan(&s3Key, &filename, &contentType, &size, &messageID, &uploaderUserID)
if errors.Is(err, sql.ErrNoRows) {
writeError(w, http.StatusNotFound, "not_found", "attachment not found")
return
}
if err != nil {
// Fallback: если в таблице нет колонки uploader — используем простой запрос
err = s.db.QueryRowContext(r.Context(),
`SELECT s3_key, filename, content_type, size, message_id FROM attachments WHERE id = $1`,
attID).Scan(&s3Key, &filename, &contentType, &size, &messageID)
if errors.Is(err, sql.ErrNoRows) {
writeError(w, http.StatusNotFound, "not_found", "attachment not found")
return
}
if err != nil {
writeError(w, http.StatusInternalServerError, "internal_error", "query failed")
return
}
}
// Проверка прав доступа
if messageID.Valid {
var senderID int64
var recipient string
err = s.db.QueryRowContext(r.Context(),
`SELECT sender_id, recipient FROM messages WHERE id = $1`, messageID.Int64).Scan(&senderID, &recipient)
if err != nil {
writeError(w, http.StatusNotFound, "not_found", "message not found")
return
}
if senderID != sess.UserID && recipient != sess.Login {
writeError(w, http.StatusForbidden, "forbidden", "access denied")
return
}
}
// Таймаут 5 секунд для S3
s3Ctx, s3Cancel := context.WithTimeout(r.Context(), 5*time.Second)
defer s3Cancel()
obj, err := s3.DownloadFile(s3Ctx, s.s3Client, s.s3Bucket, s3Key)
if err != nil {
writeError(w, http.StatusServiceUnavailable, "s3_unavailable", "S3 storage unavailable")
return
}
defer obj.Close()
w.Header().Set("Content-Type", contentType)
w.Header().Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, filename))
w.Header().Set("Content-Length", strconv.FormatInt(size, 10))
w.WriteHeader(http.StatusOK)
io.Copy(w, obj)
}
func detectMIME(data []byte) string {
if len(data) >= 8 && data[0] == 0x89 && data[1] == 0x50 && data[2] == 0x4E && data[3] == 0x47 {
return "image/png"
}
if len(data) >= 3 && data[0] == 0xFF && data[1] == 0xD8 && data[2] == 0xFF {
return "image/jpeg"
}
return http.DetectContentType(data)
}
func (s *server) authenticate(r *http.Request) (*session, error) {
token, ok := bearerToken(r)
if !ok {
+73 -28
View File
@@ -1,70 +1,115 @@
version: '3.8'
services:
postgres:
image: postgres:latest
image: postgres:16-alpine
environment:
POSTGRES_DB: mydatabase
POSTGRES_USER: myuser
POSTGRES_PASSWORD: mypassword
volumes:
- postgres_data:/var/lib/postgresql/data
POSTGRES_USER: app
POSTGRES_PASSWORD: app
POSTGRES_DB: app
ports:
- "5432:5432"
volumes:
- pgdata:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U app -d app"]
interval: 5s
timeout: 3s
retries: 5
networks:
- lab3net
minio:
image: minio/minio:RELEASE.2024-10-02T17-50-41Z
command: server /data --console-address ":9001"
environment:
MINIO_ROOT_USER: minioadmin
MINIO_ROOT_PASSWORD: minioadmin123
image: chrislusf/seaweedfs:latest
command: server -s3 -dir=/data -s3.port=9000 -master.port=9333 -volume.port=8081 -filer.port=8888
ports:
- "9000:9000"
- "9001:9001"
- "9333:9333"
volumes:
- minio_data:/data
healthcheck:
test: ["CMD", "mc", "ready", "local"]
test: ["CMD", "curl", "-f", "http://localhost:9333/cluster/status"]
interval: 5s
depends_on:
postgres:
condition: service_healthy
timeout: 3s
retries: 5
start_period: 10s
networks:
- lab3net
api1:
image: myapiimage:latest
build:
context: .
dockerfile: Dockerfile
environment:
APP_INSTANCE_ID: api1
APP_PORT: "8080"
DATABASE_URL: postgres://app:app@postgres:5432/app?sslmode=disable
S3_ENDPOINT: http://minio:9000
S3_ACCESS_KEY: minioadmin
S3_SECRET_KEY: minioadmin123
S3_BUCKET: attachments
ports:
- "8080:8080"
expose:
- "8080"
depends_on:
postgres:
condition: service_healthy
minio:
condition: service_healthy
healthcheck:
test: ["CMD", "wget", "--spider", "-q", "http://localhost:8080/health"]
interval: 5s
timeout: 3s
retries: 5
start_period: 10s
stop_grace_period: 15s
networks:
- lab3net
api2:
image: myapiimage:latest
build:
context: .
dockerfile: Dockerfile
environment:
APP_INSTANCE_ID: api2
APP_PORT: "8080"
DATABASE_URL: postgres://app:app@postgres:5432/app?sslmode=disable
S3_ENDPOINT: http://minio:9000
S3_ACCESS_KEY: minioadmin
S3_SECRET_KEY: minioadmin123
S3_BUCKET: attachments
ports:
- "8081:8080"
expose:
- "8080"
depends_on:
postgres:
condition: service_healthy
minio:
condition: service_healthy
healthcheck:
test: ["CMD", "wget", "--spider", "-q", "http://localhost:8080/health"]
interval: 5s
timeout: 3s
retries: 5
start_period: 10s
stop_grace_period: 15s
networks:
- lab3net
haproxy:
image: haproxy:2.9-alpine
ports:
- "8080:8080"
volumes:
- ./infra/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro
depends_on:
api1:
condition: service_healthy
api2:
condition: service_healthy
networks:
- lab3net
volumes:
postgres_data:
minio_data:
networks:
lab3net:
driver: bridge
volumes:
pgdata:
minio_data:
+16
View File
@@ -4,5 +4,21 @@ go 1.22
require (
github.com/lib/pq v1.10.9
github.com/minio/minio-go/v7 v7.0.71
golang.org/x/crypto v0.21.0
)
require (
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/klauspost/compress v1.17.6 // indirect
github.com/klauspost/cpuid/v2 v2.2.6 // indirect
github.com/minio/md5-simd v1.1.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rs/xid v1.5.0 // indirect
golang.org/x/net v0.23.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
)
+32
View File
@@ -1,4 +1,36 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/klauspost/compress v1.17.6 h1:60eq2E/jlfwQXtvZEeBUYADs+BwKBWURIY+Gj2eRGjI=
github.com/klauspost/compress v1.17.6/go.mod h1:/dCuZOvVtNoHsyb+cuJD3itjs3NbnF6KH9zAO4BDxPM=
github.com/klauspost/cpuid/v2 v2.0.1/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/klauspost/cpuid/v2 v2.2.6 h1:ndNyv040zDGIDh8thGkXYjnFtiN02M1PVVF+JE/48xc=
github.com/klauspost/cpuid/v2 v2.2.6/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/minio/md5-simd v1.1.2 h1:Gdi1DZK69+ZVMoNHRXJyNcxrMA4dSxoYHZSQbirFg34=
github.com/minio/md5-simd v1.1.2/go.mod h1:MzdKDxYpY2BT9XQFocsiZf/NKVtR7nkE4RoEpN+20RM=
github.com/minio/minio-go/v7 v7.0.71 h1:No9XfOKTYi6i0GnBj+WZwD8WP5GZfL7n7GOjRqCdAjA=
github.com/minio/minio-go/v7 v7.0.71/go.mod h1:4yBA8v80xGA30cfM3fz0DKYMXunWl/AV/6tWEs9ryzo=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rs/xid v1.5.0 h1:mKX4bl4iPYJtEIxp6CYiUuLQ/8DYMoz0PUdtGgMFRVc=
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
golang.org/x/crypto v0.21.0 h1:X31++rzVUdKhX5sWmSOFZxx8UW/ldWx55cbf08iNAMA=
golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs=
golang.org/x/net v0.23.0 h1:7EYJ93RZ9vYSZAIb2x3lnuvqO5zneoD6IvWjuhfxjTs=
golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
+49 -14
View File
@@ -1,27 +1,62 @@
// Package config reads the runtime configuration shared by every API replica.
package config
import "os"
import (
"log"
"os"
"strconv"
"time"
)
const (
defaultPort = "8080"
defaultSessionTTL = time.Hour
defaultIdempotencyTTL = 24 * time.Hour
defaultDrainDelay = 3 * time.Second
)
// Config is the complete runtime configuration of one API process.
type Config struct {
InstanceID string
Port string
DatabaseURL string
InstanceID string
Port string
DatabaseURL string
SessionTTL time.Duration
IdempotencyTTL time.Duration
DrainDelay time.Duration
}
// Load reads the environment; DATABASE_URL is the only mandatory value.
func Load() *Config {
cfg := &Config{
InstanceID: os.Getenv("APP_INSTANCE_ID"),
Port: os.Getenv("APP_PORT"),
DatabaseURL: os.Getenv("DATABASE_URL"),
}
if cfg.InstanceID == "" {
cfg.InstanceID = "api"
}
if cfg.Port == "" {
cfg.Port = "8080"
InstanceID: valueOr(os.Getenv("APP_INSTANCE_ID"), "api"),
Port: valueOr(os.Getenv("APP_PORT"), defaultPort),
DatabaseURL: os.Getenv("DATABASE_URL"),
SessionTTL: durationOr("SESSION_TTL_SECONDS", defaultSessionTTL),
IdempotencyTTL: durationOr("IDEMPOTENCY_TTL_SECONDS", defaultIdempotencyTTL),
DrainDelay: durationOr("DRAIN_DELAY_SECONDS", defaultDrainDelay),
}
if cfg.DatabaseURL == "" {
panic("APP_INSTANCE_ID/APP_PORT optional; DATABASE_URL is required")
log.Fatal("config: DATABASE_URL is required")
}
return cfg
}
func valueOr(raw, fallback string) string {
if raw == "" {
return fallback
}
return raw
}
func durationOr(env string, fallback time.Duration) time.Duration {
raw := os.Getenv(env)
if raw == "" {
return fallback
}
seconds, err := strconv.Atoi(raw)
if err != nil || seconds <= 0 {
log.Printf("config: ignoring invalid %s=%q, using %s", env, raw, fallback)
return fallback
}
return time.Duration(seconds) * time.Second
}
+47
View File
@@ -0,0 +1,47 @@
// Package cursor implements the opaque keyset cursor used by paged collections.
package cursor
import (
"encoding/base64"
"errors"
"strings"
"time"
"mtusi2026lab3/internal/domain"
"mtusi2026lab3/internal/validate"
)
// ErrInvalid marks a cursor that cannot be decoded or belongs to another resource.
var ErrInvalid = errors.New("invalid cursor")
// Value is a decoded (created_at, id) position bound to one collection scope.
type Value struct {
CreatedAt time.Time
ID string
}
// Encode serialises a position so clients can replay it but not forge a new one.
func Encode(scope string, createdAt time.Time, id string) string {
raw := scope + "|" + createdAt.UTC().Format(domain.TimeFormat) + "|" + id
return base64.RawURLEncoding.EncodeToString([]byte(raw))
}
// Decode validates the cursor and rejects one issued for another collection.
func Decode(encoded, scope string) (Value, error) {
raw, err := base64.RawURLEncoding.DecodeString(encoded)
if err != nil {
return Value{}, ErrInvalid
}
parts := strings.Split(string(raw), "|")
if len(parts) != 3 || parts[0] != scope {
return Value{}, ErrInvalid
}
createdAt, err := time.Parse(domain.TimeFormat, parts[1])
if err != nil {
return Value{}, ErrInvalid
}
if !validate.UUID(parts[2]) {
return Value{}, ErrInvalid
}
return Value{CreatedAt: createdAt.UTC(), ID: parts[2]}, nil
}
+16
View File
@@ -0,0 +1,16 @@
package domain
import "errors"
// ErrNotFound covers both "missing" and "not visible to this user", so the API
// never reveals that someone else's object exists.
var ErrNotFound = errors.New("object not found")
// ErrUsernameTaken means the username uniqueness constraint was violated.
var ErrUsernameTaken = errors.New("username already taken")
// ErrIdempotencyConflict means the same key was reused with a different body.
var ErrIdempotencyConflict = errors.New("idempotency key reused with another body")
// ErrUnknownMembers means member_ids references users that do not exist.
var ErrUnknownMembers = errors.New("unknown chat members")
+67
View File
@@ -0,0 +1,67 @@
// Package domain holds the JSON shapes of the public contract.
package domain
import (
"encoding/json"
"time"
)
// TimeFormat is the RFC 3339 UTC representation used by every response.
const TimeFormat = "2006-01-02T15:04:05.000000Z"
// Timestamp marshals with stable microsecond precision and a trailing Z.
type Timestamp struct {
time.Time
}
// NewTimestamp keeps the precision PostgreSQL stores and normalises to UTC.
func NewTimestamp(t time.Time) Timestamp {
return Timestamp{Time: t.UTC().Round(time.Microsecond)}
}
func (t Timestamp) MarshalJSON() ([]byte, error) {
return json.Marshal(t.UTC().Format(TimeFormat))
}
type User struct {
ID string `json:"id"`
Username string `json:"username"`
DisplayName string `json:"display_name"`
CreatedAt Timestamp `json:"created_at"`
}
type Chat struct {
ID string `json:"id"`
Title string `json:"title"`
MemberIDs []string `json:"member_ids"`
CreatedAt Timestamp `json:"created_at"`
}
type Message struct {
ID string `json:"id"`
ChatID string `json:"chat_id"`
SenderID string `json:"sender_id"`
Text string `json:"text"`
CreatedAt Timestamp `json:"created_at"`
}
type ChatPage struct {
Items []Chat `json:"items"`
NextCursor *string `json:"next_cursor"`
}
type MessagePage struct {
Items []Message `json:"items"`
NextCursor *string `json:"next_cursor"`
}
type Session struct {
Token string `json:"token"`
TokenType string `json:"token_type"`
ExpiresAt Timestamp `json:"expires_at"`
User User `json:"user"`
}
type Health struct {
Status string `json:"status"`
}
+35 -6
View File
@@ -1,16 +1,45 @@
// Package ident produces opaque identifiers: request ids, session tokens and UUIDv4 values.
package ident
import (
"crypto/rand"
"encoding/hex"
"fmt"
)
const tokenBytes = 32
const (
tokenBytes = 32
requestIDSize = 16
)
// NewToken returns a 64 character hex session token (256 bits of entropy).
func NewToken() string {
return hex.EncodeToString(randomBytes(tokenBytes))
}
// NewRequestID returns a short opaque request identifier.
func NewRequestID() string {
return hex.EncodeToString(randomBytes(requestIDSize))
}
// NewUUID returns a RFC 4122 version 4 UUID string.
func NewUUID() string {
buf := randomBytes(16)
buf[6] = (buf[6] & 0x0f) | 0x40
buf[8] = (buf[8] & 0x3f) | 0x80
return fmt.Sprintf("%x-%x-%x-%x-%x", buf[0:4], buf[4:6], buf[6:8], buf[8:10], buf[10:16])
}
func randomBytes(size int) []byte {
buf := make([]byte, size)
if _, err := rand.Read(buf); err != nil {
panic("crypto/rand unavailable: " + err.Error())
}
return buf
}
func GenerateToken() string {
buf := make([]byte, tokenBytes)
if _, err := rand.Read(buf); err != nil {
panic(err)
}
return hex.EncodeToString(buf)
b := make([]byte, 32)
rand.Read(b)
return hex.EncodeToString(b)
}
+66
View File
@@ -0,0 +1,66 @@
package s3
import (
"context"
"errors"
"io"
"os"
"strings"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
)
func NewClient() (*minio.Client, string, error) {
endpoint := os.Getenv("S3_ENDPOINT")
accessKey := os.Getenv("S3_ACCESS_KEY")
secretKey := os.Getenv("S3_SECRET_KEY")
bucket := os.Getenv("S3_BUCKET")
if endpoint == "" || accessKey == "" || secretKey == "" || bucket == "" {
return nil, "", errors.New("S3_ENDPOINT, S3_ACCESS_KEY, S3_SECRET_KEY, S3_BUCKET must be set")
}
endpoint = strings.TrimPrefix(endpoint, "http://")
endpoint = strings.TrimPrefix(endpoint, "https://")
client, err := minio.New(endpoint, &minio.Options{
Creds: credentials.NewStaticV4(accessKey, secretKey, ""),
Secure: false,
})
if err != nil {
return nil, "", err
}
return client, bucket, nil
}
func EnsureBucket(ctx context.Context, client *minio.Client, bucket string) error {
exists, err := client.BucketExists(ctx, bucket)
if err != nil {
return err
}
if !exists {
return client.MakeBucket(ctx, bucket, minio.MakeBucketOptions{})
}
return nil
}
func UploadFile(ctx context.Context, client *minio.Client, bucket, key string, data io.Reader, size int64, contentType string) error {
_, err := client.PutObject(ctx, bucket, key, data, size, minio.PutObjectOptions{
ContentType: contentType,
})
return err
}
func DownloadFile(ctx context.Context, client *minio.Client, bucket, key string) (io.ReadCloser, error) {
obj, err := client.GetObject(ctx, bucket, key, minio.GetObjectOptions{})
if err != nil {
return nil, err
}
return obj, nil
}
func DeleteFile(ctx context.Context, client *minio.Client, bucket, key string) error {
return client.RemoveObject(ctx, bucket, key, minio.RemoveObjectOptions{})
}
+51
View File
@@ -0,0 +1,51 @@
// Package store is the only layer that talks to PostgreSQL.
package store
import (
"context"
"database/sql"
"time"
_ "github.com/lib/pq"
)
const (
maxOpenConns = 20
maxIdleConns = 10
connMaxLifetime = 5 * time.Minute
pingTimeout = 2 * time.Second
)
// Store wraps the shared PostgreSQL connection pool.
type Store struct {
db *sql.DB
}
// Open creates the pool; it does not require the database to be reachable yet.
func Open(databaseURL string) (*Store, error) {
db, err := sql.Open("postgres", databaseURL)
if err != nil {
return nil, err
}
db.SetMaxOpenConns(maxOpenConns)
db.SetMaxIdleConns(maxIdleConns)
db.SetConnMaxLifetime(connMaxLifetime)
return &Store{db: db}, nil
}
// Close releases the pool.
func (s *Store) Close() error {
return s.db.Close()
}
// Ping reports whether the database answers within a short budget; readiness uses it.
func (s *Store) Ping(ctx context.Context) error {
ctx, cancel := context.WithTimeout(ctx, pingTimeout)
defer cancel()
return s.db.PingContext(ctx)
}
// scanTime normalises timestamps returned by the driver.
func utc(t time.Time) time.Time {
return t.UTC()
}
+76
View File
@@ -0,0 +1,76 @@
package store
import (
"context"
"database/sql"
"errors"
"github.com/lib/pq"
"mtusi2026lab3/internal/domain"
"mtusi2026lab3/internal/ident"
)
// UserRecord is a user together with the password hash used only for login.
type UserRecord struct {
User domain.User
PasswordHash string
}
const userColumns = `id::text, username, display_name, created_at`
// CreateUser inserts a new user; a duplicate username maps to domain.ErrUsernameTaken.
func (s *Store) CreateUser(ctx context.Context, username, displayName, passwordHash string) (domain.User, error) {
user := domain.User{ID: ident.NewUUID(), Username: username, DisplayName: displayName}
row := s.db.QueryRowContext(ctx,
`INSERT INTO users (id, username, display_name, password_hash)
VALUES ($1::uuid, $2, $3, $4)
RETURNING `+userColumns,
user.ID, user.Username, user.DisplayName, passwordHash)
if err := scanUser(row, &user); err != nil {
if isUniqueViolation(err) {
return domain.User{}, domain.ErrUsernameTaken
}
return domain.User{}, err
}
return user, nil
}
// UserByUsername returns the credential record used by Basic authentication.
func (s *Store) UserByUsername(ctx context.Context, username string) (UserRecord, error) {
row := s.db.QueryRowContext(ctx,
`SELECT `+userColumns+`, password_hash FROM users WHERE username = $1`, username)
var record UserRecord
if err := scanUser(row, &record.User); err != nil {
return UserRecord{}, err
}
if err := row.Scan(&record.User.ID, &record.User.Username, &record.User.DisplayName,
&record.User.CreatedAt.Time, &record.PasswordHash); err != nil {
return UserRecord{}, err
}
return record, nil
}
// UserByID resolves the owner of a valid session.
func (s *Store) UserByID(ctx context.Context, id string) (domain.User, error) {
var user domain.User
row := s.db.QueryRowContext(ctx, `SELECT `+userColumns+` FROM users WHERE id = $1::uuid`, id)
if err := scanUser(row, &user); err != nil {
return domain.User{}, err
}
return user, nil
}
func scanUser(row *sql.Row, user *domain.User) error {
var created sql.NullTime
if err := row.Scan(&user.ID, &user.Username, &user.DisplayName, &created); err != nil {
return err
}
user.CreatedAt = domain.NewTimestamp(created.Time)
return nil
}
func isUniqueViolation(err error) bool {
var pqErr *pq.Error
return errors.As(err, &pqErr) && pqErr.Code == "23505"
}
+87
View File
@@ -0,0 +1,87 @@
// Package validate implements the field rules stated in contracts/README.md.
package validate
import (
"strings"
"unicode/utf8"
)
const (
UsernameMinLen = 3
UsernameMaxLen = 32
DisplayNameMaxLen = 100
PasswordMinLen = 12
PasswordMaxLen = 128
TitleMaxLen = 100
MessageMaxRunes = 4000
)
// UUID reports whether s has the canonical 8-4-4-4-12 hex shape.
func UUID(s string) bool {
if len(s) != 36 {
return false
}
for i := 0; i < len(s); i++ {
switch i {
case 8, 13, 18, 23:
if s[i] != '-' {
return false
}
default:
if !isHex(s[i]) {
return false
}
}
}
return true
}
// Username enforces ^[a-z0-9_]{3,32}$.
func Username(s string) bool {
if len(s) < UsernameMinLen || len(s) > UsernameMaxLen {
return false
}
for i := 0; i < len(s); i++ {
c := s[i]
if (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '_' {
continue
}
return false
}
return true
}
// NonBlank requires at least one non-whitespace code point.
func NonBlank(s string) bool {
return strings.TrimSpace(s) != ""
}
// DisplayName allows 1..100 code points and rejects whitespace-only values.
func DisplayName(s string) bool {
return NonBlank(s) && RuneLen(s) <= DisplayNameMaxLen
}
// Password checks the 12..128 code point range; Basic auth carries UTF-8.
func Password(s string) bool {
n := RuneLen(s)
return n >= PasswordMinLen && n <= PasswordMaxLen
}
// Title allows 1..100 code points and rejects whitespace-only values.
func Title(s string) bool {
return NonBlank(s) && RuneLen(s) <= TitleMaxLen
}
// MessageText allows 1..4000 code points and rejects whitespace-only values.
func MessageText(s string) bool {
return NonBlank(s) && RuneLen(s) <= MessageMaxRunes
}
// RuneLen counts Unicode code points, not bytes.
func RuneLen(s string) int {
return utf8.RuneCountInString(s)
}
func isHex(c byte) bool {
return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')
}
+65 -1
View File
@@ -9,6 +9,7 @@ LOGIN = "test"
PASSWORD = "test123"
TIMEOUT = 5
LAB = "lab4"
failures = []
@@ -21,6 +22,69 @@ def check(condition, message):
return False
def timestamp():
import datetime
return datetime.datetime.utcnow().isoformat() + "Z"
class _Arguments:
"""Простой объект для передачи аргументов в тесты скелета."""
base_url = BASE
login = LOGIN
password = PASSWORD
timeout = TIMEOUT
lab = LAB
def arguments(*args, **kwargs):
return _Arguments()
def run(func):
name = getattr(func, "__name__", "test")
print(f"=== {name} ===")
try:
func()
except Exception as exc:
failures.append(f"{name} crashed: {exc}")
print("FAIL:", f"{name} crashed: {exc}")
class Failure(Exception):
"""Исключение для тестов"""
pass
class Client:
def __init__(self, base_url=BASE):
self.base_url = base_url.rstrip("/")
self.token = None
self.session = requests.Session()
self.session.headers.update({"Accept": "application/json"})
def login(self, login=LOGIN, password=PASSWORD):
r = self.session.post(
f"{self.base_url}/api/auth/login",
json={"login": login, "password": password},
timeout=TIMEOUT,
)
r.raise_for_status()
self.token = r.json()["token"]
self.session.headers["Authorization"] = f"Bearer {self.token}"
return self.token
def get(self, path, **kwargs):
kwargs.setdefault("timeout", TIMEOUT)
return self.session.get(f"{self.base_url}{path}", **kwargs)
def post(self, path, **kwargs):
kwargs.setdefault("timeout", TIMEOUT)
return self.session.post(f"{self.base_url}{path}", **kwargs)
def delete(self, path, **kwargs):
kwargs.setdefault("timeout", TIMEOUT)
return self.session.delete(f"{self.base_url}{path}", **kwargs)
def login():
response = requests.post(
BASE + "/api/auth/login",
@@ -94,4 +158,4 @@ if __name__ == "__main__":
sys.exit(main())
except requests.RequestException as exc:
print("request failed:", exc)
sys.exit(1)
sys.exit(1)
+41
View File
@@ -0,0 +1,41 @@
import io
import requests
BASE = "http://localhost:8080"
# 1. Логин
resp = requests.post(f"{BASE}/api/auth/login", json={"login": "test", "password": "test123"})
token = resp.json()["token"]
headers = {"Authorization": f"Bearer {token}"}
# 2. PNG 1x1
png_data = bytes.fromhex(
"89504e470d0a1a0a0000000d49484452000000010000000108060000001f15c4"
"890000000d49444154789c63fcffff3f000500010000ffff0300c4f0ff0068"
"98e7e70000000049454e44ae426082"
)
# 3. Тестируем разные опасные filename
dangerous_names = [
"../../../etc/passwd",
"../../secret.png",
"/etc/passwd",
"test/../evil.png",
"..\\..\\windows\\system32\\evil.png",
]
print("=== Dangerous filename test ===")
all_rejected = True
for name in dangerous_names:
files = {"file": (name, io.BytesIO(png_data), "image/png")}
r = requests.post(f"{BASE}/api/attachments", headers=headers, files=files)
rejected = r.status_code == 400
status = "PASS" if rejected else "FAIL"
print(f"{status}: '{name}' -> {r.status_code} {r.text[:80]}")
if not rejected:
all_rejected = False
if all_rejected:
print("\n✅ All dangerous filenames correctly rejected")
else:
print("\n❌ Some dangerous filenames were NOT rejected")
+74
View File
@@ -0,0 +1,74 @@
import socket
BASE_HOST = "localhost"
BASE_PORT = 8080
BOUNDARY = "----WebKitFormBoundary7MA4YWxkTrZu0gW"
# PNG 1x1 (валидное изображение)
PNG_HEX = (
"89504e470d0a1a0a0000000d49484452000000010000000108060000001f15c4"
"890000000d49444154789c63fcffff3f000500010000ffff0300c4f0ff0068"
"98e7e70000000049454e44ae426082"
)
PNG_DATA = bytes.fromhex(PNG_HEX)
# 1. Логин — получаем токен
import requests
resp = requests.post(f"http://{BASE_HOST}:{BASE_PORT}/api/auth/login",
json={"login": "test", "password": "test123"})
token = resp.json()["token"]
def raw_upload(filename):
"""Отправляем multipart вручную, без обработки filename."""
body = (
f"--{BOUNDARY}\r\n"
f'Content-Disposition: form-data; name="file"; filename="{filename}"\r\n'
f"Content-Type: image/png\r\n\r\n"
).encode() + PNG_DATA + f"\r\n--{BOUNDARY}--\r\n".encode()
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((BASE_HOST, BASE_PORT))
request = (
f"POST /api/attachments HTTP/1.1\r\n"
f"Host: {BASE_HOST}:{BASE_PORT}\r\n"
f"Authorization: Bearer {token}\r\n"
f"Content-Type: multipart/form-data; boundary={BOUNDARY}\r\n"
f"Content-Length: {len(body)}\r\n"
f"Connection: close\r\n\r\n"
).encode()
s.sendall(request + body)
response = b""
while True:
chunk = s.recv(4096)
if not chunk:
break
response += chunk
# Извлекаем статус из HTTP-ответа
status_line = response.split(b"\r\n")[0].decode()
status_code = int(status_line.split()[1])
return status_code
# 2. Тестируем
print("=== Raw HTTP dangerous filename test ===")
dangerous_names = [
"../../../etc/passwd",
"../../secret.png",
"/etc/passwd",
"test/../evil.png",
"..\\..\\windows\\system32\\evil.png",
]
all_rejected = True
for name in dangerous_names:
status = raw_upload(name)
rejected = status == 400
mark = "PASS" if rejected else "FAIL"
print(f"{mark}: '{name}' -> {status}")
if not rejected:
all_rejected = False
if all_rejected:
print("\n✅ All dangerous filenames correctly rejected")
else:
print("\n❌ Some dangerous filenames were NOT rejected")