Skip to content

Commit 26a045a

Browse files
authored
Merge pull request #10 from quix-labs/feature/security_addon
Add security configuration and error action configuration
2 parents f9f0005 + 4cff0e9 commit 26a045a

File tree

9 files changed

+632
-289
lines changed

9 files changed

+632
-289
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ caddy
33
.idea
44
out
55
dist/
6-
build/
6+
build/
7+
test-dataset/

Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,14 @@ all: clean test build
1616
build:
1717
$(XCADDY) build --output $(OUT_DIR)/$(BINARY_NAME) --with $(MODULE_PATH)=./
1818
chmod u+x $(OUT_DIR)/$(BINARY_NAME)
19+
20+
setcap:
21+
setcap 'cap_net_bind_service=+ep' $(OUT_DIR)/$(BINARY_NAME)
22+
1923
test:
2024
go test -v ./...
2125
clean:
2226
rm -rf $(OUT_DIR)
2327
run:
24-
$(XCADDY) run
28+
XCADDY_SETCAP=1 $(XCADDY) run
2529
./$(OUT_DIR)/$(BINARY_NAME)

README.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ the [official documentation for caddy](https://caddyserver.com/docs/build#packag
7272
## Example Caddyfile
7373

7474
### Using file_server
75+
7576
```plaintext
7677
localhost {
7778
root /your-images-directory
@@ -81,6 +82,7 @@ localhost {
8182
```
8283

8384
### Using reverse_proxy
85+
8486
```plaintext
8587
localhost {
8688
reverse_proxy your-domain.com
@@ -142,6 +144,53 @@ caddy.
142144
* Convert an image to AVIF format with lossless compression:
143145
* http://example.com/image.jpg?fm=avif&ll=true
144146

147+
## Advanced Configuration
148+
149+
This configuration allows you to control error handling with `on_fail` and `on_security_fail`.
150+
151+
You can also manage query parameter processing using `allowed_params` and `disallowed_params`.
152+
153+
This gives you fine-grained control over image processing in your Caddy server.
154+
155+
156+
### Example with `on_fail` and Security Configuration
157+
```plaintext
158+
localhost {
159+
image_processor {
160+
on_fail bypass # Default value
161+
security {
162+
on_security_fail ignore # Default value
163+
164+
disallowed_params w r ... # These parameters are disallowed in the image processing request. You can also use allowed_params to restrict parameters further.
165+
# Note: 'allowed_params' and 'disallowed_params' cannot be used together. You must choose one or the other.
166+
}
167+
}
168+
}
169+
```
170+
171+
### Explanation:
172+
173+
* `on_fail`:
174+
* `bypass` (default value): If any error occurs, the original, unprocessed image will be returned.
175+
* `abort`: If an error occurs, a 500 Internal Server Error response will be returned.
176+
177+
178+
* `on_security_fail`:
179+
* `ignore` (default value): If any security checks fail, they are ignored, and the image processing continues.
180+
* `bypass`: If any security checks fail, the original, unprocessed image will be returned.
181+
* `abort`: If any security checks fail, a 400 Bad Request response will be returned.
182+
183+
184+
* **Security Configuration** (`disallowed_params` vs `allowed_params`):
185+
* `disallowed_params`: Specifies which query parameters are not allowed.
186+
187+
For example, parameters like w (width) and r (rotation) can be restricted.
188+
189+
* `allowed_params`: Specify which query parameters are allowed. As an alternative to `disallowed_params`.
190+
191+
* **Important**: You cannot use both allowed_params and disallowed_params in the same configuration.
192+
193+
145194
## Planned Features
146195

147196
The following features are planned for future implementation:

errors.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package CADDY_FILE_SERVER
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
)
7+
8+
type AbortRequestError struct {
9+
Msg string
10+
}
11+
12+
func (e *AbortRequestError) Error() string {
13+
return fmt.Sprintf("request aborted: %s", e.Msg)
14+
}
15+
16+
var BypassRequestError = errors.New("bypass request")

go.mod

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ toolchain go1.22.2
77
require (
88
github.com/caddyserver/caddy/v2 v2.8.4
99
github.com/h2non/bimg v1.1.9
10+
github.com/klauspost/compress v1.17.11
11+
go.uber.org/zap v1.27.0
1012
)
1113

1214
require (
@@ -53,7 +55,6 @@ require (
5355
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
5456
github.com/jackc/pgtype v1.14.0 // indirect
5557
github.com/jackc/pgx/v4 v4.18.3 // indirect
56-
github.com/klauspost/compress v1.17.8 // indirect
5758
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
5859
github.com/libdns/libdns v0.2.2 // indirect
5960
github.com/manifoldco/promptui v0.9.0 // indirect
@@ -73,6 +74,7 @@ require (
7374
github.com/prometheus/procfs v0.12.0 // indirect
7475
github.com/quic-go/qpack v0.4.0 // indirect
7576
github.com/quic-go/quic-go v0.44.0 // indirect
77+
github.com/rs/xid v1.5.0 // indirect
7678
github.com/russross/blackfriday/v2 v2.1.0 // indirect
7779
github.com/shopspring/decimal v1.2.0 // indirect
7880
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
@@ -96,7 +98,6 @@ require (
9698
go.uber.org/automaxprocs v1.5.3 // indirect
9799
go.uber.org/mock v0.4.0 // indirect
98100
go.uber.org/multierr v1.11.0 // indirect
99-
go.uber.org/zap v1.27.0 // indirect
100101
go.uber.org/zap/exp v0.2.0 // indirect
101102
golang.org/x/crypto v0.23.0 // indirect
102103
golang.org/x/crypto/x509roots/fallback v0.0.0-20240507223354-67b13616a595 // indirect

0 commit comments

Comments
 (0)