Skip to content

Commit 9c2c816

Browse files
authored
hrw4u: add validation for some boolean values (#12553)
When the expected value is a boolean, we should make sure it actually is one of the accepted inputs. Its also generally confusing if they should be quoted or not, so try to give the user some indication that quotes are bad for these values.
1 parent 4b5d426 commit 9c2c816

12 files changed

+76
-3
lines changed

tools/hrw4u/src/symbols.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ def resolve_assignment(self, name: str, value: str, section: SectionType | None
7373
if validator:
7474
validator(qualifier)
7575

76+
# Add boolean value validation for http.cntl assignments.
77+
if op_key == "http.cntl.":
78+
types.SuffixGroup.BOOL_FIELDS.validate(value)
79+
7680
if isinstance(commands, (list, tuple)):
7781
if value == '""':
7882
return f"{commands[0]} {qualifier}"

tools/hrw4u/src/types.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,21 @@ class SuffixGroup(Enum):
9999
def validate(self, suffix: str) -> None:
100100
allowed_upper = {val.upper() for val in self.value}
101101
if suffix.upper() not in allowed_upper:
102-
raise ValueError(
103-
f"Invalid suffix '{suffix}' for group '{self.name}'. "
104-
f"Must be one of: {', '.join(sorted(self.value))}")
102+
# Special handling for BOOL_FIELDS to detect quoted boolean values
103+
if self == SuffixGroup.BOOL_FIELDS:
104+
bool_msg = f"Invalid boolean value '{suffix}'. Must be one of: {', '.join(sorted(self.value))}"
105+
106+
# Check if this is a quoted boolean value
107+
is_double_quoted = (suffix.startswith('"') and suffix.endswith('"'))
108+
is_single_quoted = (suffix.startswith("'") and suffix.endswith("'"))
109+
if ((is_double_quoted or is_single_quoted) and len(suffix) > 2 and suffix[1:-1].upper() in allowed_upper):
110+
raise ValueError(f"{bool_msg} and must not be quoted")
111+
else:
112+
raise ValueError(bool_msg)
113+
else:
114+
raise ValueError(
115+
f"Invalid suffix '{suffix}' for group '{self.name}'. "
116+
f"Must be one of: {', '.join(sorted(self.value))}")
105117

106118

107119
class VarType(Enum):
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Invalid boolean value 'invalid_value'. Must be one of: 0, 1, FALSE, NO, OFF, ON, TRUE, YES
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
SEND_RESPONSE {
2+
http.cntl.LOGGING = invalid_value;
3+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Invalid boolean value '"true"'. Must be one of: 0, 1, FALSE, NO, OFF, ON, TRUE, YES and must not be quoted
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
SEND_RESPONSE {
2+
http.cntl.LOGGING = "true";
3+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
(program (section SEND_RESPONSE { (sectionBody (statement http.cntl.LOGGING = (value TRUE) ;)) (sectionBody (statement http.cntl.TXN_DEBUG = (value FALSE) ;)) (sectionBody (statement http.cntl.REQ_CACHEABLE = (value YES) ;)) (sectionBody (statement http.cntl.RESP_CACHEABLE = (value NO) ;)) (sectionBody (statement http.cntl.SERVER_NO_STORE = (value ON) ;)) (sectionBody (statement http.cntl.SKIP_REMAP = (value OFF) ;)) (sectionBody (statement http.cntl.INTERCEPT_RETRY = (value 1) ;)) (sectionBody (statement http.cntl.LOGGING = (value 0) ;)) (sectionBody (statement http.cntl.TXN_DEBUG = (value true) ;)) (sectionBody (statement http.cntl.REQ_CACHEABLE = (value false) ;)) (sectionBody (statement http.cntl.RESP_CACHEABLE = (value yes) ;)) (sectionBody (statement http.cntl.SERVER_NO_STORE = (value no) ;)) (sectionBody (statement http.cntl.SKIP_REMAP = (value on) ;)) (sectionBody (statement http.cntl.INTERCEPT_RETRY = (value off) ;)) (sectionBody (statement http.cntl.LOGGING = (value True) ;)) (sectionBody (statement http.cntl.TXN_DEBUG = (value False) ;)) (sectionBody (statement http.cntl.LOGGING = (value TRue) ;)) }) <EOF>)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
SEND_RESPONSE {
2+
http.cntl.LOGGING = TRUE;
3+
http.cntl.TXN_DEBUG = FALSE;
4+
http.cntl.REQ_CACHEABLE = YES;
5+
http.cntl.RESP_CACHEABLE = NO;
6+
http.cntl.SERVER_NO_STORE = ON;
7+
http.cntl.SKIP_REMAP = OFF;
8+
http.cntl.INTERCEPT_RETRY = 1;
9+
http.cntl.LOGGING = 0;
10+
http.cntl.TXN_DEBUG = true;
11+
http.cntl.REQ_CACHEABLE = false;
12+
http.cntl.RESP_CACHEABLE = yes;
13+
http.cntl.SERVER_NO_STORE = no;
14+
http.cntl.SKIP_REMAP = on;
15+
http.cntl.INTERCEPT_RETRY = off;
16+
http.cntl.LOGGING = True;
17+
http.cntl.TXN_DEBUG = False;
18+
http.cntl.LOGGING = TRue;
19+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
cond %{SEND_RESPONSE_HDR_HOOK} [AND]
2+
set-http-cntl LOGGING TRUE
3+
set-http-cntl TXN_DEBUG FALSE
4+
set-http-cntl REQ_CACHEABLE YES
5+
set-http-cntl RESP_CACHEABLE NO
6+
set-http-cntl SERVER_NO_STORE ON
7+
set-http-cntl SKIP_REMAP OFF
8+
set-http-cntl INTERCEPT_RETRY 1
9+
set-http-cntl LOGGING 0
10+
set-http-cntl TXN_DEBUG true
11+
set-http-cntl REQ_CACHEABLE false
12+
set-http-cntl RESP_CACHEABLE yes
13+
set-http-cntl SERVER_NO_STORE no
14+
set-http-cntl SKIP_REMAP on
15+
set-http-cntl INTERCEPT_RETRY off
16+
set-http-cntl LOGGING True
17+
set-http-cntl TXN_DEBUG False
18+
set-http-cntl LOGGING TRue
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Invalid boolean value '"true"'. Must be one of: 0, 1, FALSE, NO, OFF, ON, TRUE, YES and must not be quoted

0 commit comments

Comments
 (0)