Skip to content

Commit de221e4

Browse files
committed
fix: max file size not propagated all the way through
1 parent b568c2e commit de221e4

7 files changed

Lines changed: 67 additions & 18 deletions

File tree

.github/workflows/main.yaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,9 @@ jobs:
8383
targets: |
8484
[
8585
{ "target": { "kind": "tenant", "tenant": "nav", "environment": "dev" }, "wait": true },
86-
{ "target": { "kind": "tenant", "tenant": "nav" } },
86+
{ "target": { "kind": "tenant", "tenant": "nav" } },
8787
{ "target": { "kind": "tenant", "tenant": "ldir" } },
8888
{ "target": { "kind": "tenant", "tenant": "atil" } },
89-
{ "target": { "kind": "onprem" } }
89+
{ "target": { "kind": "onprem", "tenant": "nav", "environment": "dev-fss" }, "wait": true },
90+
{ "target": { "kind": "onprem", "tenant": "nav", "environment": "prod-fss" } }
9091
]

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ make build
111111
- `-log-level` - Log level (default: `info`)
112112
- `-timeout` - Client connection timeout in seconds (default: `3`)
113113
- `-keepalive` - Client connection keepalive (default: `3`)
114-
-
114+
- `-max-file-size` - Max upload size accepted by scan endpoints (default: `400Mi`, accepts units like `Mi`, `M`, `Gi`)
115115
### Test
116116

117117
```bash

charts/Feature.yaml

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,4 @@ values:
66
displayName: Max file size allowed
77
description: Max file size allowed for scanning (should be the same value as in clamd.conf)
88
config:
9-
type: string
10-
replaceLegacyService:
11-
displayName: Use the original service name
12-
description: Temporary switch to ease migration to new rest api, set this to enable the new api
13-
config:
14-
type: bool
15-
9+
type: string

charts/templates/deployment.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ spec:
2929
env:
3030
- name: DAEMON_ENDPOINT
3131
value: clamav-daemon:3310
32+
- name: MAX_FILE_SIZE
33+
value: {{ .Values.maxFileSize | quote }}
3234
image: {{ .Values.image.repository }}:{{ .Values.image.tag }}
3335
imagePullPolicy: {{ .Values.image.pullPolicy }}
3436
livenessProbe:

charts/templates/service.yaml

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
apiVersion: v1
22
kind: Service
33
metadata:
4-
{{- if .Values.replaceLegacyService }}
5-
# use the old name of rest api to avoid changes in team applications
6-
name: clamav
7-
{{- else }}
8-
name: {{ include "clamav-rest.fullname" . | nindent 4 }}
9-
{{- end }}
4+
name: clamav
105
labels:
116
app: clamav
127
{{- include "clamav-rest.labels" . | nindent 4 }}

charts/values.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ image:
22
tag: "1.0.0"
33
repository: europe-north1-docker.pkg.dev/nais-io/nais/images/clamav-rest
44
pullPolicy: IfNotPresent
5-
maxFileSize: 400
5+
maxFileSize: "400Mi"
66

77
containerSecurityContext:
88
capabilities:

cmd/clamav-rest/config.go

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ package main
33
import (
44
"errors"
55
"flag"
6+
"fmt"
7+
"math"
68
"os"
9+
"strconv"
10+
"strings"
711
"time"
812
)
913

@@ -29,26 +33,79 @@ func LoadConfig() (*Config, error) {
2933
cfg.ServerReadTimeout = 120 * time.Second
3034
cfg.ServerReadHeaderTimeout = 10 * time.Second
3135
cfg.ServerWriteTimeout = 120 * time.Second
32-
cfg.ServerMaxRequestSize = int64(400 * 1024 * 1024)
36+
maxFileSize := "400Mi"
3337

3438
if val, ok := os.LookupEnv("DAEMON_ENDPOINT"); ok {
3539
cfg.DaemonEndpoint = val
3640
}
41+
if val, ok := os.LookupEnv("MAX_FILE_SIZE"); ok {
42+
maxFileSize = val
43+
}
3744

3845
// Override with flags
3946
flag.StringVar(&cfg.BindAddress, "bind-address", cfg.BindAddress, "Bind address")
4047
flag.StringVar(&cfg.LogLevel, "log-level", cfg.LogLevel, "Log level")
4148
flag.StringVar(&cfg.DaemonEndpoint, "daemon-endpoint", cfg.DaemonEndpoint, "ClamAV daemon endpoint")
49+
flag.StringVar(&maxFileSize, "max-file-size", maxFileSize, "Maximum file size accepted by /scan (e.g. 400Mi, 400M)")
4250
timeout := flag.Int("timeout", int(cfg.Timeout.Seconds()), "Timeout in seconds")
4351
keepalive := flag.Int("keepalive", int(cfg.Keepalive.Seconds()), "Keepalive in seconds")
4452
flag.Parse()
4553

4654
cfg.Timeout = time.Duration(*timeout) * time.Second
4755
cfg.Keepalive = time.Duration(*keepalive) * time.Second
56+
var err error
57+
cfg.ServerMaxRequestSize, err = parseByteSize(maxFileSize)
58+
if err != nil {
59+
return nil, fmt.Errorf("invalid max file size %q: %w", maxFileSize, err)
60+
}
4861

4962
if cfg.DaemonEndpoint == "" {
5063
return nil, errors.New("daemon endpoint is required")
5164
}
5265

5366
return cfg, nil
5467
}
68+
69+
func parseByteSize(value string) (int64, error) {
70+
normalized := strings.ToUpper(strings.TrimSpace(value))
71+
if normalized == "" {
72+
return 0, errors.New("value cannot be empty")
73+
}
74+
if strings.HasSuffix(normalized, "B") {
75+
normalized = strings.TrimSuffix(normalized, "B")
76+
}
77+
78+
multiplier := int64(1)
79+
for _, unit := range []struct {
80+
suffix string
81+
multiplier int64
82+
}{
83+
{suffix: "TI", multiplier: 1024 * 1024 * 1024 * 1024},
84+
{suffix: "GI", multiplier: 1024 * 1024 * 1024},
85+
{suffix: "MI", multiplier: 1024 * 1024},
86+
{suffix: "KI", multiplier: 1024},
87+
{suffix: "T", multiplier: 1000 * 1000 * 1000 * 1000},
88+
{suffix: "G", multiplier: 1000 * 1000 * 1000},
89+
{suffix: "M", multiplier: 1000 * 1000},
90+
{suffix: "K", multiplier: 1000},
91+
} {
92+
if strings.HasSuffix(normalized, unit.suffix) {
93+
multiplier = unit.multiplier
94+
normalized = strings.TrimSuffix(normalized, unit.suffix)
95+
break
96+
}
97+
}
98+
99+
size, err := strconv.ParseInt(normalized, 10, 64)
100+
if err != nil {
101+
return 0, fmt.Errorf("invalid numeric value %q", normalized)
102+
}
103+
if size < 0 {
104+
return 0, errors.New("value cannot be negative")
105+
}
106+
if size > math.MaxInt64/multiplier {
107+
return 0, errors.New("value is too large")
108+
}
109+
110+
return size * multiplier, nil
111+
}

0 commit comments

Comments
 (0)