Skip to content

Commit 973c5fd

Browse files
Merge pull request #383 from diffix/cristian/bugfix
Improve validation of statements.
2 parents a7650b8 + 9e92853 commit 973c5fd

File tree

6 files changed

+57
-9
lines changed

6 files changed

+57
-9
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## Next version
4+
- Allow casts between `int4` and `int8`.
5+
- Allow more metadata discovery queries.
6+
- Allow more statement types.
7+
38
## Version 1.0.1
49
- Fixed some docs links.
510
- Fixed setup script version.

src/auth.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,17 @@ AccessLevel get_session_access_level(void)
9292
return (AccessLevel)g_config.session_access_level;
9393
}
9494

95-
static bool is_pg_catalog_relation(Oid relation_oid)
95+
static bool is_metadata_relation(Oid relation_oid)
9696
{
97-
return get_rel_namespace(relation_oid) == PG_CATALOG_NAMESPACE;
97+
Oid namespace_oid = get_rel_namespace(relation_oid);
98+
99+
if (namespace_oid == PG_CATALOG_NAMESPACE)
100+
return true; /* PG_CATALOG relations are checked in `ExecutorCheckPerms` hook. */
101+
102+
if (strcmp(get_namespace_name(namespace_oid), "information_schema") == 0)
103+
return true; /* INFORMATION_SCHEMA relations are safe to query. */
104+
105+
return false;
98106
}
99107

100108
bool is_personal_relation(Oid relation_oid)
@@ -103,8 +111,8 @@ bool is_personal_relation(Oid relation_oid)
103111
const char *seclabel = GetSecurityLabel(&relation_object, PROVIDER_TAG);
104112

105113
if (seclabel == NULL)
106-
if (g_config.treat_unmarked_tables_as_public || is_pg_catalog_relation(relation_oid))
107-
return false; /* PG_CATALOG relations are checked in `ExecutorCheckPerms` hook. */
114+
if (g_config.treat_unmarked_tables_as_public || is_metadata_relation(relation_oid))
115+
return false;
108116
else
109117
FAILWITH_CODE(ERRCODE_INSUFFICIENT_PRIVILEGE,
110118
"Tables without an anonymization label can't be accessed in anonymized mode.");

src/query/allowed_objects.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#include "pg_diffix/utils.h"
1111

1212
static const char *const g_allowed_casts[] = {
13-
"i2tod", "i2tof", "i2toi4", "i4toi2", "i4tod", "i4tof", "i8tod", "i8tof",
13+
"i2tod", "i2tof", "i2toi4", "i4toi2", "i4tod", "i4tof", "i8tod", "i8tof", "int48", "int84",
1414
"ftod", "dtof",
1515
"int4_numeric", "float4_numeric", "float8_numeric",
1616
"numeric_float4", "numeric_float8",
@@ -59,10 +59,8 @@ static const char *const g_pg_catalog_allowed_rels[] = {
5959
"pg_opclass", "pg_operator", "pg_opfamily", "pg_policy", "pg_prepared_statements", "pg_prepared_xacts", "pg_publication",
6060
"pg_publication_rel", "pg_rewrite", "pg_roles", "pg_seclabel", "pg_seclabels", "pg_sequence", "pg_settings", "pg_shadow",
6161
"pg_shdepend", "pg_shdescription", "pg_shseclabel", "pg_stat_gssapi", "pg_subscription", "pg_subscription_rel", "pg_tablespace",
62-
"pg_trigger", "pg_ts_config", "pg_ts_dict", "pg_ts_parser", "pg_ts_template", "pg_type", "pg_user",
63-
/* `pg_proc` contains `procost` and `prorows` but both seem to be fully static data. */
64-
"pg_proc",
65-
/**/
62+
"pg_trigger", "pg_ts_config", "pg_ts_dict", "pg_ts_parser", "pg_ts_template", "pg_type", "pg_user", "pg_tables", "pg_matviews",
63+
"pg_indexes", "pg_proc" /* `pg_proc` contains `procost` and `prorows` but both seem to be fully static data. */
6664
};
6765

6866
static AllowedCols g_pg_catalog_allowed_cols[] = {

src/query/validation.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ void verify_utility_command(Node *utility_stmt)
5151
case T_LockStmt:
5252
case T_CheckPointStmt:
5353
case T_DeclareCursorStmt:
54+
case T_DeallocateStmt:
55+
case T_FetchStmt:
5456
break;
5557
default:
5658
FAILWITH("Statement requires direct access level.");

test/expected/validation.out

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,18 @@ SELECT COUNT(*) FROM test_validation
6969
-------
7070
(0 rows)
7171

72+
SELECT COUNT(*) FROM test_validation
73+
GROUP BY round(id::integer), ceil(id::integer), ceiling(id::integer), floor(id::integer);
74+
count
75+
-------
76+
(0 rows)
77+
78+
SELECT COUNT(*) FROM test_validation
79+
GROUP BY round(id::bigint), ceil(id::bigint), ceiling(id::bigint), floor(id::bigint);
80+
count
81+
-------
82+
(0 rows)
83+
7284
SELECT
7385
diffix.round_by(id::numeric, 5),
7486
diffix.round_by(id::double precision, 5),
@@ -239,6 +251,19 @@ Indexes:
239251
Indexes:
240252
"empty_test_customers_pkey" PRIMARY KEY, btree (id)
241253

254+
-- Allow discovery statements
255+
SELECT EXISTS (SELECT FROM PG_Catalog.pg_tables WHERE schemaname='public' AND tablename='test_customers');
256+
exists
257+
--------
258+
t
259+
(1 row)
260+
261+
SELECT EXISTS (SELECT FROM Information_Schema.tables WHERE table_schema='public' AND table_name='test_customers');
262+
exists
263+
--------
264+
t
265+
(1 row)
266+
242267
-- Settings and labels UDFs work
243268
SELECT * FROM diffix.show_settings() LIMIT 2;
244269
name | setting | short_desc

test/sql/validation.sql

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ SELECT COUNT(*) FROM test_validation
5454
SELECT COUNT(*) FROM test_validation
5555
GROUP BY round(id::numeric), ceil(id::numeric), ceiling(id::numeric), floor(id::numeric);
5656

57+
SELECT COUNT(*) FROM test_validation
58+
GROUP BY round(id::integer), ceil(id::integer), ceiling(id::integer), floor(id::integer);
59+
60+
SELECT COUNT(*) FROM test_validation
61+
GROUP BY round(id::bigint), ceil(id::bigint), ceiling(id::bigint), floor(id::bigint);
62+
5763
SELECT
5864
diffix.round_by(id::numeric, 5),
5965
diffix.round_by(id::double precision, 5),
@@ -130,6 +136,10 @@ SELECT (SELECT city FROM test_validation);
130136
\dt+ empty_test_customers
131137
\d+ empty_test_customers
132138

139+
-- Allow discovery statements
140+
SELECT EXISTS (SELECT FROM PG_Catalog.pg_tables WHERE schemaname='public' AND tablename='test_customers');
141+
SELECT EXISTS (SELECT FROM Information_Schema.tables WHERE table_schema='public' AND table_name='test_customers');
142+
133143
-- Settings and labels UDFs work
134144
SELECT * FROM diffix.show_settings() LIMIT 2;
135145
SELECT * FROM diffix.show_labels() WHERE objname LIKE 'public.test_customers%';

0 commit comments

Comments
 (0)