Skip to content

Commit 30f744e

Browse files
committed
Try to fix path if signature is invalid
1 parent 835ddbc commit 30f744e

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
- Add [raw](https://docs.imgproxy.net/latest/generating_the_url?id=raw) processing option.
66
- (pro) Add encrypted source URL support.
77

8+
### Changed
9+
- Fix some invalid signature cases that happen because of URL normalization.
10+
811
## [3.7.2] - 2022-08-22
912
### Changed
1013
- (docker) Faster images quantization.

fix_path.go

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"regexp"
6+
"strings"
7+
)
8+
9+
var fixPathRe = regexp.MustCompile(`/plain/(\S+)\:/([^/])`)
10+
11+
func fixPath(path string) string {
12+
for _, match := range fixPathRe.FindAllStringSubmatch(path, -1) {
13+
repl := fmt.Sprintf("/plain/%s://", match[1])
14+
if match[1] == "local" {
15+
repl += "/"
16+
}
17+
repl += match[2]
18+
path = strings.Replace(path, match[0], repl, 1)
19+
}
20+
21+
return path
22+
}

processing_handler.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,13 @@ func handleProcessing(reqID string, rw http.ResponseWriter, r *http.Request) {
218218
}
219219

220220
if err := security.VerifySignature(signature, path); err != nil {
221-
sendErrAndPanic(ctx, "security", ierrors.New(403, err.Error(), "Forbidden"))
221+
// Some proxy servers may normalize URL and make signature invalid.
222+
// Try to fix the path and repeat the check
223+
path = fixPath(path)
224+
225+
if err = security.VerifySignature(signature, path); err != nil {
226+
sendErrAndPanic(ctx, "security", ierrors.New(403, err.Error(), "Forbidden"))
227+
}
222228
}
223229

224230
po, imageURL, err := options.ParsePath(path, r.Header)

0 commit comments

Comments
 (0)