Skip to content
Merged
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
32 changes: 32 additions & 0 deletions appengine/endpoints/sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"io/ioutil"
"net/http"
"os"
"path"
"regexp"
"strings"
"time"
Expand Down Expand Up @@ -213,6 +214,37 @@ func validSignRequest(ctx context.Context, sr models.SignRequest) error {
return errors.New("sign request path cannot be empty")
}

rawPath := strings.TrimPrefix(sr.Path, "/")
cleanedPath := path.Clean(rawPath)
if strings.Contains(sr.Path, "..") {
return fmt.Errorf("sign request path %q contains invalid path traversal sequences", sr.Path)
}

envPrefixes := os.Getenv("SIGNED_URL_PATH_PREFIXES")
if envPrefixes == "" {
return errors.New("SIGNED_URL_PATH_PREFIXES environment variable is not configured")
}

allowedPrefixes := strings.Split(envPrefixes, ",")
allowed := false
for _, p := range allowedPrefixes {
p = strings.TrimSpace(p)
if p == "" {
continue
}
cleanedPrefix := path.Clean(strings.TrimPrefix(p, "/"))
if !strings.HasSuffix(cleanedPrefix, "/") {
cleanedPrefix += "/"
}
if strings.HasPrefix(cleanedPath, cleanedPrefix) || cleanedPath == strings.TrimSuffix(cleanedPrefix, "/") {
allowed = true
break
}
}
if !allowed {
return fmt.Errorf("sign request path %q is not under an allowed prefix", sr.Path)
}

return nil
}

Expand Down
Loading