Reject a null byte in a rule's name, condition or action value

Closed
JA
2mo ago
+279-0

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 DatabaseTextValidator rejects a NUL or malformed UTF-8 in the rule name and resource_type, the action action_type and value, and the condition condition_type, operator and value. This PR covered name, both values and (in 12efd2757) operator, and deferred action_type and resource_type to #416.
  • It also handles the form. RulesController#render_invalid_rule answers 422 when the selector fields are invalid, instead of re-rendering a form that cannot show them. It guards RuleImport and Family::DataImporter too.
  • 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 main takes the upstream sync that carries c28f9dc68. It stays open until then.
  • #416: #4109 covers its null-byte half (a NUL in action_type or resource_type). An unknown action_type with no NUL in it, such as "garbage", may still crash the re-render: #4109 adds a presence check and the NUL check for action_type, but no list of allowed values. That half is not verified, and is left on #416.
  • The branch up/rule-null-byte is 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 in 12efd2757, pushed once this PR's full Gatekeeper review had landed (below).

    1. operator and action_type aren't validated: confirmed, and wider than stated. Only condition_type is checked against a list (condition.rb:15). operator, action_type and resource_type carry only a presence check, so a NUL in any of them raised ArgumentError: string contains null byte at rules_controller.rb:54 (create) and :78 (update), with nothing saved.
      • Fixed for operator in 12efd2757. 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's ArgumentError). Rule, condition, action, rules controller, RuleImport and DataImporter suites: 187 runs, 0 failures.
      • Not fixed for action_type and resource_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) and action.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.
    2. The :invalid message is opaque: declined. It matches the existing null-byte rule in family/ai_promptable.rb:104, and upstream AGENTS.md asks 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.
    3. Bulk writes bypass validation: declined. app and lib have no insert_all, upsert_all, update_all, update_columns, update_column or raw SQL on rules, rule_conditions or rule_actions.
    4. Existing records may hold a null byte: declined. PostgreSQL can't store one: SELECT E'a\000b'::varchar fails with invalid byte sequence for encoding "UTF8": 0x00.
    5. create_from_grouping returns nil: declined. Its only caller already handles nil (transactions/categorizes_controller.rb:35-41), and returning nil on RecordInvalid predates 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 operator still 500s at this head: fixed by pushing 12efd2757, as the review recommends. That commit adds the guard and the two tests described under the "code master" bullet above.
    • OBSERVATION, action_type and resource_type deferred: 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.

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 pointBefore
POST /rules, PATCH /rules/:idArgumentError, a 500, instead of re-rendering the form with an error
Rule.create_from_grouping (the categorize wizard)ArgumentError escapes: it rescues only RecordInvalid
RuleImportImport 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_grouping returns nil;
  • 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:

TestBefore
Rule::ConditionTest a condition value containing a null byte is invalidvalid? was true
Rule::ActionTest an action value containing a null byte is invalidvalid? was true
Rule::ActionTest a null byte in one element of a multi-select action value is invalidvalid? was true
RuleTest a rule name containing a null byte is invalidvalid? was true
RuleTest a null byte in a compound sub-condition makes the rule invalidvalid? was true
RuleTest create_from_grouping returns nil for a grouping key with a null byteArgumentError at rule.rb:63
RulesControllerTest create with a null byte in a condition value renders 422 and saves nothingArgumentError at rules_controller.rb:54
RulesControllerTest update with a null byte in a condition value renders 422 and keeps the stored valueArgumentError at rules_controller.rb:78
RuleImportTest a null byte in a condition value fails the import with a validation errorthe stored error was the driver's
Family::DataImporterTest a null byte in a rule name raises a validation error, not the driver'sArgumentError, not RecordInvalid

Boundary tests, passing before and after:

  • a tab, a newline or "Café" stays valid, and still saves, matches and applies;
  • an is_null condition 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, Import and categorizes controller tests: 316 runs, 0 failures.
  • bin/rails test: 11,060 runs, 1 failure. It is the Provider::OpenaiTest#test_extra_headers_stringifies_nested_values_Ruby-style string, which differs only under Ruby 3.3 (the local runner); CI runs the pinned 3.4.
  • bin/rubocop on 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 RuleImport rule 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_rule search, which builds its own query.