Skip to content

Commit 62a7440

Browse files
authored
Merge pull request #43 from transloadit/tim/issue-5337-listrequest-sha384
fix: use sha384-prefixed signatures for list requests
2 parents f2fda53 + 5097904 commit 62a7440

4 files changed

Lines changed: 69 additions & 10 deletions

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
SHELL := /usr/bin/env bash
22

33
test-examples:
4-
cd ./examples && find . -type f | xargs -i sh -c "go build {} && go clean" \;
4+
go build ./examples/...
55

66
test-package:
77
go test -v -coverprofile=coverage.out -covermode=atomic .

list_request_signature_test.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package transloadit
2+
3+
import (
4+
"context"
5+
"crypto/hmac"
6+
"crypto/sha512"
7+
"encoding/hex"
8+
"io"
9+
"net/http"
10+
"net/http/httptest"
11+
"strings"
12+
"testing"
13+
)
14+
15+
func TestListRequest_UsesSha384PrefixedSignature(t *testing.T) {
16+
t.Parallel()
17+
18+
var capturedParams string
19+
var capturedSignature string
20+
21+
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
22+
query := r.URL.Query()
23+
capturedParams = query.Get("params")
24+
capturedSignature = query.Get("signature")
25+
26+
w.Header().Set("Content-Type", "application/json")
27+
_, _ = io.WriteString(w, `{"items":[],"count":0}`)
28+
}))
29+
defer server.Close()
30+
31+
client := NewClient(Config{
32+
AuthKey: "test-key",
33+
AuthSecret: "test-secret",
34+
Endpoint: server.URL,
35+
})
36+
37+
_, err := client.ListTemplates(context.Background(), &ListOptions{PageSize: 3})
38+
if err != nil {
39+
t.Fatalf("ListTemplates failed: %v", err)
40+
}
41+
42+
if capturedParams == "" {
43+
t.Fatal("params should not be empty")
44+
}
45+
if capturedSignature == "" {
46+
t.Fatal("signature should not be empty")
47+
}
48+
if !strings.HasPrefix(capturedSignature, "sha384:") {
49+
t.Fatalf("expected sha384-prefixed signature, got %q", capturedSignature)
50+
}
51+
52+
mac := hmac.New(sha512.New384, []byte("test-secret"))
53+
mac.Write([]byte(capturedParams))
54+
expected := "sha384:" + hex.EncodeToString(mac.Sum(nil))
55+
if capturedSignature != expected {
56+
t.Fatalf("signature mismatch\nexpected: %s\nactual: %s", expected, capturedSignature)
57+
}
58+
}

template_credentials_test.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,17 @@ func TestTemplateCredentials(t *testing.T) {
5353
if !found {
5454
t.Errorf("Created TemplateCredential not found id=%s", id)
5555
}
56-
// Step 4 : Update the Template credential
56+
// Step 4 : Update the Template credential.
57+
// Keep the same type because the API does not allow changing credential type.
5758
newTemplateCredentialPost := NewTemplateCredential()
5859
newtemplateCredentialName := templateCredentialName + "updated"
5960
newTemplateCredentialPost.Name = newtemplateCredentialName
60-
newTemplateCredentialPost.Type = "backblaze"
61+
newTemplateCredentialPost.Type = "s3"
6162
newtemplateCredentialContent := map[string]interface{}{
62-
"bucket": "mybucket",
63-
"app_key_id": "mykeyid",
64-
"app_key": "mykey",
63+
"key": "updated-key",
64+
"secret": "updated-secret",
65+
"bucket": "updated-bucket.example.com",
66+
"bucket_region": "eu-central-1",
6567
}
6668
newTemplateCredentialPost.Content = newtemplateCredentialContent
6769
err = client.UpdateTemplateCredential(ctx, id, newTemplateCredentialPost)
@@ -74,7 +76,7 @@ func TestTemplateCredentials(t *testing.T) {
7476
if newTemplateCredential, err = client.GetTemplateCredential(ctx, id); err != nil {
7577
t.Error(err)
7678
}
77-
checkTemplateCredential(t, newTemplateCredential, newtemplateCredentialName, newtemplateCredentialContent, "backblaze")
79+
checkTemplateCredential(t, newTemplateCredential, newtemplateCredentialName, newtemplateCredentialContent, "s3")
7880

7981
// Step 6: Delete test templateCredential
8082
if err := client.DeleteTemplateCredential(ctx, id); err != nil {

transloadit.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ package transloadit
44
import (
55
"context"
66
"crypto/hmac"
7-
"crypto/sha1"
87
"crypto/sha256"
98
"crypto/sha512"
109
"encoding/hex"
@@ -213,11 +212,11 @@ func (client *Client) listRequest(ctx context.Context, path string, listOptions
213212
return fmt.Errorf("unable to create signature: %s", err)
214213
}
215214

216-
hash := hmac.New(sha1.New, []byte(client.config.AuthSecret))
215+
hash := hmac.New(sha512.New384, []byte(client.config.AuthSecret))
217216
hash.Write(b)
218217

219218
params := string(b)
220-
signature := hex.EncodeToString(hash.Sum(nil))
219+
signature := "sha384:" + hex.EncodeToString(hash.Sum(nil))
221220

222221
v := url.Values{}
223222
v.Set("params", params)

0 commit comments

Comments
 (0)