The new package internal/imagerequest holds the request type, its two parts Source and Transform, the Format and Fit types, the dimension and fit checks, the /v1/image/ path parsing and the variant key; it imports no other package of pixa. imgcache.ParsedURL, imgcache.ImageRequest, signature.Request, imageprocessor.Request, the second Size, format and fit types and the conversions between them are gone. The image processor takes a Transform only. encurl keeps a private struct with the token's CBOR field names, so tokens made before still decode. VariantKey moves with the function that makes it. Tests follow the moved and renamed code; the tests of the removed conversions go with them. Model: opus-5-5
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"sneak.berlin/go/pixa/internal/imagerequest"
|
||||
"sneak.berlin/go/pixa/internal/signature"
|
||||
)
|
||||
|
||||
@@ -16,32 +17,35 @@ func TestSigner_Verify_ColonCannotMovePathQueryBoundary(t *testing.T) {
|
||||
|
||||
signer := signature.New(goldenSigningKey)
|
||||
|
||||
signed := &signature.Request{
|
||||
SourceHost: testHost,
|
||||
SourcePath: "/a:b",
|
||||
Width: 800,
|
||||
Height: 600,
|
||||
Format: testFormatWebP,
|
||||
Quality: 85,
|
||||
FitMode: testFitCover,
|
||||
Expires: time.Now().Add(1 * time.Hour),
|
||||
signed := &imagerequest.Request{
|
||||
Source: imagerequest.Source{
|
||||
Host: testHost,
|
||||
Path: "/a:b",
|
||||
},
|
||||
Transform: imagerequest.Transform{
|
||||
Size: imagerequest.Size{Width: 800, Height: 600},
|
||||
Format: testFormatWebP,
|
||||
Quality: 85,
|
||||
FitMode: testFitCover,
|
||||
},
|
||||
Expires: time.Now().Add(1 * time.Hour),
|
||||
}
|
||||
signed.Signature = signer.Sign(signed)
|
||||
|
||||
replayed := *signed
|
||||
replayed.SourcePath = "/a"
|
||||
replayed.SourceQuery = "b:"
|
||||
replayed.Source.Path = "/a"
|
||||
replayed.Source.Query = "b:"
|
||||
|
||||
t.Logf("signature for path %q query %q: %s",
|
||||
signed.SourcePath, signed.SourceQuery, signed.Signature)
|
||||
signed.Source.Path, signed.Source.Query, signed.Signature)
|
||||
t.Logf("signature for path %q query %q: %s",
|
||||
replayed.SourcePath, replayed.SourceQuery, signer.Sign(&replayed))
|
||||
replayed.Source.Path, replayed.Source.Query, signer.Sign(&replayed))
|
||||
|
||||
err := signer.Verify(&replayed)
|
||||
if !errors.Is(err, signature.ErrInvalid) {
|
||||
t.Errorf("Verify() of path %q query %q with the signature for "+
|
||||
"path %q = %v, want %v",
|
||||
replayed.SourcePath, replayed.SourceQuery, signed.SourcePath,
|
||||
replayed.Source.Path, replayed.Source.Query, signed.Source.Path,
|
||||
err, signature.ErrInvalid)
|
||||
}
|
||||
}
|
||||
@@ -90,16 +94,19 @@ func TestSigner_EscapedGoldenVectors(t *testing.T) {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
req := &signature.Request{
|
||||
SourceHost: testHost,
|
||||
SourcePath: tt.path,
|
||||
SourceQuery: tt.query,
|
||||
Width: 800,
|
||||
Height: 600,
|
||||
Format: testFormatWebP,
|
||||
Quality: 85,
|
||||
FitMode: testFitCover,
|
||||
Expires: time.Unix(goldenExpiresUnix, 0),
|
||||
req := &imagerequest.Request{
|
||||
Source: imagerequest.Source{
|
||||
Host: testHost,
|
||||
Path: tt.path,
|
||||
Query: tt.query,
|
||||
},
|
||||
Transform: imagerequest.Transform{
|
||||
Size: imagerequest.Size{Width: 800, Height: 600},
|
||||
Format: testFormatWebP,
|
||||
Quality: 85,
|
||||
FitMode: testFitCover,
|
||||
},
|
||||
Expires: time.Unix(goldenExpiresUnix, 0),
|
||||
}
|
||||
|
||||
gotSignature := signer.Sign(req)
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"sneak.berlin/go/pixa/internal/imagerequest"
|
||||
"sneak.berlin/go/pixa/internal/signature"
|
||||
)
|
||||
|
||||
@@ -16,7 +17,7 @@ const goldenSigningKey = "golden-test-key"
|
||||
|
||||
type goldenVector struct {
|
||||
name string
|
||||
req signature.Request
|
||||
req imagerequest.Request
|
||||
// wantSignature is the exact base64url (RFC 4648 URL-safe,
|
||||
// padded) HMAC-SHA256 signature for the request with Expires
|
||||
// set to goldenExpiresUnix.
|
||||
@@ -34,15 +35,18 @@ func goldenVectors() []goldenVector {
|
||||
return []goldenVector{
|
||||
{
|
||||
name: "resized without query",
|
||||
req: signature.Request{
|
||||
SourceHost: testHost,
|
||||
SourcePath: testPath,
|
||||
SourceQuery: "",
|
||||
Width: 800,
|
||||
Height: 600,
|
||||
Format: testFormatWebP,
|
||||
Quality: 85,
|
||||
FitMode: testFitCover,
|
||||
req: imagerequest.Request{
|
||||
Source: imagerequest.Source{
|
||||
Host: testHost,
|
||||
Path: testPath,
|
||||
Query: "",
|
||||
},
|
||||
Transform: imagerequest.Transform{
|
||||
Size: imagerequest.Size{Width: 800, Height: 600},
|
||||
Format: testFormatWebP,
|
||||
Quality: 85,
|
||||
FitMode: testFitCover,
|
||||
},
|
||||
},
|
||||
// Signed data:
|
||||
// "cdn.example.com:/photos/cat.jpg::800:600:webp:1704067200:85:cover"
|
||||
@@ -51,15 +55,18 @@ func goldenVectors() []goldenVector {
|
||||
},
|
||||
{
|
||||
name: "resized with query string",
|
||||
req: signature.Request{
|
||||
SourceHost: testHost,
|
||||
SourcePath: testPath,
|
||||
SourceQuery: "token=abc&v=2",
|
||||
Width: 800,
|
||||
Height: 600,
|
||||
Format: testFormatWebP,
|
||||
Quality: 85,
|
||||
FitMode: testFitCover,
|
||||
req: imagerequest.Request{
|
||||
Source: imagerequest.Source{
|
||||
Host: testHost,
|
||||
Path: testPath,
|
||||
Query: "token=abc&v=2",
|
||||
},
|
||||
Transform: imagerequest.Transform{
|
||||
Size: imagerequest.Size{Width: 800, Height: 600},
|
||||
Format: testFormatWebP,
|
||||
Quality: 85,
|
||||
FitMode: testFitCover,
|
||||
},
|
||||
},
|
||||
// Signed data:
|
||||
// "cdn.example.com:/photos/cat.jpg:token=abc&v=2:800:600:webp:1704067200:85:cover"
|
||||
@@ -69,15 +76,18 @@ func goldenVectors() []goldenVector {
|
||||
},
|
||||
{
|
||||
name: "original size without query",
|
||||
req: signature.Request{
|
||||
SourceHost: testHost,
|
||||
SourcePath: testPath,
|
||||
SourceQuery: "",
|
||||
Width: 0,
|
||||
Height: 0,
|
||||
Format: testFormatPNG,
|
||||
Quality: 85,
|
||||
FitMode: testFitCover,
|
||||
req: imagerequest.Request{
|
||||
Source: imagerequest.Source{
|
||||
Host: testHost,
|
||||
Path: testPath,
|
||||
Query: "",
|
||||
},
|
||||
Transform: imagerequest.Transform{
|
||||
Size: imagerequest.Size{Width: 0, Height: 0},
|
||||
Format: testFormatPNG,
|
||||
Quality: 85,
|
||||
FitMode: testFitCover,
|
||||
},
|
||||
},
|
||||
// Signed data:
|
||||
// "cdn.example.com:/photos/cat.jpg::0:0:png:1704067200:85:cover"
|
||||
@@ -86,15 +96,18 @@ func goldenVectors() []goldenVector {
|
||||
},
|
||||
{
|
||||
name: "non-default quality and fit",
|
||||
req: signature.Request{
|
||||
SourceHost: testHost,
|
||||
SourcePath: testPath,
|
||||
SourceQuery: "",
|
||||
Width: 800,
|
||||
Height: 600,
|
||||
Format: testFormatWebP,
|
||||
Quality: 40,
|
||||
FitMode: testFitContain,
|
||||
req: imagerequest.Request{
|
||||
Source: imagerequest.Source{
|
||||
Host: testHost,
|
||||
Path: testPath,
|
||||
Query: "",
|
||||
},
|
||||
Transform: imagerequest.Transform{
|
||||
Size: imagerequest.Size{Width: 800, Height: 600},
|
||||
Format: testFormatWebP,
|
||||
Quality: 40,
|
||||
FitMode: testFitContain,
|
||||
},
|
||||
},
|
||||
// Signed data:
|
||||
// "cdn.example.com:/photos/cat.jpg::800:600:webp:1704067200:40:contain"
|
||||
|
||||
@@ -5,22 +5,26 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"sneak.berlin/go/pixa/internal/imagerequest"
|
||||
"sneak.berlin/go/pixa/internal/signature"
|
||||
)
|
||||
|
||||
// signedQualityFitRequest returns a request signed for quality 85 and fit
|
||||
// mode "cover", the effective defaults the handler applies before
|
||||
// verification.
|
||||
func signedQualityFitRequest(signer *signature.Signer) *signature.Request {
|
||||
req := &signature.Request{
|
||||
SourceHost: testHost,
|
||||
SourcePath: testPath,
|
||||
Width: 800,
|
||||
Height: 600,
|
||||
Format: testFormatWebP,
|
||||
Quality: 85,
|
||||
FitMode: testFitCover,
|
||||
Expires: time.Now().Add(1 * time.Hour),
|
||||
func signedQualityFitRequest(signer *signature.Signer) *imagerequest.Request {
|
||||
req := &imagerequest.Request{
|
||||
Source: imagerequest.Source{
|
||||
Host: testHost,
|
||||
Path: testPath,
|
||||
},
|
||||
Transform: imagerequest.Transform{
|
||||
Size: imagerequest.Size{Width: 800, Height: 600},
|
||||
Format: testFormatWebP,
|
||||
Quality: 85,
|
||||
FitMode: testFitCover,
|
||||
},
|
||||
Expires: time.Now().Add(1 * time.Hour),
|
||||
}
|
||||
req.Signature = signer.Sign(req)
|
||||
|
||||
@@ -40,15 +44,15 @@ func TestSigner_Verify_QualityAndFitAreSigned(t *testing.T) {
|
||||
|
||||
cases := []struct {
|
||||
name string
|
||||
tamper func(r *signature.Request)
|
||||
tamper func(r *imagerequest.Request)
|
||||
}{
|
||||
{
|
||||
name: "replayed with different quality",
|
||||
tamper: func(r *signature.Request) { r.Quality = 40 },
|
||||
tamper: func(r *imagerequest.Request) { r.Transform.Quality = 40 },
|
||||
},
|
||||
{
|
||||
name: "replayed with different fit mode",
|
||||
tamper: func(r *signature.Request) { r.FitMode = testFitContain },
|
||||
tamper: func(r *imagerequest.Request) { r.Transform.FitMode = testFitContain },
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@@ -12,6 +12,8 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"sneak.berlin/go/pixa/internal/imagerequest"
|
||||
)
|
||||
|
||||
// Signature errors.
|
||||
@@ -22,37 +24,6 @@ var (
|
||||
ErrMissingExpiration = errors.New("signature expiration is required")
|
||||
)
|
||||
|
||||
// Request carries the components an image request signature covers. It is a
|
||||
// standalone type so that this package does not depend on imgcache, keeping
|
||||
// the import edge one-way (imgcache depends on signature, never the reverse).
|
||||
type Request struct {
|
||||
// SourceHost is the origin host (e.g. "cdn.example.com").
|
||||
SourceHost string
|
||||
// SourcePath is the path on the origin (e.g. "/photos/cat.jpg").
|
||||
SourcePath string
|
||||
// SourceQuery is the optional query string for the origin URL.
|
||||
SourceQuery string
|
||||
// Width is the requested output width in pixels.
|
||||
Width int
|
||||
// Height is the requested output height in pixels.
|
||||
Height int
|
||||
// Format is the requested output format (e.g. "webp").
|
||||
Format string
|
||||
// Quality is the requested output quality (1-100) for lossy formats.
|
||||
// It is the effective value the request resolves to: callers pass the
|
||||
// default quality when the request omits the parameter, so an omitted
|
||||
// quality signs identically to that same value stated explicitly.
|
||||
Quality int
|
||||
// FitMode is how the image is fit into the requested dimensions
|
||||
// (e.g. "cover"). Like Quality it is the effective value: callers pass
|
||||
// the default fit mode when the request omits the parameter.
|
||||
FitMode string
|
||||
// Signature is the HMAC signature to verify.
|
||||
Signature string
|
||||
// Expires is the signature expiration timestamp.
|
||||
Expires time.Time
|
||||
}
|
||||
|
||||
// Signer handles HMAC-SHA256 signature generation and verification.
|
||||
type Signer struct {
|
||||
secretKey []byte
|
||||
@@ -67,8 +38,11 @@ func New(secretKey string) *Signer {
|
||||
|
||||
// Sign generates an HMAC-SHA256 signature for the given request.
|
||||
// The signature covers: host + path + query + width + height + format +
|
||||
// expiration + quality + fit.
|
||||
func (s *Signer) Sign(req *Request) string {
|
||||
// expiration + quality + fit. The quality and fit mode are the effective
|
||||
// values the request resolves to: callers pass the defaults when the request
|
||||
// omits those parameters, so an omitted quality or fit mode signs
|
||||
// identically to that same value stated explicitly.
|
||||
func (s *Signer) Sign(req *imagerequest.Request) string {
|
||||
data := s.buildSignatureData(req)
|
||||
mac := hmac.New(sha256.New, s.secretKey)
|
||||
mac.Write([]byte(data))
|
||||
@@ -84,7 +58,7 @@ func (s *Signer) Sign(req *Request) string {
|
||||
// No suffix matching, wildcard matching, or partial matching is supported.
|
||||
// A signature for "cdn.example.com" will NOT verify for "example.com" or
|
||||
// "other.cdn.example.com", and vice versa.
|
||||
func (s *Signer) Verify(req *Request) error {
|
||||
func (s *Signer) Verify(req *imagerequest.Request) error {
|
||||
// Check expiration first
|
||||
if req.Expires.IsZero() {
|
||||
return ErrMissingExpiration
|
||||
@@ -108,7 +82,7 @@ func (s *Signer) Verify(req *Request) error {
|
||||
// GenerateSignedURL creates a complete URL with signature and expiration.
|
||||
// Returns the path portion that should be appended to the base URL.
|
||||
func (s *Signer) GenerateSignedURL(
|
||||
req *Request, ttl time.Duration,
|
||||
req *imagerequest.Request, ttl time.Duration,
|
||||
) (string, string, int64) {
|
||||
// Set expiration
|
||||
req.Expires = time.Now().Add(ttl)
|
||||
@@ -120,10 +94,11 @@ func (s *Signer) GenerateSignedURL(
|
||||
|
||||
// Build the size component
|
||||
var sizeStr string
|
||||
if req.Width == 0 && req.Height == 0 {
|
||||
if req.Transform.Size.Width == 0 && req.Transform.Size.Height == 0 {
|
||||
sizeStr = "orig"
|
||||
} else {
|
||||
sizeStr = fmt.Sprintf("%dx%d", req.Width, req.Height)
|
||||
sizeStr = fmt.Sprintf("%dx%d",
|
||||
req.Transform.Size.Width, req.Transform.Size.Height)
|
||||
}
|
||||
|
||||
// Build the path. A source query follows the source path after a "?".
|
||||
@@ -131,13 +106,13 @@ func (s *Signer) GenerateSignedURL(
|
||||
// written in Go's own percent-encoding of a URL path, which writes "?"
|
||||
// as %3F and "%" as %25, and leaves "/", ";", ",", "=" and "&" as they
|
||||
// are.
|
||||
sourcePathAndQuery := req.SourcePath
|
||||
if req.SourceQuery != "" {
|
||||
sourcePathAndQuery += "?" + req.SourceQuery
|
||||
sourcePathAndQuery := req.Source.Path
|
||||
if req.Source.Query != "" {
|
||||
sourcePathAndQuery += "?" + req.Source.Query
|
||||
}
|
||||
|
||||
unescapedPath := fmt.Sprintf("/v1/image/%s%s/%s.%s",
|
||||
req.SourceHost, sourcePathAndQuery, sizeStr, req.Format)
|
||||
req.Source.Host, sourcePathAndQuery, sizeStr, req.Transform.Format)
|
||||
|
||||
return (&url.URL{Path: unescapedPath}).EscapedPath(), sig, exp
|
||||
}
|
||||
@@ -150,17 +125,17 @@ func (s *Signer) GenerateSignedURL(
|
||||
// expansion is performed. Quality and fit are the effective transform
|
||||
// values, so replaying a signed URL with a different quality or fit mode
|
||||
// fails verification.
|
||||
func (s *Signer) buildSignatureData(req *Request) string {
|
||||
func (s *Signer) buildSignatureData(req *imagerequest.Request) string {
|
||||
return fmt.Sprintf("%s:%s:%s:%d:%d:%s:%d:%d:%s",
|
||||
req.SourceHost,
|
||||
escapePercentAndColon(req.SourcePath),
|
||||
escapePercentAndColon(req.SourceQuery),
|
||||
req.Width,
|
||||
req.Height,
|
||||
req.Format,
|
||||
req.Source.Host,
|
||||
escapePercentAndColon(req.Source.Path),
|
||||
escapePercentAndColon(req.Source.Query),
|
||||
req.Transform.Size.Width,
|
||||
req.Transform.Size.Height,
|
||||
req.Transform.Format,
|
||||
req.Expires.Unix(),
|
||||
req.Quality,
|
||||
req.FitMode,
|
||||
req.Transform.Quality,
|
||||
req.Transform.FitMode,
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"sneak.berlin/go/pixa/internal/imagerequest"
|
||||
"sneak.berlin/go/pixa/internal/signature"
|
||||
)
|
||||
|
||||
@@ -26,14 +27,17 @@ func TestSigner_Sign(t *testing.T) {
|
||||
|
||||
signer := signature.New("test-secret-key")
|
||||
|
||||
req := &signature.Request{
|
||||
SourceHost: testHost,
|
||||
SourcePath: testPath,
|
||||
SourceQuery: "",
|
||||
Width: 800,
|
||||
Height: 600,
|
||||
Format: testFormatWebP,
|
||||
Expires: time.Unix(1704067200, 0), // Fixed timestamp for reproducibility
|
||||
req := &imagerequest.Request{
|
||||
Source: imagerequest.Source{
|
||||
Host: testHost,
|
||||
Path: testPath,
|
||||
Query: "",
|
||||
},
|
||||
Transform: imagerequest.Transform{
|
||||
Size: imagerequest.Size{Width: 800, Height: 600},
|
||||
Format: testFormatWebP,
|
||||
},
|
||||
Expires: time.Unix(1704067200, 0), // Fixed timestamp for reproducibility
|
||||
}
|
||||
|
||||
sig1 := signer.Sign(req)
|
||||
@@ -51,14 +55,17 @@ func TestSigner_Sign(t *testing.T) {
|
||||
}
|
||||
|
||||
// Different input should produce different signature
|
||||
req2 := &signature.Request{
|
||||
SourceHost: testHost,
|
||||
SourcePath: "/photos/dog.jpg", // Different path
|
||||
SourceQuery: "",
|
||||
Width: 800,
|
||||
Height: 600,
|
||||
Format: testFormatWebP,
|
||||
Expires: time.Unix(1704067200, 0),
|
||||
req2 := &imagerequest.Request{
|
||||
Source: imagerequest.Source{
|
||||
Host: testHost,
|
||||
Path: "/photos/dog.jpg", // Different path
|
||||
Query: "",
|
||||
},
|
||||
Transform: imagerequest.Transform{
|
||||
Size: imagerequest.Size{Width: 800, Height: 600},
|
||||
Format: testFormatWebP,
|
||||
},
|
||||
Expires: time.Unix(1704067200, 0),
|
||||
}
|
||||
|
||||
sig3 := signer.Sign(req2)
|
||||
@@ -69,20 +76,23 @@ func TestSigner_Sign(t *testing.T) {
|
||||
|
||||
// validVerifyRequest returns a fully-populated request that verifies
|
||||
// successfully once signed.
|
||||
func validVerifyRequest() *signature.Request {
|
||||
return &signature.Request{
|
||||
SourceHost: testHost,
|
||||
SourcePath: testPath,
|
||||
Width: 800,
|
||||
Height: 600,
|
||||
Format: testFormatWebP,
|
||||
Expires: time.Now().Add(1 * time.Hour),
|
||||
func validVerifyRequest() *imagerequest.Request {
|
||||
return &imagerequest.Request{
|
||||
Source: imagerequest.Source{
|
||||
Host: testHost,
|
||||
Path: testPath,
|
||||
},
|
||||
Transform: imagerequest.Transform{
|
||||
Size: imagerequest.Size{Width: 800, Height: 600},
|
||||
Format: testFormatWebP,
|
||||
},
|
||||
Expires: time.Now().Add(1 * time.Hour),
|
||||
}
|
||||
}
|
||||
|
||||
type verifyCase struct {
|
||||
name string
|
||||
setup func() *signature.Request
|
||||
setup func() *imagerequest.Request
|
||||
wantErr error
|
||||
}
|
||||
|
||||
@@ -90,7 +100,7 @@ func verifyCases(signer *signature.Signer) []verifyCase {
|
||||
return []verifyCase{
|
||||
{
|
||||
name: "valid signature",
|
||||
setup: func() *signature.Request {
|
||||
setup: func() *imagerequest.Request {
|
||||
req := validVerifyRequest()
|
||||
req.Signature = signer.Sign(req)
|
||||
|
||||
@@ -100,7 +110,7 @@ func verifyCases(signer *signature.Signer) []verifyCase {
|
||||
},
|
||||
{
|
||||
name: "expired signature",
|
||||
setup: func() *signature.Request {
|
||||
setup: func() *imagerequest.Request {
|
||||
req := validVerifyRequest()
|
||||
req.Expires = time.Now().Add(-1 * time.Hour)
|
||||
req.Signature = signer.Sign(req)
|
||||
@@ -111,7 +121,7 @@ func verifyCases(signer *signature.Signer) []verifyCase {
|
||||
},
|
||||
{
|
||||
name: "invalid signature",
|
||||
setup: func() *signature.Request {
|
||||
setup: func() *imagerequest.Request {
|
||||
req := validVerifyRequest()
|
||||
req.Signature = "invalid-signature"
|
||||
|
||||
@@ -121,7 +131,7 @@ func verifyCases(signer *signature.Signer) []verifyCase {
|
||||
},
|
||||
{
|
||||
name: "missing expiration",
|
||||
setup: func() *signature.Request {
|
||||
setup: func() *imagerequest.Request {
|
||||
req := validVerifyRequest()
|
||||
req.Expires = time.Time{}
|
||||
req.Signature = "some-signature"
|
||||
@@ -132,10 +142,10 @@ func verifyCases(signer *signature.Signer) []verifyCase {
|
||||
},
|
||||
{
|
||||
name: "tampered request",
|
||||
setup: func() *signature.Request {
|
||||
setup: func() *imagerequest.Request {
|
||||
req := validVerifyRequest()
|
||||
req.Signature = signer.Sign(req)
|
||||
req.SourcePath = "/photos/secret.jpg"
|
||||
req.Source.Path = "/photos/secret.jpg"
|
||||
|
||||
return req
|
||||
},
|
||||
@@ -173,7 +183,7 @@ func TestSigner_Verify(t *testing.T) {
|
||||
|
||||
type tamperCase struct {
|
||||
name string
|
||||
tamper func(r *signature.Request)
|
||||
tamper func(r *imagerequest.Request)
|
||||
}
|
||||
|
||||
// exactMatchTamperCases mutates one signed component per case; every
|
||||
@@ -182,59 +192,59 @@ func exactMatchTamperCases() []tamperCase {
|
||||
return []tamperCase{
|
||||
{
|
||||
name: "parent domain does not match subdomain",
|
||||
tamper: func(r *signature.Request) { r.SourceHost = "example.com" },
|
||||
tamper: func(r *imagerequest.Request) { r.Source.Host = "example.com" },
|
||||
},
|
||||
{
|
||||
name: "subdomain does not match parent domain",
|
||||
tamper: func(r *signature.Request) { r.SourceHost = "images.cdn.example.com" },
|
||||
tamper: func(r *imagerequest.Request) { r.Source.Host = "images.cdn.example.com" },
|
||||
},
|
||||
{
|
||||
name: "sibling subdomain does not match",
|
||||
tamper: func(r *signature.Request) { r.SourceHost = "images.example.com" },
|
||||
tamper: func(r *imagerequest.Request) { r.Source.Host = "images.example.com" },
|
||||
},
|
||||
{
|
||||
name: "host with suffix appended does not match",
|
||||
tamper: func(r *signature.Request) { r.SourceHost = testHost + ".evil.com" },
|
||||
tamper: func(r *imagerequest.Request) { r.Source.Host = testHost + ".evil.com" },
|
||||
},
|
||||
{
|
||||
name: "host with prefix does not match",
|
||||
tamper: func(r *signature.Request) { r.SourceHost = "evilcdn.example.com" },
|
||||
tamper: func(r *imagerequest.Request) { r.Source.Host = "evilcdn.example.com" },
|
||||
},
|
||||
{
|
||||
name: "different path does not match",
|
||||
tamper: func(r *signature.Request) { r.SourcePath = "/photos/dog.jpg" },
|
||||
tamper: func(r *imagerequest.Request) { r.Source.Path = "/photos/dog.jpg" },
|
||||
},
|
||||
{
|
||||
name: "path suffix does not match",
|
||||
tamper: func(r *signature.Request) { r.SourcePath = testPath + "/extra" },
|
||||
tamper: func(r *imagerequest.Request) { r.Source.Path = testPath + "/extra" },
|
||||
},
|
||||
{
|
||||
name: "path prefix does not match",
|
||||
tamper: func(r *signature.Request) { r.SourcePath = "/other" + testPath },
|
||||
tamper: func(r *imagerequest.Request) { r.Source.Path = "/other" + testPath },
|
||||
},
|
||||
{
|
||||
name: "different query does not match",
|
||||
tamper: func(r *signature.Request) { r.SourceQuery = "token=xyz" },
|
||||
tamper: func(r *imagerequest.Request) { r.Source.Query = "token=xyz" },
|
||||
},
|
||||
{
|
||||
name: "added query does not match empty query",
|
||||
tamper: func(r *signature.Request) { r.SourceQuery = "extra=1" },
|
||||
tamper: func(r *imagerequest.Request) { r.Source.Query = "extra=1" },
|
||||
},
|
||||
{
|
||||
name: "removed query does not match",
|
||||
tamper: func(r *signature.Request) { r.SourceQuery = "" },
|
||||
tamper: func(r *imagerequest.Request) { r.Source.Query = "" },
|
||||
},
|
||||
{
|
||||
name: "different width does not match",
|
||||
tamper: func(r *signature.Request) { r.Width = 801 },
|
||||
tamper: func(r *imagerequest.Request) { r.Transform.Size.Width = 801 },
|
||||
},
|
||||
{
|
||||
name: "different height does not match",
|
||||
tamper: func(r *signature.Request) { r.Height = 601 },
|
||||
tamper: func(r *imagerequest.Request) { r.Transform.Size.Height = 601 },
|
||||
},
|
||||
{
|
||||
name: "different format does not match",
|
||||
tamper: func(r *signature.Request) { r.Format = testFormatPNG },
|
||||
tamper: func(r *imagerequest.Request) { r.Transform.Format = testFormatPNG },
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -248,15 +258,18 @@ func TestSigner_Verify_ExactMatchOnly(t *testing.T) {
|
||||
signer := signature.New("test-secret-key")
|
||||
|
||||
// Base request that we'll sign, then tamper with individual fields.
|
||||
baseReq := func() *signature.Request {
|
||||
req := &signature.Request{
|
||||
SourceHost: testHost,
|
||||
SourcePath: testPath,
|
||||
SourceQuery: "token=abc",
|
||||
Width: 800,
|
||||
Height: 600,
|
||||
Format: testFormatWebP,
|
||||
Expires: time.Now().Add(1 * time.Hour),
|
||||
baseReq := func() *imagerequest.Request {
|
||||
req := &imagerequest.Request{
|
||||
Source: imagerequest.Source{
|
||||
Host: testHost,
|
||||
Path: testPath,
|
||||
Query: "token=abc",
|
||||
},
|
||||
Transform: imagerequest.Transform{
|
||||
Size: imagerequest.Size{Width: 800, Height: 600},
|
||||
Format: testFormatWebP,
|
||||
},
|
||||
Expires: time.Now().Add(1 * time.Hour),
|
||||
}
|
||||
req.Signature = signer.Sign(req)
|
||||
|
||||
@@ -309,14 +322,17 @@ func TestSigner_Sign_ExactHostInData(t *testing.T) {
|
||||
sigs := make(map[string]string)
|
||||
|
||||
for _, host := range hosts {
|
||||
req := &signature.Request{
|
||||
SourceHost: host,
|
||||
SourcePath: testPath,
|
||||
SourceQuery: "",
|
||||
Width: 800,
|
||||
Height: 600,
|
||||
Format: testFormatWebP,
|
||||
Expires: time.Unix(1704067200, 0),
|
||||
req := &imagerequest.Request{
|
||||
Source: imagerequest.Source{
|
||||
Host: host,
|
||||
Path: testPath,
|
||||
Query: "",
|
||||
},
|
||||
Transform: imagerequest.Transform{
|
||||
Size: imagerequest.Size{Width: 800, Height: 600},
|
||||
Format: testFormatWebP,
|
||||
},
|
||||
Expires: time.Unix(1704067200, 0),
|
||||
}
|
||||
|
||||
sig := signer.Sign(req)
|
||||
@@ -334,13 +350,16 @@ func TestSigner_DifferentKeys(t *testing.T) {
|
||||
signer1 := signature.New("secret-key-1")
|
||||
signer2 := signature.New("secret-key-2")
|
||||
|
||||
req := &signature.Request{
|
||||
SourceHost: testHost,
|
||||
SourcePath: testPath,
|
||||
Width: 800,
|
||||
Height: 600,
|
||||
Format: testFormatWebP,
|
||||
Expires: time.Now().Add(1 * time.Hour),
|
||||
req := &imagerequest.Request{
|
||||
Source: imagerequest.Source{
|
||||
Host: testHost,
|
||||
Path: testPath,
|
||||
},
|
||||
Transform: imagerequest.Transform{
|
||||
Size: imagerequest.Size{Width: 800, Height: 600},
|
||||
Format: testFormatWebP,
|
||||
},
|
||||
Expires: time.Now().Add(1 * time.Hour),
|
||||
}
|
||||
|
||||
// Sign with key 1
|
||||
@@ -364,13 +383,16 @@ func TestGenerateSignedURL(t *testing.T) {
|
||||
|
||||
signer := signature.New("test-secret-key")
|
||||
|
||||
req := &signature.Request{
|
||||
SourceHost: testHost,
|
||||
SourcePath: testPath,
|
||||
SourceQuery: "",
|
||||
Width: 800,
|
||||
Height: 600,
|
||||
Format: testFormatWebP,
|
||||
req := &imagerequest.Request{
|
||||
Source: imagerequest.Source{
|
||||
Host: testHost,
|
||||
Path: testPath,
|
||||
Query: "",
|
||||
},
|
||||
Transform: imagerequest.Transform{
|
||||
Size: imagerequest.Size{Width: 800, Height: 600},
|
||||
Format: testFormatWebP,
|
||||
},
|
||||
}
|
||||
|
||||
ttl := 1 * time.Hour
|
||||
@@ -405,12 +427,15 @@ func TestGenerateSignedURL_OrigSize(t *testing.T) {
|
||||
|
||||
signer := signature.New("test-secret-key")
|
||||
|
||||
req := &signature.Request{
|
||||
SourceHost: testHost,
|
||||
SourcePath: testPath,
|
||||
Width: 0, // Original size
|
||||
Height: 0,
|
||||
Format: testFormatPNG,
|
||||
req := &imagerequest.Request{
|
||||
Source: imagerequest.Source{
|
||||
Host: testHost,
|
||||
Path: testPath,
|
||||
},
|
||||
Transform: imagerequest.Transform{
|
||||
Size: imagerequest.Size{Width: 0, Height: 0}, // Original size
|
||||
Format: testFormatPNG,
|
||||
},
|
||||
}
|
||||
|
||||
path, _, _ := signer.GenerateSignedURL(req, time.Hour)
|
||||
@@ -426,13 +451,16 @@ func TestGenerateSignedURL_WithQueryString(t *testing.T) {
|
||||
|
||||
signer := signature.New("test-secret-key-for-testing!")
|
||||
|
||||
req := &signature.Request{
|
||||
SourceHost: testHost,
|
||||
SourcePath: testPath,
|
||||
SourceQuery: "token=abc&v=2",
|
||||
Width: 800,
|
||||
Height: 600,
|
||||
Format: testFormatWebP,
|
||||
req := &imagerequest.Request{
|
||||
Source: imagerequest.Source{
|
||||
Host: testHost,
|
||||
Path: testPath,
|
||||
Query: "token=abc&v=2",
|
||||
},
|
||||
Transform: imagerequest.Transform{
|
||||
Size: imagerequest.Size{Width: 800, Height: 600},
|
||||
Format: testFormatWebP,
|
||||
},
|
||||
}
|
||||
|
||||
path, _, _ := signer.GenerateSignedURL(req, time.Hour)
|
||||
@@ -460,12 +488,15 @@ func TestGenerateSignedURL_WithoutQueryString(t *testing.T) {
|
||||
|
||||
signer := signature.New("test-secret-key-for-testing!")
|
||||
|
||||
req := &signature.Request{
|
||||
SourceHost: testHost,
|
||||
SourcePath: testPath,
|
||||
Width: 800,
|
||||
Height: 600,
|
||||
Format: testFormatWebP,
|
||||
req := &imagerequest.Request{
|
||||
Source: imagerequest.Source{
|
||||
Host: testHost,
|
||||
Path: testPath,
|
||||
},
|
||||
Transform: imagerequest.Transform{
|
||||
Size: imagerequest.Size{Width: 800, Height: 600},
|
||||
Format: testFormatWebP,
|
||||
},
|
||||
}
|
||||
|
||||
path, _, _ := signer.GenerateSignedURL(req, time.Hour)
|
||||
@@ -485,16 +516,19 @@ func TestGenerateSignedURL_ReadmeQueryExample(t *testing.T) {
|
||||
|
||||
signer := signature.New("example-signing-key-for-documentation")
|
||||
|
||||
req := signature.Request{
|
||||
SourceHost: testHost,
|
||||
SourcePath: "/img",
|
||||
SourceQuery: "url=https://origin.example.com/my%20cat.jpg",
|
||||
Width: 800,
|
||||
Height: 600,
|
||||
Format: testFormatWebP,
|
||||
Quality: 85,
|
||||
FitMode: testFitCover,
|
||||
Expires: time.Unix(1704067200, 0),
|
||||
req := imagerequest.Request{
|
||||
Source: imagerequest.Source{
|
||||
Host: testHost,
|
||||
Path: "/img",
|
||||
Query: "url=https://origin.example.com/my%20cat.jpg",
|
||||
},
|
||||
Transform: imagerequest.Transform{
|
||||
Size: imagerequest.Size{Width: 800, Height: 600},
|
||||
Format: testFormatWebP,
|
||||
Quality: 85,
|
||||
FitMode: testFitCover,
|
||||
},
|
||||
Expires: time.Unix(1704067200, 0),
|
||||
}
|
||||
|
||||
const wantSig = "37aFa9GSVCZM1kRY9SzMOk2fOg_M7Wt5gKNR4xM-FYA="
|
||||
|
||||
Reference in New Issue
Block a user