Skip to content

Commit 7f386ea

Browse files
authored
Use a more specific error for http source rejections (#415)
* use a more specific error for http source rejections * version bump * update unit tests
1 parent cf1590e commit 7f386ea

File tree

4 files changed

+19
-15
lines changed

4 files changed

+19
-15
lines changed

errors.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ import (
44
"context"
55
"errors"
66
"fmt"
7-
"github.com/cshum/imagor/imagorpath"
87
"net/http"
98
"regexp"
109
"strconv"
1110
"strings"
11+
12+
"github.com/cshum/imagor/imagorpath"
1213
)
1314

1415
var (
@@ -18,6 +19,8 @@ var (
1819
ErrInvalid = NewError("invalid", http.StatusBadRequest)
1920
// ErrMethodNotAllowed method not allowed error
2021
ErrMethodNotAllowed = NewError("method not allowed", http.StatusMethodNotAllowed)
22+
// ErrSourceNotAllowed http source not allowed error
23+
ErrSourceNotAllowed = NewError("http source not allowed", http.StatusForbidden)
2124
// ErrSignatureMismatch URL signature mismatch error
2225
ErrSignatureMismatch = NewError("url signature mismatch", http.StatusForbidden)
2326
// ErrTimeout timeout error

imagor.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@ import (
55
"encoding/json"
66
"errors"
77
"fmt"
8-
"github.com/cshum/imagor/imagorpath"
9-
"go.uber.org/zap"
10-
"golang.org/x/sync/semaphore"
11-
"golang.org/x/sync/singleflight"
128
"io"
139
"net/http"
1410
"net/url"
@@ -18,10 +14,15 @@ import (
1814
"strings"
1915
"sync"
2016
"time"
17+
18+
"github.com/cshum/imagor/imagorpath"
19+
"go.uber.org/zap"
20+
"golang.org/x/sync/semaphore"
21+
"golang.org/x/sync/singleflight"
2122
)
2223

2324
// Version imagor version
24-
const Version = "1.4.9"
25+
const Version = "1.4.10"
2526

2627
// Loader image loader interface
2728
type Loader interface {

loader/httploader/httploader.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ func (h *HTTPLoader) Get(r *http.Request, image string) (*imagor.Blob, error) {
158158
u.Fragment = ""
159159

160160
if !isURLAllowed(u, h.AllowedSources) {
161-
return nil, imagor.ErrInvalid
161+
return nil, imagor.ErrSourceNotAllowed
162162
}
163163
client := &http.Client{
164164
Transport: h.Transport,
@@ -256,7 +256,7 @@ func (h *HTTPLoader) checkRedirect(r *http.Request, via []*http.Request) error {
256256
return errors.New("stopped after 10 redirects")
257257
}
258258
if !isURLAllowed(r.URL, h.AllowedSources) {
259-
return imagor.ErrInvalid
259+
return imagor.ErrSourceNotAllowed
260260
}
261261
return nil
262262
}

loader/httploader/httploader_test.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -108,17 +108,17 @@ func TestWithAllowedSources(t *testing.T) {
108108
{
109109
name: "not allowed source",
110110
target: "https://foo.boo/boooo",
111-
err: "imagor: 400 invalid",
111+
err: "imagor: 403 http source not allowed",
112112
},
113113
{
114114
name: "not allowed source",
115115
target: "https://foo.barr/baz",
116-
err: "imagor: 400 invalid",
116+
err: "imagor: 403 http source not allowed",
117117
},
118118
{
119119
name: "not allowed source",
120120
target: "https://boo.bar/baz",
121-
err: "imagor: 400 invalid",
121+
err: "imagor: 403 http source not allowed",
122122
},
123123
{
124124
name: "csv allowed source",
@@ -163,17 +163,17 @@ func TestWithAllowedSourceRegexp(t *testing.T) {
163163
{
164164
name: "not allowed source",
165165
target: "https://goo2.org/https://goo.org/image.png",
166-
err: "imagor: 400 invalid",
166+
err: "imagor: 403 http source not allowed",
167167
},
168168
{
169169
name: "not allowed source",
170170
target: "https://foo.com/dogs/../cats/cat.jpg",
171-
err: "imagor: 400 invalid",
171+
err: "imagor: 403 http source not allowed",
172172
},
173173
{
174174
name: "not allowed source",
175175
target: "https://foo.com/dogs/dog.jpg?size=small",
176-
err: "imagor: 400 invalid",
176+
err: "imagor: 403 http source not allowed",
177177
},
178178
})
179179
}
@@ -203,7 +203,7 @@ func TestWithAllowedSourcesRedirect(t *testing.T) {
203203

204204
b, err := blob.ReadAll()
205205
assert.Empty(t, b)
206-
assert.ErrorIs(t, err, imagor.ErrInvalid)
206+
assert.ErrorIs(t, err, imagor.ErrSourceNotAllowed)
207207
})
208208

209209
t.Run("Allowed redirect", func(t *testing.T) {

0 commit comments

Comments
 (0)