Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions cmd/oceanmedia/playlist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (
"github.com/ausocean/cloud/gauth"
"github.com/ausocean/cloud/model"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/utils"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -221,6 +222,25 @@ func TestServerSignURLCache(t *testing.T) {
assert.Equal(t, 2, cs.callCount())
}

// TestServerSignURLCacheDoesNotAliasUnsafeKeys repeatedly passes a zero-copy
// string to the cache while mutating its backing buffer, as Fiber does when it
// reuses a request buffer. Every URI is distinct, so every call must sign a new
// URL rather than hitting an entry whose key was mutated to look like it.
func TestServerSignURLCacheDoesNotAliasUnsafeKeys(t *testing.T) {
cs := &countingSigner{}
s := newServer(nil, cs, testJWTSecret, time.Hour)

const n = 4096
buf := make([]byte, len("https://example.com/bucket/00000000.ts"))
for i := 0; i < n; i++ {
copy(buf, fmt.Sprintf("https://example.com/bucket/%08x.ts", i))
_, err := s.signURL(context.Background(), utils.UnsafeString(buf))
require.NoError(t, err)
}

assert.Equal(t, n, cs.callCount())
}

func newTestApp(s *server) *fiber.App {
app := fiber.New()
app.Get("/segment", s.handleSegment)
Expand Down
6 changes: 6 additions & 0 deletions cmd/oceanmedia/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ package main
import (
"context"
"log"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -109,6 +110,11 @@ func newServer(store datastore.Store, signer urlSigner, jwtSecret []byte, signed
// signURL returns a pre-signed URL for the given storage URI, caching the
// result until half the signed URL time to live has elapsed.
func (s *server) signURL(ctx context.Context, rawURI string) (string, error) {
// rawURI may be a zero-copy string owned by the request context (for
// example fiber.Ctx.Query). It is used as a map key, which outlives the
// request, so copy it first. Otherwise the request buffer can be reused
// and mutate the key, causing every lookup to hit the same cached URL.
rawURI = strings.Clone(rawURI)
now := s.now()

s.signMu.Lock()
Expand Down
Loading