Reject a null byte in a rule's name, condition or action value
Closed as superseded, 2026-10-12, on the owner's instruction ("Close #410 as superseded"). Upstream fixed we-promise/sure#4101 with a different PR before this one was opened there: we-promise/sure#4109, "Reject null bytes in rule fields" (c28f9dc68 on mirror/upstream-main, merged 2026-10-10, "Fixes #4101").
- #4109 covers everything this PR did, and more. Its
DatabaseTextValidatorrejects a NUL or malformed UTF-8 in the rulenameandresource_type, the actionaction_typeandvalue, and the conditioncondition_type,operatorandvalue. This PR coveredname, bothvalues and (in12efd2757)operator, and deferredaction_typeandresource_typeto #416. - It also handles the form.
RulesController#render_invalid_ruleanswers 422 when the selector fields are invalid, instead of re-rendering a form that cannot show them. It guardsRuleImportandFamily::DataImportertoo. - So nothing here goes upstream. This branch also no longer merges with
mirror/upstream-main(mergeable_state: dirty). - #388 is fixed for the fork once fork
maintakes the upstream sync that carriesc28f9dc68. It stays open until then. - #416: #4109 covers its null-byte half (a NUL in
action_typeorresource_type). An unknownaction_typewith no NUL in it, such as"garbage", may still crash the re-render: #4109 adds a presence check and the NUL check foraction_type, but no list of allowed values. That half is not verified, and is left on #416. - The branch
up/rule-null-byteis left in place. This session cannot delete branches.
Fixes #388.
Upstream issue: we-promise/sure#4101
Upstream PR: none. Superseded by we-promise/sure#4109 (above), so this branch was never opened upstream.
This is the fork pre-flight for the upstream change. It targets mirror/upstream-main, and the branch is one commit on upstream main 94e71a8c2 (2026-10-10).
Process note. #388's Issue Gatekeeper Review had not landed when this was built. On 2026-10-10 the owner replied "Go with your recommendation" to a recommendation naming #388 and #389. I took that as the override rule 7 asks for.
Review findings, fork side.
-
Gatekeeper light review, comment 6104520857, on
e2140fc: DRAFT_ON_TRACK, risk MEDIUM, confidence HIGH, "No material concerns identified at this stage". It confirms CI green at this head, so nothing needs changing. Next steps: mark ready for the full review, then open the upstream draft. -
"code master" review (comment 6105940559), hold, on
e2140fc: finding 1 is confirmed, and its operator half is fixed in12efd2757, pushed once this PR's full Gatekeeper review had landed (below).operatorandaction_typearen't validated: confirmed, and wider than stated. Onlycondition_typeis checked against a list (condition.rb:15).operator,action_typeandresource_typecarry only a presence check, so a NUL in any of them raisedArgumentError: string contains null byteatrules_controller.rb:54(create) and:78(update), with nothing saved.- Fixed for
operatorin12efd2757. The condition's check now covers operator and value. New tests: "a condition operator containing a null byte is invalid" and "create with a null byte in a condition operator renders 422 and saves nothing". Both failed first (Expected true to be nil or false, and the driver'sArgumentError). Rule, condition, action, rules controller,RuleImportandDataImportersuites: 187 runs, 0 failures. - Not fixed for
action_typeandresource_type:- Adding them to the check stops the importers' 500, but not the form's. The 422 re-render calls
rule.registry(rules/_form.html.erb:4) andaction.executor(rule/actions/_action.html.erb:14), which raise for any unknown value. - That re-render crash predates this PR. Two actions typed
"garbage"trip it with no null byte at all. - Decided (owner, 2026-10-11): leave both to #416, a separate issue about the unknown-type re-render, which this PR does not touch.
- Adding them to the check stops the importers' 500, but not the form's. The 422 re-render calls
- Fixed for
- The
:invalidmessage is opaque: declined. It matches the existing null-byte rule infamily/ai_promptable.rb:104, and upstreamAGENTS.mdasks for consistency with nearby code. A typed field can't hold a NUL; one only arrives in a crafted request or a JSON-escaped NUL. - Bulk writes bypass validation: declined.
appandlibhave noinsert_all,upsert_all,update_all,update_columns,update_columnor raw SQL onrules,rule_conditionsorrule_actions. - Existing records may hold a null byte: declined. PostgreSQL can't store one:
SELECT E'a\000b'::varcharfails withinvalid byte sequence for encoding "UTF8": 0x00. create_from_groupingreturns nil: declined. Its only caller already handles nil (transactions/categorizes_controller.rb:35-41), and returning nil onRecordInvalidpredates this PR.
- GraphQL, jobs and other endpoints: declined. There is no GraphQL. The API rules routes are read-only (
only: [ :index, :show ]), and no job builds a rule.
-
Full Gatekeeper review, in three parts: comment 6106692653, 6106980565 and 6107052517, on
e2140fc. CHANGES_REQUESTED, risk MEDIUM, confidence HIGH, CI passing.- MODERATE, a null byte in the condition
operatorstill 500s at this head: fixed by pushing12efd2757, as the review recommends. That commit adds the guard and the two tests described under the "code master" bullet above. - OBSERVATION,
action_typeandresource_typedeferred: the review calls it "an accepted deferral, not a blocker". Decided: #416, as above. - Test evidence: adequate. Requirements alignment: partial until the operator commit, which is now on this branch.
- MODERATE, a null byte in the condition
The upstream description that was prepared follows. It was never posted.
Title: Reject a null byte in a rule's name, condition or action value
Closes we-promise/sure#4101. Fork pre-flight: jaysbeekay/sure#410.
Problem
A rule whose name, condition value or action value contains a null byte ("\x00") passes validation. The pg driver then raises ArgumentError: string contains null byte on the INSERT, because Postgres text cannot hold a NUL. Every writer of a rule is affected:
| Entry point | Before |
|---|---|
POST /rules, PATCH /rules/:id | ArgumentError, a 500, instead of re-rendering the form with an error |
Rule.create_from_grouping (the categorize wizard) | ArgumentError escapes: it rescues only RecordInvalid |
RuleImport | Import marked failed with the driver message, not one naming the field |
Family::DataImporter#import_rules (Sure import) | ArgumentError out of import!; SureImport#publish records the driver message |
It does not depend on the operator: the raise comes from writing the row, not from the query an operator builds.
Change
Rule#name, Rule::Condition#value and Rule::Action#value each get a validation that rejects a value containing "\x00" with errors.add(..., :invalid), following Family::AiPromptable#validate_ai_prompt_overrides_schema.
- Rejected, not stripped. A rule is never saved different from the one written.
- Multi-select actions are covered. The action check runs after the
value=writer, so a multi-select array has already been joined into the stored string. - No controller changes. A nested condition, sub-condition or action that fails makes the parent rule invalid through
accepts_nested_attributes_for.
After the change:
- the form renders a 422;
create_from_groupingreturnsnil;- both imports record "Validation failed: Conditions value is invalid", or the name or action equivalent.
Tabs, newlines and non-ASCII text are still accepted, and they still match.
app/models/rule.rb | 6 +++
app/models/rule/action.rb | 7 +++
app/models/rule/condition.rb | 6 +++
test/controllers/rules_controller_test.rb | 37 ++++++++++++++++
test/models/family/data_importer_test.rb | 24 ++++++++++
test/models/rule/action_test.rb | 27 +++++++++++
test/models/rule/condition_test.rb | 32 +++++++++++++
test/models/rule_import_test.rb | 27 +++++++++++
test/models/rule_test.rb | 74 +++++++++++++++++++++++++++++++
9 files changed, 240 insertions(+)
Tests
Each new test failed on main before the change:
| Test | Before |
|---|---|
Rule::ConditionTest a condition value containing a null byte is invalid | valid? was true |
Rule::ActionTest an action value containing a null byte is invalid | valid? was true |
Rule::ActionTest a null byte in one element of a multi-select action value is invalid | valid? was true |
RuleTest a rule name containing a null byte is invalid | valid? was true |
RuleTest a null byte in a compound sub-condition makes the rule invalid | valid? was true |
RuleTest create_from_grouping returns nil for a grouping key with a null byte | ArgumentError at rule.rb:63 |
RulesControllerTest create with a null byte in a condition value renders 422 and saves nothing | ArgumentError at rules_controller.rb:54 |
RulesControllerTest update with a null byte in a condition value renders 422 and keeps the stored value | ArgumentError at rules_controller.rb:78 |
RuleImportTest a null byte in a condition value fails the import with a validation error | the stored error was the driver's |
Family::DataImporterTest a null byte in a rule name raises a validation error, not the driver's | ArgumentError, not RecordInvalid |
Boundary tests, passing before and after:
- a tab, a newline or "Café" stays valid, and still saves, matches and applies;
- an
is_nullcondition with a nil value stays valid; - a value with invalid UTF-8 is handled too:
String#include?("\x00")returns true or false on it without raising.
Mutations, each caught:
- removing each of the three validations in turn;
- making the check always false;
- widening it to any control character, or to any non-ASCII character.
Validation
- Rule, rule run, condition/action, rules controller,
RuleImport,SureImport,Family::DataImporter,Importand categorizes controller tests: 316 runs, 0 failures. bin/rails test: 11,060 runs, 1 failure. It is theProvider::OpenaiTest#test_extra_headers_stringifies_nested_values_Ruby-stylestring, which differs only under Ruby 3.3 (the local runner); CI runs the pinned 3.4.bin/rubocopon the changed files: no offenses.bin/brakeman: 0 warnings. No ERB or JavaScript changed.
Not in this change
- NUL handling for other models' text columns, or a global params filter. These share the same driver behaviour, but I have not reproduced the problem there.
- A null byte in a
RuleImportrule name. The import looks the rule up by name before saving, but a CSV holding a raw NUL already fails when the import record itself is saved, so the importer can't reach this case today. - The categorize wizard's
preview_rulesearch, which builds its own query.