Skip to content

Commit c1fda66

Browse files
committed
Add query q check
1 parent 8b3c0b4 commit c1fda66

File tree

4 files changed

+37
-12
lines changed

4 files changed

+37
-12
lines changed

sql/010-core.sql

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,19 @@ AS $$
9393
END;
9494
$$ LANGUAGE plpgsql;
9595

96+
-- Query field should never be present in an encrypted column
97+
DROP FUNCTION IF EXISTS _cs_encrypted_check_q(jsonb);
98+
CREATE FUNCTION _cs_encrypted_check_q(val jsonb)
99+
RETURNS boolean
100+
AS $$
101+
BEGIN
102+
IF val ? 'q'
103+
RAISE 'Encrypted column should not have a query (q) field (%).', val->>'q';
104+
END IF;
105+
RETURN true;
106+
END;
107+
$$ LANGUAGE plpgsql;
108+
96109
-- Ident field should include table and column
97110
DROP FUNCTION IF EXISTS _cs_encrypted_check_i_ct(jsonb);
98111
CREATE FUNCTION _cs_encrypted_check_i_ct(val jsonb)
@@ -132,6 +145,7 @@ BEGIN ATOMIC
132145
_cs_encrypted_check_k(val) AND
133146
_cs_encrypted_check_k_ct(val) AND
134147
_cs_encrypted_check_k_sv(val) AND
148+
_cs_encrypted_check_q(val) AND
135149
_cs_encrypted_check_p(val)
136150
);
137151
END;

sql/020-config-schema.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ AS $$
6262
IF (SELECT bool_and(index = ANY('{match, ore, unique, ste_vec}')) FROM _cs_extract_indexes(val) AS index) THEN
6363
RETURN true;
6464
END IF;
65-
RAISE 'Invalid index (%) in configuration. Index should be one of {match, ore, unique, ste_vec}', val;
65+
RAISE 'Configuration has an invalid index (%). Index should be one of {match, ore, unique, ste_vec}', val;
6666
END IF;
6767
RETURN true;
6868
END;
@@ -78,7 +78,7 @@ AS $$
7878
IF EXISTS (SELECT jsonb_array_elements_text(jsonb_path_query_array(val, '$.tables.*.*.cast_as')) = ANY('{text, int, small_int, big_int, real, double, boolean, date, jsonb}')) THEN
7979
RETURN true;
8080
END IF;
81-
RAISE 'Invalid cast (%) in configuration. Cast should be one of {text, int, small_int, big_int, real, double, boolean, date, jsonb}', val;
81+
RAISE 'Configuration has an invalid cast_as (%). Cast should be one of {text, int, small_int, big_int, real, double, boolean, date, jsonb}', val;
8282
END;
8383
$$ LANGUAGE plpgsql;
8484

tests/config.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ TRUNCATE TABLE cs_configuration_v1;
211211

212212
DO $$
213213
BEGIN
214-
RAISE NOTICE 'Configuration tests: 4 errors expected';
214+
RAISE NOTICE 'cs_configuration_v1 constraint tests: 4 errors expected here';
215215
END;
216216
$$ LANGUAGE plpgsql;
217217
--

tests/core.sql

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ $$ LANGUAGE plpgsql;
5959

6060
-- -----------------------------------------------
6161
---
62-
-- cs_enncrypted)v1 tyoe
62+
-- cs_encrypted_v1 tyoe
6363
-- Validate configuration schema
6464
-- Try and insert many invalid configurations
6565
-- None should exist
@@ -70,6 +70,12 @@ TRUNCATE TABLE users;
7070
\set ON_ERROR_STOP off
7171
\set ON_ERROR_ROLLBACK on
7272

73+
DO $$
74+
BEGIN
75+
RAISE NOTICE 'cs_encrypted_v1 constraint tests: 10 errors expected here';
76+
END;
77+
$$ LANGUAGE plpgsql;
78+
7379

7480
-- no version
7581
INSERT INTO users (name_encrypted) VALUES (
@@ -120,12 +126,9 @@ INSERT INTO users (name_encrypted) VALUES (
120126
}'::jsonb
121127
);
122128

123-
124-
125129
-- pt
126130
INSERT INTO users (name_encrypted) VALUES (
127131
'{
128-
"v": 1,
129132
"v": 1,
130133
"k": "pt",
131134
"i": {
@@ -138,7 +141,6 @@ INSERT INTO users (name_encrypted) VALUES (
138141
--pt with ciphertext
139142
INSERT INTO users (name_encrypted) VALUES (
140143
'{
141-
"v": 1,
142144
"v": 1,
143145
"k": "pt",
144146
"c": "ciphertext",
@@ -149,11 +151,9 @@ INSERT INTO users (name_encrypted) VALUES (
149151
}'::jsonb
150152
);
151153

152-
153154
-- ct without ciphertext
154155
INSERT INTO users (name_encrypted) VALUES (
155156
'{
156-
"v": 1,
157157
"v": 1,
158158
"k": "ct",
159159
"i": {
@@ -167,7 +167,6 @@ INSERT INTO users (name_encrypted) VALUES (
167167
-- ct with plaintext
168168
INSERT INTO users (name_encrypted) VALUES (
169169
'{
170-
"v": 1,
171170
"v": 1,
172171
"k": "ct",
173172
"p": "plaintext",
@@ -182,7 +181,6 @@ INSERT INTO users (name_encrypted) VALUES (
182181
-- ciphertext without ct
183182
INSERT INTO users (name_encrypted) VALUES (
184183
'{
185-
"v": 1,
186184
"v": 1,
187185
"c": "ciphertext",
188186
"i": {
@@ -192,6 +190,19 @@ INSERT INTO users (name_encrypted) VALUES (
192190
}'::jsonb
193191
);
194192

193+
-- ciphertext with invalid q
194+
INSERT INTO users (name_encrypted) VALUES (
195+
'{
196+
"v": 1,
197+
"c": "ciphertext",
198+
"i": {
199+
"t": "users",
200+
"c": "name"
201+
},
202+
"q": "invalid"
203+
}'::jsonb
204+
);
205+
195206
-- Nothing should be in the DB
196207
DO $$
197208
BEGIN

0 commit comments

Comments
 (0)