Skip to content

Commit 7cb2aba

Browse files
fix(qryptinvite): validate quota and TTL env values (#93)
Co-authored-by: rissrice2105-agent <rissrice2105-agent@users.noreply.github.com>
1 parent db641c7 commit 7cb2aba

2 files changed

Lines changed: 50 additions & 2 deletions

File tree

internal/qryptinvite/config.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ func ConfigFromEnv() Config {
4545
c.RedeemURL = v
4646
}
4747
if v := os.Getenv("AGENTBBS_QRYPT_INVITE_TTL"); v != "" {
48-
if d, err := time.ParseDuration(v); err == nil {
48+
if d, err := time.ParseDuration(v); err == nil && d > 0 {
4949
c.TTL = d
5050
}
5151
}
5252
if v := os.Getenv("AGENTBBS_QRYPT_INVITE_QUOTA"); v != "" {
53-
if n, err := strconv.Atoi(v); err == nil {
53+
if n, err := strconv.Atoi(v); err == nil && n >= 0 {
5454
c.Quota = n
5555
}
5656
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package qryptinvite
2+
3+
import (
4+
"strconv"
5+
"testing"
6+
"time"
7+
)
8+
9+
func TestConfigFromEnvRejectsNonPositiveTTL(t *testing.T) {
10+
for _, value := range []string{"0s", "-1h"} {
11+
t.Run(value, func(t *testing.T) {
12+
t.Setenv("AGENTBBS_QRYPT_INVITE_TTL", value)
13+
14+
if got := ConfigFromEnv().TTL; got != DefaultTTL {
15+
t.Fatalf("TTL = %s, want default %s", got, DefaultTTL)
16+
}
17+
})
18+
}
19+
}
20+
21+
func TestConfigFromEnvQuotaValidation(t *testing.T) {
22+
tests := []struct {
23+
value string
24+
want int
25+
}{
26+
{value: "-1", want: DefaultQuota},
27+
{value: "0", want: 0},
28+
{value: "12", want: 12},
29+
}
30+
31+
for _, tt := range tests {
32+
t.Run(strconv.Quote(tt.value), func(t *testing.T) {
33+
t.Setenv("AGENTBBS_QRYPT_INVITE_QUOTA", tt.value)
34+
35+
if got := ConfigFromEnv().Quota; got != tt.want {
36+
t.Fatalf("Quota = %d, want %d", got, tt.want)
37+
}
38+
})
39+
}
40+
}
41+
42+
func TestConfigFromEnvAcceptsPositiveTTL(t *testing.T) {
43+
t.Setenv("AGENTBBS_QRYPT_INVITE_TTL", "24h")
44+
45+
if got := ConfigFromEnv().TTL; got != 24*time.Hour {
46+
t.Fatalf("TTL = %s, want %s", got, 24*time.Hour)
47+
}
48+
}

0 commit comments

Comments
 (0)