diff --git a/scripts/compose-smoke.sh b/scripts/compose-smoke.sh index c283798..67c1d0d 100755 --- a/scripts/compose-smoke.sh +++ b/scripts/compose-smoke.sh @@ -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 ]