diff --git a/README.md b/README.md index a8a9242..3ab10a7 100644 --- a/README.md +++ b/README.md @@ -473,9 +473,11 @@ nor change what it asks for. `jxl` for a form with no format. The token holds the source's host, path and query and the size, format, quality, -fit and expiry, encrypted with a key derived from `signing_key`. The source -URL's scheme is not kept: the image is fetched like any other (see Routes), and -the blocked networks still apply. +fit and expiry, encrypted with a key derived from `signing_key`. The host is +kept in lower case and an empty path as `/`, as for `/v1/image/`, so +`https://CDN.Example.com` and `https://cdn.example.com/` are one source image, +fetched and cached once. The source URL's scheme is not kept: the image is +fetched like any other (see Routes), and the blocked networks still apply. How long the URL lasts is chosen on the page, from 1 minute to 1 year, or never. The expiry is fixed in the token when the URL is made and cannot be changed or diff --git a/TODO.md b/TODO.md index 4c56de7..3817c02 100644 --- a/TODO.md +++ b/TODO.md @@ -30,6 +30,11 @@ P2: security: per-origin rate limiting # Completed Steps +- 2026-10-16 the generator page lower-cases the source host and writes an empty + source path as `/` before it makes the encrypted URL, as `/v1/image/` does + (closes #293). A host written in upper and in lower case, or a source URL with + no path and with the path `/`, made two cache keys and two upstream requests + for one image. - 2026-10-16 the source URL a request fetches is built with `net/url` from the source host, path and query, not joined as text (closes #288). The source path, decoded, is fetched percent-encoded again, so `100%25.png` fetches diff --git a/internal/handlers/auth.go b/internal/handlers/auth.go index d51b569..27105f2 100644 --- a/internal/handlers/auth.go +++ b/internal/handlers/auth.go @@ -127,7 +127,7 @@ func (s *Handlers) HandleGenerateURL() http.HandlerFunc { sourceURL := r.FormValue("url") // Validate source URL - parsed, err := url.Parse(sourceURL) + parsed, err := parseSourceURL(sourceURL) if err != nil || parsed.Host == "" { s.renderGeneratorWithForm(w, r, "Invalid source URL", r.Form, http.StatusBadRequest) @@ -198,6 +198,25 @@ func (s *Handlers) HandleGenerateURL() http.HandlerFunc { } } +// parseSourceURL parses the source URL entered on the generator page with its +// host lower-cased and an empty path written as "/", as /v1/image/ reads a +// source URL, so that each spelling of one source URL makes one cache key and +// one upstream request. +func parseSourceURL(sourceURL string) (*url.URL, error) { + parsed, err := url.Parse(sourceURL) + if err != nil { + return nil, err + } + + parsed.Host = strings.ToLower(parsed.Host) + + if parsed.Path == "" { + parsed.Path = "/" + } + + return parsed, nil +} + // fetchedAsWritten reports whether fetchURL, the URL pixa fetches for an // image URL made from the source URL source, is source as it was written, // apart from the scheme, which the token does not keep. It is not when source