xref: /sqlite-3.40.0/test/trustschema1.test (revision 05b32ee3)
10dfa5255Sdrh# 2020-01-08
20dfa5255Sdrh#
30dfa5255Sdrh# The author disclaims copyright to this source code.  In place of
40dfa5255Sdrh# a legal notice, here is a blessing:
50dfa5255Sdrh#
60dfa5255Sdrh#    May you do good and not evil.
70dfa5255Sdrh#    May you find forgiveness for yourself and forgive others.
80dfa5255Sdrh#    May you share freely, never taking more than you give.
90dfa5255Sdrh#
100dfa5255Sdrh#***********************************************************************
110dfa5255Sdrh#
120dfa5255Sdrh# Test cases for managing execution of code snippets found in untrusted
130dfa5255Sdrh# schemas.
140dfa5255Sdrh#
150dfa5255Sdrh
160dfa5255Sdrhset testdir [file dirname $argv0]
170dfa5255Sdrhsource $testdir/tester.tcl
180dfa5255Sdrhset testprefix trustschema1
190dfa5255Sdrh
20*05b32ee3Sdrh# edgy functions used in generated columns
21*05b32ee3Sdrh#
220dfa5255Sdrhproc f1 {x} {return $x}
230dfa5255Sdrhdo_test 1.100 {
240dfa5255Sdrh  db function f1 -innocuous -deterministic f1
250dfa5255Sdrh  db function f2 -deterministic f1
260dfa5255Sdrh  db function f3 -directonly -deterministic f1
270dfa5255Sdrh  db eval {
280dfa5255Sdrh    CREATE TABLE t1(a, b AS (f1(a+1)), c AS (f2(a+2)));
290dfa5255Sdrh    INSERT INTO t1 VALUES(100),(200);
300dfa5255Sdrh  }
310dfa5255Sdrh} {}
320dfa5255Sdrhdo_catchsql_test 1.110 {
330dfa5255Sdrh  SELECT a, b, c FROM t1;
340dfa5255Sdrh} {0 {100 101 102 200 201 202}}
350dfa5255Sdrhdo_execsql_test 1.120 {
360dfa5255Sdrh  PRAGMA trusted_schema=OFF;
370dfa5255Sdrh} {}
380dfa5255Sdrhdo_catchsql_test 1.130 {
390dfa5255Sdrh  SELECT a, b FROM t1;
400dfa5255Sdrh} {0 {100 101 200 201}}
410dfa5255Sdrhdo_catchsql_test 1.140 {
420dfa5255Sdrh  SELECT a, b, c FROM t1;
430dfa5255Sdrh} {1 {unsafe use of f2()}}
44*05b32ee3Sdrhdo_catchsql_test 1.150 {
45*05b32ee3Sdrh  PRAGMA trusted_schema=ON;
46*05b32ee3Sdrh  DROP TABLE t1;
47*05b32ee3Sdrh  CREATE TABLE t1(a, b AS (f3(a+1)));
48*05b32ee3Sdrh} {1 {unsafe use of f3()}}
49*05b32ee3Sdrhdo_execsql_test 1.160 {
50*05b32ee3Sdrh  PRAGMA trusted_schema=OFF;
51*05b32ee3Sdrh  CREATE TEMP TABLE temp1(a,b AS (f3(a+1)));
52*05b32ee3Sdrh  INSERT INTO temp1(a) VALUES(100),(900);
53*05b32ee3Sdrh  SELECT * FROM temp1;
54*05b32ee3Sdrh} {100 101 900 901}
550dfa5255Sdrh
56*05b32ee3Sdrh# edgy functions used in CHECK constraints
57*05b32ee3Sdrh#
582eeca204Sdrhdo_catchsql_test 1.200 {
59*05b32ee3Sdrh  PRAGMA trusted_schema=ON;
602eeca204Sdrh  CREATE TABLE t2(a,b,c,CHECK(f3(c)==c));
612eeca204Sdrh} {1 {unsafe use of f3()}}
622eeca204Sdrhdo_catchsql_test 1.210 {
632eeca204Sdrh  PRAGMA trusted_schema=Off;
642eeca204Sdrh  CREATE TABLE t2(a,b,c,CHECK(f2(c)==c));
652eeca204Sdrh} {1 {unsafe use of f2()}}
662eeca204Sdrhdo_catchsql_test 1.211 {
672eeca204Sdrh  PRAGMA trusted_schema=On;
682eeca204Sdrh  CREATE TABLE t2(a,b,c,CHECK(f2(c)==c));
692eeca204Sdrh} {0 {}}
702eeca204Sdrhdo_catchsql_test 1.220 {
712eeca204Sdrh  INSERT INTO t2 VALUES(1,2,3);
722eeca204Sdrh  SELECT * FROM t2;
732eeca204Sdrh} {0 {1 2 3}}
742eeca204Sdrhdo_catchsql_test 1.230 {
752eeca204Sdrh  PRAGMA trusted_schema=off;
762eeca204Sdrh  INSERT INTO t2 VALUES(4,5,6);
772eeca204Sdrh} {1 {unsafe use of f2()}}
782eeca204Sdrhdo_execsql_test 1.231 {
792eeca204Sdrh  SELECT * FROM t2;
802eeca204Sdrh} {1 2 3}
81*05b32ee3Sdrh# Ok to put as many edgy functions as you want in a
82*05b32ee3Sdrh# TEMP table.
83*05b32ee3Sdrhdo_execsql_test 1.240 {
84*05b32ee3Sdrh  PRAGMA trusted_schema=OFF;
85*05b32ee3Sdrh  CREATE TEMP TABLE temp2(a, b, CHECK(f3(b)==b));
86*05b32ee3Sdrh  INSERT INTO temp2(a,b) VALUES(1,2),('x','y');
87*05b32ee3Sdrh  SELECT * FROM temp2;
88*05b32ee3Sdrh} {1 2 x y}
892eeca204Sdrh
90*05b32ee3Sdrh# edgy functions used in DEFAULT constraints
91*05b32ee3Sdrh#
922eeca204Sdrhdo_catchsql_test 1.300 {
932eeca204Sdrh  CREATE TABLE t3(a,b DEFAULT(f2(25)));
942eeca204Sdrh} {0 {}}
952eeca204Sdrhdo_catchsql_test 1.310 {
962eeca204Sdrh  PRAGMA trusted_schema=Off;
972eeca204Sdrh  INSERT INTO t3(a) VALUES(1);
982eeca204Sdrh} {1 {unsafe use of f2()}}
992eeca204Sdrhdo_catchsql_test 1.311 {
1002eeca204Sdrh  INSERT INTO t3(a,b) VALUES(1,2);
1012eeca204Sdrh} {0 {}}
102*05b32ee3Sdrhdo_execsql_test 1.320 {
103*05b32ee3Sdrh  CREATE TEMP TABLE temp3(a, b DEFAULT(f3(31)));
104*05b32ee3Sdrh  INSERT INTO temp3(a) VALUES(22);
105*05b32ee3Sdrh  SELECT * FROM temp3;
106*05b32ee3Sdrh} {22 31}
107*05b32ee3Sdrh
108*05b32ee3Sdrh# edgy functions used in partial indexes.
109*05b32ee3Sdrh#
110*05b32ee3Sdrhdo_execsql_test 1.400 {
111*05b32ee3Sdrh  CREATE TABLE t4(a,b,c);
112*05b32ee3Sdrh  INSERT INTO t4 VALUES(1,2,3),('a','b','c'),(4,'d',0);
113*05b32ee3Sdrh  SELECT * FROM t4;
114*05b32ee3Sdrh  CREATE TEMP TABLE temp4(a,b,c);
115*05b32ee3Sdrh  INSERT INTO temp4 SELECT * FROM t4;
116*05b32ee3Sdrh} {1 2 3 a b c 4 d 0}
117*05b32ee3Sdrhdo_catchsql_test 1.410 {
118*05b32ee3Sdrh  CREATE INDEX t4a ON t4(a) WHERE f3(c);
119*05b32ee3Sdrh} {1 {unsafe use of f3()}}
120*05b32ee3Sdrhdo_catchsql_test 1.420 {
121*05b32ee3Sdrh  PRAGMA trusted_schema=OFF;
122*05b32ee3Sdrh  CREATE INDEX t4a ON t4(a) WHERE f2(c);
123*05b32ee3Sdrh} {1 {unsafe use of f2()}}
124*05b32ee3Sdrhdo_execsql_test 1.421 {
125*05b32ee3Sdrh  CREATE INDEX t4a ON t4(a) WHERE f1(c);
126*05b32ee3Sdrh  SELECT a FROM t4 WHERE f1(c) ORDER BY a;
127*05b32ee3Sdrh} {1}
128*05b32ee3Sdrhdo_execsql_test 1.430 {
129*05b32ee3Sdrh  PRAGMA trusted_schema=ON;
130*05b32ee3Sdrh  CREATE INDEX t4b ON t4(b) WHERE f2(c);
131*05b32ee3Sdrh  SELECT b FROM t4 WHERE f2(c) ORDER BY b;
132*05b32ee3Sdrh} {2}
133*05b32ee3Sdrhdo_execsql_test 1.440 {
134*05b32ee3Sdrh  PRAGMA trusted_schema=OFF;
135*05b32ee3Sdrh  CREATE INDEX temp4a ON temp4(a) WHERE f3(c);
136*05b32ee3Sdrh  SELECT a FROM temp4 WHERE f2(c) ORDER BY a;
137*05b32ee3Sdrh} {1}
138*05b32ee3Sdrh
139*05b32ee3Sdrh# edgy functions used in index expressions
140*05b32ee3Sdrh#
141*05b32ee3Sdrhdo_execsql_test 1.500 {
142*05b32ee3Sdrh  CREATE TABLE t5(a,b,c);
143*05b32ee3Sdrh  INSERT INTO t5 VALUES(1,2,3),(4,5,6),(7,0,-3);
144*05b32ee3Sdrh  SELECT * FROM t5;
145*05b32ee3Sdrh  CREATE TEMP TABLE temp5(a,b,c);
146*05b32ee3Sdrh  INSERT INTO temp5 SELECT * FROM t5;
147*05b32ee3Sdrh} {1 2 3 4 5 6 7 0 -3}
148*05b32ee3Sdrhdo_catchsql_test 1.510 {
149*05b32ee3Sdrh  CREATE INDEX t5x1 ON t5(a+f3(b));
150*05b32ee3Sdrh} {1 {unsafe use of f3()}}
151*05b32ee3Sdrhdo_catchsql_test 1.520 {
152*05b32ee3Sdrh  PRAGMA trusted_schema=OFF;
153*05b32ee3Sdrh  CREATE INDEX t5x1 ON t5(a+f2(b));
154*05b32ee3Sdrh} {1 {unsafe use of f2()}}
155*05b32ee3Sdrhdo_execsql_test 1.521 {
156*05b32ee3Sdrh  CREATE INDEX t5x1 ON t5(a+f1(b));
157*05b32ee3Sdrh  SELECT * FROM t5 INDEXED BY t5x1 WHERE a+f1(b)=3;
158*05b32ee3Sdrh} {1 2 3}
159*05b32ee3Sdrhdo_execsql_test 1.530 {
160*05b32ee3Sdrh  PRAGMA trusted_schema=ON;
161*05b32ee3Sdrh  CREATE INDEX t5x2 ON t5(b+f2(c));
162*05b32ee3Sdrh  SELECT * FROM t5 INDEXED BY t5x2 WHERE b+f2(c)=11;
163*05b32ee3Sdrh} {4 5 6}
164*05b32ee3Sdrhdo_execsql_test 1.540 {
165*05b32ee3Sdrh  PRAGMA trusted_schema=OFF;
166*05b32ee3Sdrh  CREATE INDEX temp5x1 ON temp5(a+f3(b));
167*05b32ee3Sdrh  SELECT * FROM temp5 INDEXED BY temp5x1 WHERE a+f3(b)=7;
168*05b32ee3Sdrh} {7 0 -3}
169*05b32ee3Sdrh
170*05b32ee3Sdrh# edgy functions in VIEWs
171*05b32ee3Sdrh#
172*05b32ee3Sdrhreset_db
173*05b32ee3Sdrhdb function f1 -innocuous -deterministic f1
174*05b32ee3Sdrhdb function f2 -deterministic f1
175*05b32ee3Sdrhdb function f3 -directonly -deterministic f1
176*05b32ee3Sdrhdo_execsql_test 2.100 {
177*05b32ee3Sdrh  CREATE TABLE t1(a,b,c);
178*05b32ee3Sdrh  INSERT INTO t1 VALUES(1,2,3),(100,50,75),(-11,22,-33);
179*05b32ee3Sdrh  CREATE VIEW v1a AS SELECT f3(a+b) FROM t1;
180*05b32ee3Sdrh  SELECT f3(a+b) FROM t1;
181*05b32ee3Sdrh} {3 150 11}
182*05b32ee3Sdrhdo_catchsql_test 2.110 {
183*05b32ee3Sdrh  PRAGMA trusted_schema=ON;
184*05b32ee3Sdrh  SELECT * FROM v1a;
185*05b32ee3Sdrh} {1 {unsafe use of f3()}}
186*05b32ee3Sdrhdo_catchsql_test 2.111 {
187*05b32ee3Sdrh  PRAGMA trusted_schema=OFF;
188*05b32ee3Sdrh  SELECT * FROM v1a;
189*05b32ee3Sdrh} {1 {unsafe use of f3()}}
190*05b32ee3Sdrhdo_execsql_test 2.120 {
191*05b32ee3Sdrh  DROP VIEW v1a;
192*05b32ee3Sdrh  CREATE TEMP VIEW v1a AS SELECT f3(a+b) FROM t1;
193*05b32ee3Sdrh  SELECT * FROM v1a;
194*05b32ee3Sdrh} {3 150 11}
195*05b32ee3Sdrhdo_execsql_test 2.130 {
196*05b32ee3Sdrh  CREATE VIEW v1b AS SELECT f2(b+c) FROM t1;
197*05b32ee3Sdrh  SELECT f2(b+c) FROM t1;
198*05b32ee3Sdrh} {5 125 -11}
199*05b32ee3Sdrhdo_catchsql_test 2.140 {
200*05b32ee3Sdrh  PRAGMA trusted_schema=ON;
201*05b32ee3Sdrh  SELECT * FROM v1b;
202*05b32ee3Sdrh} {0 {5 125 -11}}
203*05b32ee3Sdrhdo_catchsql_test 2.141 {
204*05b32ee3Sdrh  PRAGMA trusted_schema=OFF;
205*05b32ee3Sdrh  SELECT * FROM v1b;
206*05b32ee3Sdrh} {1 {unsafe use of f2()}}
207*05b32ee3Sdrhdo_execsql_test 2.150 {
208*05b32ee3Sdrh  DROP VIEW v1b;
209*05b32ee3Sdrh  CREATE TEMP VIEW v1b AS SELECT f2(b+c) FROM t1;
210*05b32ee3Sdrh  SELECT * FROM v1b;
211*05b32ee3Sdrh} {5 125 -11}
212*05b32ee3Sdrh
213*05b32ee3Sdrh# edgy functions inside of triggers
214*05b32ee3Sdrh#
215*05b32ee3Sdrhdo_execsql_test 3.100 {
216*05b32ee3Sdrh  DELETE FROM t1;
217*05b32ee3Sdrh  CREATE TABLE t2(x);
218*05b32ee3Sdrh  CREATE TRIGGER r1 AFTER INSERT ON t1 BEGIN
219*05b32ee3Sdrh    INSERT INTO t2(x) SELECT f3(new.a);
220*05b32ee3Sdrh  END;
221*05b32ee3Sdrh} {}
222*05b32ee3Sdrhdo_catchsql_test 3.110 {
223*05b32ee3Sdrh  INSERT INTO t1 VALUES(7,6,5);
224*05b32ee3Sdrh} {1 {unsafe use of f3()}}
225*05b32ee3Sdrhdo_execsql_test 3.111 {
226*05b32ee3Sdrh  SELECT * FROM t1;
227*05b32ee3Sdrh  SELECT * FROM t2;
228*05b32ee3Sdrh} {}
229*05b32ee3Sdrh
230*05b32ee3Sdrhdo_execsql_test 3.120 {
231*05b32ee3Sdrh  DROP TRIGGER r1;
232*05b32ee3Sdrh  CREATE TRIGGER r1 AFTER INSERT ON t1 BEGIN
233*05b32ee3Sdrh    INSERT INTO t2(x) SELECT f2(new.a)+100;
234*05b32ee3Sdrh  END;
235*05b32ee3Sdrh  PRAGMA trusted_schema=ON;
236*05b32ee3Sdrh  INSERT INTO t1 VALUES(7,6,5);
237*05b32ee3Sdrh  SELECT * FROM t1, t2;
238*05b32ee3Sdrh} {7 6 5 107}
239*05b32ee3Sdrhdo_catchsql_test 3.130 {
240*05b32ee3Sdrh  DELETE FROM t1;
241*05b32ee3Sdrh  DELETE FROM t2;
242*05b32ee3Sdrh  PRAGMA trusted_schema=OFF;
243*05b32ee3Sdrh  INSERT INTO t1 VALUES(7,6,5);
244*05b32ee3Sdrh} {1 {unsafe use of f2()}}
245*05b32ee3Sdrhdo_execsql_test 3.131 {
246*05b32ee3Sdrh  SELECT * FROM t1;
247*05b32ee3Sdrh  SELECT * FROM t2;
248*05b32ee3Sdrh} {}
249*05b32ee3Sdrh
2502eeca204Sdrh
2510dfa5255Sdrhfinish_test
252