2020-08-20 10:54:26 +02:00
package middleware
import (
2024-02-07 15:44:33 +01:00
"context"
2024-02-26 16:08:03 +01:00
"net/http/httptest"
"testing"
"time"
2024-02-07 15:44:33 +01:00
userpb "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1"
2026-06-23 12:23:54 +03:00
"github.com/owncloud/ocis/v2/ocis-pkg/log"
2024-02-07 15:44:33 +01:00
"github.com/owncloud/ocis/v2/services/proxy/pkg/config"
2025-03-04 16:02:36 +01:00
revactx "github.com/owncloud/reva/v2/pkg/ctx"
2024-02-07 15:44:33 +01:00
"github.com/stretchr/testify/assert"
2024-02-26 16:08:03 +01:00
"go-micro.dev/v4/store"
2020-08-20 10:54:26 +02:00
)
2020-11-16 13:42:23 +01:00
func TestSignedURLAuth_shouldServe ( t * testing . T ) {
2022-08-04 17:38:55 +02:00
pua := SignedURLAuthenticator {}
2020-08-20 10:54:26 +02:00
tests := [] struct {
url string
2020-11-21 07:58:19 +01:00
enabled bool
2020-08-20 10:54:26 +02:00
expected bool
}{
2020-11-21 07:58:19 +01:00
{ "https://example.com/example.jpg" , true , false },
{ "https://example.com/example.jpg?OC-Signature=something" , true , true },
{ "https://example.com/example.jpg" , false , false },
{ "https://example.com/example.jpg?OC-Signature=something" , false , false },
2020-08-20 10:54:26 +02:00
}
for _ , tt := range tests {
2022-08-08 17:23:47 +02:00
pua . PreSignedURLConfig . Enabled = tt . enabled
2020-08-20 10:54:26 +02:00
r := httptest . NewRequest ( "" , tt . url , nil )
2020-11-16 13:42:23 +01:00
result := pua . shouldServe ( r )
2020-08-20 10:54:26 +02:00
if result != tt . expected {
t . Errorf ( "with %s expected %t got %t" , tt . url , tt . expected , result )
}
}
}
2020-11-16 13:42:23 +01:00
func TestSignedURLAuth_allRequiredParametersPresent ( t * testing . T ) {
2022-08-04 17:38:55 +02:00
pua := SignedURLAuthenticator {}
2020-08-20 10:54:26 +02:00
baseURL := "https://example.com/example.jpg?"
tests := [] struct {
2024-02-07 15:44:33 +01:00
params string
errorMessage string
2020-08-20 10:54:26 +02:00
}{
2024-02-07 15:44:33 +01:00
{ "OC-Signature=something&OC-Credential=something&OC-Date=something&OC-Expires=something&OC-Verb=something" , "" },
{ "OC-Credential=something&OC-Date=something&OC-Expires=something&OC-Verb=something" , "required OC-Signature parameter not found" },
{ "OC-Signature=something&OC-Date=something&OC-Expires=something&OC-Verb=something" , "required OC-Credential parameter not found" },
{ "OC-Signature=something&OC-Credential=something&OC-Expires=something&OC-Verb=something" , "required OC-Date parameter not found" },
{ "OC-Signature=something&OC-Credential=something&OC-Date=something&OC-Verb=something" , "required OC-Expires parameter not found" },
{ "OC-Signature=something&OC-Credential=something&OC-Date=something&OC-Expires=something" , "required OC-Verb parameter not found" },
2020-08-20 10:54:26 +02:00
}
for _ , tt := range tests {
r := httptest . NewRequest ( "" , baseURL + tt . params , nil )
2024-02-07 15:44:33 +01:00
err := pua . allRequiredParametersArePresent ( r . URL . Query ())
if tt . errorMessage != "" {
assert . EqualError ( t , err , tt . errorMessage , tt . params )
} else {
assert . Nil ( t , err )
2020-08-20 10:54:26 +02:00
}
}
}
2020-11-16 13:42:23 +01:00
func TestSignedURLAuth_requestMethodMatches ( t * testing . T ) {
2022-08-04 17:38:55 +02:00
pua := SignedURLAuthenticator {}
2020-08-20 10:54:26 +02:00
tests := [] struct {
2024-02-07 15:44:33 +01:00
method string
url string
errorMessage string
2020-08-20 10:54:26 +02:00
}{
2024-02-07 15:44:33 +01:00
{ "GET" , "https://example.com/example.jpg?OC-Verb=GET" , "" },
{ "GET" , "https://example.com/example.jpg?OC-Verb=get" , "" },
{ "POST" , "https://example.com/example.jpg?OC-Verb=GET" , "required OC-Verb parameter did not match request method" },
2020-08-20 10:54:26 +02:00
}
for _ , tt := range tests {
r := httptest . NewRequest ( tt . method , tt . url , nil )
2024-02-07 15:44:33 +01:00
err := pua . requestMethodMatches ( r . Method , r . URL . Query ())
if tt . errorMessage != "" {
assert . EqualError ( t , err , tt . errorMessage , tt . url )
} else {
assert . Nil ( t , err )
2020-08-20 10:54:26 +02:00
}
}
}
2020-11-16 13:42:23 +01:00
func TestSignedURLAuth_requestMethodIsAllowed ( t * testing . T ) {
2022-08-04 17:38:55 +02:00
pua := SignedURLAuthenticator {}
2020-08-20 10:54:26 +02:00
tests := [] struct {
2024-02-07 15:44:33 +01:00
method string
allowed [] string
errorMessage string
2020-08-20 10:54:26 +02:00
}{
2024-02-07 15:44:33 +01:00
{ "GET" , [] string {}, "request method is not listed in PreSignedURLConfig AllowedHTTPMethods" },
{ "GET" , [] string { "POST" }, "request method is not listed in PreSignedURLConfig AllowedHTTPMethods" },
{ "GET" , [] string { "GET" }, "" },
{ "GET" , [] string { "get" }, "" },
{ "GET" , [] string { "POST" , "GET" }, "" },
2020-08-20 10:54:26 +02:00
}
for _ , tt := range tests {
2022-08-08 17:23:47 +02:00
pua . PreSignedURLConfig . AllowedHTTPMethods = tt . allowed
2024-02-07 15:44:33 +01:00
err := pua . requestMethodIsAllowed ( tt . method )
if tt . errorMessage != "" {
assert . EqualError ( t , err , tt . errorMessage )
} else {
assert . Nil ( t , err )
2020-08-20 10:54:26 +02:00
}
}
}
2020-11-16 13:42:23 +01:00
func TestSignedURLAuth_urlIsExpired ( t * testing . T ) {
2020-08-20 10:54:26 +02:00
nowFunc := func () time . Time {
2020-11-16 16:45:29 +01:00
t , _ := time . Parse ( time . RFC3339 , "2020-02-02T12:30:00.000Z" )
2020-08-20 10:54:26 +02:00
return t
}
2024-02-07 15:44:33 +01:00
pua := SignedURLAuthenticator {
Now : nowFunc ,
}
2020-08-20 10:54:26 +02:00
tests := [] struct {
2024-02-07 15:44:33 +01:00
url string
errorMessage string
2020-08-20 10:54:26 +02:00
}{
2024-02-07 15:44:33 +01:00
// a valid signed url
{ "http://example.com/example.jpg?OC-Date=2020-02-02T12:29:00.000Z&OC-Expires=61" , "" },
// invalid expiry
{ "http://example.com/example.jpg?OC-Date=2020-02-02T12:29:00.000Z&OC-Expires=invalid" , "time: invalid duration \"invalids\"" },
// wrong date format on OC-Date
{ "http://example.com/example.jpg?OC-Date=2020-02-02TTT12:29:00.000Z&OC-Expires=5" , "parsing time \"2020-02-02TTT12:29:00.000Z\" as \"2006-01-02T15:04:05Z07:00\": cannot parse \"TT12:29:00.000Z\" as \"15\"" },
// expired - 12:29:00 + 59s < 12:30
{ "http://example.com/example.jpg?OC-Date=2020-02-02T12:29:00.000Z&OC-Expires=59" , "URL is expired" },
// expired - basically url was created yesterday
{ "http://example.com/example.jpg?OC-Date=2020-02-01T12:29:00.000Z&OC-Expires=59" , "URL is expired" },
// future OC-Date - also valid now
{ "http://example.com/example.jpg?OC-Date=2020-02-03T12:29:00.000Z&OC-Expires=59" , "" },
2020-08-20 10:54:26 +02:00
}
for _ , tt := range tests {
r := httptest . NewRequest ( "" , tt . url , nil )
2024-02-07 15:44:33 +01:00
err := pua . urlIsExpired ( r . URL . Query ())
if tt . errorMessage != "" {
assert . EqualError ( t , err , tt . errorMessage , tt . url )
} else {
assert . Nil ( t , err )
2020-08-20 10:54:26 +02:00
}
}
}
2020-11-16 13:42:23 +01:00
func TestSignedURLAuth_createSignature ( t * testing . T ) {
2022-08-04 17:38:55 +02:00
pua := SignedURLAuthenticator {}
2020-08-20 10:54:26 +02:00
expected := "27d2ebea381384af3179235114801dcd00f91e46f99fca72575301cf3948101d"
2020-11-16 13:42:23 +01:00
s := pua . createSignature ( "something" , [] byte ( "somerandomkey" ))
2020-08-20 10:54:26 +02:00
if s != expected {
t . Fail ()
}
}
2024-02-07 15:44:33 +01:00
2026-06-23 12:23:54 +03:00
// TestSignedURLAuth_validateEmptySigningKeyStore guards against an out-of-range
// panic when the signing-key store returns no record without an error. This
// happens with the "noop" store (e.g. when OCIS_CACHE_STORE=noop, which also
// governs the presigned-URL signing-key store). The request must fail
// authentication, not panic.
func TestSignedURLAuth_validateEmptySigningKeyStore ( t * testing . T ) {
pua := SignedURLAuthenticator {
PreSignedURLConfig : config . PreSignedURL {
AllowedHTTPMethods : [] string { "get" },
Enabled : true ,
},
Store : store . NewNoopStore (),
Logger : log . NewLogger (),
Now : func () time . Time {
tm , _ := time . Parse ( time . RFC3339 , "2019-05-14T11:02:00.000Z" )
return tm
},
}
u := userpb . User { Id : & userpb . UserId { OpaqueId : "useri" }, DisplayName : "Test User" }
ctx := revactx . ContextSetUser ( context . Background (), & u )
r := httptest . NewRequest ( "" , "http://cloud.example.net/?OC-Credential=alice&OC-Date=2019-05-14T11%3A01%3A58.135Z&OC-Expires=1200&OC-Verb=GET&OC-Signature=f9e53a1ee23caef10f72ec392c1b537317491b687bfdd224c782be197d9ca2b6" , nil ). WithContext ( ctx )
var err error
assert . NotPanics ( t , func () { err = pua . validate ( r ) }, "validate must not panic when the signing key store is empty" )
assert . Error ( t , err , "validate must reject the request when no signing key is found" )
}
2024-02-07 15:44:33 +01:00
func TestSignedURLAuth_validate ( t * testing . T ) {
nowFunc := func () time . Time {
t , _ := time . Parse ( time . RFC3339 , "2020-02-02T12:30:00.000Z" )
return t
}
cfg := config . PreSignedURL {
AllowedHTTPMethods : [] string { "get" },
Enabled : true ,
}
pua := SignedURLAuthenticator {
PreSignedURLConfig : cfg ,
2024-02-26 16:08:03 +01:00
Store : store . NewMemoryStore (),
2024-02-07 15:44:33 +01:00
Now : nowFunc ,
}
2024-02-26 16:08:03 +01:00
pua . Store . Write ( & store . Record {
Key : "useri" ,
Value : [] byte ( "1234567890" ),
Metadata : nil ,
})
2024-02-07 15:44:33 +01:00
tests := [] struct {
now string
url string
errorMessage string
}{
{ "2020-02-02T12:30:00.000Z" , "http://example.com/example.jpg?OC-Date=2020-02-02T12:29:00.000Z&OC-Expires=invalid" , "required OC-Signature parameter not found" },
{ "2020-02-02T12:30:00.000Z" , "http://cloud.example.net/?OC-Credential=alice&OC-Date=2019-05-14T11%3A01%3A58.135Z&OC-Expires=1200&OC-Verb=GET&OC-Signature=f9e53a1ee23caef10f72ec392c1b537317491b687bfdd224c782be197d9ca2b6" , "URL is expired" },
{ "2019-05-14T11:02:00.000Z" , "http://cloud.example.net/?OC-Credential=alice&OC-Date=2019-05-14T11%3A01%3A58.135Z&OC-Expires=1200&OC-Verb=GET&OC-Signature=f9e53a1ee23caef10f72ec392c1b537317491b687bfdd224c782be197d9ca2b" , "signature mismatch: expected f9e53a1ee23caef10f72ec392c1b537317491b687bfdd224c782be197d9ca2b6 != actual f9e53a1ee23caef10f72ec392c1b537317491b687bfdd224c782be197d9ca2b" },
{ "2019-05-14T11:02:00.000Z" , "http://cloud.example.net/?OC-Credential=alice&OC-Date=2019-05-14T11%3A01%3A58.135Z&OC-Expires=1200&OC-Verb=GET&OC-Signature=f9e53a1ee23caef10f72ec392c1b537317491b687bfdd224c782be197d9ca2b6" , "" },
{ "2019-05-14T11:02:00.000Z" , "http://cloud.example.net/?OC-Date=2019-05-14T11%3A01%3A58.135Z&OC-Expires=1200&OC-Verb=GET&OC-Credential=alice&OC-Signature=f9e53a1ee23caef10f72ec392c1b537317491b687bfdd224c782be197d9ca2b6" , "" },
{ "2019-05-14T11:02:00.000Z" , "http://cloud.example.net/?OC-Algo=PBKDF2%2F10000-SHA512&OC-Date=2019-05-14T11%3A01%3A58.135Z&OC-Expires=1200&OC-Verb=GET&OC-Credential=alice&OC-Signature=f9e53a1ee23caef10f72ec392c1b537317491b687bfdd224c782be197d9ca2b6" , "" },
{ "2024-02-07T12:03:11.966Z" , "http://localhost:33001/try?id=1&id=2&OC-Credential=user&OC-Date=2024-02-07T12%3A03%3A11.966Z&OC-Expires=2&OC-Verb=GET&OC-Algo=PBKDF2%2F10000-SHA512&OC-Signature=86e21a1efbf0be989a206109cfedf70a22f338dc8995e849ce002032bc6741c5" , "" },
}
for _ , tt := range tests {
u := userpb . User {
Id : & userpb . UserId { OpaqueId : "useri" },
DisplayName : "Test User" ,
}
ctx := revactx . ContextSetUser ( context . Background (), & u )
pua . Now = func () time . Time {
t , _ := time . Parse ( time . RFC3339 , tt . now )
return t
}
r := httptest . NewRequest ( "" , tt . url , nil ). WithContext ( ctx )
err := pua . validate ( r )
if tt . errorMessage == "" {
assert . Nil ( t , err )
} else {
assert . EqualError ( t , err , tt . errorMessage )
}
}
}