Skip to content

Commit cde8bea

Browse files
committed
fix(ingress): only reject email addresses that are unsafe as a path
Signed-off-by: Kevin Wang <wy721@qq.com>
1 parent f449c90 commit cde8bea

3 files changed

Lines changed: 18 additions & 2 deletions

File tree

‎custom-domain/dstack-ingress/scripts/entrypoint.sh‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ DNS_SETUP_MODE=${DNS_SETUP_MODE:-wait}
4949
# Normalising here rather than per mode is what fixes the real bug: ACME_STAGING
5050
# used to be read only on the tls-alpn-01 path, so setting it under dns-01
5151
# silently issued *production* certificates.
52+
ACME_EMAIL_VAR=ACME_EMAIL
53+
[ -n "${ACME_EMAIL:-}" ] || ACME_EMAIL_VAR=CERTBOT_EMAIL
5254
ACME_EMAIL=${ACME_EMAIL:-${CERTBOT_EMAIL:-}}
5355
ACME_STAGING=${ACME_STAGING:-${CERTBOT_STAGING:-false}}
5456

@@ -87,7 +89,7 @@ if [ -z "${ROUTING_MAP:-}" ] && [ -z "$TARGET_ENDPOINT" ]; then
8789
echo "Error: nothing to route to; set TARGET_ENDPOINT or ROUTING_MAP" >&2
8890
exit 1
8991
fi
90-
if [ -n "$ACME_EMAIL" ] && ! ACME_EMAIL=$(sanitize_email "$ACME_EMAIL"); then
92+
if [ -n "$ACME_EMAIL" ] && ! ACME_EMAIL=$(sanitize_email "$ACME_EMAIL" "$ACME_EMAIL_VAR"); then
9193
exit 1
9294
fi
9395
if ! TXT_PREFIX=$(sanitize_dns_label "$TXT_PREFIX"); then

‎custom-domain/dstack-ingress/scripts/functions.sh‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,10 @@ sanitize_nonnegative_integer() {
129129
sanitize_email() {
130130
local candidate="$1"
131131
local name="${2:-ACME_EMAIL}"
132-
if [[ "$candidate" =~ ^[A-Za-z0-9._%+-]+@[A-Za-z0-9]([A-Za-z0-9-]*[A-Za-z0-9])?(\.[A-Za-z0-9]([A-Za-z0-9-]*[A-Za-z0-9])?)+$ ]]; then
132+
# Only what keeps it a safe directory name for lego and certbot: one @
133+
# with something on both sides, no /, whitespace or control characters,
134+
# and no leading dot.
135+
if [[ "$candidate" =~ ^[^@./[:space:][:cntrl:]][^@/[:space:][:cntrl:]]*@[^@/[:space:][:cntrl:]]+$ ]]; then
133136
echo "$candidate"
134137
else
135138
echo "Error: Invalid ${name} value: $candidate" >&2

‎custom-domain/dstack-ingress/scripts/tests/test_sanitizers.sh‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ assert_equal "$(sanitize_positive_integer 08 INTERVAL)" "8" "sanitize_positive_i
5050
assert_equal "$(sanitize_nonnegative_integer 0 WAIT)" "0" "sanitize_nonnegative_integer accepts 0"
5151
assert_equal "$(sanitize_nonnegative_integer 120 WAIT)" "120" "sanitize_nonnegative_integer accepts 120"
5252
assert_equal "$(sanitize_email ops+acme@mail.example.com)" "ops+acme@mail.example.com" "sanitize_email accepts an address"
53+
assert_equal "$(sanitize_email "o'brien@example.com")" "o'brien@example.com" "sanitize_email accepts an apostrophe"
54+
assert_equal "$(sanitize_email ops@my_host.example.com)" "ops@my_host.example.com" "sanitize_email accepts _ in the domain"
55+
assert_equal "$(sanitize_email user@localhost)" "user@localhost" "sanitize_email accepts a single-label domain"
5356
assert_equal "$(sanitize_domain example.com)" "example.com" "sanitize_domain accepts fqdn"
5457
assert_equal "$(sanitize_domain '*.example.com')" "*.example.com" "sanitize_domain accepts wildcard"
5558
assert_equal "$(sanitize_target_endpoint http://service:80/path)" "http://service:80/path" "sanitize_target_endpoint accepts http"
@@ -99,6 +102,14 @@ assert_fails "sanitize_nonnegative_integer rejects a suffix" sanitize_nonnegativ
99102
assert_fails "sanitize_email rejects path traversal" sanitize_email ../../../../evidences/x@x.com
100103
assert_fails "sanitize_email rejects a missing domain" sanitize_email ops@
101104
assert_fails "sanitize_email rejects newlines" sanitize_email $'ops@example.com\nx'
105+
assert_fails "sanitize_email rejects a missing local part" sanitize_email @example.com
106+
assert_fails "sanitize_email rejects two @" sanitize_email a@b@example.com
107+
assert_fails "sanitize_email rejects a leading dot" sanitize_email ..@example.com
108+
assert_fails "sanitize_email rejects a slash in the domain" sanitize_email ops@example.com/..
109+
assert_fails "sanitize_email rejects spaces" sanitize_email 'ops @example.com'
110+
assert_fails "sanitize_email rejects tabs" sanitize_email $'ops@example.com\t'
111+
assert_fails "sanitize_email rejects control characters" sanitize_email $'ops@exa\x01mple.com'
112+
assert_equal "$(sanitize_email bad CERTBOT_EMAIL 2>&1)" "Error: Invalid CERTBOT_EMAIL value: bad" "sanitize_email names the variable"
102113
assert_equal "$(check_distinct_ports PORT=443 "EVIDENCE_PORT=$(sanitize_port 0443)" || echo clash)" "clash" "check_distinct_ports sees a normalized port clash"
103114
assert_fails "check_distinct_ports rejects a shared port" check_distinct_ports PORT=443 TLSALPN_PORT=9443 EVIDENCE_PORT=443
104115
assert_fails "sanitize_domain rejects invalid domain" sanitize_domain bad_domain

0 commit comments

Comments
 (0)