The image now refuses a country code that is not a code

The country rule had been fixed in the source and proven in-process: a
two-character non-code refused with the rule quoted, lower case normalised,
DELETE removing the rule and invalidating the snapshot, and a delete of a rule
that was never written naming the code. On the shipped image the only country
assertion in the gate was still "PUT a real country, get 451" — so every one of
those sentences was a claim about a handler, not a measurement of the process
listening on 8080. The defect this ticket describes (a rule made of any two
characters, shown to the operator as a denial and enforced by nothing, with no
way back) was only ever caught by tests that never ran against the image.

The new scene measures it over a socket: four refused codes and the sentence
each one answers with, none of them leaving a row behind, a refused write not
disturbing the rule that is already there, lower case on the wire becoming the
upper-case rule rather than a second row, that rule being the one the engine
enforces (451), DELETE answering 204 and the next request being decided without
it (404), and a delete of a rule that was never written naming the code.

The scene caught its own bug on the first run: two of its reads of the refusal
message were issued without -X PUT, so they were GETs against a route that does
not exist and printed an empty body — while the status assertion on the same URL
would have passed, because it did carry the verb. A gate that checks a status in
one request and the reason in another is not checking the reason.

Gate numbers, stated as measured rather than as hoped. This scene is 19/19 in
both runs that carried it: /tmp/c25_compose2.log answered 180 passed / 4 failed
and /tmp/c25_compose3.log 179/5, and every failure in both is in the ban-write
scene, not here. So the tree as committed has not been measured with a green
compose gate, and this commit does not claim it has. The cause of those failures
is now identified (OPEN-42): the scene reads the ban pillar immediately after the
last refusal, while the ban write attempt and its failure are recorded a few
seconds later — so the counters read 0 and /readyz reads 200 while the log for the
same run says the ban was not persisted. A run that inserted an 18 s wait before
those checks answered 184/0 (/tmp/c25_diag1.log); that wait was a diagnostic and is
not in this commit. The gate goes green for this ticket only once OPEN-42 is fixed
in the scene itself.
This commit is contained in:
Pavlo Ratushnyi
2026-10-03 00:08:36 +02:00
parent 75ee8221de
commit 6422021a20
+64
View File
@@ -1063,6 +1063,70 @@ check 'and its flag is computed from the filtered count' 'yes' \
admin -X DELETE "$EDGE/admin-api/blocks?source=operator" >/dev/null
rm -f "$TMPH" "$TMPB"
echo '- a country rule is a rule, not a string'
# OPEN-25, measured on the shipped image before the fix (openwaf:ci at 5394751):
# PUT /admin-api/countries/12?allowed=false answered 204, PUT /countries/U.
# answered 204, and both were then listed to the operator as a denial — while a
# source in a real country still reached the upstream with 404, because the
# engine matches two-letter codes and these were not codes. Only length was
# checked, so /countries/USA was the one case refused. And there was no way
# back: DELETE /admin-api/countries/{code} did not exist, so the two junk rows
# stayed in the named volume and survived `compose down` — an operator's
# country table with permanent litter in it.
#
# The in-process suite has covered the fixed behaviour since; what was missing
# is the same sentence measured over a socket on the image that ships it.
printf ' info the country this run resolves for %s: %s\n' "$GEO_IP" "${COUNTRY:-unknown}"
check 'the geo lookup gives this scene a real country to reason about' 'yes' \
"$([ -n "${COUNTRY:-}" ] && echo yes || echo no)"
JUNK_BEFORE=$(admin "$EDGE/admin-api/countries" | jcount "b['code']=='12' or b['code']=='U.' or b['code']=='USA'")
printf ' info junk rows in the country table before this scene: %s\n' "$JUNK_BEFORE"
check 'no two-character junk is sitting in the country table already' '0' "$JUNK_BEFORE"
check 'a two-character non-code is refused, not stored' '400' \
"$(code -H "X-Admin-Token: $OPENWAF_ADMIN_TOKEN" -X PUT "$EDGE/admin-api/countries/12?allowed=false")"
check 'and the refusal names the rule and the value' 'code must be a two-letter ISO country code; got "12"' \
"$(admin -X PUT "$EDGE/admin-api/countries/12?allowed=false" | jval error)"
check 'punctuation of the right length is refused too' '400' \
"$(code -H "X-Admin-Token: $OPENWAF_ADMIN_TOKEN" -X PUT "$EDGE/admin-api/countries/U.?allowed=false")"
check 'and so is a question mark, sent as a path and not as a query' '400' \
"$(code -H "X-Admin-Token: $OPENWAF_ADMIN_TOKEN" -X PUT "$EDGE/admin-api/countries/%3F%3F?allowed=false")"
check 'a three-letter code is refused by the same sentence, not by its length' 'two-letter ISO country code' \
"$(admin -X PUT "$EDGE/admin-api/countries/USA?allowed=false" | jval error | grep -o 'two-letter ISO country code')"
check 'and none of the four wrote a row' '0' \
"$(admin "$EDGE/admin-api/countries" | jcount "b['code']=='12' or b['code']=='U.' or b['code']=='??' or b['code']=='USA'")"
# A refused write must not disturb the rule that is already there: the answer
# has to be about the code that was typed, not about whatever the table held.
admin -X PUT "$EDGE/admin-api/countries/$COUNTRY?allowed=false" >/dev/null
admin -X PUT -H 'Content-Type: application/json' -d '{"geo":"DenyList","trustProxyHeaders":true}' "$EDGE/admin-api/policy" >/dev/null
code -H "$(xff "$GEO_IP")" "$EDGE/no-such-page" >/dev/null
check 'a refused write leaves the rule that was already there alone' '1' \
"$(admin "$EDGE/admin-api/countries" | jcount "b['code']=='$COUNTRY' and b['isAllowed'] is False")"
check 'lower case on the wire becomes the rule, not a second row' '204' \
"$(code -H "X-Admin-Token: $OPENWAF_ADMIN_TOKEN" -X PUT "$EDGE/admin-api/countries/$(printf '%s' "$COUNTRY" | tr '[:upper:]' '[:lower:]')?allowed=false")"
check 'and the table holds the upper-case code, never the one typed' 'yes' \
"$([ "$(admin "$EDGE/admin-api/countries" | jcount "b['code']=='$COUNTRY'")" = "1" ] && [ "$(admin "$EDGE/admin-api/countries" | jcount "b['code']=='$(printf '%s' "$COUNTRY" | tr '[:upper:]' '[:lower:]')'")" = "0" ] && echo yes || echo no)"
check 'the normalised rule is the one the engine enforces' '451' \
"$(code -H "$(xff "$GEO_IP")" "$EDGE/no-such-page")"
check 'removing the rule answers 204' '204' \
"$(code -H "X-Admin-Token: $OPENWAF_ADMIN_TOKEN" -X DELETE "$EDGE/admin-api/countries/$COUNTRY")"
check 'and the table no longer lists it' '0' \
"$(admin "$EDGE/admin-api/countries" | jcount "b['code']=='$COUNTRY'")"
# The delete has to reach the decision, not only the table: a rule removed from
# storage while the snapshot still carries it is a denial the operator can
# neither see nor switch off.
check 'the next request is decided without the removed rule' '404' \
"$(code -H "$(xff "$GEO_IP")" "$EDGE/no-such-page")"
check 'deleting a rule that was never written says so, naming the code' '404' \
"$(code -H "X-Admin-Token: $OPENWAF_ADMIN_TOKEN" -X DELETE "$EDGE/admin-api/countries/ZZ")"
check 'and the sentence is about that code' 'no country rule for "ZZ"' \
"$(admin -X DELETE "$EDGE/admin-api/countries/ZZ" | jval error)"
check 'a lower-case delete finds the same answer' '404' \
"$(code -H "X-Admin-Token: $OPENWAF_ADMIN_TOKEN" -X DELETE "$EDGE/admin-api/countries/zz")"
check 'the junk that used to be eternal is still not in the table' '0' \
"$(admin "$EDGE/admin-api/countries" | jcount "b['code']=='12' or b['code']=='U.' or b['code']=='??' or b['code']=='USA'")"
admin -X PUT -H 'Content-Type: application/json' -d '{"geo":"Off"}' "$EDGE/admin-api/policy" >/dev/null
echo
printf 'compose smoke: %d passed, %d failed\n' "$PASS" "$FAIL"
[ "$FAIL" -eq 0 ]