@@ -39,13 +39,13 @@ CREATE POLICY <replaceable class="parameter">name</replaceable> ON <replaceable
39
39
</para>
40
40
41
41
<para>
42
- A policy grants theability toSELECT, INSERT, UPDATE , orDELETE rows
43
- which match the relevant policy expression. Existing table rows are
44
- checked against the expression specified via USING, while new rows that
45
- would be created via INSERT or UPDATE are checked against the expression
46
- specified via WITH CHECK. When a USING expression returns true for a given
42
+ A policy grants thepermission toselect, insert, update , ordelete rows
43
+ that match the relevant policy expression. Existing table rows are
44
+ checked against the expression specified via<literal> USING</literal> , while new rows that
45
+ would be created via<literal> INSERT</literal> or<literal> UPDATE</literal> are checked against the expression
46
+ specified via<literal> WITH CHECK</literal> . When a<literal> USING</literal> expression returns true for a given
47
47
row then that row is visible to the user, while if a false or null is
48
- returned then the row is not visible. When a WITH CHECK expression
48
+ returned then the row is not visible. When a<literal> WITH CHECK</literal> expression
49
49
returns true for a row then that row is added, while if a false or null is
50
50
returned then an error occurs.
51
51
</para>
@@ -56,20 +56,21 @@ CREATE POLICY <replaceable class="parameter">name</replaceable> ON <replaceable
56
56
in order to prevent the inadvertent exposure of the protected data to
57
57
user-defined functions which might not be trustworthy. However,
58
58
functions and operators marked by the system (or the system
59
- administrator) as LEAKPROOF may be evaluated before policy
59
+ administrator) as<literal> LEAKPROOF</literal> may be evaluated before policy
60
60
expressions, as they are assumed to be trustworthy.
61
61
</para>
62
62
63
63
<para>
64
- For INSERT and UPDATE queries, WITH CHECK expressions are enforced after
65
- BEFORE triggers are fired, and before any data modifications are made.
66
- Thus a BEFORE ROW trigger may modify the data to be inserted, affecting
67
- the result of the security policy check. WITH CHECK expressions are
68
- enforced before any other constraints.
64
+ For <command>INSERT</command> and <command>UPDATE</command> statements,
65
+ <literal>WITH CHECK</literal> expressions are enforced after
66
+ <literal>BEFORE</literal> triggers are fired, and before any data modifications are made.
67
+ Thus a <literal>BEFORE ROW</literal> trigger may modify the data to be inserted, affecting
68
+ the result of the security policy check. <literal>WITH CHECK</literal> expressions are
69
+ enforced before any other constraints.
69
70
</para>
70
71
71
72
<para>
72
- Policy names are per-table, therefore one policy name can be used for many
73
+ Policy names are per-table. Therefore, one policy name can be used for many
73
74
different tables and have a definition for each table which is appropriate to
74
75
that table.
75
76
</para>
@@ -78,46 +79,19 @@ CREATE POLICY <replaceable class="parameter">name</replaceable> ON <replaceable
78
79
Policies can be applied for specific commands or for specific roles. The
79
80
default for newly created policies is that they apply for all commands and
80
81
roles, unless otherwise specified. If multiple policies apply to a given
81
- query , they will be combined usingOR (although <literal>ON CONFLICT DO
82
+ statement , they will be combined using<quote>or</quote> (although <literal>ON CONFLICT DO
82
83
UPDATE</> and <literal>INSERT</> policies are not combined in this way, but
83
84
rather enforced as noted at each stage of <literal>ON CONFLICT</> execution).
84
- Further, for commands which can have both USING and WITH CHECK policies (ALL
85
- and UPDATE), if no WITH CHECK policy is defined then the USING policy will be
86
- used for both what rows are visible (normal USING case) and which rows will
87
- be allowed to be added (WITH CHECK case).
88
85
</para>
89
86
90
87
<para>
91
- Note that while policies will be applied for explicit queries against tables
92
- in the system, they are not applied when the system is performing internal
93
- referential integrity checks or validating constraints. This means there are
94
- indirect ways to determine that a given value exists. An example of this is
95
- attempting to insert a duplicate value into a column which is the primary key
96
- or has a unique constraint. If the insert fails then the user can infer that
97
- the value already exists (this example assumes that the user is permitted by
98
- policy to insert records which they are not allowed to see). Another example
99
- is where a user is allowed to insert into a table which references another,
100
- otherwise hidden table. Existence can be determined by the user inserting
101
- values into the referencing table, where success would indicate that the
102
- value exists in the referenced table. These issues can be addressed by
103
- carefully crafting policies which prevent users from being able to insert,
104
- delete, or update records at all which might possibly indicate a value they
105
- are not otherwise able to see, or by using generated values (e.g.: surrogate
106
- keys) instead.
88
+ Further, for commands that can have both <literal>USING</literal>
89
+ and <literal>WITH CHECK</literal> policies (<literal>ALL</literal>
90
+ and <literal>UPDATE</literal>), if no <literal>WITH CHECK</literal> policy
91
+ is defined, then the <literal>USING</literal> policy will be used for both
92
+ what rows are visible (normal <literal>USING</literal> case) and which rows
93
+ will be allowed to be added (<literal>WITH CHECK</literal> case).
107
94
</para>
108
-
109
- <para>
110
- Regarding how policy expressions interact with the user: as the expressions
111
- are added to the user's query directly, they will be run with the rights of
112
- the user running the overall query. Therefore, users who are using a given
113
- policy must be able to access any tables or functions referenced in the
114
- expression or they will simply receive a permission denied error when
115
- attempting to query the table that has row-level security enabled. This does not change how views
116
- work, however. As with normal queries and views, permission checks and
117
- policies for the tables which are referenced by a view will use the view
118
- owner's rights and any policies which apply to the view owner.
119
- </para>
120
-
121
95
</refsect1>
122
96
123
97
<refsect1>
@@ -194,15 +168,14 @@ CREATE POLICY <replaceable class="parameter">name</replaceable> ON <replaceable
194
168
the table if row level security is enabled and only rows where the
195
169
expression evaluates to true will be allowed. An error will be thrown
196
170
if the expression evaluates to false or null for any of the records
197
- inserted or any of the recordswhich result from the update.
171
+ inserted or any of the recordsthat result from the update.
198
172
</para>
199
173
</listitem>
200
174
</varlistentry>
201
175
202
176
</variablelist>
203
- </refsect1>
204
177
205
- <refsect1 >
178
+ <refsect2 >
206
179
<title>Per-Command policies</title>
207
180
208
181
<variablelist>
@@ -216,20 +189,21 @@ CREATE POLICY <replaceable class="parameter">name</replaceable> ON <replaceable
216
189
<literal>ALL</literal> policy exists and more specific policies
217
190
exist, then both the <literal>ALL</literal> policy and the more
218
191
specific policy (or policies) will be combined using
219
- <literal>OR</literal >, as usual for overlapping policies.
192
+ <quote>or</quote >, as usual for overlapping policies.
220
193
Additionally, <literal>ALL</literal> policies will be applied to
221
194
both the selection side of a query and the modification side, using
222
- the USING policy for both if only a USING policy has been defined.
223
-
195
+ the <literal>USING</literal> policy for both if only a <literal>USING</literal> policy has been defined.
196
+ </para>
197
+ <para>
224
198
As an example, if an <literal>UPDATE</literal> is issued, then the
225
199
<literal>ALL</literal> policy will be applicable to both what the
226
200
<literal>UPDATE</literal> will be able to select out as rows to be
227
- updated (with the USING expression being applied), and it will be
228
- applied to rowswhich result from the <literal>UPDATE</literal>
201
+ updated (with the<literal> USING</literal> expression being applied), and it will be
202
+ applied to rowsthat result from the <literal>UPDATE</literal>
229
203
statement, to check if they are permitted to be added to the table
230
- (using the WITH CHECK expression, if defined, and the USING expression
231
- otherwise). If an INSERT or UPDATE command attempts to add rows to
232
- the tablewhich do not pass the <literal>ALL</literal> WITH CHECK
204
+ (using the<literal> WITH CHECK</literal> expression, if defined, and the<literal> USING</literal> expression
205
+ otherwise). If an<command> INSERT</command> or<command> UPDATE</command> command attempts to add rows to
206
+ the tablethat do not pass the <literal>ALL</literal><literal> WITH CHECK</literal>
233
207
expression, the entire command will be aborted. Note that if only a
234
208
<literal>USING</literal> clause is specified then that clause will be
235
209
used for both <literal>USING</literal> and
@@ -244,9 +218,9 @@ CREATE POLICY <replaceable class="parameter">name</replaceable> ON <replaceable
244
218
<para>
245
219
Using <literal>SELECT</literal> for a policy means that it will apply
246
220
to <literal>SELECT</literal> commands. The result is that only those
247
- records from the relationwhich pass the <literal>SELECT</literal>
221
+ records from the relationthat pass the <literal>SELECT</literal>
248
222
policy will be returned, even if other records exist in the relation.
249
- The <literal>SELECT</literal> policy only accepts the USING expression
223
+ The <literal>SELECT</literal> policy only accepts the<literal> USING</literal> expression
250
224
as it only ever applies in cases where records are being retrieved from
251
225
the relation.
252
226
</para>
@@ -258,18 +232,18 @@ CREATE POLICY <replaceable class="parameter">name</replaceable> ON <replaceable
258
232
<listitem>
259
233
<para>
260
234
Using <literal>INSERT</literal> for a policy means that it will apply
261
- to <literal>INSERT</literal> commands. Rows being insertedwhich do
262
- not pass this policy will result in a policy violationERROR and the
235
+ to <literal>INSERT</literal> commands. Rows being insertedthat do
236
+ not pass this policy will result in a policy violationerror, and the
263
237
entire <literal>INSERT</literal> command will be aborted. The
264
- <literal>INSERT</literal> policy only accepts the WITH CHECK expression
238
+ <literal>INSERT</literal> policy only accepts the<literal> WITH CHECK</literal> expression
265
239
as it only ever applies in cases where records are being added to the
266
240
relation.
267
241
</para>
268
242
<para>
269
243
Note that <literal>INSERT</literal> with <literal>ON CONFLICT DO
270
244
UPDATE</literal> requires that any <literal>INSERT</literal> policy
271
- WITH CHECK expression passes for any rows appended to the relation by
272
- the INSERT path only.
245
+ <literal> WITH CHECK</literal> expression passes for any rows appended to the relation by
246
+ the<literal> INSERT</literal> path only.
273
247
</para>
274
248
</listitem>
275
249
</varlistentry>
@@ -291,8 +265,8 @@ CREATE POLICY <replaceable class="parameter">name</replaceable> ON <replaceable
291
265
defines what rows are allowed to be added back into the relation
292
266
(similar to the <literal>INSERT</literal> policy). Any rows whose
293
267
resulting values do not pass the <literal>WITH CHECK</literal>
294
- expression will cause anERROR and the entire command will be aborted.
295
- Note that if only a <literal>USING</literal> clause is specified then
268
+ expression will cause anerror, and the entire command will be aborted.
269
+ Note that if only a <literal>USING</literal> clause is specified, then
296
270
that clause will be used for both <literal>USING</literal> and
297
271
<literal>WITH CHECK</literal> cases.
298
272
</para>
@@ -304,11 +278,11 @@ CREATE POLICY <replaceable class="parameter">name</replaceable> ON <replaceable
304
278
<literal>UPDATE</literal> policy must always pass when the
305
279
<literal>UPDATE</literal> path is taken. Any existing row that
306
280
necessitates that the <literal>UPDATE</literal> path be taken must pass
307
- the (UPDATE or ALL) <literal>USING</literal> qualifications (combined
308
- using <literal>OR</literal >), which are always enforced as WITH CHECK
309
- options in this context (the <literal>UPDATE</literal> path will
281
+ the (<literal> UPDATE</literal> or<literal> ALL</literal> ) <literal>USING</literal> qualifications (combined
282
+ using <quote>or</quote >), which are always enforced as<literal> WITH CHECK</literal>
283
+ options in this context. (The <literal>UPDATE</literal> path will
310
284
<emphasis>never</> be silently avoided; an error will be thrown
311
- instead). Finally, the final row appended to the relation must pass
285
+ instead.) Finally, the final row appended to the relation must pass
312
286
any <literal>WITH CHECK</literal> options that a conventional
313
287
<literal>UPDATE</literal> is required to pass.
314
288
</para>
@@ -320,21 +294,22 @@ CREATE POLICY <replaceable class="parameter">name</replaceable> ON <replaceable
320
294
<listitem>
321
295
<para>
322
296
Using <literal>DELETE</literal> for a policy means that it will apply
323
- to <literal>DELETE</literal> commands. Only rowswhich pass this
324
- policy will be seen by a <literal>DELETE</literal> command.Rows may
325
- be visible through a <literal>SELECT</literal>which are not seen by a
326
- <literal>DELETE</literal>,as they do not pass the USING expression
327
- for the <literal>DELETE</literal>, and rowswhich are not visible
328
- through the <literal>SELECT</literal> policy may be deleted if they
329
- pass the <literal>DELETE</literal> USING policy. The
330
- <literal>DELETE</literal> policy only accepts the USING expression as
297
+ to <literal>DELETE</literal> commands. Only rowsthat pass this
298
+ policy will be seen by a <literal>DELETE</literal> command.There can be rows
299
+ that are visible through a <literal>SELECT</literal>that are not seen by a
300
+ <literal>DELETE</literal>,if they do not pass the<literal> USING</literal> expression
301
+ for the <literal>DELETE</literal>. Conversely, there can be rowsthat are not visible
302
+ through the <literal>SELECT</literal> policybut may be deleted if they
303
+ pass the <literal>DELETE</literal><literal> USING</literal> policy. The
304
+ <literal>DELETE</literal> policy only accepts the<literal> USING</literal> expression as
331
305
it only ever applies in cases where records are being extracted from
332
306
the relation for deletion.
333
307
</para>
334
308
</listitem>
335
309
</varlistentry>
336
310
337
311
</variablelist>
312
+ </refsect2>
338
313
</refsect1>
339
314
340
315
<refsect1>
@@ -345,11 +320,35 @@ CREATE POLICY <replaceable class="parameter">name</replaceable> ON <replaceable
345
320
</para>
346
321
347
322
<para>
348
- In order to maintain <firstterm>referential integrity</firstterm> between
349
- two related tables, policies are not applied when the system performs
350
- checks on foreign key constraints.
323
+ Note that while policies will be applied for explicit queries against tables
324
+ in the system, they are not applied when the system is performing internal
325
+ referential integrity checks or validating constraints. This means there are
326
+ indirect ways to determine that a given value exists. An example of this is
327
+ attempting to insert a duplicate value into a column which is the primary key
328
+ or has a unique constraint. If the insert fails then the user can infer that
329
+ the value already exists. (This example assumes that the user is permitted by
330
+ policy to insert records which they are not allowed to see.) Another example
331
+ is where a user is allowed to insert into a table which references another,
332
+ otherwise hidden table. Existence can be determined by the user inserting
333
+ values into the referencing table, where success would indicate that the
334
+ value exists in the referenced table. These issues can be addressed by
335
+ carefully crafting policies that prevent users from being able to insert,
336
+ delete, or update records at all which might possibly indicate a value they
337
+ are not otherwise able to see, or by using generated values (e.g., surrogate
338
+ keys) instead.
351
339
</para>
352
340
341
+ <para>
342
+ Regarding how policy expressions interact with the user: as the expressions
343
+ are added to the user's query directly, they will be run with the rights of
344
+ the user running the overall query. Therefore, users who are using a given
345
+ policy must be able to access any tables or functions referenced in the
346
+ expression or they will simply receive a permission denied error when
347
+ attempting to query the table that has row-level security enabled. This does not change how views
348
+ work, however. As with normal queries and views, permission checks and
349
+ policies for the tables which are referenced by a view will use the view
350
+ owner's rights and any policies which apply to the view owner.
351
+ </para>
353
352
</refsect1>
354
353
355
354
<refsect1>