fix: validate dimensions and fit mode on encrypted URLs (closes #62) #126
@@ -0,0 +1,163 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"maps"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"sneak.berlin/go/pixa/internal/encurl"
|
||||
"sneak.berlin/go/pixa/internal/imgcache"
|
||||
)
|
||||
|
||||
// Generator form field names, and a value that is not a number.
|
||||
const (
|
||||
widthField = "width"
|
||||
heightField = "height"
|
||||
qualityField = "quality"
|
||||
ttlField = "ttl"
|
||||
fitField = "fit"
|
||||
notANumber = "abc"
|
||||
)
|
||||
|
||||
// generatePost submits the /generate form with a valid session and CSRF token
|
||||
// plus the caller's extra fields, returning the recorder.
|
||||
func generatePost(
|
||||
t *testing.T, extra url.Values,
|
||||
) *httptest.ResponseRecorder {
|
||||
t.Helper()
|
||||
|
||||
h, srv := newCSRFTestRouter(t)
|
||||
|
||||
sessionCookie := newSessionCookie(t, h)
|
||||
cookies, token := csrfCredentials(t, srv, []*http.Cookie{sessionCookie})
|
||||
cookies = append(cookies, sessionCookie)
|
||||
|
||||
form := url.Values{
|
||||
sourceURLField: {testSourceURL},
|
||||
csrfTokenField: {token},
|
||||
}
|
||||
maps.Copy(form, extra)
|
||||
|
||||
return postForm(srv, "/generate", cookies, form)
|
||||
}
|
||||
|
||||
// TestGeneratePostRejectsNonNumericWidth verifies that a non-numeric width is
|
||||
// rejected with 400 naming the field rather than being coerced to 0 and
|
||||
// minting a 0-width token.
|
||||
func TestGeneratePostRejectsNonNumericWidth(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
rec := generatePost(t, url.Values{"width": {"abc"}})
|
||||
|
||||
if rec.Code != http.StatusBadRequest {
|
||||
t.Fatalf("status = %d, want %d", rec.Code, http.StatusBadRequest)
|
||||
}
|
||||
|
||||
if strings.Contains(rec.Body.String(), "/v1/e/") {
|
||||
t.Error("a token was generated for non-numeric width")
|
||||
}
|
||||
}
|
||||
|
||||
// TestGeneratePostRejectsOverLimitWidth verifies that a width beyond
|
||||
// MaxDimension is rejected at generation time so an unusable token cannot be
|
||||
// minted.
|
||||
func TestGeneratePostRejectsOverLimitWidth(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
rec := generatePost(t, url.Values{"width": {"100000"}})
|
||||
|
||||
if rec.Code != http.StatusBadRequest {
|
||||
t.Fatalf("status = %d, want %d", rec.Code, http.StatusBadRequest)
|
||||
}
|
||||
|
||||
if strings.Contains(rec.Body.String(), "/v1/e/") {
|
||||
t.Error("a token was generated for an over-limit width")
|
||||
}
|
||||
}
|
||||
|
||||
// TestGeneratePostRejectsBadField verifies that each generator field whose
|
||||
// value is not a number, out of range, or unrecognized is rejected with 400,
|
||||
// mints no token, and is named in the error shown on the page.
|
||||
func TestGeneratePostRejectsBadField(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tests := []struct {
|
||||
field, value, wantError string
|
||||
}{
|
||||
{widthField, notANumber, "invalid width: not a number"},
|
||||
{widthField, "-1", "width is negative"},
|
||||
{widthField, "8193", "width is above 8192"},
|
||||
{heightField, notANumber, "invalid height: not a number"},
|
||||
{heightField, "8193", "height is above 8192"},
|
||||
{qualityField, notANumber, "invalid quality: not a number"},
|
||||
{qualityField, "0", "invalid quality: must be from 1 to 100"},
|
||||
{qualityField, "101", "invalid quality: must be from 1 to 100"},
|
||||
{ttlField, notANumber, "invalid ttl: not a number"},
|
||||
{ttlField, "-1", "invalid ttl: must be from 0 to"},
|
||||
{ttlField, "10000000000", "invalid ttl: must be from 0 to"},
|
||||
{fitField, "bogus", "invalid fit mode: bogus"},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.field+"="+tt.value, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
rec := generatePost(t, url.Values{tt.field: {tt.value}})
|
||||
body := rec.Body.String()
|
||||
|
||||
if rec.Code != http.StatusBadRequest {
|
||||
t.Fatalf("status = %d, want %d", rec.Code, http.StatusBadRequest)
|
||||
}
|
||||
|
||||
if strings.Contains(body, "/v1/e/") {
|
||||
t.Error("a token was generated")
|
||||
}
|
||||
|
||||
if !strings.Contains(body, tt.wantError) {
|
||||
t.Errorf("page does not show %q", tt.wantError)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestBuildGeneratePayloadDefaultAndLimits verifies that an empty quality
|
||||
// takes the default, and that the largest accepted width, height, quality and
|
||||
// ttl are accepted with an expiry still in the future.
|
||||
func TestBuildGeneratePayloadDefaultAndLimits(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
parsed, err := url.Parse(testSourceURL)
|
||||
if err != nil {
|
||||
t.Fatalf("url.Parse() error = %v", err)
|
||||
}
|
||||
|
||||
payload, _, _, err := buildGeneratePayload(parsed, url.Values{})
|
||||
if err != nil {
|
||||
t.Fatalf("empty form: error = %v", err)
|
||||
}
|
||||
|
||||
if payload.Quality != encurl.DefaultQuality {
|
||||
t.Errorf("empty quality gave %d, want %d",
|
||||
payload.Quality, encurl.DefaultQuality)
|
||||
}
|
||||
|
||||
_, expiresAt, _, err := buildGeneratePayload(parsed, url.Values{
|
||||
widthField: {strconv.Itoa(imgcache.MaxDimension)},
|
||||
heightField: {strconv.Itoa(imgcache.MaxDimension)},
|
||||
qualityField: {strconv.Itoa(maxQuality)},
|
||||
ttlField: {strconv.Itoa(maxTTL)},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("largest accepted values: error = %v", err)
|
||||
}
|
||||
|
||||
if !expiresAt.After(time.Now()) {
|
||||
t.Errorf("ttl %d gave expiry %v, want a time in the future",
|
||||
maxTTL, expiresAt)
|
||||
}
|
||||
}
|
||||
@@ -276,3 +276,18 @@ func TestHandleImage_ETagHeader(t *testing.T) {
|
||||
t.Errorf("ETag should be quoted, got %q", etag)
|
||||
}
|
||||
}
|
||||
|
||||
// TestHandleImage_InvalidFitMode_Returns400 verifies that the plain image
|
||||
// route rejects an unrecognized fit mode with 400.
|
||||
func TestHandleImage_InvalidFitMode_Returns400(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
fix := setupTestHandler(t)
|
||||
|
||||
status := getImage(t, fix,
|
||||
"/v1/image/"+fix.goodHost+"/images/photo.jpg/50x50.jpeg?fit=bogus")
|
||||
|
||||
if status != http.StatusBadRequest {
|
||||
t.Fatalf("status = %d, want %d", status, http.StatusBadRequest)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
|
||||
"sneak.berlin/go/pixa/internal/encurl"
|
||||
"sneak.berlin/go/pixa/internal/imgcache"
|
||||
)
|
||||
|
||||
// newEncTestServer builds a router serving the encrypted-URL route with a
|
||||
// generator seeded by the shared test signing key. The image service is left
|
||||
// nil: these tests exercise validation that rejects a token before any image
|
||||
// is fetched, so the handler must never reach the service.
|
||||
func newEncTestServer(t *testing.T) (*encurl.Generator, http.Handler) {
|
||||
t.Helper()
|
||||
|
||||
encGen, err := encurl.NewGenerator(testSigningKey)
|
||||
if err != nil {
|
||||
t.Fatalf("encurl.NewGenerator() error = %v", err)
|
||||
}
|
||||
|
||||
h := &Handlers{
|
||||
log: slog.New(slog.DiscardHandler),
|
||||
encGen: encGen,
|
||||
}
|
||||
|
||||
r := chi.NewRouter()
|
||||
r.Get("/v1/e/{token}/*", h.HandleImageEnc())
|
||||
|
||||
return encGen, r
|
||||
}
|
||||
|
||||
// getEncToken issues a GET for the given token and returns the recorder.
|
||||
func getEncToken(srv http.Handler, token string) *httptest.ResponseRecorder {
|
||||
req := httptest.NewRequestWithContext(
|
||||
context.Background(), http.MethodGet, "/v1/e/"+token+"/img.jpg", nil)
|
||||
rec := httptest.NewRecorder()
|
||||
srv.ServeHTTP(rec, req)
|
||||
|
||||
return rec
|
||||
}
|
||||
|
||||
// TestHandleImageEnc_OverLimitDimension_Returns400 verifies that a decrypted
|
||||
// token requesting a dimension beyond MaxDimension is rejected with 400
|
||||
// instead of reaching the image processor and libvips.
|
||||
func TestHandleImageEnc_OverLimitDimension_Returns400(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
encGen, srv := newEncTestServer(t)
|
||||
|
||||
token, err := encGen.Generate(&encurl.Payload{
|
||||
SourceHost: "cdn.example.com",
|
||||
SourcePath: "/photo.jpg",
|
||||
Width: 100000,
|
||||
Height: 100000,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Generate() error = %v", err)
|
||||
}
|
||||
|
||||
rec := getEncToken(srv, token)
|
||||
|
||||
if rec.Code != http.StatusBadRequest {
|
||||
t.Fatalf("status = %d, want %d", rec.Code, http.StatusBadRequest)
|
||||
}
|
||||
}
|
||||
|
||||
// TestHandleImageEnc_InvalidFitMode_Returns400 verifies that a decrypted token
|
||||
// carrying an unrecognized fit mode is rejected with 400 rather than surfacing
|
||||
// as a 500 from the image processor's default branch.
|
||||
func TestHandleImageEnc_InvalidFitMode_Returns400(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
encGen, srv := newEncTestServer(t)
|
||||
|
||||
token, err := encGen.Generate(&encurl.Payload{
|
||||
SourceHost: "cdn.example.com",
|
||||
SourcePath: "/photo.jpg",
|
||||
Width: 800,
|
||||
Height: 600,
|
||||
FitMode: imgcache.FitMode("bogus"),
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Generate() error = %v", err)
|
||||
}
|
||||
|
||||
rec := getEncToken(srv, token)
|
||||
|
||||
if rec.Code != http.StatusBadRequest {
|
||||
t.Fatalf("status = %d, want %d", rec.Code, http.StatusBadRequest)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package imgcache
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestValidateImageRequest(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
req ImageRequest
|
||||
wantErr error
|
||||
}{
|
||||
{
|
||||
name: "within bounds",
|
||||
req: ImageRequest{Size: Size{Width: 800, Height: 600}, FitMode: FitCover},
|
||||
},
|
||||
{
|
||||
name: "original size and empty fit",
|
||||
req: ImageRequest{Size: Size{Width: 0, Height: 0}},
|
||||
},
|
||||
{
|
||||
name: "width over limit",
|
||||
req: ImageRequest{Size: Size{Width: MaxDimension + 1, Height: 600}},
|
||||
wantErr: ErrDimensionTooLarge,
|
||||
},
|
||||
{
|
||||
name: "height over limit",
|
||||
req: ImageRequest{Size: Size{Width: 800, Height: MaxDimension + 1}},
|
||||
wantErr: ErrDimensionTooLarge,
|
||||
},
|
||||
{
|
||||
name: "negative width",
|
||||
req: ImageRequest{Size: Size{Width: -1, Height: 600}},
|
||||
wantErr: ErrInvalidSize,
|
||||
},
|
||||
{
|
||||
name: "invalid fit mode",
|
||||
req: ImageRequest{Size: Size{Width: 800, Height: 600}, FitMode: "bogus"},
|
||||
wantErr: ErrInvalidFitMode,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
err := ValidateImageRequest(&tt.req)
|
||||
if tt.wantErr == nil {
|
||||
if err != nil {
|
||||
t.Fatalf("ValidateImageRequest() error = %v, want nil", err)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if !errors.Is(err, tt.wantErr) {
|
||||
t.Fatalf("ValidateImageRequest() error = %v, want %v", err, tt.wantErr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user