Files
llmgw/internal/web/landing.go
T
Ilker ManapandClaude Opus 5 8d35faf6f8
CI / test (push) Successful in 56s
Give the landing page somewhere to send people
The page explained the API, said keys come from an operator, and left the
reader there. Someone who reaches the address without a key had no route to
getting one, which is where onboarding stopped.

`landing.contact` is that route, because the gateway cannot guess who to ask.
An address becomes a mailto link, an http(s) URL stays a link, and anything
else — a channel, a queue, a person — is shown as text. Other schemes are
never linked: this string comes from configuration, configuration is not
always written by whoever runs the gateway, and a javascript: href on a page
served to every visitor is a short path to a stored XSS. Escaping the text
does not make a scheme safe.

Unconfigured, it now says something rather than nothing, and the page explains
that a key arrives as a one-time link so the reader knows what to expect
before it lands.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-16 09:22:42 +02:00

85 lines
2.4 KiB
Go

package web
import (
"net/http"
"strings"
"time"
)
// LandingData is what the landing page shows. It is deliberately almost
// nothing: this page is served without a key, to anyone who can reach the
// address.
type LandingData struct {
BaseURL string
AuthEnabled bool
Contact string
ContactHref string
Notice string
Version string
Now string
}
// Landing serves the page at "/" on the client-facing port.
//
// Without it that path is a 404, which is what someone sees when they paste the
// address into a browser — an endpoint that looks broken rather than one that
// tells them how to use it.
//
// It lists no models and no cluster state. Everything that says what is here
// stays behind the key, including /v1/models.
func Landing(version string, authEnabled bool, contact, notice string) http.HandlerFunc {
text, href := contactLink(contact)
return func(w http.ResponseWriter, r *http.Request) {
// Only the root: a catch-all would turn every mistyped path into this
// page, hiding genuine 404s from anyone debugging a client.
if r.URL.Path != "/" {
http.NotFound(w, r)
return
}
render(w, "landing", LandingData{
BaseURL: baseURL(r),
AuthEnabled: authEnabled,
Contact: text,
ContactHref: href,
Notice: notice,
Version: version,
Now: time.Now().UTC().Format(time.RFC3339),
})
}
}
// baseURL reconstructs the address the client actually used.
//
// Behind a reverse proxy the scheme and host this process sees are the internal
// ones, so the forwarded headers are what make the copy-pasteable example on
// the page correct. They are attacker-controlled when nothing sets them, which
// is why they are only reflected into a displayed URL and never used for a
// decision.
func baseURL(r *http.Request) string {
scheme := "http"
switch {
case r.TLS != nil:
scheme = "https"
case r.Header.Get("X-Forwarded-Proto") != "":
scheme = firstValue(r.Header.Get("X-Forwarded-Proto"))
}
host := r.Host
if fwd := r.Header.Get("X-Forwarded-Host"); fwd != "" {
host = firstValue(fwd)
}
if host == "" {
host = "localhost:8080"
}
return scheme + "://" + host
}
// firstValue takes the first entry of a comma-separated forwarded header: a
// chain of proxies appends rather than replaces.
func firstValue(v string) string {
if i := strings.IndexByte(v, ','); i >= 0 {
v = v[:i]
}
return strings.TrimSpace(v)
}