mirror of
https://github.com/J3vb/OwnCord.git
synced 2026-09-03 03:50:00 +03:00
`Server/go.mod` declared `github.com/owncord/server` while the public repository is `github.com/J3vb/OwnCord`. Nothing resolves that path — there is no `owncord` GitHub org and no vanity-import host serving go-import metadata for it — so every import line in the tree named a location that does not exist. It compiles because a main module's own path is never fetched, which is exactly why it went unnoticed. The obvious fix — an AST-aware import rewriter (`gomvpkg`, `go mod edit`) — is wrong here, and provably so. Six of the 722 occurrences are not imports at all: `api/main_test.go:20` (a goleak `IgnoreTopFunction` pattern), `telemetry/metrics.go:17-19` (three OTel instrumentation-scope names), `invariants/syncutil_locks.go:73` (a diagnostic message), and `invariants/syncutil_locks_test.go:56` (an import line inside a raw-string Go fixture). An import rewriter touches none of them, and the compiler cannot see any of them either. Done as one scripted substitution over `git ls-files`, anchored on the full `github.com/owncord/server` string. The anchor matters: `owncord-server` is a different identifier — the OTel `service.name` (`config/config.go`, `telemetry/telemetry_otel.go`) and the GHCR image name (`.github/workflows/release.yml`, `docker-compose.yml`) — and a looser pattern would have moved it. It is untouched: 10 occurrences across 9 files, before and after. 350 files, 728 insertions, 728 deletions. 722 occurrences in 344 Go files, plus `go.mod:1`, the `sed` at `Makefile:67`, `Server/CLAUDE.md:3`, `docs/architecture/server.md:5`, and the ledger pair (`findings-ledger.json:3758` plus a `render-ledger.mjs` re-render of `FINDINGS.md`). Zero in any workflow, zero in the Dockerfile, zero in `Server/.golangci.yml` (no `local-prefixes`, `gci`, `importas` or `depguard` rule keys on the module path, so import grouping is not configured anywhere). The plan's blast-radius estimate missed one thing, and it is the one that would have gone red: **gofmt**. `J` (0x4A) sorts before every lowercase letter, so in the 36 files where a module-local import shares a contiguous group with a third-party one, the module's imports must move above `github.com/go-chi/...`. `gofmt -l` was clean before the substitution and listed exactly 36 files after it; `gofmt -w` on those 36 restores it to clean. `gofmt` is an enforced gate — the `formatters` block in `Server/.golangci.yml`, which is S-05 — so a substitution-only commit fails Lint. Verified: both directions, and the line accounting is exact. Every added line in this diff contains the new module path (728) and every removed line contains the old one (728); the count of changed lines containing neither is **zero**, so the gofmt re-sort moved module-path lines only and touched no third-party import. The residual check (`git ls-files -z | xargs -0 grep -n 'github\.com/owncord/server'`) returns exactly two hits, both deliberately out of scope: the RL-13 row in `docs/audit-2026-08-23-repository-layout.md` and the measurement row in this phase's own plan. The compiler-invisible half was proven by reverting *only* `api/main_test.go:20` to the old path on the otherwise-renamed tree: `go build ./...` and `go vet ./api/` both still pass — they see nothing wrong — while `go test ./api/` FAILS, because the runtime function name now carries the new path and goleak stops ignoring `ws.(*Hub).Run.func1`. Restoring the line makes it pass. `go.sum` is byte-identical (no `go mod tidy` was run and none was needed). All four build-tag variants compile; `go vet ./...`, `go vet -tags otel,wazero ./...` and `go vet -tags deadlock ./...` pass; `go test -race ./...` is 16/16 packages green; `go test -tags deadlock ./...` passes; the tag-gated `./plugin/...` (wazero) and `./telemetry/...` (otel) runs pass. `golangci-lint` v2.11.3 — the pinned CI version, rebuilt locally against Go 1.26 because the packaged binary cannot load a 1.26 config — reports **0 issues**. `go run ./cmd/genprotocol` leaves `git diff --exit-code ws/message_types.go ../Client/src/lib/protocolTypes.ts` clean, so the rename does not reach the generated protocol constants. `npx prettier --check .` and `node .superpowers/render-ledger.mjs --check` pass. Not included: `docs/audit-2026-08-23-repository-layout.md` and `docs/plans/b1-repository-foundation-2026-08-25.md` keep the old path — they are the audit row and the measurement that motivated this change, and rewriting them would erase the record of what was measured. They are why the residual check needs a two-path allowance rather than being empty; that allowance is stated above rather than hidden in a pathspec. `telemetry/metrics.go:19` declares `scopeVoice` for a `Server/voice` package that does not exist; the substitution carried the dead path forward verbatim as `github.com/J3vb/OwnCord/Server/voice` rather than fixing it, because correcting a real observability bug inside a mechanical rename would hide it in a 350-file diff. It needs its own item. No `go.work`, no second module, and no vanity-import host was set up — the new path resolves against the real repository, but nothing imports this module as a library, so `go get` reachability was not exercised either way. Refs RL-13, L-12
235 lines
7.9 KiB
Go
235 lines
7.9 KiB
Go
// gif_handler.go — server-side proxy for the Klipy GIF API.
|
|
//
|
|
// The Klipy API key lives in server config and never leaves the server: the
|
|
// client asks its own server for GIFs and the server does the upstream call.
|
|
// This closes the "secret in the client bundle" hole — a VITE_ variable is
|
|
// inlined into the shipped bundle by design and can never hold a credential.
|
|
//
|
|
// Default-off contract: with no gif.api_key configured both endpoints answer
|
|
// 503 with error code GIF_DISABLED, which the client uses to hide/disable the
|
|
// GIF picker instead of showing a broken one.
|
|
|
|
package api
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"io"
|
|
"log/slog"
|
|
"net/http"
|
|
"net/url"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/J3vb/OwnCord/Server/auth"
|
|
"github.com/J3vb/OwnCord/Server/config"
|
|
"github.com/J3vb/OwnCord/Server/db"
|
|
"github.com/J3vb/OwnCord/Server/plugin"
|
|
"github.com/go-chi/chi/v5"
|
|
)
|
|
|
|
// gifAPIBase is the upstream Klipy API root. It is a var only so tests can
|
|
// point it at a local stub; production never reassigns it.
|
|
var gifAPIBase = "https://api.klipy.com/v2"
|
|
|
|
const (
|
|
// gifDefaultLimit / gifMaxLimit bound the number of results requested.
|
|
gifDefaultLimit = 20
|
|
gifMaxLimit = 50
|
|
|
|
// gifMaxQueryLen caps the search term length before it is forwarded.
|
|
gifMaxQueryLen = 100
|
|
|
|
// gifUpstreamTimeout is the total budget for one upstream call.
|
|
gifUpstreamTimeout = 10 * time.Second
|
|
|
|
// gifMaxResponseBytes caps the upstream body we are willing to read so a
|
|
// hostile or oversized response cannot exhaust server memory.
|
|
gifMaxResponseBytes = 2 << 20 // 2 MiB
|
|
)
|
|
|
|
// gifClient performs the upstream call. It reuses the same SSRF-guarded dialer
|
|
// as the plugin host_http capability (resolve once, reject private/loopback/
|
|
// link-local/CGN addresses, dial only vetted IPs) rather than a bare
|
|
// http.Get, and refuses to follow redirects — the upstream host is fixed.
|
|
var gifClient = &http.Client{
|
|
Timeout: gifUpstreamTimeout,
|
|
Transport: &http.Transport{DialContext: plugin.GuardedDialContext()},
|
|
CheckRedirect: func(_ *http.Request, _ []*http.Request) error {
|
|
return http.ErrUseLastResponse
|
|
},
|
|
}
|
|
|
|
// gifMediaFormat is a single renderable variant of a GIF.
|
|
type gifMediaFormat struct {
|
|
URL string `json:"url"`
|
|
}
|
|
|
|
// gifResult is one GIF. Decoding the upstream body into this struct and
|
|
// re-encoding it IS the field allowlist: anything Klipy returns that is not
|
|
// declared here (including any echo of our API key) is dropped on the floor
|
|
// and never reaches the client.
|
|
type gifResult struct {
|
|
ID string `json:"id"`
|
|
Title string `json:"title"`
|
|
MediaFormats struct {
|
|
TinyGif *gifMediaFormat `json:"tinygif,omitempty"`
|
|
Gif *gifMediaFormat `json:"gif,omitempty"`
|
|
} `json:"media_formats"`
|
|
}
|
|
|
|
// gifResponse is the JSON envelope returned by both GIF endpoints.
|
|
type gifResponse struct {
|
|
Results []gifResult `json:"results"`
|
|
}
|
|
|
|
// MountGIFRoutes registers the authenticated GIF proxy endpoints.
|
|
//
|
|
// Both routes require a session (same as sibling content endpoints) and share
|
|
// a dedicated per-IP rate-limit bucket — the picker searches on every debounced
|
|
// keystroke, so it must not share the empty-prefix bucket used by password and
|
|
// TOTP endpoints.
|
|
func MountGIFRoutes(r chi.Router, database *db.DB, limiter *auth.RateLimiter, cfg *config.Config) {
|
|
r.Route("/api/v1/gif", func(r chi.Router) {
|
|
r.Use(AuthMiddleware(database))
|
|
r.Use(rateLimitMiddlewareWithPrefix(limiter, "gif:", gifRateLimitPerMinute, time.Minute, cfg.Server.TrustedProxies))
|
|
|
|
r.Get("/search", handleGIFProxy(cfg.GIF.APIKey, "/search", true))
|
|
r.Get("/trending", handleGIFProxy(cfg.GIF.APIKey, "/featured", false))
|
|
})
|
|
}
|
|
|
|
// handleGIFProxy returns a handler that forwards a GIF request upstream with
|
|
// the server-held API key. requireQuery marks the endpoints that take a `q`
|
|
// search term (search) versus those that do not (trending).
|
|
func handleGIFProxy(apiKey, upstreamPath string, requireQuery bool) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
if apiKey == "" {
|
|
writeJSON(w, http.StatusServiceUnavailable, errorResponse{
|
|
Error: "GIF_DISABLED",
|
|
Message: "GIF search is not configured on this server",
|
|
})
|
|
return
|
|
}
|
|
|
|
params := url.Values{
|
|
"key": {apiKey},
|
|
"media_filter": {"gif,tinygif"},
|
|
}
|
|
|
|
limit, ok := parseGIFLimit(r.URL.Query().Get("limit"))
|
|
if !ok {
|
|
writeJSON(w, http.StatusBadRequest, errorResponse{
|
|
Error: "INVALID_INPUT",
|
|
Message: "limit must be an integer between 1 and " + strconv.Itoa(gifMaxLimit),
|
|
})
|
|
return
|
|
}
|
|
params.Set("limit", strconv.Itoa(limit))
|
|
|
|
if requireQuery {
|
|
q := strings.TrimSpace(r.URL.Query().Get("q"))
|
|
if q == "" {
|
|
writeJSON(w, http.StatusBadRequest, errorResponse{
|
|
Error: "INVALID_INPUT",
|
|
Message: "q is required",
|
|
})
|
|
return
|
|
}
|
|
if len(q) > gifMaxQueryLen {
|
|
writeJSON(w, http.StatusBadRequest, errorResponse{
|
|
Error: "INVALID_INPUT",
|
|
Message: "q must be at most " + strconv.Itoa(gifMaxQueryLen) + " characters",
|
|
})
|
|
return
|
|
}
|
|
params.Set("q", q)
|
|
}
|
|
|
|
results, err := fetchGIFs(r, gifAPIBase+upstreamPath+"?"+params.Encode(), apiKey, limit)
|
|
if err != nil {
|
|
writeJSON(w, http.StatusBadGateway, errorResponse{
|
|
Error: "BAD_GATEWAY",
|
|
Message: "GIF provider is unavailable",
|
|
})
|
|
return
|
|
}
|
|
|
|
writeJSON(w, http.StatusOK, gifResponse{Results: results})
|
|
}
|
|
}
|
|
|
|
// fetchGIFs performs the upstream request and returns the allowlisted results.
|
|
// It never returns the upstream error to the caller and never logs the request
|
|
// URL, because that URL carries the API key.
|
|
func fetchGIFs(r *http.Request, upstreamURL, apiKey string, limit int) ([]gifResult, error) {
|
|
req, err := http.NewRequestWithContext(r.Context(), http.MethodGet, upstreamURL, nil)
|
|
if err != nil {
|
|
slog.Warn("gif proxy: building upstream request failed", "error", redactKey(err.Error(), apiKey))
|
|
return nil, err
|
|
}
|
|
|
|
resp, err := gifClient.Do(req)
|
|
if err != nil {
|
|
// url.Error embeds the request URL, which contains the API key.
|
|
slog.Warn("gif proxy: upstream request failed", "error", redactKey(err.Error(), apiKey))
|
|
return nil, err
|
|
}
|
|
defer func() { _ = resp.Body.Close() }()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
slog.Warn("gif proxy: upstream returned non-200", "status", resp.StatusCode)
|
|
return nil, errGIFUpstream
|
|
}
|
|
|
|
var upstream gifResponse
|
|
if err := json.NewDecoder(io.LimitReader(resp.Body, gifMaxResponseBytes)).Decode(&upstream); err != nil {
|
|
slog.Warn("gif proxy: decoding upstream response failed", "error", redactKey(err.Error(), apiKey))
|
|
return nil, err
|
|
}
|
|
|
|
// Drop entries missing either renderable format and honour our own limit
|
|
// even if upstream ignored it. Non-nil so the JSON is [] and never null.
|
|
results := make([]gifResult, 0, len(upstream.Results))
|
|
for _, g := range upstream.Results {
|
|
if g.MediaFormats.TinyGif == nil || g.MediaFormats.Gif == nil {
|
|
continue
|
|
}
|
|
if len(results) >= limit {
|
|
break
|
|
}
|
|
results = append(results, g)
|
|
}
|
|
return results, nil
|
|
}
|
|
|
|
// errGIFUpstream marks a non-200 upstream response.
|
|
var errGIFUpstream = errors.New("gif proxy: upstream error")
|
|
|
|
// redactKey removes the API key from a string destined for the logs. It
|
|
// matches both the literal key and its percent-encoded query-string form
|
|
// (url.Error embeds the encoded request URL, and params.Encode() escapes any
|
|
// character outside [A-Za-z0-9-_.~] — common in base64-style keys) so an
|
|
// encoded form is caught too.
|
|
func redactKey(s, apiKey string) string {
|
|
if apiKey == "" {
|
|
return s
|
|
}
|
|
s = strings.ReplaceAll(s, apiKey, "[REDACTED]")
|
|
return strings.ReplaceAll(s, url.QueryEscape(apiKey), "[REDACTED]")
|
|
}
|
|
|
|
// parseGIFLimit parses and validates the `limit` query param. An empty value
|
|
// yields the default; anything non-numeric or out of range is rejected.
|
|
func parseGIFLimit(raw string) (int, bool) {
|
|
if raw == "" {
|
|
return gifDefaultLimit, true
|
|
}
|
|
n, err := strconv.Atoi(raw)
|
|
if err != nil || n < 1 || n > gifMaxLimit {
|
|
return 0, false
|
|
}
|
|
return n, true
|
|
}
|