Skip to content

Commit

Permalink
Fix S3 Storage signature with /#/ path (#1022)
Browse files Browse the repository at this point in the history
]
  • Loading branch information
ije authored Jan 15, 2025
1 parent cf286fa commit 56bad85
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 7 deletions.
6 changes: 4 additions & 2 deletions server/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,10 @@ func withLRUCache[T any](key string, fetch func() (T, error)) (data T, err error
// delete the oldest item if cache is full
if cacheLRU.Len() > 1000 {
el := cacheLRU.Front()
cacheLRU.Remove(el)
cacheStore.Delete(el.Value.(cacheRecord).key)
if el != nil {
cacheLRU.Remove(el)
cacheStore.Delete(el.Value.(cacheRecord).key)
}
}

return
Expand Down
10 changes: 8 additions & 2 deletions server/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -1563,6 +1563,7 @@ func esmRouter(db DB, buildStorage storage.Storage) rex.Handle {
}
}

BUILD:
buildCtx := &BuildContext{
npmrc: npmrc,
db: db,
Expand Down Expand Up @@ -1636,7 +1637,6 @@ func esmRouter(db DB, buildStorage storage.Storage) rex.Handle {

// if the path is `ESMBuild`, return the built js/css content
if pathKind == EsmBuild {
// if the build
if esm.SubPath != buildCtx.esm.SubPath {
buf, recycle := NewBuffer()
defer recycle()
Expand All @@ -1656,7 +1656,13 @@ func esmRouter(db DB, buildStorage storage.Storage) rex.Handle {
f, fi, err := buildStorage.Get(savePath)
if err != nil {
if err == storage.ErrNotFound {
return rex.Status(404, "File not found")
// seem the build file is non-exist in the storage
// let's remove the build meta from the database and clear the cache
// then re-build the module
key := npmrc.zoneId + ":" + buildCtx.Path()
db.Delete(key)
cacheStore.Delete("lru:" + key)
goto BUILD
}
return rex.Status(500, err.Error())
}
Expand Down
2 changes: 1 addition & 1 deletion server/storage/storage_s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ func (s3 *s3Storage) sign(req *http.Request) {
canonicalHeaders[i] = key + ":" + strings.Join(req.Header.Values(key), ",")
}
}
canonicalRequest := strings.Join([]string{req.Method, escapePath(req.URL.EscapedPath()), req.URL.Query().Encode(), strings.Join(canonicalHeaders, "\n") + "\n", strings.Join(signedHeaders, ";"), req.Header.Get("X-Amz-Content-Sha256")}, "\n")
canonicalRequest := strings.Join([]string{req.Method, escapePath(req.URL.Path), req.URL.Query().Encode(), strings.Join(canonicalHeaders, "\n") + "\n", strings.Join(signedHeaders, ";"), req.Header.Get("X-Amz-Content-Sha256")}, "\n")
stringToSign := strings.Join([]string{"AWS4-HMAC-SHA256", datetime, scope, toHex(sha256Sum(canonicalRequest))}, "\n")
signingKey := hmacSum(hmacSum(hmacSum(hmacSum([]byte("AWS4"+s3.secretAccessKey), date), s3.region), "s3"), "aws4_request")
signature := hmacSum(signingKey, stringToSign)
Expand Down
10 changes: 9 additions & 1 deletion server/storage/storage_s3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,16 @@ func TestS3Storage(t *testing.T) {
if err != nil {
t.Fatal(err)
}
err = s3.Put("test/%23/hello.txt", bytes.NewReader([]byte("Hello, world!")))
if err != nil {
t.Fatal(err)
}

keys, err := s3.List("test/")
if err != nil {
t.Fatal(err)
}
if len(keys) != 2 {
if len(keys) != 3 {
t.Fatalf("invalid keys length(%d), expected 2", len(keys))
}

Expand Down Expand Up @@ -102,6 +106,10 @@ func TestS3Storage(t *testing.T) {
if err != nil {
t.Fatal(err)
}
err = s3.Delete("test/%23/hello.txt")
if err != nil {
t.Fatal(err)
}

keys, err = s3.List("test/")
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion test/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ if (import.meta.main) {
try {
console.log("Cleaning up...");
await Promise.all([
Deno.remove(".esmd/log", { recursive: true }),
Deno.remove(".esmd/esm.db"),
Deno.remove(".esmd/storage", { recursive: true }),
]);
} catch (_) {
Expand Down

0 comments on commit 56bad85

Please sign in to comment.