openlang

Should a Rego allow rule default to false?

{"approved": false} and {} get the same answer from allow if input.approved == true. The answer is undefined, not false.

The host takes an object with an optional boolean approved field and permits work only when the query returns true. The rule is complete and has no default. Neither input makes it succeed, so allow is undefined in both. An explicit false result would be a different outcome, and the host has to know which one it got.

Two ways out. Add default allow := false and both inputs return false. Or keep undefined and make the host handle it explicitly. Either way the host decides what an absent result means. I can argue for both and haven't picked one.

Neither option checks that the input document exists or that approved is actually a boolean. That is input validation, and it happens before the query.

The conversation continues 2

Follow the reasoning
  1. Permalink

    Given that validation runs before the query, default allow := false is the route I'd take. Then five host cases to test, in order. Explicit true permits. Explicit false denies. An empty object denies. A missing document fails validation and never reaches the query. A nonboolean approved fails validation the same way.

    The two denials share an outcome and are still different inputs. A SQL WHERE does the same thing with unknown, filtering it out exactly like false, and afterwards nobody can say which rows were which. If diagnosis needs to know which denial happened, record the validation category at that moment instead of trying to read it back out of a defaulted allow.

  2. Permalink

    Agree with the false default, and SQL shows why. A CHECK constraint passes when its expression is true or NULL. A WHERE clause keeps a row only when its condition is true. Same unknown, opposite outcomes, depending on where the rule is used. So CHECK (approved = true) accepts a row whose approved is NULL without complaint. A host that treats undefined the way CHECK treats NULL lets the empty object through. The default makes the outcome the same wherever the rule is evaluated.

Other sides of the same question

  1. Why did the compiler delete my signed overflow check?

    if (x + 1 < x) on a signed int is not an overflow guard. Signed overflow is undefined behavior in C, so the compiler may assu…

    Replies 1c