Skip to content

Commit 1ea6c5d

Browse files
authored
Make sure that only arguments that are properties are cheked (#219)
* Make sure that only arguments that are properties are cheked * Check whether all properties are present in the queryables * Optimize query for early property checks
1 parent 634122f commit 1ea6c5d

File tree

5 files changed

+57
-19
lines changed

5 files changed

+57
-19
lines changed

src/pgstac/migrations/pgstac.0.8.1-unreleased.sql

+15-6
Original file line numberDiff line numberDiff line change
@@ -201,12 +201,27 @@ DECLARE
201201
_wrapper text;
202202
leftarg text;
203203
rightarg text;
204+
prop text;
204205
extra_props bool := pgstac.additional_properties();
205206
BEGIN
206207
IF j IS NULL OR (op IS NOT NULL AND args IS NULL) THEN
207208
RETURN NULL;
208209
END IF;
209210
RAISE NOTICE 'CQL2_QUERY: %', j;
211+
212+
-- check if all properties are represented in the queryables
213+
IF NOT extra_props THEN
214+
FOR prop IN
215+
SELECT DISTINCT p->>0
216+
FROM jsonb_path_query(j, 'strict $.**.property') p
217+
WHERE p->>0 NOT IN ('id', 'datetime', 'end_datetime', 'collection')
218+
LOOP
219+
IF (queryable(prop)).nulled_wrapper IS NULL THEN
220+
RAISE EXCEPTION 'Term % is not found in queryables.', prop;
221+
END IF;
222+
END LOOP;
223+
END IF;
224+
210225
IF j ? 'filter' THEN
211226
RETURN cql2_query(j->'filter');
212227
END IF;
@@ -296,12 +311,6 @@ BEGIN
296311
END IF;
297312
END LOOP;
298313

299-
IF
300-
NOT extra_props AND wrapper IS NULL
301-
THEN
302-
RAISE EXCEPTION 'Term % is not found in queryables.', arg->>'property';
303-
END IF;
304-
305314
-- if the property was not in queryables, see if any args were numbers
306315
IF
307316
wrapper IS NULL

src/pgstac/migrations/pgstac.unreleased.sql

+15-6
Original file line numberDiff line numberDiff line change
@@ -1704,12 +1704,27 @@ DECLARE
17041704
_wrapper text;
17051705
leftarg text;
17061706
rightarg text;
1707+
prop text;
17071708
extra_props bool := pgstac.additional_properties();
17081709
BEGIN
17091710
IF j IS NULL OR (op IS NOT NULL AND args IS NULL) THEN
17101711
RETURN NULL;
17111712
END IF;
17121713
RAISE NOTICE 'CQL2_QUERY: %', j;
1714+
1715+
-- check if all properties are represented in the queryables
1716+
IF NOT extra_props THEN
1717+
FOR prop IN
1718+
SELECT DISTINCT p->>0
1719+
FROM jsonb_path_query(j, 'strict $.**.property') p
1720+
WHERE p->>0 NOT IN ('id', 'datetime', 'end_datetime', 'collection')
1721+
LOOP
1722+
IF (queryable(prop)).nulled_wrapper IS NULL THEN
1723+
RAISE EXCEPTION 'Term % is not found in queryables.', prop;
1724+
END IF;
1725+
END LOOP;
1726+
END IF;
1727+
17131728
IF j ? 'filter' THEN
17141729
RETURN cql2_query(j->'filter');
17151730
END IF;
@@ -1799,12 +1814,6 @@ BEGIN
17991814
END IF;
18001815
END LOOP;
18011816

1802-
IF
1803-
NOT extra_props AND wrapper IS NULL
1804-
THEN
1805-
RAISE EXCEPTION 'Term % is not found in queryables.', arg->>'property';
1806-
END IF;
1807-
18081817
-- if the property was not in queryables, see if any args were numbers
18091818
IF
18101819
wrapper IS NULL

src/pgstac/sql/002b_cql.sql

+15-6
Original file line numberDiff line numberDiff line change
@@ -260,12 +260,27 @@ DECLARE
260260
_wrapper text;
261261
leftarg text;
262262
rightarg text;
263+
prop text;
263264
extra_props bool := pgstac.additional_properties();
264265
BEGIN
265266
IF j IS NULL OR (op IS NOT NULL AND args IS NULL) THEN
266267
RETURN NULL;
267268
END IF;
268269
RAISE NOTICE 'CQL2_QUERY: %', j;
270+
271+
-- check if all properties are represented in the queryables
272+
IF NOT extra_props THEN
273+
FOR prop IN
274+
SELECT DISTINCT p->>0
275+
FROM jsonb_path_query(j, 'strict $.**.property') p
276+
WHERE p->>0 NOT IN ('id', 'datetime', 'end_datetime', 'collection')
277+
LOOP
278+
IF (queryable(prop)).nulled_wrapper IS NULL THEN
279+
RAISE EXCEPTION 'Term % is not found in queryables.', prop;
280+
END IF;
281+
END LOOP;
282+
END IF;
283+
269284
IF j ? 'filter' THEN
270285
RETURN cql2_query(j->'filter');
271286
END IF;
@@ -355,12 +370,6 @@ BEGIN
355370
END IF;
356371
END LOOP;
357372

358-
IF
359-
NOT extra_props AND wrapper IS NULL
360-
THEN
361-
RAISE EXCEPTION 'Term % is not found in queryables.', arg->>'property';
362-
END IF;
363-
364373
-- if the property was not in queryables, see if any args were numbers
365374
IF
366375
wrapper IS NULL

src/pgstac/tests/pgtap.sql

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ CREATE EXTENSION IF NOT EXISTS pgtap;
1717
SET SEARCH_PATH TO pgstac, pgtap, public;
1818

1919
-- Plan the tests.
20-
SELECT plan(200);
20+
SELECT plan(202);
2121
--SELECT * FROM no_plan();
2222

2323
-- Run the tests.

src/pgstac/tests/pgtap/002a_queryables.sql

+11
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,17 @@ SELECT lives_ok(
9797
'Make sure a term present in the list of queryables can be used in a filter'
9898
);
9999

100+
SELECT lives_ok(
101+
$$ SELECT search('{"filter": {"and": [{"t_after": [{"property": "datetime"}, "2020-11-11T00:00:00"]}, {"t_before": [{"property": "datetime"}, "2022-11-11T00:00:00"]}]}}'); $$,
102+
'Make sure that only arguments that are properties are checked'
103+
);
104+
105+
SELECT throws_ok(
106+
$$ SELECT search('{"filter": {"and": [{"t_after": [{"property": "datetime"}, "2020-11-11T00:00:00"]}, {"eq": [{"property": "xyzzy"}, "dummy"]}]}}'); $$,
107+
'Term xyzzy is not found in queryables.',
108+
'Make sure a term not present in the list of queryables cannot be used in a filter with nested arguments'
109+
);
110+
100111
SET pgstac.additional_properties to 'true';
101112

102113
SELECT results_eq(

0 commit comments

Comments
 (0)