mirror of
https://github.com/postgres/postgres.git
synced 2025-06-14 18:42:34 +03:00
Perform RLS WITH CHECK before constraints, etc
The RLS capability is built on top of the WITH CHECK OPTION system which was added for auto-updatable views, however, unlike WCOs on views (which are mandated by the SQL spec to not fire until after all other constraints and checks are done), it makes much more sense for RLS checks to happen earlier than constraint and uniqueness checks. This patch reworks the structure which holds the WCOs a bit to be explicitly either VIEW or RLS checks and the RLS-related checks are done prior to the constraint and uniqueness checks. This also allows better error reporting as we are now reporting when a violation is due to a WITH CHECK OPTION and when it's due to an RLS policy violation, which was independently noted by Craig Ringer as being confusing. The documentation is also updated to include a paragraph about when RLS WITH CHECK handling is performed, as there have been a number of questions regarding that and the documentation was previously silent on the matter. Author: Dean Rasheed, with some kabitzing and comment changes by me.
This commit is contained in:
@ -1673,9 +1673,15 @@ ExecConstraints(ResultRelInfo *resultRelInfo,
|
||||
|
||||
/*
|
||||
* ExecWithCheckOptions -- check that tuple satisfies any WITH CHECK OPTIONs
|
||||
* of the specified kind.
|
||||
*
|
||||
* Note that this needs to be called multiple times to ensure that all kinds of
|
||||
* WITH CHECK OPTIONs are handled (both those from views which have the WITH
|
||||
* CHECK OPTION set and from row level security policies). See ExecInsert()
|
||||
* and ExecUpdate().
|
||||
*/
|
||||
void
|
||||
ExecWithCheckOptions(ResultRelInfo *resultRelInfo,
|
||||
ExecWithCheckOptions(WCOKind kind, ResultRelInfo *resultRelInfo,
|
||||
TupleTableSlot *slot, EState *estate)
|
||||
{
|
||||
Relation rel = resultRelInfo->ri_RelationDesc;
|
||||
@ -1700,6 +1706,13 @@ ExecWithCheckOptions(ResultRelInfo *resultRelInfo,
|
||||
WithCheckOption *wco = (WithCheckOption *) lfirst(l1);
|
||||
ExprState *wcoExpr = (ExprState *) lfirst(l2);
|
||||
|
||||
/*
|
||||
* Skip any WCOs which are not the kind we are looking for at this
|
||||
* time.
|
||||
*/
|
||||
if (wco->kind != kind)
|
||||
continue;
|
||||
|
||||
/*
|
||||
* WITH CHECK OPTION checks are intended to ensure that the new tuple
|
||||
* is visible (in the case of a view) or that it passes the
|
||||
@ -1714,19 +1727,42 @@ ExecWithCheckOptions(ResultRelInfo *resultRelInfo,
|
||||
char *val_desc;
|
||||
Bitmapset *modifiedCols;
|
||||
|
||||
modifiedCols = GetModifiedColumns(resultRelInfo, estate);
|
||||
val_desc = ExecBuildSlotValueDescription(RelationGetRelid(rel),
|
||||
slot,
|
||||
tupdesc,
|
||||
modifiedCols,
|
||||
64);
|
||||
switch (wco->kind)
|
||||
{
|
||||
/*
|
||||
* For WITH CHECK OPTIONs coming from views, we might be able to
|
||||
* provide the details on the row, depending on the permissions
|
||||
* on the relation (that is, if the user could view it directly
|
||||
* anyway). For RLS violations, we don't include the data since
|
||||
* we don't know if the user should be able to view the tuple as
|
||||
* as that depends on the USING policy.
|
||||
*/
|
||||
case WCO_VIEW_CHECK:
|
||||
modifiedCols = GetModifiedColumns(resultRelInfo, estate);
|
||||
val_desc = ExecBuildSlotValueDescription(RelationGetRelid(rel),
|
||||
slot,
|
||||
tupdesc,
|
||||
modifiedCols,
|
||||
64);
|
||||
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_WITH_CHECK_OPTION_VIOLATION),
|
||||
errmsg("new row violates WITH CHECK OPTION for \"%s\"",
|
||||
wco->viewname),
|
||||
val_desc ? errdetail("Failing row contains %s.", val_desc) :
|
||||
0));
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_WITH_CHECK_OPTION_VIOLATION),
|
||||
errmsg("new row violates WITH CHECK OPTION for \"%s\"",
|
||||
wco->relname),
|
||||
val_desc ? errdetail("Failing row contains %s.",
|
||||
val_desc) : 0));
|
||||
break;
|
||||
case WCO_RLS_INSERT_CHECK:
|
||||
case WCO_RLS_UPDATE_CHECK:
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
|
||||
errmsg("new row violates row level security policy for \"%s\"",
|
||||
wco->relname)));
|
||||
break;
|
||||
default:
|
||||
elog(ERROR, "unrecognized WCO kind: %u", wco->kind);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user