Security performance improvements #1

Merged
marvin merged 6 commits from security-performance-improvements into main 2026-10-22 12:08:53 +00:00
Owner
No description provided.
tchivert added 3 commits 2026-10-22 11:21:55 +00:00
Security:
- Add HTTP server timeouts (ReadTimeout, WriteTimeout, IdleTimeout)
- Add http.MaxBytesReader to limit POST body size before parsing
- Fix file permissions to 0600 for paste files
- Add TCP connection limit (--max-conns, default 100)
- Fix purge localhost check using net.SplitHostPort (was broken for IPv6)
- Add rate limiting with golang.org/x/time/rate (--rate-limit)
- Fix concurrent purge race with sync.Mutex + atomic.Bool
- Docker: run as non-root user, pin base images

Performance:
- Add Cache-Control headers for static assets (index.html, favicon)
- Add graceful shutdown on SIGTERM/SIGINT
- Use filepath.Join instead of string concatenation for paths

Observability:
- Add structured logging with log/slog (replaces log.Printf)
- Add Prometheus metrics endpoint (--metrics-addr)
- Add /health endpoint for health checks
- Metrics: pastes, bytes, HTTP requests, TCP connections, purges, removes

UX:
- Fix hardcoded HTTP link to HTTPS in index.html
- Fix mobile textarea min-height (560px -> 40vh, with media queries)
- Add responsive styles for small screens (max-width: 600px)

Code quality:
- Fix double strings.Split in handleRemove
- Update Go version to 1.22
- Update Dockerfile: golang:1.22-alpine, alpine:3.19
- Add go.sum with pinned dependencies
- Update README with new flags and security/monitoring docs
- go.mod: go 1.22 -> go 1.27
- Dockerfile: golang:1.22-alpine -> golang:1.27-alpine
- Dockerfile: alpine:3.19 -> alpine:3.24
- README: update Go version requirement
tchivert added 1 commit 2026-10-22 11:38:19 +00:00
Address review feedback: fix blockers and non-blocking issues
build / test (push) Failing after 1s
build / build (push) Skipped
build / test (pull_request) Failing after 1s
build / build (pull_request) Skipped
7d25ef3804
Blockers fixed:
- Docker: create /app/data with tbin ownership, use named volume in docker-compose
  (was: bind mount broke non-root user, MkdirAll failed on startup)
- Remove /metrics from main HTTP mux (was: exposed publicly even when --metrics-addr empty)
- Implement per-IP rate limiting with ipLimiter (map + cleanup goroutine)
  (was: single global limiter, docs said per-IP but wasn't)
- Fix Prometheus label cardinality: use fixed endpoint labels instead of r.URL.Path
  (was: unbounded memory growth from unique paths as labels)

Non-blocking fixes:
- Remove redundant purgeRunning atomic.Bool (keep only purgeMu.TryLock)
- Remove dead code: getClientIP (replaced by getRemoteIP), unreachable health case
  in handleHTTP, _ = host in TCP limiter
- Fix MaxBytesReader/413: use r.ParseForm() to detect body-too-large errors properly
- Track TCP handlers and async operations with sync.WaitGroup for graceful shutdown
- CI: add test job with go build, vet, gofmt check, go test before Docker build
- CI: bump actions/checkout to v4, add PR trigger
- docker-compose: use named volume tbin-data instead of bind mount
- Dockerfile: pin tbin user to UID 1000, add VOLUME /app/data
- README: document non-root user, named volume, per-IP rate limiting

All checks pass: go build, go vet, gofmt -l
tchivert added 1 commit 2026-10-22 11:58:16 +00:00
Fix CI test job and remaining review nits
build / test (push) Successful in 54s
build / test (pull_request) Successful in 7s
build / build (push) Successful in 1m57s
build / build (pull_request) Failing after 3m13s
c400569181
CI (blocker):
- Replace container: golang:1.27-alpine with actions/setup-go@v5
  (runner couldn't pull from Docker Hub, job failed in ~1s)
- Use go-version-file: go.mod to auto-sync Go version

Code nits:
- Return proper 413 for oversized bodies using errors.As with http.MaxBytesError
  (was: always 400 'Invalid form data' when MaxBytesReader triggered)
- Apply same 413 handling to /remove endpoint
- Add maxIPLimiterEntries=10000 cap to ipLimiter.get()
  Evicts idle entries when full, returns zero-rate limiter if still full
  (was: unbounded map growth between 10-min cleanup intervals)

All checks pass: go build, go vet, gofmt -l clean
tchivert added 1 commit 2026-10-22 12:05:42 +00:00
Split CI into separate test and build workflows
test / test (pull_request) Successful in 6s
build / build (push) Successful in 1m58s
test / test (push) Successful in 6s
52914207bf
- test.yml: runs on any push or PR (branches: main)
  - go build, go vet, gofmt check, go test
  - Fast feedback on PRs without pushing images

- build.yml: runs only on push to main
  - Builds and pushes Docker image to registry
  - No longer triggered by PRs or feature branches
  - Removes 'needs: test' dependency (separate workflows now)

This ensures the :latest Docker image is only updated after
code is merged to main, not from unvalidated PR branches.
marvin merged commit 52914207bf into main 2026-10-22 12:08:53 +00:00
marvin deleted branch security-performance-improvements 2026-10-22 12:08:53 +00:00
Sign in to join this conversation.
No Reviewers
No labels
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: tchivert/tbin#1