|
| 1 | +<!--- |
| 2 | + Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + or more contributor license agreements. See the NOTICE file |
| 4 | + distributed with this work for additional information |
| 5 | + regarding copyright ownership. The ASF licenses this file |
| 6 | + to you under the Apache License, Version 2.0 (the |
| 7 | + "License"); you may not use this file except in compliance |
| 8 | + with the License. You may obtain a copy of the License at |
| 9 | +
|
| 10 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +
|
| 12 | + Unless required by applicable law or agreed to in writing, |
| 13 | + software distributed under the License is distributed on an |
| 14 | + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + KIND, either express or implied. See the License for the |
| 16 | + specific language governing permissions and limitations |
| 17 | + under the License. |
| 18 | +--> |
| 19 | + |
| 20 | +# Securing DataFusion |
| 21 | + |
| 22 | +DataFusion is an embedded query engine, not an authorization boundary. If an |
| 23 | +application accepts SQL from users, the application is responsible for deciding |
| 24 | +which data and operations each user may access. The settings below can reduce |
| 25 | +what a query can do, but they do not replace application authorization or |
| 26 | +operating-system isolation. |
| 27 | + |
| 28 | +## Restrict SQL statements |
| 29 | + |
| 30 | +[`SQLOptions`] allows an application to reject classes of SQL statements when |
| 31 | +creating a `DataFrame`. DDL, DML, and other statements are all allowed by |
| 32 | +default. Disable the classes that the application does not need and pass the |
| 33 | +options to `SessionContext::sql_with_options` for every user-provided query: |
| 34 | + |
| 35 | +```rust |
| 36 | +use datafusion::prelude::*; |
| 37 | + |
| 38 | +let options = SQLOptions::new() |
| 39 | + .with_allow_ddl(false) |
| 40 | + .with_allow_dml(false) |
| 41 | + .with_allow_statements(false); |
| 42 | + |
| 43 | +let dataframe = ctx.sql_with_options(sql, options).await?; |
| 44 | +``` |
| 45 | + |
| 46 | +These checks reject statement types such as `CREATE TABLE`, `INSERT`, and |
| 47 | +`SET`; they do not decide which tables or rows a user is authorized to read. |
| 48 | +Expose only the appropriate catalogs and tables to each user, and enforce |
| 49 | +application-specific access rules separately. |
| 50 | + |
| 51 | +## Limit file access |
| 52 | + |
| 53 | +[`SessionContext::enable_url_table()`] is an opt-in feature that lets SQL query |
| 54 | +local files by path. Leave it disabled when users should only query tables |
| 55 | +registered by the application. If it is needed, run DataFusion with filesystem |
| 56 | +permissions limited to the files the application intends to expose. |
| 57 | + |
| 58 | +## Set query memory limits |
| 59 | + |
| 60 | +The `datafusion.runtime.memory_limit` setting defaults to `NULL` (no configured |
| 61 | +query memory limit). Set an appropriate limit for the workload using the |
| 62 | +[runtime configuration settings](../user-guide/configs.md#runtime-configuration-settings). |
| 63 | +This limits memory used by DataFusion's query execution memory pool; use |
| 64 | +process- or container-level resource limits as well when a hard bound on total |
| 65 | +application memory is required. |
| 66 | + |
| 67 | +Also review the capabilities of custom table providers, functions, and other |
| 68 | +extensions registered by the application: they determine which external data |
| 69 | +and operations queries can reach. |
| 70 | + |
| 71 | +[sqloptions]: https://docs.rs/datafusion/latest/datafusion/execution/context/struct.SQLOptions.html |
| 72 | +[sessioncontext::enable_url_table()]: https://docs.rs/datafusion/latest/datafusion/execution/context/struct.SessionContext.html#method.enable_url_table |
0 commit comments