Security

Row rules

One expression per resource per action, applied to the query. Roles answer whether somebody may use an endpoint. This answers the question after it: which rows, and under what conditions.

// internal/rowrules/rules.go
func init() {
Register("Order", Rules{
List: `@request.auth.id != "" && user_id = @request.auth.id`,
View: `user_id = @request.auth.id`,
Create: `@request.auth.id != ""`,
Update: `user_id = @request.auth.id && status = "draft"`,
Delete: `user_id = @request.auth.id && status = "draft"`,
})
}

The language is the one ?filter= uses, so a rule can cross a relation (customer.user_id = @request.auth.id), quantify over many (members.user_id ?= @request.auth.id), or talk about the request (@request.body.role:changed = false).

Three layers, not three alternatives

internal/authz may this kind of person use this endpoint at all
internal/rowrules which rows may they see, as a condition on the query
internal/policies is this particular loaded record an exception

The middle one is new, and it is the only one that can filter a list. A Go callback over a loaded record cannot: by the time it runs the page has already been chosen, so the caller gets somebody else's rows minus the ones the callback refused, which is a shorter page rather than a correct one.

Applied to the query, not checked afterwards

For a list that filters it. For one record it means the row is not found, which is a 404 rather than a 403. That is deliberate: a 403 on a row you may not read confirms the row exists, and for an invoice number or a user id that is the whole of the information.

GET /api/v1/orders/o_other 404
PUT /api/v1/orders/o_other 404
DELETE /api/v1/orders/o_other 404

Three things worth knowing

An empty rule adds nothing. That is the default, and it is not the same as denying everything: a project that has not written a rule has not thereby decided nobody may read anything. Use Denied for a rule that refuses everybody, which is a thing you sometimes want to say out loud.

An admin, and the application itself, are not subject to these. A nightly invoice run reads every invoice, and "only your own" would make it send none. authz.AsSystem(ctx) is how a job says so.

A create rule is evaluated before the row exists, so it can only talk about the request: @request.auth and @request.body. Naming a column there is reported rather than quietly ignored, with a message saying why.

A rule that does not parse fails closedIt refuses the query rather than running it unfiltered. This is the one mistake where failing open would hand every row to everybody. Every rule is also parsed at start-up, so a typo is a line in the log before the first request rather than a 500 during one.

Where they are enforced

Four places in each generated service, which is the whole point: the question has to be asked at every place a row can be read or written, and one of them is always missed.

func (s *OrderService) rows(ctx context.Context, query *gorm.DB, action rowrules.Action) (*gorm.DB, error) {
return rowrules.Scope(ctx, query, "Order", action, &models.Order{})
}
// List -> rowrules.List
// GetByID -> rowrules.View
// load -> rowrules.Update or rowrules.Delete, by what is about to happen

A hand-written service calls the same thing. A resource that has declared no rules gets its query back untouched, so this costs nothing until you use it.

Nothing is injected into your rules file

grit generate resource does not add a commented-out block to internal/rowrules/rules.go. The file is yours, it has a worked example at the top, and a generator that writes into it would be a generator that has to be able to take it out again. A resource gets a rule when you write one.