Debian Codegraph @

www/appfunc.html

raw
1 <!DOCTYPE html>
2 <html><head>
3 <meta name="viewport" content="width=device-width, initial-scale=1.0">
4 <meta http-equiv="content-type" content="text/html; charset=UTF-8">
5 <link href="sqlite.css" rel="stylesheet">
6 <title>Application-Defined SQL Functions</title>
7 <!-- path= -->
8 </head>
9 <body>
10 <div class=nosearch>
11 <a href="index.html">
12 <img class="logo" src="images/sqlite370_banner.gif" alt="SQLite" border="0">
13 </a>
14 <div><!-- IE hack to prevent disappearing logo --></div>
15 <div class="tagline desktoponly">
16 Small. Fast. Reliable.<br>Choose any three.
17 </div>
18 <div class="menu mainmenu">
19 <ul>
20 <li><a href="index.html">Home</a>
21 <li class='mobileonly'><a href="javascript:void(0)" onclick='toggle_div("submenu")'>Menu</a>
22 <li class='wideonly'><a href='about.html'>About</a>
23 <li class='desktoponly'><a href="docs.html">Documentation</a>
24 <li class='desktoponly'><a href="download.html">Download</a>
25 <li class='wideonly'><a href='copyright.html'>License</a>
26 <li class='desktoponly'><a href="support.html">Support</a>
27 <li class='desktoponly'><a href="prosupport.html">Purchase</a>
28 <li class='search' id='search_menubutton'>
29 <a href="javascript:void(0)" onclick='toggle_search()'>Search</a>
30 </ul>
31 </div>
32 <div class="menu submenu" id="submenu">
33 <ul>
34 <li><a href='about.html'>About</a>
35 <li><a href='docs.html'>Documentation</a>
36 <li><a href='download.html'>Download</a>
37 <li><a href='support.html'>Support</a>
38 <li><a href='prosupport.html'>Purchase</a>
39 </ul>
40 </div>
41 <div class="searchmenu" id="searchmenu">
42 <form method="GET" action="search">
43 <select name="s" id="searchtype">
44 <option value="d">Search Documentation</option>
45 <option value="c">Search Changelog</option>
46 </select>
47 <input type="text" name="q" id="searchbox" value="">
48 <input type="submit" value="Go">
49 </form>
50 </div>
51 </div>
52 <script>
53 function toggle_div(nm) {
54 var w = document.getElementById(nm);
55 if( w.style.display=="block" ){
56 w.style.display = "none";
57 }else{
58 w.style.display = "block";
59 }
60 }
61 function toggle_search() {
62 var w = document.getElementById("searchmenu");
63 if( w.style.display=="block" ){
64 w.style.display = "none";
65 } else {
66 w.style.display = "block";
67 setTimeout(function(){
68 document.getElementById("searchbox").focus()
69 }, 30);
70 }
71 }
72 function div_off(nm){document.getElementById(nm).style.display="none";}
73 window.onbeforeunload = function(e){div_off("submenu");}
74 /* Disable the Search feature if we are not operating from CGI, since */
75 /* Search is accomplished using CGI and will not work without it. */
76 if( !location.origin || !location.origin.match || !location.origin.match(/http/) ){
77 document.getElementById("search_menubutton").style.display = "none";
78 }
79 /* Used by the Hide/Show button beside syntax diagrams, to toggle the */
80 function hideorshow(btn,obj){
81 var x = document.getElementById(obj);
82 var b = document.getElementById(btn);
83 if( x.style.display!='none' ){
84 x.style.display = 'none';
85 b.innerHTML='show';
86 }else{
87 x.style.display = '';
88 b.innerHTML='hide';
89 }
90 return false;
91 }
92 var antiRobot = 0;
93 function antiRobotGo(){
94 if( antiRobot!=3 ) return;
95 antiRobot = 7;
96 var j = document.getElementById("mtimelink");
97 if(j && j.hasAttribute("data-href")) j.href=j.getAttribute("data-href");
98 }
99 function antiRobotDefense(){
100 document.body.onmousedown=function(){
101 antiRobot |= 2;
102 antiRobotGo();
103 document.body.onmousedown=null;
104 }
105 document.body.onmousemove=function(){
106 antiRobot |= 2;
107 antiRobotGo();
108 document.body.onmousemove=null;
109 }
110 setTimeout(function(){
111 antiRobot |= 1;
112 antiRobotGo();
113 }, 100)
114 antiRobotGo();
115 }
116 antiRobotDefense();
117 </script>
118 <div class=fancy>
119 <div class=nosearch>
120 <div class="fancy_title">
121 Application-Defined SQL Functions
122 </div>
123 <div class="fancy_toc">
124 <a onclick="toggle_toc()">
125 <span class="fancy_toc_mark" id="toc_mk">&#x25ba;</span>
126 Table Of Contents
127 </a>
128 <div id="toc_sub"><div class="fancy-toc1"><a href="#executive_summary">1. Executive Summary</a></div>
129 <div class="fancy-toc1"><a href="#defining_new_sql_functions">2. Defining New SQL Functions</a></div>
130 <div class="fancy-toc2"><a href="#common_parameters">2.1. Common Parameters</a></div>
131 <div class="fancy-toc2"><a href="#multiple_calls_to_sqlite3_create_function_for_the_same_function">2.2. Multiple Calls To sqlite3_create_function() For The Same Function</a></div>
132 <div class="fancy-toc2"><a href="#callbacks">2.3. Callbacks</a></div>
133 <div class="fancy-toc3"><a href="#the_scalar_function_callback">2.3.1. The Scalar Function Callback</a></div>
134 <div class="fancy-toc3"><a href="#the_aggregate_function_callbacks">2.3.2. The Aggregate Function Callbacks</a></div>
135 <div class="fancy-toc3"><a href="#the_window_function_callbacks">2.3.3. The Window Function Callbacks</a></div>
136 <div class="fancy-toc3"><a href="#examples">2.3.4. Examples</a></div>
137 <div class="fancy-toc1"><a href="#security_implications">3. Security Implications</a></div>
138 </div>
139 </div>
140 <script>
141 function toggle_toc(){
142 var sub = document.getElementById("toc_sub")
143 var mk = document.getElementById("toc_mk")
144 if( sub.style.display!="block" ){
145 sub.style.display = "block";
146 mk.innerHTML = "&#x25bc;";
147 } else {
148 sub.style.display = "none";
149 mk.innerHTML = "&#x25ba;";
150 }
151 }
152 </script>
153 </div>
154
155
156
157
158
159 <h1 id="executive_summary"><span>1. </span>Executive Summary</h1>
160
161 <p>Applications that use SQLite can define custom SQL functions that call
162 back into application code to compute their results. The custom SQL
163 function implementations can be embedded in the application code itself,
164 or can be <a href="loadext.html">loadable extensions</a>.
165
166 </p><p>Application-defined or custom SQL functions are created using the
167 <a href="c3ref/create_function.html">sqlite3_create_function()</a> family of interfaces.
168 Custom SQL functions can be scalar functions, aggregate functions,
169 or <a href="windowfunctions.html">window functions</a>.
170 Custom SQL functions can have any number of arguments from 0 up to
171 <a href="limits.html#max_function_arg">SQLITE_MAX_FUNCTION_ARG</a>.
172 The <a href="c3ref/create_function.html">sqlite3_create_function()</a> interface specifies callbacks that are
173 invoked to carry out the processing for the new SQL function.
174
175 </p><p>SQLite also supports custom <a href="vtab.html#tabfunc2">table-valued functions</a>, but they are
176 implemented by a different mechanism that is not covered in this document.
177
178 </p><h1 id="defining_new_sql_functions"><span>2. </span>Defining New SQL Functions</h1>
179
180 <p>
181 The <a href="c3ref/create_function.html">sqlite3_create_function()</a> family of interfaces is used to create
182 new custom SQL functions. Each member of this family is a wrapper around
183 a common core. All family members accomplish the same thing; they merely
184 have different calling signatures.
185
186 </p><ul>
187 <li><p><b><a href="c3ref/create_function.html">sqlite3_create_function()</a></b> &rarr;
188 The original version of sqlite3_create_function() allows the application
189 to create a single new SQL function that can be either a scalar or an
190 aggregate. The name of the function is specified using UTF8.
191
192 </p></li><li><p><b><a href="c3ref/create_function.html">sqlite3_create_function16()</a></b> &rarr;
193 This variant works exactly like the sqlite3_create_function() original
194 except that the name of the function itself is specified as a UTF16
195 string rather than as a UTF8 string.
196
197 </p></li><li><p><b><a href="c3ref/create_function.html">sqlite3_create_function_v2()</a></b> &rarr;
198 This variant works like the original sqlite3_create_function() except
199 that it includes an additional parameter that is a pointer to a
200 destructor for the <a href="c3ref/user_data.html">sqlite3_user_data()</a> pointer that is passed in
201 as the 5th argument to all of the sqlite3_create_function() variants.
202 That destructor function (if it is non-NULL) is called when the
203 custom function is deleted - usually when the database connection is
204 closing.
205
206 </p></li><li><p><b><a href="c3ref/create_function.html">sqlite3_create_window_function()</a></b> &rarr;
207 This variant works like the original sqlite3_create_function() except
208 that it accepts a different set of callback pointers - the callback
209 pointers used by <a href="windowfunctions.html">window function</a> definitions.
210 </p></li></ul>
211
212 <h2 id="common_parameters"><span>2.1. </span>Common Parameters</h2>
213
214 <p>Many of the parameters passed to the <a href="c3ref/create_function.html">sqlite3_create_function()</a>
215 family of interfaces are common across the entire family.
216
217 </p><ol>
218 <li><p><b>db</b> &rarr;
219 The 1st parameter is always a pointer to the <a href="c3ref/sqlite3.html">database connection</a>
220 on which the custom SQL function will work. Custom SQL functions are
221 created separately for each database connection. There is no short-hand
222 mechanism for creating SQL functions that work across all database
223 connections.
224
225 </p></li><li><p><b>zFunctionName</b> &rarr;
226 The 2nd parameter is the name of the SQL function that is being
227 created. The name is usually in UTF8, except that the name should
228 be in UTF16 in the native byte order for <a href="c3ref/create_function.html">sqlite3_create_function16()</a>.
229 </p><p>
230 The maximum length of a SQL function name is 255 bytes of UTF8.
231 Any attempt to create a function with a longer name will result in
232 an <a href="rescode.html#misuse">SQLITE_MISUSE</a> error.
233 </p>
234 The SQL function creation interfaces may be called multiple
235 times with the same function name.
236 If two calls have the same function number but a different number of
237 arguments, for example, then two variants of the SQL function will
238 be registered, each taking a different number of arguments.
239
240 </li><li><p><b>nArg</b> &rarr;
241 The 3rd parameter is always the number of arguments that the function
242 accepts. The value must be an integer between -1 and
243 <a href="limits.html#max_function_arg">SQLITE_MAX_FUNCTION_ARG</a> (default value: 127). A value of -1 means
244 that the SQL function is a variadic function that can take any number
245 of arguments between 0 and <a href="limits.html#max_function_arg">SQLITE_MAX_FUNCTION_ARG</a>.
246
247 </p></li><li><p><b>eTextRep</b> &rarr;
248 The 4th parameter is a 32-bit integer flag whose bits convey various
249 properties about the new function. The original purpose of this
250 parameter was to specify the preferred text encoding for the function,
251 using one of the following constants:
252 </p><ul>
253 <li> <a href="c3ref/c_any.html">SQLITE_UTF8</a>
254 </li><li> <a href="c3ref/c_any.html">SQLITE_UTF16BE</a>
255 </li><li> <a href="c3ref/c_any.html">SQLITE_UTF16LE</a>
256 </li></ul>
257 All custom SQL functions will accept text in any encoding. Encoding
258 conversions will happen automatically. The preferred encoding merely
259 specifies the encoding for which the function implementation is optimized.
260 It is possible to specify multiple functions with the same name and the
261 same number of arguments, but different preferred encodings and different
262 callbacks used to implement the function, and SQLite will chose the
263 set of callbacks for which the input encodings most closely match the
264 preferred encoding.
265 <p>
266 The 4th parameter as more recently be extended with additional flag bits
267 to convey additional information about the function. The additional
268 bits include:
269 </p><ul>
270 <li> <a href="c3ref/c_deterministic.html#sqlitedeterministic">SQLITE_DETERMINISTIC</a>
271 </li><li> <a href="c3ref/c_deterministic.html#sqlitedirectonly">SQLITE_DIRECTONLY</a>
272 </li><li> <a href="c3ref/c_deterministic.html#sqliteinnocuous">SQLITE_INNOCUOUS</a>
273 </li><li> <a href="c3ref/c_deterministic.html#sqlitesubtype">SQLITE_SUBTYPE</a>
274 </li></ul>
275 <p>
276 Additional bits may be added in future versions of SQLite.
277
278 </p></li><li><p><b>pApp</b> &rarr;
279 The 5th parameter is an arbitrary pointer that is passed through
280 into the callback routines. SQLite itself does nothing with this
281 pointer, except to make it available to the callbacks, and to pass
282 it into the destructor when the function is unregistered.
283 </p></li></ol>
284
285 <h2 id="multiple_calls_to_sqlite3_create_function_for_the_same_function"><span>2.2. </span>Multiple Calls To sqlite3_create_function() For The Same Function</h2>
286
287 <p>
288 It is common for an application to invoke sqlite3_create_function() multiple
289 times for the same SQL function. For example, if an SQL function can take
290 either 2 or 3 arguments, then sqlite3_create_function() would be invoked
291 once for the 2-argument version and a second time for the 3-argument version.
292 The underlying implementation (the callbacks) can be different for both
293 variants.
294
295 </p><p>
296 An application can also register multiple SQL functions with the same name
297 and same number of arguments, but a different preferred text encoding.
298 In that case, SQLite will invoke the function using the callbacks for
299 the version whose preferred text encoding most closely matches the database
300 text encoding. In this way, multiple implementations of the same function
301 can be provided that are optimized for UTF8 or UTF16.
302
303 </p><p>
304 If multiple calls to sqlite3_create_function() specify the same function name,
305 and the same number of arguments, and the same preferred text encoding, then
306 the callbacks and other parameters of the second call overwrite the first,
307 and the destructor callback from the first call (if it exists) is invoked.
308
309
310 </p><h2 id="callbacks"><span>2.3. </span>Callbacks</h2>
311
312 <p>
313 SQLite evaluates an SQL function by invoking callback routines.
314
315 </p><h3 id="the_scalar_function_callback"><span>2.3.1. </span>The Scalar Function Callback</h3>
316
317 <p>Scalar SQL functions are implemented by a single callback in the
318 <b>xFunc</b> parameter to sqlite3_create_function().
319 The following code demonstrations the implementation of a "noop(X)"
320 scalar SQL function that merely returns its argument:
321
322 </p><div class="codeblock"><pre>static void noopfunc(
323 sqlite3_context *context,
324 int argc,
325 sqlite3_value **argv
326 ){
327 assert( argc==1 );
328 sqlite3_result_value(context, argv&#91;0&#93;);
329 }
330 </pre></div>
331
332 <p>
333 The 1st parameter, <b>context</b>, is a pointer to an opaque object
334 that describes the content from which the SQL function was invoked. This
335 context point becomes the first parameter to many other routines that
336 the function implement might to invoke, including:
337
338 <div class='columns' style='columns: 15em auto;'>
339 <ul style='padding-top:0;'>
340 <li><a href='c3ref/aggregate_context.html'>sqlite3_aggregate_context</a></li>
341 <li><a href='c3ref/context_db_handle.html'>sqlite3_context_db_handle</a></li>
342 <li><a href='c3ref/get_auxdata.html'>sqlite3_get_auxdata</a></li>
343 <li><a href='c3ref/result_blob.html'>sqlite3_result_blob</a></li>
344 <li><a href='c3ref/result_blob.html'>sqlite3_result_blob64</a></li>
345 <li><a href='c3ref/result_blob.html'>sqlite3_result_double</a></li>
346 <li><a href='c3ref/result_blob.html'>sqlite3_result_error</a></li>
347 <li><a href='c3ref/result_blob.html'>sqlite3_result_error16</a></li>
348 <li><a href='c3ref/result_blob.html'>sqlite3_result_error_code</a></li>
349 <li><a href='c3ref/result_blob.html'>sqlite3_result_error_nomem</a></li>
350 <li><a href='c3ref/result_blob.html'>sqlite3_result_error_toobig</a></li>
351 <li><a href='c3ref/result_blob.html'>sqlite3_result_int</a></li>
352 <li><a href='c3ref/result_blob.html'>sqlite3_result_int64</a></li>
353 <li><a href='c3ref/result_blob.html'>sqlite3_result_null</a></li>
354 <li><a href='c3ref/result_blob.html'>sqlite3_result_pointer</a></li>
355 <li><a href='c3ref/result_subtype.html'>sqlite3_result_subtype</a></li>
356 <li><a href='c3ref/result_blob.html'>sqlite3_result_text</a></li>
357 <li><a href='c3ref/result_blob.html'>sqlite3_result_text16</a></li>
358 <li><a href='c3ref/result_blob.html'>sqlite3_result_text16be</a></li>
359 <li><a href='c3ref/result_blob.html'>sqlite3_result_text16le</a></li>
360 <li><a href='c3ref/result_blob.html'>sqlite3_result_text64</a></li>
361 <li><a href='c3ref/result_blob.html'>sqlite3_result_value</a></li>
362 <li><a href='c3ref/result_blob.html'>sqlite3_result_zeroblob</a></li>
363 <li><a href='c3ref/result_blob.html'>sqlite3_result_zeroblob64</a></li>
364 <li><a href='c3ref/get_auxdata.html'>sqlite3_set_auxdata</a></li>
365 <li><a href='c3ref/user_data.html'>sqlite3_user_data</a></li>
366 </ul>
367 </div>
368
369
370 </p><p>The <a href="c3ref/result_blob.html">sqlite3_result() family of functions</a> are
371 used to specify the result of the scalar SQL function. One or more of
372 these should be invoked by the callback to set the function return value.
373 If none of these routines are invoked for a specific callback, then the
374 return value will be NULL.
375
376 </p><p>The <a href="c3ref/user_data.html">sqlite3_user_data()</a> routine returns a copy of the <b>pArg</b>
377 pointer that was given to <a href="c3ref/create_function.html">sqlite3_create_function()</a> when the SQL
378 function was created.
379
380 </p><p>The <a href="c3ref/context_db_handle.html">sqlite3_context_db_handle()</a> routine returns a pointer to the
381 <a href="c3ref/sqlite3.html">database connection</a> object.
382
383 </p><p>The <a href="c3ref/aggregate_context.html">sqlite3_aggregate_context()</a> routine is used only in the
384 implementations of aggregate and window functions. Scalar functions
385 may not use <a href="c3ref/aggregate_context.html">sqlite3_aggregate_context()</a>. The <a href="c3ref/aggregate_context.html">sqlite3_aggregate_context()</a>
386 function is included in the interface list only for completeness.
387
388 </p><p>
389 The 2nd and 3rd arguments to the scalar SQL function implemenetation,
390 <b>argc</b> and <b>argv</b>, are
391 the number of arguments to the SQL function itself and the values for
392 each argument of the SQL function.
393 Argument values can be of any datatype and are thus stored in
394 instances of the <a href="c3ref/value.html">sqlite3_value</a> object.
395 Specific C-language values can be extracted from this object using
396 the <a href="c3ref/value_blob.html">sqlite3_value() family of interfaces</a>.
397
398 </p><h3 id="the_aggregate_function_callbacks"><span>2.3.2. </span>The Aggregate Function Callbacks</h3>
399
400 <p>Aggregate SQL functions are implemented by using two callback
401 functions, <b>xStep</b> and <b>xFinal</b>. The xStep() function
402 is called for each row of the aggregate and the xFinal() function
403 is invoked to compute the final answer at the end.
404 The following (slightly simplified) version of the built-in
405 count() function illustrates:
406
407 </p><div class="codeblock"><pre>typedef struct CountCtx CountCtx;
408 struct CountCtx {
409 i64 n;
410 };
411 static void countStep(sqlite3_context *context, int argc, sqlite3_value **argv){
412 CountCtx *p;
413 p = sqlite3_aggregate_context(context, sizeof(*p));
414 if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(argv&#91;0&#93;)) && p ){
415 p->n++;
416 }
417 }
418 static void countFinalize(sqlite3_context *context){
419 CountCtx *p;
420 p = sqlite3_aggregate_context(context, 0);
421 sqlite3_result_int64(context, p ? p->n : 0);
422 }
423 </pre></div>
424
425 <p>Recall that there are two versions of the count() aggregate.
426 With zero arguments, count() returns a count of the number of rows.
427 With one argument, count() returns the number of times that the
428 argument was non-NULL.
429
430 </p><p>The countStep() callback is invoked once for each row in the aggregate.
431 As you can see, the count is incremented if either there are no arguments,
432 or if the one argument is not NULL.
433
434 </p><p>The step function for an aggregate should always begin with a call
435 to the <a href="c3ref/aggregate_context.html">sqlite3_aggregate_context()</a> routine to fetch the persistent
436 state of the aggregate function. On the first invocation of the step()
437 function, the aggregate context is initialized to a block of memory
438 that is N bytes in size, where N is the second parameter to
439 sqlite3_aggregate_context() and that memory is zeroed. On all subsequent
440 calls to the step() function, the same block of memory is returned.
441 Except, sqlite3_aggregate_context() might return NULL in the case of
442 an out-of-memory error, so aggregate functions should be prepared to
443 deal with that case.
444
445 </p><p>After all rows are processed the countFinalize() routine is called
446 exactly once. This routine computes the final result and invokes
447 one of the <a href="c3ref/result_blob.html">sqlite3_result()</a> family of functions
448 to set the final result. The aggregate context will be freed automatically
449 by SQLite, though the xFinalize() routine must clean up any substructure
450 associated with the aggregate context before it returns. If the xStep()
451 method is called one or more times, then SQLite guarantees thta the
452 xFinal() method will be called at once, even if the query aborts.
453
454 </p><h3 id="the_window_function_callbacks"><span>2.3.3. </span>The Window Function Callbacks</h3>
455
456 <p><a href="windowfunctions.html">Window functions</a> use the same xStep() and xFinal() callbacks that
457 aggregate functions use, plus two others: <b>xValue</b> and <b>xInverse</b>.
458 See the documentation on
459 <a href="windowfunctions.html#udfwinfunc">application-defined window functions</a> for further details.
460
461 </p><h3 id="examples"><span>2.3.4. </span>Examples</h3>
462
463 <p>There are dozens and dozens of SQL function implementations scattered
464 throughout the SQLite source code that can be used as example applications.
465 The built-in SQL functions use the same interface as application-defined
466 SQL functions, so built-in functions can be used as examples too.
467 Search for "sqlite3_context" in the SQLite source code to find examples.
468
469 <a name="sec"></a>
470
471 </p><h1 id="security_implications"><span>3. </span>Security Implications</h1>
472
473 <p>
474 Application-defined SQL functions can become security vulnerabilities if
475 not carefully managed. Suppose, for example, an application defines
476 a new "system(X)" SQL function that runs its argument X as a command and
477 returns the integer result code. Perhaps the implementation is like this:
478
479 </p><div class="codeblock"><pre>static void systemFunc(
480 sqlite3_context *context,
481 int argc,
482 sqlite3_value **argv
483 ){
484 const char *zCmd = (const char*)sqlite3_value_text(argv&#91;0&#93;);
485 if( zCmd!=0 ){
486 int rc = system(zCmd);
487 sqlite3_result_int(context, rc);
488 }
489 }
490 </pre></div>
491
492 <p>
493 This is a function with powerful side-effects. Most programmers would
494 be naturally cautious about using it, but probably would not see the
495 harm in merely having it available. But there is great risk in merely
496 defining such a function, even if the application itself never invokes
497 it!
498
499 </p><p>
500 Suppose the application normally does a query against table TAB1
501 when it starts up. If an attacker can gain access to the database
502 file and modify the schema like this:
503
504 </p><div class="codeblock"><pre>ALTER TABLE tab1 RENAME TO tab1_real;
505 CREATE VIEW tab1 AS SELECT * FROM tab1 WHERE system('rm -rf *') IS NOT NULL;
506 </pre></div>
507
508 <p>
509 Then, when the application attempts to open the database, register the
510 system() function, then run an innocent query against the "tab1" table,
511 it instead deletes all the files in its working directory. Yikes!
512
513 </p><p>
514 To prevent this kind of mischief, applications that create their own
515 custom SQL functions should take one or more of the following safety
516 precautions. The more precautions taken the better:
517
518 </p><ol>
519 <li><p>
520 Invoke <a href="c3ref/db_config.html">sqlite3_db_config</a>(db,<a href="c3ref/c_dbconfig_defensive.html#sqlitedbconfigtrustedschema">SQLITE_DBCONFIG_TRUSTED_SCHEMA</a>,0,0)
521 on each <a href="c3ref/sqlite3.html">database connection</a> as soon as it is opened.
522 This prevents application-defined functions from being used in places
523 where an attacker might be able to surreptiously invoke them by modifying
524 a database schema:
525 </p><ul>
526 <li> In VIEWs.
527 </li><li> In TRIGGERs.
528 </li><li> In CHECK constraints of a table definition.
529 </li><li> In DEFAULT constraints of a table definition.
530 </li><li> In the definitions of generated columns.
531 </li><li> In the expression part of an index on an expression.
532 </li><li> In the WHERE clause of a partial index.
533 </li></ul>
534 <p>
535 To put it another way, this setting requires that application-defined
536 functions only be run directly by top-level SQL invoked from the application
537 itself, not as a consequence of doing some other innocent-looking query.
538
539 </p></li><li><p>
540 Use the <a href="pragma.html#pragma_trusted_schema">PRAGMA trusted_schema=OFF</a> SQL statement to disable trusted
541 schema. This has the same effect as the previous bullet, but does not
542 require the use of C-code and hence can be performed in programs written
543 in another programming language and that do not have access SQLite
544 C-language APIs.
545
546 </p></li><li><p>
547 Compile SQLite using the <a href="compile.html#trusted_schema">-DSQLITE_TRUSTED_SCHEMA=0</a> compile-time option.
548 This make SQLite distrust application-defined functions inside of
549 the schema by default.
550
551 </p></li><li><p>
552 If any application-defined SQL functions have potentially dangerous
553 side-effects, or if they could potentially leak sensitive information
554 to an attacker if misused, then tag those functions using the
555 <a href="c3ref/c_deterministic.html#sqlitedirectonly">SQLITE_DIRECTONLY</a> option on the "enc" parameter. This means
556 that the function can never be run from schema-code even if the
557 trusted-schema option is on.
558
559 </p></li><li><p>
560 Never tag an application-defined SQL function with <a href="c3ref/c_deterministic.html#sqliteinnocuous">SQLITE_INNOCUOUS</a>
561 unless you really need to and you have checked the implementation closely
562 and are certain that it can do no harm even if it falls under the
563 control of an attacker.
564 </p></li></ol>
565 <p align="center"><small><i>This page last modified on <a href="https://sqlite.org/docsrc/honeypot" id="mtimelink" data-href="https://sqlite.org/docsrc/finfo/pages/appfunc.in?m=fe78d4d736">2024-04-16 17:22:18</a> UTC </small></i></p>
566