mirror of
https://github.com/simonw/datasette.git
synced 2026-09-17 05:54:18 +02:00
Compare commits
4 commits
main
...
insert-dia
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
693f4ed601 | ||
|
|
76e6a3bc38 | ||
|
|
f3fa6ecf61 | ||
|
|
a76646e3fd |
223 changed files with 6155 additions and 28340 deletions
39
.github/actions/setup-sqlite-version/action.yml
vendored
39
.github/actions/setup-sqlite-version/action.yml
vendored
|
|
@ -1,39 +0,0 @@
|
||||||
name: "Setup SQLite version"
|
|
||||||
description: "Build and activate a specific SQLite version from its amalgamation archive"
|
|
||||||
inputs:
|
|
||||||
version:
|
|
||||||
description: "The SQLite version to install"
|
|
||||||
required: true
|
|
||||||
cflags:
|
|
||||||
description: "CFLAGS to use when compiling SQLite"
|
|
||||||
required: false
|
|
||||||
default: ""
|
|
||||||
skip-activate:
|
|
||||||
description: "Set to true to skip modifying the library path"
|
|
||||||
required: false
|
|
||||||
default: "false"
|
|
||||||
fallback-urls:
|
|
||||||
description: "Whitespace-separated fallback download URLs to try after sqlite.org"
|
|
||||||
required: false
|
|
||||||
default: ""
|
|
||||||
outputs:
|
|
||||||
sqlite-location:
|
|
||||||
description: "Directory containing the compiled SQLite library"
|
|
||||||
value: ${{ steps.build.outputs.sqlite-location }}
|
|
||||||
runs:
|
|
||||||
using: "composite"
|
|
||||||
steps:
|
|
||||||
- shell: bash
|
|
||||||
run: mkdir -p "$RUNNER_TEMP/sqlite-versions/downloads"
|
|
||||||
- uses: actions/cache@v6
|
|
||||||
with:
|
|
||||||
path: ${{ runner.temp }}/sqlite-versions/downloads
|
|
||||||
key: setup-sqlite-version-${{ inputs.version }}-amalgamation-v1
|
|
||||||
- id: build
|
|
||||||
shell: bash
|
|
||||||
run: bash "$GITHUB_ACTION_PATH/setup-sqlite-version.sh"
|
|
||||||
env:
|
|
||||||
SQLITE_VERSION: ${{ inputs.version }}
|
|
||||||
SQLITE_CFLAGS: ${{ inputs.cflags }}
|
|
||||||
SQLITE_SKIP_ACTIVATE: ${{ inputs.skip-activate }}
|
|
||||||
SQLITE_EXTRA_FALLBACK_URLS: ${{ inputs.fallback-urls }}
|
|
||||||
|
|
@ -1,144 +0,0 @@
|
||||||
#!/usr/bin/env bash
|
|
||||||
set -euo pipefail
|
|
||||||
|
|
||||||
version_spec="${SQLITE_VERSION:?SQLITE_VERSION is required}"
|
|
||||||
cflags="${SQLITE_CFLAGS:-}"
|
|
||||||
skip_activate="${SQLITE_SKIP_ACTIVATE:-false}"
|
|
||||||
extra_fallback_urls="${SQLITE_EXTRA_FALLBACK_URLS:-}"
|
|
||||||
|
|
||||||
case "$version_spec" in
|
|
||||||
3.46 | 3.46.0)
|
|
||||||
sqlite_version="3.46.0"
|
|
||||||
sqlite_year="2024"
|
|
||||||
amalgamation_id="3460000"
|
|
||||||
builtin_fallback_urls="https://static.simonwillison.net/static/2026/sqlite-amalgamation-3460000.zip"
|
|
||||||
;;
|
|
||||||
3.25 | 3.25.0)
|
|
||||||
sqlite_version="3.25.0"
|
|
||||||
sqlite_year="2018"
|
|
||||||
amalgamation_id="3250000"
|
|
||||||
builtin_fallback_urls="https://static.simonwillison.net/static/2026/sqlite-amalgamation-3250000.zip?v=1"
|
|
||||||
;;
|
|
||||||
*)
|
|
||||||
echo "::error::Unsupported SQLite version '$version_spec'. Add its release year and amalgamation id to $GITHUB_ACTION_PATH/setup-sqlite-version.sh."
|
|
||||||
exit 1
|
|
||||||
;;
|
|
||||||
esac
|
|
||||||
|
|
||||||
case "$(uname -s)" in
|
|
||||||
Linux)
|
|
||||||
library_name="libsqlite3.so.0"
|
|
||||||
library_path_var="LD_LIBRARY_PATH"
|
|
||||||
;;
|
|
||||||
Darwin)
|
|
||||||
library_name="libsqlite3.dylib"
|
|
||||||
library_path_var="DYLD_LIBRARY_PATH"
|
|
||||||
;;
|
|
||||||
*)
|
|
||||||
echo "::error::Unsupported platform $(uname -s)"
|
|
||||||
exit 1
|
|
||||||
;;
|
|
||||||
esac
|
|
||||||
|
|
||||||
runner_temp="${RUNNER_TEMP:-}"
|
|
||||||
if [ -z "$runner_temp" ]; then
|
|
||||||
runner_temp="$(mktemp -d)"
|
|
||||||
fi
|
|
||||||
|
|
||||||
filename="sqlite-amalgamation-${amalgamation_id}"
|
|
||||||
official_url="https://www.sqlite.org/${sqlite_year}/${filename}.zip"
|
|
||||||
download_dir="${runner_temp}/sqlite-versions/downloads"
|
|
||||||
source_root="${runner_temp}/sqlite-versions/source"
|
|
||||||
source_dir="${source_root}/${filename}"
|
|
||||||
build_dir="${runner_temp}/sqlite-versions/build/${sqlite_version}"
|
|
||||||
archive_path="${download_dir}/${filename}.zip"
|
|
||||||
|
|
||||||
mkdir -p "$download_dir" "$source_root" "$build_dir"
|
|
||||||
|
|
||||||
download_archive() {
|
|
||||||
local url
|
|
||||||
local candidate_path="${archive_path}.tmp"
|
|
||||||
local urls=("$official_url")
|
|
||||||
|
|
||||||
for url in $builtin_fallback_urls $extra_fallback_urls; do
|
|
||||||
urls+=("$url")
|
|
||||||
done
|
|
||||||
|
|
||||||
rm -f "$candidate_path"
|
|
||||||
for url in "${urls[@]}"; do
|
|
||||||
echo "Downloading SQLite ${sqlite_version} amalgamation from ${url}"
|
|
||||||
if curl \
|
|
||||||
--fail \
|
|
||||||
--location \
|
|
||||||
--show-error \
|
|
||||||
--retry 5 \
|
|
||||||
--retry-delay 2 \
|
|
||||||
--retry-max-time 180 \
|
|
||||||
--retry-all-errors \
|
|
||||||
--connect-timeout 20 \
|
|
||||||
--max-time 240 \
|
|
||||||
--output "$candidate_path" \
|
|
||||||
"$url"; then
|
|
||||||
mv "$candidate_path" "$archive_path"
|
|
||||||
return 0
|
|
||||||
fi
|
|
||||||
|
|
||||||
echo "::warning::Download failed from ${url}"
|
|
||||||
rm -f "$candidate_path"
|
|
||||||
done
|
|
||||||
|
|
||||||
echo "::error::Could not download SQLite ${sqlite_version} amalgamation"
|
|
||||||
return 1
|
|
||||||
}
|
|
||||||
|
|
||||||
if [ ! -f "${source_dir}/sqlite3.c" ]; then
|
|
||||||
if [ ! -f "$archive_path" ]; then
|
|
||||||
download_archive
|
|
||||||
fi
|
|
||||||
|
|
||||||
rm -rf "$source_dir"
|
|
||||||
unzip -q "$archive_path" -d "$source_root"
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [ ! -f "${source_dir}/sqlite3.c" ]; then
|
|
||||||
echo "::error::Expected ${source_dir}/sqlite3.c after extracting ${archive_path}"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
read -r -a cflag_args <<< "$cflags"
|
|
||||||
|
|
||||||
echo "Compiling SQLite ${sqlite_version} to ${build_dir}/${library_name}"
|
|
||||||
gcc \
|
|
||||||
-fPIC \
|
|
||||||
-shared \
|
|
||||||
"${cflag_args[@]}" \
|
|
||||||
"${source_dir}/sqlite3.c" \
|
|
||||||
"-I${source_dir}" \
|
|
||||||
-o "${build_dir}/${library_name}"
|
|
||||||
|
|
||||||
if [ "$library_name" = "libsqlite3.so.0" ]; then
|
|
||||||
ln -sf "$library_name" "${build_dir}/libsqlite3.so"
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [ -n "${GITHUB_OUTPUT:-}" ]; then
|
|
||||||
echo "sqlite-location=${build_dir}" >> "$GITHUB_OUTPUT"
|
|
||||||
else
|
|
||||||
echo "sqlite-location=${build_dir}"
|
|
||||||
fi
|
|
||||||
|
|
||||||
case "$(printf '%s' "$skip_activate" | tr '[:upper:]' '[:lower:]')" in
|
|
||||||
true | 1 | yes)
|
|
||||||
echo "Skipping ${library_path_var} activation"
|
|
||||||
;;
|
|
||||||
*)
|
|
||||||
existing_value="${!library_path_var:-}"
|
|
||||||
if [ -n "${GITHUB_ENV:-}" ]; then
|
|
||||||
if [ -n "$existing_value" ]; then
|
|
||||||
echo "${library_path_var}=${build_dir}:${existing_value}" >> "$GITHUB_ENV"
|
|
||||||
else
|
|
||||||
echo "${library_path_var}=${build_dir}" >> "$GITHUB_ENV"
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
echo "Added ${build_dir} to ${library_path_var}"
|
|
||||||
;;
|
|
||||||
esac
|
|
||||||
56
.github/workflows/deploy-latest.yml
vendored
56
.github/workflows/deploy-latest.yml
vendored
|
|
@ -14,46 +14,24 @@ jobs:
|
||||||
deploy:
|
deploy:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: Check deployment prerequisites
|
|
||||||
id: deployment-prerequisites
|
|
||||||
env:
|
|
||||||
GCP_SA_KEY: ${{ secrets.GCP_SA_KEY }}
|
|
||||||
LATEST_DATASETTE_SECRET: ${{ secrets.LATEST_DATASETTE_SECRET }}
|
|
||||||
run: |
|
|
||||||
missing=()
|
|
||||||
for variable in GCP_SA_KEY LATEST_DATASETTE_SECRET; do
|
|
||||||
if [[ -z "${!variable:-}" ]]; then
|
|
||||||
missing+=("$variable")
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
if (( ${#missing[@]} )); then
|
|
||||||
echo "::notice::Skipping deployment because required environment variables are missing: ${missing[*]}"
|
|
||||||
echo "available=false" >> "$GITHUB_OUTPUT"
|
|
||||||
else
|
|
||||||
echo "available=true" >> "$GITHUB_OUTPUT"
|
|
||||||
fi
|
|
||||||
- name: Check out datasette
|
- name: Check out datasette
|
||||||
if: ${{ steps.deployment-prerequisites.outputs.available == 'true' }}
|
uses: actions/checkout@v6
|
||||||
uses: actions/checkout@v7
|
|
||||||
- name: Set up Python
|
- name: Set up Python
|
||||||
if: ${{ steps.deployment-prerequisites.outputs.available == 'true' }}
|
|
||||||
uses: actions/setup-python@v6
|
uses: actions/setup-python@v6
|
||||||
with:
|
with:
|
||||||
python-version: "3.13"
|
python-version: "3.13"
|
||||||
cache: pip
|
cache: pip
|
||||||
- name: Install Python dependencies
|
- name: Install Python dependencies
|
||||||
if: ${{ steps.deployment-prerequisites.outputs.available == 'true' }}
|
|
||||||
run: |
|
run: |
|
||||||
python -m pip install --upgrade pip
|
python -m pip install --upgrade pip
|
||||||
python -m pip install . --group dev
|
python -m pip install . --group dev
|
||||||
python -m pip install sphinx-to-sqlite==0.1a1 "s3-credentials>=0.17"
|
python -m pip install sphinx-to-sqlite==0.1a1
|
||||||
- name: Run tests
|
- name: Run tests
|
||||||
if: ${{ steps.deployment-prerequisites.outputs.available == 'true' && github.ref == 'refs/heads/main' }}
|
if: ${{ github.ref == 'refs/heads/main' }}
|
||||||
run: |
|
run: |
|
||||||
pytest -n auto -m "not serial"
|
pytest -n auto -m "not serial"
|
||||||
pytest -m "serial"
|
pytest -m "serial"
|
||||||
- name: Build fixtures.db and other files needed to deploy the demo
|
- name: Build fixtures.db and other files needed to deploy the demo
|
||||||
if: ${{ steps.deployment-prerequisites.outputs.available == 'true' }}
|
|
||||||
run: |-
|
run: |-
|
||||||
python tests/fixtures.py \
|
python tests/fixtures.py \
|
||||||
fixtures.db \
|
fixtures.db \
|
||||||
|
|
@ -61,18 +39,14 @@ jobs:
|
||||||
fixtures-metadata.json \
|
fixtures-metadata.json \
|
||||||
plugins \
|
plugins \
|
||||||
--extra-db-filename extra_database.db
|
--extra-db-filename extra_database.db
|
||||||
# Package the config with the plugins, excluding test-only plugin secrets
|
|
||||||
# that reference temporary files outside the deployed container.
|
|
||||||
jq 'del(.plugins)' fixtures-config.json > plugins/fixtures-config.json
|
|
||||||
- name: Build docs.db
|
- name: Build docs.db
|
||||||
if: ${{ steps.deployment-prerequisites.outputs.available == 'true' && github.ref == 'refs/heads/main' }}
|
if: ${{ github.ref == 'refs/heads/main' }}
|
||||||
run: |-
|
run: |-
|
||||||
cd docs
|
cd docs
|
||||||
DISABLE_SPHINX_INLINE_TABS=1 sphinx-build -b xml . _build
|
DISABLE_SPHINX_INLINE_TABS=1 sphinx-build -b xml . _build
|
||||||
sphinx-to-sqlite ../docs.db _build
|
sphinx-to-sqlite ../docs.db _build
|
||||||
cd ..
|
cd ..
|
||||||
- name: Set up the alternate-route demo
|
- name: Set up the alternate-route demo
|
||||||
if: ${{ steps.deployment-prerequisites.outputs.available == 'true' }}
|
|
||||||
run: |
|
run: |
|
||||||
echo '
|
echo '
|
||||||
from datasette import hookimpl
|
from datasette import hookimpl
|
||||||
|
|
@ -84,7 +58,6 @@ jobs:
|
||||||
' > plugins/alternative_route.py
|
' > plugins/alternative_route.py
|
||||||
cp fixtures.db fixtures2.db
|
cp fixtures.db fixtures2.db
|
||||||
- name: And the counters writable stored query demo
|
- name: And the counters writable stored query demo
|
||||||
if: ${{ steps.deployment-prerequisites.outputs.available == 'true' }}
|
|
||||||
run: |
|
run: |
|
||||||
cat > plugins/counters.py <<EOF
|
cat > plugins/counters.py <<EOF
|
||||||
from datasette import hookimpl
|
from datasette import hookimpl
|
||||||
|
|
@ -124,15 +97,12 @@ jobs:
|
||||||
# cat metadata.json
|
# cat metadata.json
|
||||||
- id: auth
|
- id: auth
|
||||||
name: Authenticate to Google Cloud
|
name: Authenticate to Google Cloud
|
||||||
if: ${{ steps.deployment-prerequisites.outputs.available == 'true' }}
|
|
||||||
uses: google-github-actions/auth@v3
|
uses: google-github-actions/auth@v3
|
||||||
with:
|
with:
|
||||||
credentials_json: ${{ secrets.GCP_SA_KEY }}
|
credentials_json: ${{ secrets.GCP_SA_KEY }}
|
||||||
- name: Set up Cloud SDK
|
- name: Set up Cloud SDK
|
||||||
if: ${{ steps.deployment-prerequisites.outputs.available == 'true' }}
|
|
||||||
uses: google-github-actions/setup-gcloud@v3
|
uses: google-github-actions/setup-gcloud@v3
|
||||||
- name: Deploy to Cloud Run
|
- name: Deploy to Cloud Run
|
||||||
if: ${{ steps.deployment-prerequisites.outputs.available == 'true' }}
|
|
||||||
env:
|
env:
|
||||||
LATEST_DATASETTE_SECRET: ${{ secrets.LATEST_DATASETTE_SECRET }}
|
LATEST_DATASETTE_SECRET: ${{ secrets.LATEST_DATASETTE_SECRET }}
|
||||||
run: |-
|
run: |-
|
||||||
|
|
@ -147,16 +117,16 @@ jobs:
|
||||||
--plugins-dir=plugins \
|
--plugins-dir=plugins \
|
||||||
--branch=$GITHUB_SHA \
|
--branch=$GITHUB_SHA \
|
||||||
--version-note=$GITHUB_SHA \
|
--version-note=$GITHUB_SHA \
|
||||||
--extra-options="--config plugins/fixtures-config.json --setting template_debug 1 --setting trace_debug 1 --crossdb --root" \
|
--extra-options="--setting template_debug 1 --setting trace_debug 1 --crossdb --root" \
|
||||||
--install 'datasette-ephemeral-tables>=0.2.2' \
|
--install 'datasette-ephemeral-tables>=0.2.2' \
|
||||||
--service "datasette-latest$SUFFIX" \
|
--service "datasette-latest$SUFFIX" \
|
||||||
--secret $LATEST_DATASETTE_SECRET
|
--secret $LATEST_DATASETTE_SECRET
|
||||||
- name: Upload latest documentation database to S3 (only for main)
|
- name: Deploy to docs as well (only for main)
|
||||||
if: ${{ steps.deployment-prerequisites.outputs.available == 'true' && github.ref == 'refs/heads/main' }}
|
if: ${{ github.ref == 'refs/heads/main' }}
|
||||||
env:
|
|
||||||
AWS_ACCESS_KEY_ID: ${{ secrets.S3_DATASETTE_DOCS_ACCESS_KEY }}
|
|
||||||
AWS_SECRET_ACCESS_KEY: ${{ secrets.S3_DATASETTE_DOCS_SECRET_KEY }}
|
|
||||||
run: |-
|
run: |-
|
||||||
# Keep development documentation separate from the stable release database.
|
# Deploy docs.db to a different service
|
||||||
s3-credentials put-object datasette-docs latest/docs.db docs.db \
|
datasette publish cloudrun docs.db \
|
||||||
--content-type application/octet-stream
|
--branch=$GITHUB_SHA \
|
||||||
|
--version-note=$GITHUB_SHA \
|
||||||
|
--extra-options="--setting template_debug 1" \
|
||||||
|
--service=datasette-docs-latest
|
||||||
|
|
|
||||||
16
.github/workflows/documentation-links.yml
vendored
Normal file
16
.github/workflows/documentation-links.yml
vendored
Normal file
|
|
@ -0,0 +1,16 @@
|
||||||
|
name: Read the Docs Pull Request Preview
|
||||||
|
on:
|
||||||
|
pull_request:
|
||||||
|
types:
|
||||||
|
- opened
|
||||||
|
|
||||||
|
permissions:
|
||||||
|
pull-requests: write
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
documentation-links:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: readthedocs/actions/preview@v1
|
||||||
|
with:
|
||||||
|
project-slug: "datasette"
|
||||||
12
.github/workflows/playwright.yml
vendored
12
.github/workflows/playwright.yml
vendored
|
|
@ -2,15 +2,9 @@ name: Playwright
|
||||||
|
|
||||||
on:
|
on:
|
||||||
push:
|
push:
|
||||||
branches:
|
|
||||||
- main
|
|
||||||
pull_request:
|
pull_request:
|
||||||
workflow_dispatch:
|
workflow_dispatch:
|
||||||
|
|
||||||
concurrency:
|
|
||||||
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
|
|
||||||
cancel-in-progress: ${{ github.event_name == 'pull_request' }}
|
|
||||||
|
|
||||||
permissions:
|
permissions:
|
||||||
contents: read
|
contents: read
|
||||||
|
|
||||||
|
|
@ -22,7 +16,7 @@ jobs:
|
||||||
matrix:
|
matrix:
|
||||||
browser: [chromium, firefox, webkit]
|
browser: [chromium, firefox, webkit]
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v7
|
- uses: actions/checkout@v6
|
||||||
- name: Set up Python 3.14
|
- name: Set up Python 3.14
|
||||||
uses: actions/setup-python@v6
|
uses: actions/setup-python@v6
|
||||||
with:
|
with:
|
||||||
|
|
@ -31,14 +25,14 @@ jobs:
|
||||||
cache: pip
|
cache: pip
|
||||||
cache-dependency-path: pyproject.toml
|
cache-dependency-path: pyproject.toml
|
||||||
- name: Cache uv
|
- name: Cache uv
|
||||||
uses: actions/cache@v6
|
uses: actions/cache@v5
|
||||||
with:
|
with:
|
||||||
path: ~/.cache/uv
|
path: ~/.cache/uv
|
||||||
key: ${{ runner.os }}-py3.14-uv-${{ hashFiles('pyproject.toml') }}
|
key: ${{ runner.os }}-py3.14-uv-${{ hashFiles('pyproject.toml') }}
|
||||||
restore-keys: |
|
restore-keys: |
|
||||||
${{ runner.os }}-py3.14-uv-
|
${{ runner.os }}-py3.14-uv-
|
||||||
- name: Cache Playwright browsers
|
- name: Cache Playwright browsers
|
||||||
uses: actions/cache@v6
|
uses: actions/cache@v5
|
||||||
with:
|
with:
|
||||||
path: ~/.cache/ms-playwright/
|
path: ~/.cache/ms-playwright/
|
||||||
key: ${{ runner.os }}-playwright-${{ matrix.browser }}-${{ hashFiles('pyproject.toml') }}
|
key: ${{ runner.os }}-playwright-${{ matrix.browser }}-${{ hashFiles('pyproject.toml') }}
|
||||||
|
|
|
||||||
15
.github/workflows/prettier.yml
vendored
15
.github/workflows/prettier.yml
vendored
|
|
@ -1,15 +1,6 @@
|
||||||
name: Check JavaScript for conformance with Prettier
|
name: Check JavaScript for conformance with Prettier
|
||||||
|
|
||||||
on:
|
on: [push]
|
||||||
push:
|
|
||||||
branches:
|
|
||||||
- main
|
|
||||||
pull_request:
|
|
||||||
workflow_dispatch:
|
|
||||||
|
|
||||||
concurrency:
|
|
||||||
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
|
|
||||||
cancel-in-progress: ${{ github.event_name == 'pull_request' }}
|
|
||||||
|
|
||||||
permissions:
|
permissions:
|
||||||
contents: read
|
contents: read
|
||||||
|
|
@ -19,8 +10,8 @@ jobs:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: Check out repo
|
- name: Check out repo
|
||||||
uses: actions/checkout@v7
|
uses: actions/checkout@v6
|
||||||
- uses: actions/cache@v6
|
- uses: actions/cache@v5
|
||||||
name: Configure npm caching
|
name: Configure npm caching
|
||||||
with:
|
with:
|
||||||
path: ~/.npm
|
path: ~/.npm
|
||||||
|
|
|
||||||
32
.github/workflows/publish.yml
vendored
32
.github/workflows/publish.yml
vendored
|
|
@ -2,7 +2,7 @@ name: Publish Python Package
|
||||||
|
|
||||||
on:
|
on:
|
||||||
release:
|
release:
|
||||||
types: [published]
|
types: [created]
|
||||||
|
|
||||||
permissions:
|
permissions:
|
||||||
contents: read
|
contents: read
|
||||||
|
|
@ -14,7 +14,7 @@ jobs:
|
||||||
matrix:
|
matrix:
|
||||||
python-version: ["3.10", "3.11", "3.12", "3.13", "3.14"]
|
python-version: ["3.10", "3.11", "3.12", "3.13", "3.14"]
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v7
|
- uses: actions/checkout@v6
|
||||||
- name: Set up Python ${{ matrix.python-version }}
|
- name: Set up Python ${{ matrix.python-version }}
|
||||||
uses: actions/setup-python@v6
|
uses: actions/setup-python@v6
|
||||||
with:
|
with:
|
||||||
|
|
@ -35,7 +35,7 @@ jobs:
|
||||||
permissions:
|
permissions:
|
||||||
id-token: write
|
id-token: write
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v7
|
- uses: actions/checkout@v6
|
||||||
- name: Set up Python
|
- name: Set up Python
|
||||||
uses: actions/setup-python@v6
|
uses: actions/setup-python@v6
|
||||||
with:
|
with:
|
||||||
|
|
@ -51,14 +51,12 @@ jobs:
|
||||||
- name: Publish
|
- name: Publish
|
||||||
uses: pypa/gh-action-pypi-publish@release/v1
|
uses: pypa/gh-action-pypi-publish@release/v1
|
||||||
|
|
||||||
# After the first non-prerelease 1.0 release, disable this job on 0.65.x,
|
|
||||||
# even for later 0.65 releases, so they cannot overwrite the 1.0 stable docs.
|
|
||||||
deploy_static_docs:
|
deploy_static_docs:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
needs: [deploy]
|
needs: [deploy]
|
||||||
if: "!github.event.release.prerelease"
|
if: "!github.event.release.prerelease"
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v7
|
- uses: actions/checkout@v6
|
||||||
- name: Set up Python
|
- name: Set up Python
|
||||||
uses: actions/setup-python@v6
|
uses: actions/setup-python@v6
|
||||||
with:
|
with:
|
||||||
|
|
@ -68,27 +66,33 @@ jobs:
|
||||||
- name: Install dependencies
|
- name: Install dependencies
|
||||||
run: |
|
run: |
|
||||||
python -m pip install . --group dev
|
python -m pip install . --group dev
|
||||||
python -m pip install sphinx-to-sqlite==0.1a1 "s3-credentials>=0.17"
|
python -m pip install sphinx-to-sqlite==0.1a1
|
||||||
- name: Build docs.db
|
- name: Build docs.db
|
||||||
run: |-
|
run: |-
|
||||||
cd docs
|
cd docs
|
||||||
DISABLE_SPHINX_INLINE_TABS=1 sphinx-build -b xml . _build
|
DISABLE_SPHINX_INLINE_TABS=1 sphinx-build -b xml . _build
|
||||||
sphinx-to-sqlite ../docs.db _build
|
sphinx-to-sqlite ../docs.db _build
|
||||||
cd ..
|
cd ..
|
||||||
- name: Upload stable documentation database to S3
|
- id: auth
|
||||||
env:
|
name: Authenticate to Google Cloud
|
||||||
AWS_ACCESS_KEY_ID: ${{ secrets.S3_DATASETTE_DOCS_ACCESS_KEY }}
|
uses: google-github-actions/auth@v2
|
||||||
AWS_SECRET_ACCESS_KEY: ${{ secrets.S3_DATASETTE_DOCS_SECRET_KEY }}
|
with:
|
||||||
|
credentials_json: ${{ secrets.GCP_SA_KEY }}
|
||||||
|
- name: Set up Cloud SDK
|
||||||
|
uses: google-github-actions/setup-gcloud@v3
|
||||||
|
- name: Deploy stable-docs.datasette.io to Cloud Run
|
||||||
run: |-
|
run: |-
|
||||||
s3-credentials put-object datasette-docs docs.db docs.db \
|
gcloud config set run/region us-central1
|
||||||
--content-type application/octet-stream
|
gcloud config set project datasette-222320
|
||||||
|
datasette publish cloudrun docs.db \
|
||||||
|
--service=datasette-docs-stable
|
||||||
|
|
||||||
deploy_docker:
|
deploy_docker:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
needs: [deploy]
|
needs: [deploy]
|
||||||
if: "!github.event.release.prerelease"
|
if: "!github.event.release.prerelease"
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v7
|
- uses: actions/checkout@v6
|
||||||
- name: Build and push to Docker Hub
|
- name: Build and push to Docker Hub
|
||||||
env:
|
env:
|
||||||
DOCKER_USER: ${{ secrets.DOCKER_USER }}
|
DOCKER_USER: ${{ secrets.DOCKER_USER }}
|
||||||
|
|
|
||||||
2
.github/workflows/push_docker_tag.yml
vendored
2
.github/workflows/push_docker_tag.yml
vendored
|
|
@ -13,7 +13,7 @@ jobs:
|
||||||
deploy_docker:
|
deploy_docker:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v7
|
- uses: actions/checkout@v6
|
||||||
- name: Build and push to Docker Hub
|
- name: Build and push to Docker Hub
|
||||||
env:
|
env:
|
||||||
DOCKER_USER: ${{ secrets.DOCKER_USER }}
|
DOCKER_USER: ${{ secrets.DOCKER_USER }}
|
||||||
|
|
|
||||||
13
.github/workflows/spellcheck.yml
vendored
13
.github/workflows/spellcheck.yml
vendored
|
|
@ -1,15 +1,6 @@
|
||||||
name: Check spelling in documentation
|
name: Check spelling in documentation
|
||||||
|
|
||||||
on:
|
on: [push, pull_request]
|
||||||
push:
|
|
||||||
branches:
|
|
||||||
- main
|
|
||||||
pull_request:
|
|
||||||
workflow_dispatch:
|
|
||||||
|
|
||||||
concurrency:
|
|
||||||
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
|
|
||||||
cancel-in-progress: ${{ github.event_name == 'pull_request' }}
|
|
||||||
|
|
||||||
permissions:
|
permissions:
|
||||||
contents: read
|
contents: read
|
||||||
|
|
@ -18,7 +9,7 @@ jobs:
|
||||||
spellcheck:
|
spellcheck:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v7
|
- uses: actions/checkout@v6
|
||||||
- name: Set up Python
|
- name: Set up Python
|
||||||
uses: actions/setup-python@v6
|
uses: actions/setup-python@v6
|
||||||
with:
|
with:
|
||||||
|
|
|
||||||
2
.github/workflows/stable-docs.yml
vendored
2
.github/workflows/stable-docs.yml
vendored
|
|
@ -15,7 +15,7 @@ jobs:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout repository
|
- name: Checkout repository
|
||||||
uses: actions/checkout@v7
|
uses: actions/checkout@v6
|
||||||
with:
|
with:
|
||||||
fetch-depth: 0 # We need all commits to find docs/ changes
|
fetch-depth: 0 # We need all commits to find docs/ changes
|
||||||
- name: Set up Git user
|
- name: Set up Git user
|
||||||
|
|
|
||||||
40
.github/workflows/test-coverage.yml
vendored
Normal file
40
.github/workflows/test-coverage.yml
vendored
Normal file
|
|
@ -0,0 +1,40 @@
|
||||||
|
name: Calculate test coverage
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- main
|
||||||
|
pull_request:
|
||||||
|
branches:
|
||||||
|
- main
|
||||||
|
permissions:
|
||||||
|
contents: read
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
test:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: Check out datasette
|
||||||
|
uses: actions/checkout@v6
|
||||||
|
- name: Set up Python
|
||||||
|
uses: actions/setup-python@v6
|
||||||
|
with:
|
||||||
|
python-version: '3.12'
|
||||||
|
cache: 'pip'
|
||||||
|
cache-dependency-path: '**/pyproject.toml'
|
||||||
|
- name: Install Python dependencies
|
||||||
|
run: |
|
||||||
|
python -m pip install --upgrade pip
|
||||||
|
python -m pip install . --group dev
|
||||||
|
python -m pip install pytest-cov
|
||||||
|
- name: Run tests
|
||||||
|
run: |-
|
||||||
|
ls -lah
|
||||||
|
cat .coveragerc
|
||||||
|
pytest -m "not serial" --cov=datasette --cov-config=.coveragerc --cov-report xml:coverage.xml --cov-report term -x
|
||||||
|
ls -lah
|
||||||
|
- name: Upload coverage report
|
||||||
|
uses: codecov/codecov-action@v1
|
||||||
|
with:
|
||||||
|
token: ${{ secrets.CODECOV_TOKEN }}
|
||||||
|
file: coverage.xml
|
||||||
10
.github/workflows/test-pyodide.yml
vendored
10
.github/workflows/test-pyodide.yml
vendored
|
|
@ -2,15 +2,9 @@ name: Test in Pyodide with shot-scraper
|
||||||
|
|
||||||
on:
|
on:
|
||||||
push:
|
push:
|
||||||
branches:
|
|
||||||
- main
|
|
||||||
pull_request:
|
pull_request:
|
||||||
workflow_dispatch:
|
workflow_dispatch:
|
||||||
|
|
||||||
concurrency:
|
|
||||||
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
|
|
||||||
cancel-in-progress: ${{ github.event_name == 'pull_request' }}
|
|
||||||
|
|
||||||
permissions:
|
permissions:
|
||||||
contents: read
|
contents: read
|
||||||
|
|
||||||
|
|
@ -18,7 +12,7 @@ jobs:
|
||||||
test:
|
test:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v7
|
- uses: actions/checkout@v6
|
||||||
- name: Set up Python 3.10
|
- name: Set up Python 3.10
|
||||||
uses: actions/setup-python@v6
|
uses: actions/setup-python@v6
|
||||||
with:
|
with:
|
||||||
|
|
@ -26,7 +20,7 @@ jobs:
|
||||||
cache: 'pip'
|
cache: 'pip'
|
||||||
cache-dependency-path: '**/pyproject.toml'
|
cache-dependency-path: '**/pyproject.toml'
|
||||||
- name: Cache Playwright browsers
|
- name: Cache Playwright browsers
|
||||||
uses: actions/cache@v6
|
uses: actions/cache@v5
|
||||||
with:
|
with:
|
||||||
path: ~/.cache/ms-playwright/
|
path: ~/.cache/ms-playwright/
|
||||||
key: ${{ runner.os }}-browsers
|
key: ${{ runner.os }}-browsers
|
||||||
|
|
|
||||||
19
.github/workflows/test-sqlite-support.yml
vendored
19
.github/workflows/test-sqlite-support.yml
vendored
|
|
@ -1,15 +1,6 @@
|
||||||
name: Test SQLite versions
|
name: Test SQLite versions
|
||||||
|
|
||||||
on:
|
on: [push, pull_request]
|
||||||
push:
|
|
||||||
branches:
|
|
||||||
- main
|
|
||||||
pull_request:
|
|
||||||
workflow_dispatch:
|
|
||||||
|
|
||||||
concurrency:
|
|
||||||
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
|
|
||||||
cancel-in-progress: ${{ github.event_name == 'pull_request' }}
|
|
||||||
|
|
||||||
permissions:
|
permissions:
|
||||||
contents: read
|
contents: read
|
||||||
|
|
@ -21,10 +12,10 @@ jobs:
|
||||||
strategy:
|
strategy:
|
||||||
matrix:
|
matrix:
|
||||||
platform: [ubuntu-latest]
|
platform: [ubuntu-latest]
|
||||||
python-version: ["3.13"]
|
python-version: ["3.10", "3.11", "3.12", "3.13", "3.14"]
|
||||||
sqlite-version: [
|
sqlite-version: [
|
||||||
#"3", # latest version
|
#"3", # latest version
|
||||||
#"3.46",
|
"3.46",
|
||||||
#"3.45",
|
#"3.45",
|
||||||
#"3.27",
|
#"3.27",
|
||||||
#"3.26",
|
#"3.26",
|
||||||
|
|
@ -34,7 +25,7 @@ jobs:
|
||||||
#"3.23.1" # 2018-04-10, before UPSERT
|
#"3.23.1" # 2018-04-10, before UPSERT
|
||||||
]
|
]
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v7
|
- uses: actions/checkout@v6
|
||||||
- name: Set up Python ${{ matrix.python-version }}
|
- name: Set up Python ${{ matrix.python-version }}
|
||||||
uses: actions/setup-python@v6
|
uses: actions/setup-python@v6
|
||||||
with:
|
with:
|
||||||
|
|
@ -43,7 +34,7 @@ jobs:
|
||||||
cache: pip
|
cache: pip
|
||||||
cache-dependency-path: pyproject.toml
|
cache-dependency-path: pyproject.toml
|
||||||
- name: Set up SQLite ${{ matrix.sqlite-version }}
|
- name: Set up SQLite ${{ matrix.sqlite-version }}
|
||||||
uses: ./.github/actions/setup-sqlite-version
|
uses: asg017/sqlite-versions@71ea0de37ae739c33e447af91ba71dda8fcf22e6
|
||||||
with:
|
with:
|
||||||
version: ${{ matrix.sqlite-version }}
|
version: ${{ matrix.sqlite-version }}
|
||||||
cflags: "-DSQLITE_ENABLE_DESERIALIZE -DSQLITE_ENABLE_FTS5 -DSQLITE_ENABLE_FTS4 -DSQLITE_ENABLE_FTS3_PARENTHESIS -DSQLITE_ENABLE_RTREE -DSQLITE_ENABLE_JSON1"
|
cflags: "-DSQLITE_ENABLE_DESERIALIZE -DSQLITE_ENABLE_FTS5 -DSQLITE_ENABLE_FTS4 -DSQLITE_ENABLE_FTS3_PARENTHESIS -DSQLITE_ENABLE_RTREE -DSQLITE_ENABLE_JSON1"
|
||||||
|
|
|
||||||
37
.github/workflows/test.yml
vendored
37
.github/workflows/test.yml
vendored
|
|
@ -1,15 +1,6 @@
|
||||||
name: Test
|
name: Test
|
||||||
|
|
||||||
on:
|
on: [push, pull_request]
|
||||||
push:
|
|
||||||
branches:
|
|
||||||
- main
|
|
||||||
pull_request:
|
|
||||||
workflow_dispatch:
|
|
||||||
|
|
||||||
concurrency:
|
|
||||||
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
|
|
||||||
cancel-in-progress: ${{ github.event_name == 'pull_request' }}
|
|
||||||
|
|
||||||
permissions:
|
permissions:
|
||||||
contents: read
|
contents: read
|
||||||
|
|
@ -18,22 +9,17 @@ jobs:
|
||||||
test:
|
test:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
strategy:
|
strategy:
|
||||||
fail-fast: false
|
|
||||||
matrix:
|
matrix:
|
||||||
python-version: ["3.10", "3.11", "3.12", "3.13", "3.14", "3.15"]
|
python-version: ["3.10", "3.11", "3.12", "3.13", "3.14"]
|
||||||
include:
|
|
||||||
- python-version: "3.14"
|
|
||||||
coverage: true
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v7
|
- uses: actions/checkout@v6
|
||||||
- name: Set up Python ${{ matrix.python-version }}
|
- name: Set up Python ${{ matrix.python-version }}
|
||||||
uses: actions/setup-python@v7
|
uses: actions/setup-python@v6
|
||||||
with:
|
with:
|
||||||
python-version: ${{ matrix.python-version }}
|
python-version: ${{ matrix.python-version }}
|
||||||
allow-prereleases: true
|
allow-prereleases: true
|
||||||
cache: pip
|
cache: pip
|
||||||
cache-dependency-path: pyproject.toml
|
cache-dependency-path: pyproject.toml
|
||||||
check-latest: true
|
|
||||||
- name: Build extension for --load-extension test
|
- name: Build extension for --load-extension test
|
||||||
run: |-
|
run: |-
|
||||||
(cd tests && gcc ext.c -fPIC -shared -o ext.so)
|
(cd tests && gcc ext.c -fPIC -shared -o ext.so)
|
||||||
|
|
@ -41,27 +27,12 @@ jobs:
|
||||||
run: |
|
run: |
|
||||||
pip install . --group dev
|
pip install . --group dev
|
||||||
pip freeze
|
pip freeze
|
||||||
- name: Install pytest-cov
|
|
||||||
if: ${{ matrix.coverage }}
|
|
||||||
run: pip install pytest-cov
|
|
||||||
- name: Run tests
|
- name: Run tests
|
||||||
run: |
|
run: |
|
||||||
if [ "${{ matrix.coverage }}" = "true" ]; then
|
|
||||||
COV="--cov=datasette --cov-config=.coveragerc"
|
|
||||||
pytest -n auto -m "not serial" $COV --cov-report=
|
|
||||||
pytest -m "serial" $COV --cov-append --cov-report xml:coverage.xml --cov-report term
|
|
||||||
else
|
|
||||||
pytest -n auto -m "not serial"
|
pytest -n auto -m "not serial"
|
||||||
pytest -m "serial"
|
pytest -m "serial"
|
||||||
fi
|
|
||||||
# And the test that exceeds a localhost HTTPS server
|
# And the test that exceeds a localhost HTTPS server
|
||||||
tests/test_datasette_https_server.sh
|
tests/test_datasette_https_server.sh
|
||||||
- name: Upload coverage report
|
|
||||||
if: ${{ matrix.coverage }}
|
|
||||||
uses: codecov/codecov-action@v5
|
|
||||||
with:
|
|
||||||
token: ${{ secrets.CODECOV_TOKEN }}
|
|
||||||
files: coverage.xml
|
|
||||||
- name: Black
|
- name: Black
|
||||||
run: |
|
run: |
|
||||||
black --version
|
black --version
|
||||||
|
|
|
||||||
2
.github/workflows/tmate-mac.yml
vendored
2
.github/workflows/tmate-mac.yml
vendored
|
|
@ -10,6 +10,6 @@ jobs:
|
||||||
build:
|
build:
|
||||||
runs-on: macos-latest
|
runs-on: macos-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v7
|
- uses: actions/checkout@v6
|
||||||
- name: Setup tmate session
|
- name: Setup tmate session
|
||||||
uses: mxschmitt/action-tmate@v3
|
uses: mxschmitt/action-tmate@v3
|
||||||
|
|
|
||||||
2
.github/workflows/tmate.yml
vendored
2
.github/workflows/tmate.yml
vendored
|
|
@ -11,7 +11,7 @@ jobs:
|
||||||
build:
|
build:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v7
|
- uses: actions/checkout@v6
|
||||||
- name: Setup tmate session
|
- name: Setup tmate session
|
||||||
uses: mxschmitt/action-tmate@v3
|
uses: mxschmitt/action-tmate@v3
|
||||||
env:
|
env:
|
||||||
|
|
|
||||||
2
.gitignore
vendored
2
.gitignore
vendored
|
|
@ -5,8 +5,6 @@ datasets.json
|
||||||
|
|
||||||
scratchpad
|
scratchpad
|
||||||
|
|
||||||
ignored/
|
|
||||||
|
|
||||||
.vscode
|
.vscode
|
||||||
|
|
||||||
uv.lock
|
uv.lock
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
FROM python:3.11-slim-bookworm AS build
|
FROM python:3.11.0-slim-bullseye as build
|
||||||
|
|
||||||
# Version of Datasette to install, e.g. 0.55
|
# Version of Datasette to install, e.g. 0.55
|
||||||
# docker build . -t datasette --build-arg VERSION=0.55
|
# docker build . -t datasette --build-arg VERSION=0.55
|
||||||
|
|
|
||||||
3
Justfile
3
Justfile
|
|
@ -33,11 +33,10 @@ export DATASETTE_SECRET := "not_a_secret"
|
||||||
uv run codespell datasette -S datasette/static --ignore-words docs/codespell-ignore-words.txt
|
uv run codespell datasette -S datasette/static --ignore-words docs/codespell-ignore-words.txt
|
||||||
uv run codespell tests --ignore-words docs/codespell-ignore-words.txt
|
uv run codespell tests --ignore-words docs/codespell-ignore-words.txt
|
||||||
|
|
||||||
# Run linters: black, ruff, prettier, cog
|
# Run linters: black, ruff, cog
|
||||||
@lint: codespell
|
@lint: codespell
|
||||||
uv run black datasette tests --check
|
uv run black datasette tests --check
|
||||||
uv run ruff check datasette tests
|
uv run ruff check datasette tests
|
||||||
npm run prettier -- --check
|
|
||||||
uv run cog --check README.md docs/*.rst
|
uv run cog --check README.md docs/*.rst
|
||||||
|
|
||||||
# Apply ruff fixes
|
# Apply ruff fixes
|
||||||
|
|
|
||||||
|
|
@ -36,7 +36,7 @@ You can also install it using `pip` or `pipx`:
|
||||||
|
|
||||||
pip install datasette
|
pip install datasette
|
||||||
|
|
||||||
Datasette requires Python 3.10 or higher. We also have [detailed installation instructions](https://docs.datasette.io/en/stable/installation.html) covering other options such as Docker.
|
Datasette requires Python 3.8 or higher. We also have [detailed installation instructions](https://docs.datasette.io/en/stable/installation.html) covering other options such as Docker.
|
||||||
|
|
||||||
## Basic usage
|
## Basic usage
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,15 +1,8 @@
|
||||||
from datasette.permissions import Permission # noqa
|
from datasette.permissions import Permission # noqa
|
||||||
from datasette.version import __version_info__, __version__ # noqa
|
from datasette.version import __version_info__, __version__ # noqa
|
||||||
from datasette.events import Event # noqa
|
from datasette.events import Event # noqa
|
||||||
from datasette.background_tasks import BackgroundTask, BackgroundTaskSupervisor # noqa
|
from datasette.tokens import TokenHandler, TokenRestrictions # noqa
|
||||||
from datasette.tokens import TokenHandler, TokenInvalid, TokenRestrictions # noqa
|
from datasette.utils.asgi import Forbidden, NotFound, Request, Response # noqa
|
||||||
from datasette.utils.asgi import ( # noqa
|
|
||||||
Forbidden,
|
|
||||||
NotFound,
|
|
||||||
PayloadTooLarge,
|
|
||||||
Request,
|
|
||||||
Response,
|
|
||||||
)
|
|
||||||
from datasette.utils import actor_matches_allow # noqa
|
from datasette.utils import actor_matches_allow # noqa
|
||||||
from datasette.views import Context # noqa
|
from datasette.views import Context # noqa
|
||||||
from .hookspecs import hookimpl # noqa
|
from .hookspecs import hookimpl # noqa
|
||||||
|
|
|
||||||
|
|
@ -89,8 +89,7 @@ def pytest_runtest_protocol(item, nextitem):
|
||||||
continue
|
continue
|
||||||
try:
|
try:
|
||||||
ds.close()
|
ds.close()
|
||||||
except Exception as e: # noqa: BLE001
|
except Exception as e:
|
||||||
# Surfaced as a pytest warning; teardown must not fail the run
|
|
||||||
item.warn(
|
item.warn(
|
||||||
pytest.PytestUnraisableExceptionWarning(
|
pytest.PytestUnraisableExceptionWarning(
|
||||||
f"Error closing Datasette instance: {e!r}"
|
f"Error closing Datasette instance: {e!r}"
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,7 @@
|
||||||
import time
|
|
||||||
|
|
||||||
from itsdangerous import BadSignature
|
|
||||||
|
|
||||||
from datasette import hookimpl
|
from datasette import hookimpl
|
||||||
|
from itsdangerous import BadSignature
|
||||||
from datasette.utils import baseconv
|
from datasette.utils import baseconv
|
||||||
|
import time
|
||||||
|
|
||||||
|
|
||||||
@hookimpl
|
@hookimpl
|
||||||
|
|
|
||||||
939
datasette/app.py
939
datasette/app.py
File diff suppressed because it is too large
Load diff
|
|
@ -1,227 +0,0 @@
|
||||||
"""
|
|
||||||
Supervised background-task registration for Datasette core.
|
|
||||||
|
|
||||||
Plugins that need long-lived background work (a polling loop, a queue
|
|
||||||
consumer, a scheduled job runner) register it with
|
|
||||||
``datasette.add_background_task(func, name=None)`` - typically from a
|
|
||||||
``startup`` plugin hook - instead of fire-and-forgetting their own
|
|
||||||
``asyncio.create_task()``. Core owns:
|
|
||||||
|
|
||||||
- **references**: every launched ``asyncio.Task`` is kept alive on a
|
|
||||||
:class:`BackgroundTaskSupervisor`, so it can never be silently garbage
|
|
||||||
collected the way an unreferenced ``create_task()`` call can be;
|
|
||||||
- **launch timing**: registered work is buffered until
|
|
||||||
:meth:`BackgroundTaskSupervisor.launch_all` runs, which core arranges to
|
|
||||||
happen only after *every* plugin's ``startup`` hook has finished - so
|
|
||||||
a task that depends on another plugin having registered something first
|
|
||||||
doesn't need ``tryfirst=True`` ordering tricks;
|
|
||||||
- **crash surfacing**: an unhandled exception in a background task is
|
|
||||||
logged with its full traceback to the ``datasette.background_tasks``
|
|
||||||
logger and recorded on the handle, instead of becoming an "Task
|
|
||||||
exception was never retrieved" warning nobody sees;
|
|
||||||
- **cancellation**: :meth:`BackgroundTaskSupervisor.cancel_all` cancels
|
|
||||||
every task still running and waits (with a grace period) for them to
|
|
||||||
actually stop.
|
|
||||||
"""
|
|
||||||
|
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
import asyncio
|
|
||||||
import datetime
|
|
||||||
import functools
|
|
||||||
import logging
|
|
||||||
from collections.abc import Awaitable, Callable
|
|
||||||
|
|
||||||
logger = logging.getLogger("datasette.background_tasks")
|
|
||||||
|
|
||||||
|
|
||||||
def _utcnow_iso() -> str:
|
|
||||||
return datetime.datetime.now(datetime.timezone.utc).isoformat()
|
|
||||||
|
|
||||||
|
|
||||||
def _function_path(func: Callable) -> str:
|
|
||||||
"""Describe the callable without guessing which plugin registered it."""
|
|
||||||
while isinstance(func, functools.partial):
|
|
||||||
func = func.func
|
|
||||||
if not hasattr(func, "__qualname__"):
|
|
||||||
func = type(func).__call__
|
|
||||||
return f"{func.__module__}.{func.__qualname__}"
|
|
||||||
|
|
||||||
|
|
||||||
class BackgroundTask:
|
|
||||||
"""A handle to a single piece of supervised background work.
|
|
||||||
|
|
||||||
States: ``registered`` (added but not yet launched) -> ``running`` ->
|
|
||||||
one of ``completed`` (returned cleanly), ``crashed`` (raised an
|
|
||||||
exception other than ``CancelledError`` - see ``.exception``), or
|
|
||||||
``cancelled`` (``.cancel()`` was called, or it was still running at
|
|
||||||
shutdown).
|
|
||||||
"""
|
|
||||||
|
|
||||||
def __init__(
|
|
||||||
self,
|
|
||||||
name: str,
|
|
||||||
func: Callable[[object], Awaitable[None]],
|
|
||||||
):
|
|
||||||
self.name = name
|
|
||||||
self.state = "registered"
|
|
||||||
self.task: asyncio.Task | None = None
|
|
||||||
self.exception: BaseException | None = None
|
|
||||||
self.started_at: str | None = None
|
|
||||||
self.function = _function_path(func)
|
|
||||||
self._func = func
|
|
||||||
self._supervisor: BackgroundTaskSupervisor | None = None
|
|
||||||
|
|
||||||
def cancel(self) -> None:
|
|
||||||
"""Cancel this task.
|
|
||||||
|
|
||||||
If it has already been launched, cancels the underlying
|
|
||||||
``asyncio.Task`` - its state becomes ``cancelled`` once the
|
|
||||||
cancellation is observed (asynchronously, via the task's done
|
|
||||||
callback). If it has not been launched yet, this is a no-op as
|
|
||||||
far as asyncio is concerned (there's no task to cancel) but it
|
|
||||||
deregisters the handle from its supervisor so it never runs.
|
|
||||||
"""
|
|
||||||
if self.task is not None:
|
|
||||||
self.task.cancel()
|
|
||||||
elif self._supervisor is not None:
|
|
||||||
self._supervisor._deregister(self)
|
|
||||||
|
|
||||||
def __repr__(self) -> str:
|
|
||||||
return f"<BackgroundTask name={self.name!r} state={self.state!r}>"
|
|
||||||
|
|
||||||
|
|
||||||
class BackgroundTaskSupervisor:
|
|
||||||
"""Owns registration and launch of every :class:`BackgroundTask` for a
|
|
||||||
single ``Datasette`` instance.
|
|
||||||
|
|
||||||
Registration (:meth:`add`) is separate from launch
|
|
||||||
(:meth:`launch_all`): plugins register work whenever convenient
|
|
||||||
(typically from a ``startup`` hook, but request handlers can register
|
|
||||||
dynamic per-job work too), and it either sits buffered until
|
|
||||||
:meth:`launch_all` runs, or - if :meth:`launch_all` has already run -
|
|
||||||
starts immediately.
|
|
||||||
|
|
||||||
Strong references to every :class:`BackgroundTask` (and its
|
|
||||||
``asyncio.Task``) are kept for the life of the instance, by design -
|
|
||||||
that's what makes the enrichments-style "fire-and-forget task gets
|
|
||||||
garbage collected mid-flight" bug impossible here. There is currently
|
|
||||||
no pruning of completed/crashed/cancelled tasks, so a plugin that
|
|
||||||
dynamically registers many short-lived tasks over a long process
|
|
||||||
lifetime (a per-job registration pattern, e.g. one task per queued
|
|
||||||
job) will grow this list without bound. That's an accepted v1
|
|
||||||
trade-off in favour of full introspection (``/-/tasks``); revisit
|
|
||||||
with a pruning or capping policy if unbounded growth is reported in
|
|
||||||
practice.
|
|
||||||
"""
|
|
||||||
|
|
||||||
def __init__(self, datasette):
|
|
||||||
self._datasette = datasette
|
|
||||||
self._tasks: list[BackgroundTask] = []
|
|
||||||
self._names = set()
|
|
||||||
self._launched = False
|
|
||||||
self._lock = asyncio.Lock()
|
|
||||||
|
|
||||||
def add(self, func, name=None) -> BackgroundTask:
|
|
||||||
base_name = name or getattr(func, "__qualname__", None) or repr(func)
|
|
||||||
actual_name = self._unique_name(base_name)
|
|
||||||
handle = BackgroundTask(actual_name, func)
|
|
||||||
handle._supervisor = self
|
|
||||||
self._tasks.append(handle)
|
|
||||||
self._names.add(actual_name)
|
|
||||||
if self._launched:
|
|
||||||
self._launch_one(handle)
|
|
||||||
return handle
|
|
||||||
|
|
||||||
def _unique_name(self, base_name: str) -> str:
|
|
||||||
if base_name not in self._names:
|
|
||||||
return base_name
|
|
||||||
n = 2
|
|
||||||
while f"{base_name}-{n}" in self._names:
|
|
||||||
n += 1
|
|
||||||
return f"{base_name}-{n}"
|
|
||||||
|
|
||||||
def _deregister(self, handle: BackgroundTask) -> None:
|
|
||||||
try:
|
|
||||||
self._tasks.remove(handle)
|
|
||||||
except ValueError:
|
|
||||||
pass
|
|
||||||
self._names.discard(handle.name)
|
|
||||||
|
|
||||||
def _launch_one(self, handle: BackgroundTask) -> None:
|
|
||||||
handle.state = "running"
|
|
||||||
handle.started_at = _utcnow_iso()
|
|
||||||
handle.task = asyncio.create_task(
|
|
||||||
handle._func(self._datasette), name=handle.name
|
|
||||||
)
|
|
||||||
handle.task.add_done_callback(functools.partial(_on_task_done, handle))
|
|
||||||
|
|
||||||
async def launch_all(self) -> None:
|
|
||||||
"""Launch every currently-registered task that hasn't launched
|
|
||||||
yet. Idempotent and safe to call concurrently: subsequent (or
|
|
||||||
racing) calls are no-ops once the first has set ``self._launched``.
|
|
||||||
"""
|
|
||||||
if self._launched:
|
|
||||||
return
|
|
||||||
async with self._lock:
|
|
||||||
if self._launched:
|
|
||||||
return
|
|
||||||
self._launched = True
|
|
||||||
for handle in list(self._tasks):
|
|
||||||
if handle.task is None:
|
|
||||||
self._launch_one(handle)
|
|
||||||
|
|
||||||
async def cancel_all(self, grace: float = 5.0) -> None:
|
|
||||||
"""Cancel every task that isn't already done, then wait up to
|
|
||||||
``grace`` seconds for them to actually finish. Stragglers still
|
|
||||||
running after that are logged by name (but left to finish or not
|
|
||||||
on their own - this does not forcibly kill them, asyncio has no
|
|
||||||
mechanism for that).
|
|
||||||
"""
|
|
||||||
handles_by_task = {
|
|
||||||
handle.task: handle for handle in self._tasks if handle.task is not None
|
|
||||||
}
|
|
||||||
pending = [task for task in handles_by_task if not task.done()]
|
|
||||||
for task in pending:
|
|
||||||
task.cancel()
|
|
||||||
if not pending:
|
|
||||||
return
|
|
||||||
_done, not_done = await asyncio.wait(pending, timeout=grace)
|
|
||||||
if not_done:
|
|
||||||
names = sorted(handles_by_task[task].name for task in not_done)
|
|
||||||
logger.warning(
|
|
||||||
"%d background task(s) did not finish within the %.1fs grace "
|
|
||||||
"period after cancellation: %s",
|
|
||||||
len(names),
|
|
||||||
grace,
|
|
||||||
", ".join(names),
|
|
||||||
)
|
|
||||||
|
|
||||||
def tasks(self) -> list[BackgroundTask]:
|
|
||||||
"""Return every registered :class:`BackgroundTask`, launched or
|
|
||||||
not, in registration order. Used by the ``/-/tasks`` debug
|
|
||||||
endpoint.
|
|
||||||
"""
|
|
||||||
return list(self._tasks)
|
|
||||||
|
|
||||||
@property
|
|
||||||
def launched(self) -> bool:
|
|
||||||
"""Whether :meth:`launch_all` has run yet - lets ``/-/tasks``
|
|
||||||
distinguish "no tasks registered" from "tasks registered but
|
|
||||||
nothing has armed the launch yet" without reaching for the
|
|
||||||
private ``_launched`` attribute.
|
|
||||||
"""
|
|
||||||
return self._launched
|
|
||||||
|
|
||||||
|
|
||||||
def _on_task_done(handle: BackgroundTask, task: asyncio.Task) -> None:
|
|
||||||
if task.cancelled():
|
|
||||||
handle.state = "cancelled"
|
|
||||||
return
|
|
||||||
exc = task.exception()
|
|
||||||
if exc is not None:
|
|
||||||
handle.state = "crashed"
|
|
||||||
handle.exception = exc
|
|
||||||
logger.error("Background task %r crashed", handle.name, exc_info=exc)
|
|
||||||
return
|
|
||||||
handle.state = "completed"
|
|
||||||
|
|
@ -1,8 +1,7 @@
|
||||||
import hashlib
|
|
||||||
|
|
||||||
from datasette import hookimpl
|
from datasette import hookimpl
|
||||||
|
from datasette.utils.asgi import Response, BadRequest
|
||||||
from datasette.utils import to_css_class
|
from datasette.utils import to_css_class
|
||||||
from datasette.utils.asgi import BadRequest, Response
|
import hashlib
|
||||||
|
|
||||||
_BLOB_COLUMN = "_blob_column"
|
_BLOB_COLUMN = "_blob_column"
|
||||||
_BLOB_HASH = "_blob_hash"
|
_BLOB_HASH = "_blob_hash"
|
||||||
|
|
|
||||||
147
datasette/cli.py
147
datasette/cli.py
|
|
@ -1,45 +1,43 @@
|
||||||
import asyncio
|
import asyncio
|
||||||
|
import uvicorn
|
||||||
|
import click
|
||||||
|
from click import formatting
|
||||||
|
from click.types import CompositeParamType
|
||||||
|
from click_default_group import DefaultGroup
|
||||||
import functools
|
import functools
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
import pathlib
|
import pathlib
|
||||||
|
from runpy import run_module
|
||||||
import shutil
|
import shutil
|
||||||
|
from subprocess import call
|
||||||
import sys
|
import sys
|
||||||
import textwrap
|
import textwrap
|
||||||
import webbrowser
|
import webbrowser
|
||||||
from runpy import run_module
|
|
||||||
from subprocess import call
|
|
||||||
|
|
||||||
import click
|
|
||||||
import uvicorn
|
|
||||||
from click import formatting
|
|
||||||
from click.types import CompositeParamType
|
|
||||||
from click_default_group import DefaultGroup
|
|
||||||
|
|
||||||
from .app import (
|
from .app import (
|
||||||
|
Datasette,
|
||||||
DEFAULT_SETTINGS,
|
DEFAULT_SETTINGS,
|
||||||
SETTINGS,
|
SETTINGS,
|
||||||
SQLITE_LIMIT_ATTACHED,
|
SQLITE_LIMIT_ATTACHED,
|
||||||
Datasette,
|
|
||||||
pm,
|
pm,
|
||||||
)
|
)
|
||||||
from .inspect import inspect_tables
|
from .inspect import inspect_tables
|
||||||
from .utils import (
|
from .utils import (
|
||||||
ConnectionProblem,
|
|
||||||
LoadExtension,
|
LoadExtension,
|
||||||
SpatialiteConnectionProblem,
|
|
||||||
SpatialiteNotFound,
|
|
||||||
StartupError,
|
StartupError,
|
||||||
StaticMount,
|
|
||||||
ValueAsBooleanError,
|
|
||||||
check_connection,
|
check_connection,
|
||||||
deep_dict_update,
|
deep_dict_update,
|
||||||
find_spatialite,
|
find_spatialite,
|
||||||
|
parse_metadata,
|
||||||
|
ConnectionProblem,
|
||||||
|
SpatialiteConnectionProblem,
|
||||||
initial_path_for_datasette,
|
initial_path_for_datasette,
|
||||||
pairs_to_nested_config,
|
pairs_to_nested_config,
|
||||||
parse_metadata,
|
|
||||||
temporary_docker_directory,
|
temporary_docker_directory,
|
||||||
value_as_boolean,
|
value_as_boolean,
|
||||||
|
SpatialiteNotFound,
|
||||||
|
StaticMount,
|
||||||
|
ValueAsBooleanError,
|
||||||
)
|
)
|
||||||
from .utils.sqlite import sqlite3
|
from .utils.sqlite import sqlite3
|
||||||
from .utils.testing import TestClient
|
from .utils.testing import TestClient
|
||||||
|
|
@ -77,7 +75,7 @@ class Setting(CompositeParamType):
|
||||||
# Datasette 1.0, we turn bare setting names into setting.name
|
# Datasette 1.0, we turn bare setting names into setting.name
|
||||||
# Type checking for those older settings
|
# Type checking for those older settings
|
||||||
default = DEFAULT_SETTINGS[name]
|
default = DEFAULT_SETTINGS[name]
|
||||||
name = f"settings.{name}"
|
name = "settings.{}".format(name)
|
||||||
if isinstance(default, bool):
|
if isinstance(default, bool):
|
||||||
try:
|
try:
|
||||||
return name, "true" if value_as_boolean(value) else "false"
|
return name, "true" if value_as_boolean(value) else "false"
|
||||||
|
|
@ -173,6 +171,7 @@ async def inspect_(files, sqlite_extensions):
|
||||||
@cli.group()
|
@cli.group()
|
||||||
def publish():
|
def publish():
|
||||||
"""Publish specified SQLite database files to the internet along with a Datasette-powered interface and API"""
|
"""Publish specified SQLite database files to the internet along with a Datasette-powered interface and API"""
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
# Register publish plugins
|
# Register publish plugins
|
||||||
|
|
@ -497,7 +496,6 @@ def uninstall(packages, yes):
|
||||||
"--internal",
|
"--internal",
|
||||||
type=click.Path(),
|
type=click.Path(),
|
||||||
help="Path to a persistent Datasette internal SQLite database",
|
help="Path to a persistent Datasette internal SQLite database",
|
||||||
envvar="DATASETTE_INTERNAL",
|
|
||||||
)
|
)
|
||||||
def serve(
|
def serve(
|
||||||
files,
|
files,
|
||||||
|
|
@ -580,27 +578,27 @@ def serve(
|
||||||
# https://github.com/simonw/datasette/issues/2389
|
# https://github.com/simonw/datasette/issues/2389
|
||||||
deep_dict_update(config_data, settings_updates)
|
deep_dict_update(config_data, settings_updates)
|
||||||
|
|
||||||
kwargs = {
|
kwargs = dict(
|
||||||
"immutables": immutable,
|
immutables=immutable,
|
||||||
"cache_headers": not reload,
|
cache_headers=not reload,
|
||||||
"cors": cors,
|
cors=cors,
|
||||||
"inspect_data": inspect_data,
|
inspect_data=inspect_data,
|
||||||
"config": config_data,
|
config=config_data,
|
||||||
"metadata": metadata_data,
|
metadata=metadata_data,
|
||||||
"sqlite_extensions": sqlite_extensions,
|
sqlite_extensions=sqlite_extensions,
|
||||||
"template_dir": template_dir,
|
template_dir=template_dir,
|
||||||
"plugins_dir": plugins_dir,
|
plugins_dir=plugins_dir,
|
||||||
"static_mounts": static,
|
static_mounts=static,
|
||||||
"settings": None, # These are passed in config= now
|
settings=None, # These are passed in config= now
|
||||||
"memory": memory,
|
memory=memory,
|
||||||
"secret": secret,
|
secret=secret,
|
||||||
"version_note": version_note,
|
version_note=version_note,
|
||||||
"pdb": pdb,
|
pdb=pdb,
|
||||||
"crossdb": crossdb,
|
crossdb=crossdb,
|
||||||
"nolock": nolock,
|
nolock=nolock,
|
||||||
"internal": internal,
|
internal=internal,
|
||||||
"default_deny": default_deny,
|
default_deny=default_deny,
|
||||||
}
|
)
|
||||||
|
|
||||||
# Separate directories from files
|
# Separate directories from files
|
||||||
directories = [f for f in files if os.path.isdir(f)]
|
directories = [f for f in files if os.path.isdir(f)]
|
||||||
|
|
@ -623,7 +621,9 @@ def serve(
|
||||||
conn.close()
|
conn.close()
|
||||||
else:
|
else:
|
||||||
raise click.ClickException(
|
raise click.ClickException(
|
||||||
f"Invalid value for '[FILES]...': Path '{file}' does not exist."
|
"Invalid value for '[FILES]...': Path '{}' does not exist.".format(
|
||||||
|
file
|
||||||
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
# Check for duplicate files by resolving all paths to their absolute forms
|
# Check for duplicate files by resolving all paths to their absolute forms
|
||||||
|
|
@ -664,6 +664,16 @@ def serve(
|
||||||
# Private utility mechanism for writing unit tests
|
# Private utility mechanism for writing unit tests
|
||||||
return ds
|
return ds
|
||||||
|
|
||||||
|
# Run async soundness checks before startup hooks, since invoke_startup
|
||||||
|
# now populates internal tables which requires querying each database
|
||||||
|
run_sync(lambda: check_databases(ds))
|
||||||
|
|
||||||
|
# Run the "startup" plugin hooks
|
||||||
|
try:
|
||||||
|
run_sync(ds.invoke_startup)
|
||||||
|
except StartupError as e:
|
||||||
|
raise click.ClickException(e.args[0])
|
||||||
|
|
||||||
if headers and not get:
|
if headers and not get:
|
||||||
raise click.ClickException("--headers can only be used with --get")
|
raise click.ClickException("--headers can only be used with --get")
|
||||||
|
|
||||||
|
|
@ -671,23 +681,10 @@ def serve(
|
||||||
raise click.ClickException("--token can only be used with --get")
|
raise click.ClickException("--token can only be used with --get")
|
||||||
|
|
||||||
if get:
|
if get:
|
||||||
# --get means we don't run Uvicorn at all
|
|
||||||
run_sync(lambda: check_databases(ds))
|
|
||||||
|
|
||||||
try:
|
|
||||||
run_sync(ds.invoke_startup)
|
|
||||||
except StartupError as e:
|
|
||||||
raise click.ClickException(e.args[0])
|
|
||||||
|
|
||||||
# --get never launches background tasks: TestClient's request below
|
|
||||||
# flows through the full ASGI stack, including the
|
|
||||||
# AsgiRunOnFirstRequest fallback, which would otherwise launch them.
|
|
||||||
ds._suppress_background_tasks = True
|
|
||||||
|
|
||||||
client = TestClient(ds)
|
client = TestClient(ds)
|
||||||
request_headers = {}
|
request_headers = {}
|
||||||
if token:
|
if token:
|
||||||
request_headers["Authorization"] = f"Bearer {token}"
|
request_headers["Authorization"] = "Bearer {}".format(token)
|
||||||
cookies = {}
|
cookies = {}
|
||||||
if actor:
|
if actor:
|
||||||
cookies["ds_actor"] = client.actor_cookie(json.loads(actor))
|
cookies["ds_actor"] = client.actor_cookie(json.loads(actor))
|
||||||
|
|
@ -708,23 +705,6 @@ def serve(
|
||||||
sys.exit(exit_code)
|
sys.exit(exit_code)
|
||||||
return
|
return
|
||||||
|
|
||||||
# check_databases, invoke_startup() and the uvicorn server all run on a
|
|
||||||
# single event loop, so that anything a plugin's "startup" hook schedules
|
|
||||||
# on the loop (asyncio.create_task, Lock/Queue/Event objects, ...) is
|
|
||||||
# still alive when the server starts handling requests.
|
|
||||||
async def _serve_async():
|
|
||||||
# Populate internal catalog tables before invoke_startup
|
|
||||||
await check_databases(ds)
|
|
||||||
|
|
||||||
# Run the full startup sequence (immutable-database table-count
|
|
||||||
# precompute + the "startup" plugin hooks) via the same entry point
|
|
||||||
# AsgiLifespan/AsgiRunOnFirstRequest use, so it's not skipped when
|
|
||||||
# uvicorn's lifespan.startup fires moments later.
|
|
||||||
try:
|
|
||||||
await ds._startup_sequence()
|
|
||||||
except StartupError as e:
|
|
||||||
raise click.ClickException(e.args[0])
|
|
||||||
|
|
||||||
# Start the server
|
# Start the server
|
||||||
url = None
|
url = None
|
||||||
if root:
|
if root:
|
||||||
|
|
@ -736,26 +716,19 @@ def serve(
|
||||||
if open_browser:
|
if open_browser:
|
||||||
if url is None:
|
if url is None:
|
||||||
# Figure out most convenient URL - to table, database or homepage
|
# Figure out most convenient URL - to table, database or homepage
|
||||||
path = await initial_path_for_datasette(ds)
|
path = run_sync(lambda: initial_path_for_datasette(ds))
|
||||||
url = f"http://{host}:{port}{path}"
|
url = f"http://{host}:{port}{path}"
|
||||||
webbrowser.open(url)
|
webbrowser.open(url)
|
||||||
uvicorn_kwargs = {
|
uvicorn_kwargs = dict(
|
||||||
"host": host,
|
host=host, port=port, log_level="info", lifespan="on", workers=1
|
||||||
"port": port,
|
)
|
||||||
"log_level": "info",
|
|
||||||
"lifespan": "on",
|
|
||||||
"workers": 1,
|
|
||||||
}
|
|
||||||
if uds:
|
if uds:
|
||||||
uvicorn_kwargs["uds"] = uds
|
uvicorn_kwargs["uds"] = uds
|
||||||
if ssl_keyfile:
|
if ssl_keyfile:
|
||||||
uvicorn_kwargs["ssl_keyfile"] = ssl_keyfile
|
uvicorn_kwargs["ssl_keyfile"] = ssl_keyfile
|
||||||
if ssl_certfile:
|
if ssl_certfile:
|
||||||
uvicorn_kwargs["ssl_certfile"] = ssl_certfile
|
uvicorn_kwargs["ssl_certfile"] = ssl_certfile
|
||||||
server = uvicorn.Server(uvicorn.Config(ds.app(), **uvicorn_kwargs))
|
uvicorn.run(ds.app(), **uvicorn_kwargs)
|
||||||
await server.serve()
|
|
||||||
|
|
||||||
asyncio.run(_serve_async())
|
|
||||||
|
|
||||||
|
|
||||||
@cli.command()
|
@cli.command()
|
||||||
|
|
@ -912,7 +885,7 @@ async def check_databases(ds):
|
||||||
)
|
)
|
||||||
except ConnectionProblem as e:
|
except ConnectionProblem as e:
|
||||||
raise click.UsageError(
|
raise click.UsageError(
|
||||||
f"Connection to {database.path} failed check: {e.args[0]!s}"
|
f"Connection to {database.path} failed check: {str(e.args[0])}"
|
||||||
)
|
)
|
||||||
# If --crossdb and more than SQLITE_LIMIT_ATTACHED show warning
|
# If --crossdb and more than SQLITE_LIMIT_ATTACHED show warning
|
||||||
if (
|
if (
|
||||||
|
|
@ -920,5 +893,9 @@ async def check_databases(ds):
|
||||||
and len([db for db in ds.databases.values() if not db.is_memory])
|
and len([db for db in ds.databases.values() if not db.is_memory])
|
||||||
> SQLITE_LIMIT_ATTACHED
|
> SQLITE_LIMIT_ATTACHED
|
||||||
):
|
):
|
||||||
msg = f"Warning: --crossdb only works with the first {SQLITE_LIMIT_ATTACHED} attached databases"
|
msg = (
|
||||||
|
"Warning: --crossdb only works with the first {} attached databases".format(
|
||||||
|
SQLITE_LIMIT_ATTACHED
|
||||||
|
)
|
||||||
|
)
|
||||||
click.echo(click.style(msg, bold=True, fg="yellow"), err=True)
|
click.echo(click.style(msg, bold=True, fg="yellow"), err=True)
|
||||||
|
|
|
||||||
|
|
@ -64,14 +64,14 @@ class ColumnType:
|
||||||
Return an HTML string to render this cell value, or None to
|
Return an HTML string to render this cell value, or None to
|
||||||
fall through to the default render_cell plugin hook chain.
|
fall through to the default render_cell plugin hook chain.
|
||||||
"""
|
"""
|
||||||
return
|
return None
|
||||||
|
|
||||||
async def validate(self, value, datasette):
|
async def validate(self, value, datasette):
|
||||||
"""
|
"""
|
||||||
Validate a value before it is written. Return None if valid,
|
Validate a value before it is written. Return None if valid,
|
||||||
or a string error message if invalid.
|
or a string error message if invalid.
|
||||||
"""
|
"""
|
||||||
return
|
return None
|
||||||
|
|
||||||
async def transform_value(self, value, datasette):
|
async def transform_value(self, value, datasette):
|
||||||
"""
|
"""
|
||||||
|
|
|
||||||
|
|
@ -40,12 +40,12 @@ def _origin_tuple(value):
|
||||||
scheme = (parsed.scheme or "").lower()
|
scheme = (parsed.scheme or "").lower()
|
||||||
host = (parsed.hostname or "").lower()
|
host = (parsed.hostname or "").lower()
|
||||||
if not scheme or not host:
|
if not scheme or not host:
|
||||||
raise ValueError(f"missing scheme or host in {value!r}")
|
raise ValueError("missing scheme or host in {!r}".format(value))
|
||||||
port = parsed.port # may raise ValueError on bad ports
|
port = parsed.port # may raise ValueError on bad ports
|
||||||
if port is None:
|
if port is None:
|
||||||
port = DEFAULT_PORTS.get(scheme)
|
port = DEFAULT_PORTS.get(scheme)
|
||||||
if port is None:
|
if port is None:
|
||||||
raise ValueError(f"unknown default port for scheme {scheme!r}")
|
raise ValueError("unknown default port for scheme {!r}".format(scheme))
|
||||||
return scheme, host, port
|
return scheme, host, port
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -125,7 +125,9 @@ class CrossOriginProtectionMiddleware:
|
||||||
return
|
return
|
||||||
await self._forbid(
|
await self._forbid(
|
||||||
send,
|
send,
|
||||||
f"Sec-Fetch-Site was {sec_fetch_site!r}, expected 'same-origin' or 'none'",
|
"Sec-Fetch-Site was {!r}, expected 'same-origin' or 'none'".format(
|
||||||
|
sec_fetch_site
|
||||||
|
),
|
||||||
)
|
)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
@ -139,11 +141,11 @@ class CrossOriginProtectionMiddleware:
|
||||||
request_scheme = self._request_scheme(scope)
|
request_scheme = self._request_scheme(scope)
|
||||||
try:
|
try:
|
||||||
origin_tuple = _origin_tuple(origin)
|
origin_tuple = _origin_tuple(origin)
|
||||||
expected_tuple = _origin_tuple(f"{request_scheme}://{host}")
|
expected_tuple = _origin_tuple("{}://{}".format(request_scheme, host))
|
||||||
except ValueError:
|
except ValueError:
|
||||||
await self._forbid(
|
await self._forbid(
|
||||||
send,
|
send,
|
||||||
f"Malformed Origin {origin!r} or Host {host!r}",
|
"Malformed Origin {!r} or Host {!r}".format(origin, host),
|
||||||
)
|
)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
@ -153,7 +155,7 @@ class CrossOriginProtectionMiddleware:
|
||||||
|
|
||||||
await self._forbid(
|
await self._forbid(
|
||||||
send,
|
send,
|
||||||
f"Origin {origin!r} does not match Host {host!r}",
|
"Origin {!r} does not match Host {!r}".format(origin, host),
|
||||||
)
|
)
|
||||||
|
|
||||||
def _request_scheme(self, scope):
|
def _request_scheme(self, scope):
|
||||||
|
|
@ -161,8 +163,7 @@ class CrossOriginProtectionMiddleware:
|
||||||
try:
|
try:
|
||||||
if self.datasette.setting("force_https_urls"):
|
if self.datasette.setting("force_https_urls"):
|
||||||
return "https"
|
return "https"
|
||||||
except Exception: # noqa: BLE001, S110
|
except Exception:
|
||||||
# Settings may not be readable this early; fall back to the ASGI scheme
|
|
||||||
pass
|
pass
|
||||||
return scope.get("scheme") or "http"
|
return scope.get("scheme") or "http"
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,35 +1,33 @@
|
||||||
import asyncio
|
import asyncio
|
||||||
import atexit
|
import atexit
|
||||||
|
from collections import namedtuple
|
||||||
import inspect
|
import inspect
|
||||||
import os
|
import os
|
||||||
|
from pathlib import Path
|
||||||
import queue
|
import queue
|
||||||
|
import sqlite_utils
|
||||||
import sys
|
import sys
|
||||||
import tempfile
|
import tempfile
|
||||||
import threading
|
import threading
|
||||||
import uuid
|
import uuid
|
||||||
from collections import namedtuple
|
|
||||||
from pathlib import Path
|
|
||||||
|
|
||||||
import sqlite_utils
|
|
||||||
|
|
||||||
from .inspect import inspect_hash
|
|
||||||
from .tracer import trace
|
from .tracer import trace
|
||||||
from .utils import (
|
from .utils import (
|
||||||
call_with_supported_arguments,
|
call_with_supported_arguments,
|
||||||
detect_fts,
|
detect_fts,
|
||||||
detect_primary_keys,
|
detect_primary_keys,
|
||||||
detect_spatialite,
|
detect_spatialite,
|
||||||
escape_sqlite,
|
|
||||||
get_all_foreign_keys,
|
get_all_foreign_keys,
|
||||||
get_outbound_foreign_keys,
|
get_outbound_foreign_keys,
|
||||||
md5_not_usedforsecurity,
|
md5_not_usedforsecurity,
|
||||||
sqlite3,
|
|
||||||
sqlite_timelimit,
|
sqlite_timelimit,
|
||||||
table_column_details,
|
sqlite3,
|
||||||
table_columns,
|
table_columns,
|
||||||
|
table_column_details,
|
||||||
)
|
)
|
||||||
from .utils.sql_analysis import SQLAnalysis, analyze_sql_tables
|
from .utils.sql_analysis import SQLAnalysis, analyze_sql_tables
|
||||||
from .utils.sqlite import sqlite_derived_table_dependencies, sqlite_hidden_table_names
|
from .utils.sqlite import sqlite_hidden_table_names
|
||||||
|
from .inspect import inspect_hash
|
||||||
|
|
||||||
connections = threading.local()
|
connections = threading.local()
|
||||||
|
|
||||||
|
|
@ -85,7 +83,6 @@ class Database:
|
||||||
self.cached_hash = None
|
self.cached_hash = None
|
||||||
self.cached_size = None
|
self.cached_size = None
|
||||||
self._cached_table_counts = None
|
self._cached_table_counts = None
|
||||||
self._cached_derived_table_dependencies = None
|
|
||||||
self._write_thread = None
|
self._write_thread = None
|
||||||
self._write_queue = None
|
self._write_queue = None
|
||||||
self._closed = False
|
self._closed = False
|
||||||
|
|
@ -94,15 +91,16 @@ class Database:
|
||||||
# These are used when in non-threaded mode:
|
# These are used when in non-threaded mode:
|
||||||
self._read_connection = None
|
self._read_connection = None
|
||||||
self._write_connection = None
|
self._write_connection = None
|
||||||
# Track file and memory connections, including reads on worker threads,
|
# This is used to track all file connections so they can be closed
|
||||||
# so close() can release all of them from the calling thread.
|
self._all_file_connections = []
|
||||||
self._all_connections = []
|
|
||||||
if not is_temp_disk:
|
if not is_temp_disk:
|
||||||
self.mode = mode
|
self.mode = mode
|
||||||
|
|
||||||
def _check_not_closed(self):
|
def _check_not_closed(self):
|
||||||
if self._closed:
|
if self._closed:
|
||||||
raise DatasetteClosedError(f"Database {self.name!r} has been closed")
|
raise DatasetteClosedError(
|
||||||
|
"Database {!r} has been closed".format(self.name)
|
||||||
|
)
|
||||||
|
|
||||||
def _remove_pending_execute_future(self, future):
|
def _remove_pending_execute_future(self, future):
|
||||||
with self._pending_execute_futures_lock:
|
with self._pending_execute_futures_lock:
|
||||||
|
|
@ -141,18 +139,15 @@ class Database:
|
||||||
if write:
|
if write:
|
||||||
extra_kwargs["isolation_level"] = "IMMEDIATE"
|
extra_kwargs["isolation_level"] = "IMMEDIATE"
|
||||||
if self.memory_name:
|
if self.memory_name:
|
||||||
uri = f"file:{self.memory_name}?mode=memory&cache=shared"
|
uri = "file:{}?mode=memory&cache=shared".format(self.memory_name)
|
||||||
conn = sqlite3.connect(
|
conn = sqlite3.connect(
|
||||||
uri, uri=True, check_same_thread=False, **extra_kwargs
|
uri, uri=True, check_same_thread=False, **extra_kwargs
|
||||||
)
|
)
|
||||||
if not write:
|
if not write:
|
||||||
conn.execute("PRAGMA query_only=1")
|
conn.execute("PRAGMA query_only=1")
|
||||||
self._all_connections.append(conn)
|
|
||||||
return conn
|
return conn
|
||||||
if self.is_memory:
|
if self.is_memory:
|
||||||
conn = sqlite3.connect(":memory:", uri=True, check_same_thread=False)
|
return sqlite3.connect(":memory:", uri=True)
|
||||||
self._all_connections.append(conn)
|
|
||||||
return conn
|
|
||||||
|
|
||||||
# mode=ro or immutable=1?
|
# mode=ro or immutable=1?
|
||||||
if self.is_mutable:
|
if self.is_mutable:
|
||||||
|
|
@ -169,7 +164,7 @@ class Database:
|
||||||
conn = sqlite3.connect(
|
conn = sqlite3.connect(
|
||||||
f"file:{self.path}{qs}", uri=True, check_same_thread=False, **extra_kwargs
|
f"file:{self.path}{qs}", uri=True, check_same_thread=False, **extra_kwargs
|
||||||
)
|
)
|
||||||
self._all_connections.append(conn)
|
self._all_file_connections.append(conn)
|
||||||
if self.is_temp_disk and not self._wal_enabled:
|
if self.is_temp_disk and not self._wal_enabled:
|
||||||
conn.execute("PRAGMA journal_mode=WAL")
|
conn.execute("PRAGMA journal_mode=WAL")
|
||||||
self._wal_enabled = True
|
self._wal_enabled = True
|
||||||
|
|
@ -197,22 +192,23 @@ class Database:
|
||||||
write_thread.join(timeout=10)
|
write_thread.join(timeout=10)
|
||||||
if write_thread.is_alive():
|
if write_thread.is_alive():
|
||||||
sys.stderr.write(
|
sys.stderr.write(
|
||||||
f"Datasette: write thread for {self.name!r} did not exit within 10s\n"
|
"Datasette: write thread for {!r} did not exit within 10s\n".format(
|
||||||
|
self.name
|
||||||
|
)
|
||||||
)
|
)
|
||||||
sys.stderr.flush()
|
sys.stderr.flush()
|
||||||
for future in pending_execute_futures:
|
for future in pending_execute_futures:
|
||||||
try:
|
try:
|
||||||
future.result()
|
future.result()
|
||||||
except Exception: # noqa: BLE001, S110
|
except Exception:
|
||||||
# Shutdown teardown - a failed pending write must not block close()
|
|
||||||
pass
|
pass
|
||||||
# Close anything still tracked in _all_connections
|
# Close anything still tracked in _all_file_connections
|
||||||
for connection in self._all_connections:
|
for connection in self._all_file_connections:
|
||||||
try:
|
try:
|
||||||
connection.close()
|
connection.close()
|
||||||
except Exception: # noqa: BLE001, S110
|
except Exception:
|
||||||
pass
|
pass
|
||||||
self._all_connections = []
|
self._all_file_connections = []
|
||||||
# Drop per-thread cached read connections we can reach
|
# Drop per-thread cached read connections we can reach
|
||||||
try:
|
try:
|
||||||
delattr(connections, self._thread_local_id)
|
delattr(connections, self._thread_local_id)
|
||||||
|
|
@ -222,13 +218,13 @@ class Database:
|
||||||
if self._read_connection is not None:
|
if self._read_connection is not None:
|
||||||
try:
|
try:
|
||||||
self._read_connection.close()
|
self._read_connection.close()
|
||||||
except Exception: # noqa: BLE001, S110
|
except Exception:
|
||||||
pass
|
pass
|
||||||
self._read_connection = None
|
self._read_connection = None
|
||||||
if self._write_connection is not None:
|
if self._write_connection is not None:
|
||||||
try:
|
try:
|
||||||
self._write_connection.close()
|
self._write_connection.close()
|
||||||
except Exception: # noqa: BLE001, S110
|
except Exception:
|
||||||
pass
|
pass
|
||||||
self._write_connection = None
|
self._write_connection = None
|
||||||
if self.is_temp_disk:
|
if self.is_temp_disk:
|
||||||
|
|
@ -250,34 +246,19 @@ class Database:
|
||||||
request=None,
|
request=None,
|
||||||
return_all=False,
|
return_all=False,
|
||||||
returning_limit=EXECUTE_WRITE_RETURNING_LIMIT,
|
returning_limit=EXECUTE_WRITE_RETURNING_LIMIT,
|
||||||
transaction=True,
|
|
||||||
time_limit_ms=2000,
|
|
||||||
):
|
):
|
||||||
self._check_not_closed()
|
self._check_not_closed()
|
||||||
if returning_limit < 0:
|
if returning_limit < 0:
|
||||||
raise ValueError("returning_limit must be >= 0")
|
raise ValueError("returning_limit must be >= 0")
|
||||||
|
|
||||||
def execute_sql(conn):
|
def _inner(conn):
|
||||||
cursor = conn.execute(sql, params or [])
|
cursor = conn.execute(sql, params or [])
|
||||||
return ExecuteWriteResult.from_cursor(
|
return ExecuteWriteResult.from_cursor(
|
||||||
cursor, return_all=return_all, returning_limit=returning_limit
|
cursor, return_all=return_all, returning_limit=returning_limit
|
||||||
)
|
)
|
||||||
|
|
||||||
def _inner(conn):
|
|
||||||
try:
|
|
||||||
if time_limit_ms is None:
|
|
||||||
return execute_sql(conn)
|
|
||||||
with sqlite_timelimit(conn, time_limit_ms):
|
|
||||||
return execute_sql(conn)
|
|
||||||
except (sqlite3.OperationalError, sqlite3.DatabaseError) as e:
|
|
||||||
if e.args == ("interrupted",):
|
|
||||||
raise QueryInterrupted(e, sql, params)
|
|
||||||
raise
|
|
||||||
|
|
||||||
with trace("sql", database=self.name, sql=sql.strip(), params=params):
|
with trace("sql", database=self.name, sql=sql.strip(), params=params):
|
||||||
results = await self.execute_write_fn(
|
results = await self.execute_write_fn(_inner, block=block, request=request)
|
||||||
_inner, block=block, request=request, transaction=transaction
|
|
||||||
)
|
|
||||||
return results
|
return results
|
||||||
|
|
||||||
async def execute_write_script(self, sql, block=True, request=None):
|
async def execute_write_script(self, sql, block=True, request=None):
|
||||||
|
|
@ -328,9 +309,9 @@ class Database:
|
||||||
finally:
|
finally:
|
||||||
isolated_connection.close()
|
isolated_connection.close()
|
||||||
try:
|
try:
|
||||||
self._all_connections.remove(isolated_connection)
|
self._all_file_connections.remove(isolated_connection)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
# May already have been cleared by close().
|
# Was probably a memory connection
|
||||||
pass
|
pass
|
||||||
|
|
||||||
if self.ds.executor is None:
|
if self.ds.executor is None:
|
||||||
|
|
@ -367,19 +348,9 @@ class Database:
|
||||||
self.ds._prepare_connection(self._write_connection, self.name)
|
self.ds._prepare_connection(self._write_connection, self.name)
|
||||||
if transaction:
|
if transaction:
|
||||||
with self._write_connection:
|
with self._write_connection:
|
||||||
self._write_connection.execute("BEGIN IMMEDIATE")
|
|
||||||
result = fn(self._write_connection)
|
result = fn(self._write_connection)
|
||||||
else:
|
else:
|
||||||
result = fn(self._write_connection)
|
result = fn(self._write_connection)
|
||||||
if not block:
|
|
||||||
# There is no write thread here, so the write has already
|
|
||||||
# finished. Hand back the same (task_id, reply_future) shape
|
|
||||||
# _send_to_write_thread() returns, with the future already
|
|
||||||
# resolved, so the block=False path below is identical in
|
|
||||||
# both modes.
|
|
||||||
reply_future = asyncio.get_running_loop().create_future()
|
|
||||||
reply_future.set_result(result)
|
|
||||||
result = (uuid.uuid4(), reply_future)
|
|
||||||
else:
|
else:
|
||||||
result = await self._send_to_write_thread(
|
result = await self._send_to_write_thread(
|
||||||
fn, block=block, transaction=transaction
|
fn, block=block, transaction=transaction
|
||||||
|
|
@ -395,8 +366,7 @@ class Database:
|
||||||
async def _dispatch_events_after_write():
|
async def _dispatch_events_after_write():
|
||||||
try:
|
try:
|
||||||
await reply_future
|
await reply_future
|
||||||
except Exception: # noqa: BLE001
|
except Exception:
|
||||||
# The write failed; skip success events regardless of why
|
|
||||||
# if the write failed, don't emit success events
|
# if the write failed, don't emit success events
|
||||||
return
|
return
|
||||||
for event in pending_events:
|
for event in pending_events:
|
||||||
|
|
@ -449,9 +419,11 @@ class Database:
|
||||||
self._write_thread = threading.Thread(
|
self._write_thread = threading.Thread(
|
||||||
target=self._execute_writes, daemon=True
|
target=self._execute_writes, daemon=True
|
||||||
)
|
)
|
||||||
self._write_thread.name = f"_execute_writes for database {self.name}"
|
self._write_thread.name = "_execute_writes for database {}".format(
|
||||||
|
self.name
|
||||||
|
)
|
||||||
self._write_thread.start()
|
self._write_thread.start()
|
||||||
task_id = uuid.uuid4()
|
task_id = uuid.uuid5(uuid.NAMESPACE_DNS, "datasette.io")
|
||||||
loop = asyncio.get_running_loop()
|
loop = asyncio.get_running_loop()
|
||||||
reply_future = loop.create_future()
|
reply_future = loop.create_future()
|
||||||
self._write_queue.put(
|
self._write_queue.put(
|
||||||
|
|
@ -470,8 +442,7 @@ class Database:
|
||||||
try:
|
try:
|
||||||
conn = self.connect(write=True)
|
conn = self.connect(write=True)
|
||||||
self.ds._prepare_connection(conn, self.name)
|
self.ds._prepare_connection(conn, self.name)
|
||||||
except Exception as e: # noqa: BLE001
|
except Exception as e:
|
||||||
# Stored and re-raised to whoever queues the next write
|
|
||||||
conn_exception = e
|
conn_exception = e
|
||||||
while True:
|
while True:
|
||||||
task = self._write_queue.get()
|
task = self._write_queue.get()
|
||||||
|
|
@ -479,8 +450,7 @@ class Database:
|
||||||
if conn is not None:
|
if conn is not None:
|
||||||
try:
|
try:
|
||||||
conn.close()
|
conn.close()
|
||||||
except Exception: # noqa: BLE001, S110
|
except Exception:
|
||||||
# Best-effort close as the write thread exits
|
|
||||||
pass
|
pass
|
||||||
return
|
return
|
||||||
exception = None
|
exception = None
|
||||||
|
|
@ -495,25 +465,23 @@ class Database:
|
||||||
finally:
|
finally:
|
||||||
isolated_connection.close()
|
isolated_connection.close()
|
||||||
try:
|
try:
|
||||||
self._all_connections.remove(isolated_connection)
|
self._all_file_connections.remove(isolated_connection)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
# May already have been cleared by close().
|
# Was probably a memory connection
|
||||||
pass
|
pass
|
||||||
except Exception as e: # noqa: BLE001
|
except Exception as e:
|
||||||
# Write thread must survive any task failure or the database wedges
|
sys.stderr.write("{}\n".format(e))
|
||||||
sys.stderr.write(f"{e}\n")
|
|
||||||
sys.stderr.flush()
|
sys.stderr.flush()
|
||||||
exception = e
|
exception = e
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
if task.transaction:
|
if task.transaction:
|
||||||
with conn:
|
with conn:
|
||||||
conn.execute("BEGIN IMMEDIATE")
|
|
||||||
result = task.fn(conn)
|
result = task.fn(conn)
|
||||||
else:
|
else:
|
||||||
result = task.fn(conn)
|
result = task.fn(conn)
|
||||||
except Exception as e: # noqa: BLE001
|
except Exception as e:
|
||||||
sys.stderr.write(f"{e}\n")
|
sys.stderr.write("{}\n".format(e))
|
||||||
sys.stderr.flush()
|
sys.stderr.flush()
|
||||||
exception = e
|
exception = e
|
||||||
_deliver_write_result(task, result, exception)
|
_deliver_write_result(task, result, exception)
|
||||||
|
|
@ -580,7 +548,9 @@ class Database:
|
||||||
raise QueryInterrupted(e, sql, params)
|
raise QueryInterrupted(e, sql, params)
|
||||||
if log_sql_errors:
|
if log_sql_errors:
|
||||||
sys.stderr.write(
|
sys.stderr.write(
|
||||||
f"ERROR: conn={conn}, sql = {sql!r}, params = {params}: {e}\n"
|
"ERROR: conn={}, sql = {}, params = {}: {}\n".format(
|
||||||
|
conn, repr(sql), params, e
|
||||||
|
)
|
||||||
)
|
)
|
||||||
sys.stderr.flush()
|
sys.stderr.flush()
|
||||||
raise
|
raise
|
||||||
|
|
@ -633,7 +603,7 @@ class Database:
|
||||||
try:
|
try:
|
||||||
table_count = (
|
table_count = (
|
||||||
await self.execute(
|
await self.execute(
|
||||||
f"select count(*) from (select * from {escape_sqlite(table)} limit {self.count_limit + 1})",
|
f"select count(*) from (select * from [{table}] limit {self.count_limit + 1})",
|
||||||
custom_time_limit=limit,
|
custom_time_limit=limit,
|
||||||
)
|
)
|
||||||
).rows[0][0]
|
).rows[0][0]
|
||||||
|
|
@ -737,9 +707,9 @@ class Database:
|
||||||
column_names
|
column_names
|
||||||
and len(column_names) == 2
|
and len(column_names) == 2
|
||||||
and ("id" in column_names or "pk" in column_names)
|
and ("id" in column_names or "pk" in column_names)
|
||||||
and set(column_names) != {"id", "pk"}
|
and not set(column_names) == {"id", "pk"}
|
||||||
):
|
):
|
||||||
return next(c for c in column_names if c not in ("id", "pk"))
|
return [c for c in column_names if c not in ("id", "pk")][0]
|
||||||
# Couldn't find a label:
|
# Couldn't find a label:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
@ -785,17 +755,6 @@ class Database:
|
||||||
|
|
||||||
return hidden_tables
|
return hidden_tables
|
||||||
|
|
||||||
async def derived_table_dependencies(self):
|
|
||||||
"""Return implementation tables and the tables they derive from."""
|
|
||||||
schema_version = (await self.execute("PRAGMA schema_version")).first()[0]
|
|
||||||
if (
|
|
||||||
self._cached_derived_table_dependencies is None
|
|
||||||
or self._cached_derived_table_dependencies[0] != schema_version
|
|
||||||
):
|
|
||||||
dependencies = await self.execute_fn(sqlite_derived_table_dependencies)
|
|
||||||
self._cached_derived_table_dependencies = (schema_version, dependencies)
|
|
||||||
return self._cached_derived_table_dependencies[1]
|
|
||||||
|
|
||||||
async def view_names(self):
|
async def view_names(self):
|
||||||
results = await self.execute("select name from sqlite_master where type='view'")
|
results = await self.execute("select name from sqlite_master where type='view'")
|
||||||
return [r[0] for r in results.rows]
|
return [r[0] for r in results.rows]
|
||||||
|
|
@ -892,10 +851,10 @@ def _apply_write_wrapper(fn, wrapper_factory, track_event):
|
||||||
class WriteTask:
|
class WriteTask:
|
||||||
__slots__ = (
|
__slots__ = (
|
||||||
"fn",
|
"fn",
|
||||||
"isolated_connection",
|
"task_id",
|
||||||
"loop",
|
"loop",
|
||||||
"reply_future",
|
"reply_future",
|
||||||
"task_id",
|
"isolated_connection",
|
||||||
"transaction",
|
"transaction",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -936,7 +895,7 @@ class QueryInterrupted(Exception):
|
||||||
self.params = params
|
self.params = params
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return f"QueryInterrupted: {self.e}"
|
return "QueryInterrupted: {}".format(self.e)
|
||||||
|
|
||||||
|
|
||||||
class MultipleValues(Exception):
|
class MultipleValues(Exception):
|
||||||
|
|
|
||||||
|
|
@ -2,8 +2,8 @@ from datasette import hookimpl
|
||||||
from datasette.permissions import Action
|
from datasette.permissions import Action
|
||||||
from datasette.resources import (
|
from datasette.resources import (
|
||||||
DatabaseResource,
|
DatabaseResource,
|
||||||
QueryResource,
|
|
||||||
TableResource,
|
TableResource,
|
||||||
|
QueryResource,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -61,12 +61,6 @@ def register_actions():
|
||||||
description="Create tables",
|
description="Create tables",
|
||||||
resource_class=DatabaseResource,
|
resource_class=DatabaseResource,
|
||||||
),
|
),
|
||||||
Action(
|
|
||||||
name="create-view",
|
|
||||||
abbr="cv",
|
|
||||||
description="Create views",
|
|
||||||
resource_class=DatabaseResource,
|
|
||||||
),
|
|
||||||
Action(
|
Action(
|
||||||
name="store-query",
|
name="store-query",
|
||||||
abbr="sq",
|
abbr="sq",
|
||||||
|
|
@ -117,12 +111,6 @@ def register_actions():
|
||||||
description="Drop tables",
|
description="Drop tables",
|
||||||
resource_class=TableResource,
|
resource_class=TableResource,
|
||||||
),
|
),
|
||||||
Action(
|
|
||||||
name="drop-view",
|
|
||||||
abbr="dv",
|
|
||||||
description="Drop views",
|
|
||||||
resource_class=TableResource,
|
|
||||||
),
|
|
||||||
# Query-level actions (child-level)
|
# Query-level actions (child-level)
|
||||||
Action(
|
Action(
|
||||||
name="view-query",
|
name="view-query",
|
||||||
|
|
|
||||||
|
|
@ -6,17 +6,6 @@ import markupsafe
|
||||||
from datasette import hookimpl
|
from datasette import hookimpl
|
||||||
from datasette.column_types import ColumnType, SQLiteType
|
from datasette.column_types import ColumnType, SQLiteType
|
||||||
|
|
||||||
_HTTP_URL_RE = re.compile(r"https?://\S+", re.IGNORECASE)
|
|
||||||
|
|
||||||
|
|
||||||
def _normalize_http_url(value):
|
|
||||||
if not isinstance(value, str):
|
|
||||||
return None
|
|
||||||
normalized = value.strip()
|
|
||||||
if not _HTTP_URL_RE.fullmatch(normalized):
|
|
||||||
return None
|
|
||||||
return normalized
|
|
||||||
|
|
||||||
|
|
||||||
class UrlColumnType(ColumnType):
|
class UrlColumnType(ColumnType):
|
||||||
name = "url"
|
name = "url"
|
||||||
|
|
@ -26,10 +15,7 @@ class UrlColumnType(ColumnType):
|
||||||
async def render_cell(self, value, column, table, database, datasette, request):
|
async def render_cell(self, value, column, table, database, datasette, request):
|
||||||
if not value or not isinstance(value, str):
|
if not value or not isinstance(value, str):
|
||||||
return None
|
return None
|
||||||
normalized = _normalize_http_url(value)
|
escaped = markupsafe.escape(value.strip())
|
||||||
if normalized is None:
|
|
||||||
return markupsafe.escape(value.strip())
|
|
||||||
escaped = markupsafe.escape(normalized)
|
|
||||||
return markupsafe.Markup(f'<a href="{escaped}">{escaped}</a>')
|
return markupsafe.Markup(f'<a href="{escaped}">{escaped}</a>')
|
||||||
|
|
||||||
async def validate(self, value, datasette):
|
async def validate(self, value, datasette):
|
||||||
|
|
@ -37,7 +23,7 @@ class UrlColumnType(ColumnType):
|
||||||
return None
|
return None
|
||||||
if not isinstance(value, str):
|
if not isinstance(value, str):
|
||||||
return "URL must be a string"
|
return "URL must be a string"
|
||||||
if _normalize_http_url(value) is None:
|
if not re.match(r"^https?://\S+$", value.strip()):
|
||||||
return "Invalid URL"
|
return "Invalid URL"
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,8 @@
|
||||||
|
from datasette import hookimpl
|
||||||
import datetime
|
import datetime
|
||||||
import os
|
import os
|
||||||
import time
|
import time
|
||||||
|
|
||||||
from datasette import hookimpl
|
|
||||||
|
|
||||||
|
|
||||||
def header(key, request):
|
def header(key, request):
|
||||||
key = key.replace("_", "-").encode("utf-8")
|
key = key.replace("_", "-").encode("utf-8")
|
||||||
|
|
|
||||||
|
|
@ -17,29 +17,18 @@ UNION/INTERSECT operations. The order of evaluation is:
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from .config import config_permissions_sql as config_permissions_sql
|
|
||||||
from .defaults import (
|
|
||||||
DEFAULT_ALLOW_ACTIONS as DEFAULT_ALLOW_ACTIONS,
|
|
||||||
)
|
|
||||||
from .defaults import (
|
|
||||||
default_action_permissions_sql as default_action_permissions_sql,
|
|
||||||
)
|
|
||||||
from .defaults import (
|
|
||||||
# Avoid "datasette.default_permissions" does not explicitly export attribute
|
|
||||||
default_allow_sql_check as default_allow_sql_check,
|
|
||||||
)
|
|
||||||
from .defaults import (
|
|
||||||
default_query_permissions_sql as default_query_permissions_sql,
|
|
||||||
)
|
|
||||||
from .restrictions import (
|
|
||||||
ActorRestrictions as ActorRestrictions,
|
|
||||||
)
|
|
||||||
|
|
||||||
# Re-export all hooks and public utilities
|
# Re-export all hooks and public utilities
|
||||||
from .restrictions import (
|
from .restrictions import (
|
||||||
actor_restrictions_sql as actor_restrictions_sql,
|
actor_restrictions_sql as actor_restrictions_sql,
|
||||||
)
|
|
||||||
from .restrictions import (
|
|
||||||
restrictions_allow_action as restrictions_allow_action,
|
restrictions_allow_action as restrictions_allow_action,
|
||||||
|
ActorRestrictions as ActorRestrictions,
|
||||||
)
|
)
|
||||||
from .root import root_user_permissions_sql as root_user_permissions_sql
|
from .root import root_user_permissions_sql as root_user_permissions_sql
|
||||||
|
from .config import config_permissions_sql as config_permissions_sql
|
||||||
|
from .defaults import (
|
||||||
|
# Avoid "datasette.default_permissions" does not explicitly export attribute
|
||||||
|
default_allow_sql_check as default_allow_sql_check,
|
||||||
|
default_action_permissions_sql as default_action_permissions_sql,
|
||||||
|
default_query_permissions_sql as default_query_permissions_sql,
|
||||||
|
DEFAULT_ALLOW_ACTIONS as DEFAULT_ALLOW_ACTIONS,
|
||||||
|
)
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ Applies permission rules from datasette.yaml configuration.
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from typing import TYPE_CHECKING, Any
|
from typing import TYPE_CHECKING, Any, List, Optional, Set, Tuple
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from datasette.app import Datasette
|
from datasette.app import Datasette
|
||||||
|
|
@ -55,8 +55,8 @@ class ConfigPermissionProcessor:
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
datasette: Datasette,
|
datasette: "Datasette",
|
||||||
actor: dict | None,
|
actor: Optional[dict],
|
||||||
action: str,
|
action: str,
|
||||||
):
|
):
|
||||||
self.datasette = datasette
|
self.datasette = datasette
|
||||||
|
|
@ -74,8 +74,8 @@ class ConfigPermissionProcessor:
|
||||||
self.restrictions = actor.get("_r", {}) if actor else {}
|
self.restrictions = actor.get("_r", {}) if actor else {}
|
||||||
|
|
||||||
# Pre-compute restriction info for efficiency
|
# Pre-compute restriction info for efficiency
|
||||||
self.restricted_databases: set[str] = set()
|
self.restricted_databases: Set[str] = set()
|
||||||
self.restricted_tables: set[tuple[str, str]] = set()
|
self.restricted_tables: Set[Tuple[str, str]] = set()
|
||||||
|
|
||||||
if self.has_restrictions:
|
if self.has_restrictions:
|
||||||
self.restricted_databases = {
|
self.restricted_databases = {
|
||||||
|
|
@ -92,27 +92,16 @@ class ConfigPermissionProcessor:
|
||||||
# Tables implicitly reference their parent databases
|
# Tables implicitly reference their parent databases
|
||||||
self.restricted_databases.update(db for db, _ in self.restricted_tables)
|
self.restricted_databases.update(db for db, _ in self.restricted_tables)
|
||||||
|
|
||||||
# Resolve identity keys once per action, rather than scanning the
|
def evaluate_allow_block(self, allow_block: Any) -> Optional[bool]:
|
||||||
# restriction allowlist for every configured table's allow block.
|
|
||||||
self.restricted_table_keys = {
|
|
||||||
(db, self.action_obj.normalize_child(table) if self.action_obj else table)
|
|
||||||
for db, table in self.restricted_tables
|
|
||||||
}
|
|
||||||
|
|
||||||
def evaluate_allow_block(self, allow_block: Any) -> bool | None:
|
|
||||||
"""Evaluate an allow block against the current actor."""
|
"""Evaluate an allow block against the current actor."""
|
||||||
if allow_block is None:
|
if allow_block is None:
|
||||||
return None
|
return None
|
||||||
# Values passed using ``-s permissions.* 1`` or ``0`` are parsed as
|
|
||||||
# integers, but should retain the CLI's boolean 1/0 behavior.
|
|
||||||
if isinstance(allow_block, int) and allow_block in (0, 1):
|
|
||||||
return bool(allow_block)
|
|
||||||
return actor_matches_allow(self.actor, allow_block)
|
return actor_matches_allow(self.actor, allow_block)
|
||||||
|
|
||||||
def is_in_restriction_allowlist(
|
def is_in_restriction_allowlist(
|
||||||
self,
|
self,
|
||||||
parent: str | None,
|
parent: Optional[str],
|
||||||
child: str | None,
|
child: Optional[str],
|
||||||
) -> bool:
|
) -> bool:
|
||||||
"""Check if resource is allowed by actor restrictions."""
|
"""Check if resource is allowed by actor restrictions."""
|
||||||
if not self.has_restrictions:
|
if not self.has_restrictions:
|
||||||
|
|
@ -132,10 +121,8 @@ class ConfigPermissionProcessor:
|
||||||
if parent:
|
if parent:
|
||||||
table_restrictions = (self.restrictions.get("r", {}) or {}).get(parent, {})
|
table_restrictions = (self.restrictions.get("r", {}) or {}).get(parent, {})
|
||||||
if child:
|
if child:
|
||||||
child_key = (
|
table_actions = table_restrictions.get(child, [])
|
||||||
self.action_obj.normalize_child(child) if self.action_obj else child
|
if self.action_checks.intersection(table_actions):
|
||||||
)
|
|
||||||
if (parent, child_key) in self.restricted_table_keys:
|
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
# Parent query should proceed if any child in this database is allowlisted
|
# Parent query should proceed if any child in this database is allowlisted
|
||||||
|
|
@ -156,9 +143,9 @@ class ConfigPermissionProcessor:
|
||||||
|
|
||||||
def add_permissions_rule(
|
def add_permissions_rule(
|
||||||
self,
|
self,
|
||||||
parent: str | None,
|
parent: Optional[str],
|
||||||
child: str | None,
|
child: Optional[str],
|
||||||
permissions_block: dict | None,
|
permissions_block: Optional[dict],
|
||||||
scope_desc: str,
|
scope_desc: str,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Add a rule from a permissions:{action} block."""
|
"""Add a rule from a permissions:{action} block."""
|
||||||
|
|
@ -178,8 +165,8 @@ class ConfigPermissionProcessor:
|
||||||
|
|
||||||
def add_allow_block_rule(
|
def add_allow_block_rule(
|
||||||
self,
|
self,
|
||||||
parent: str | None,
|
parent: Optional[str],
|
||||||
child: str | None,
|
child: Optional[str],
|
||||||
allow_block: Any,
|
allow_block: Any,
|
||||||
scope_desc: str,
|
scope_desc: str,
|
||||||
) -> None:
|
) -> None:
|
||||||
|
|
@ -211,8 +198,8 @@ class ConfigPermissionProcessor:
|
||||||
|
|
||||||
def _add_restriction_gate_denies(
|
def _add_restriction_gate_denies(
|
||||||
self,
|
self,
|
||||||
parent: str | None,
|
parent: Optional[str],
|
||||||
child: str | None,
|
child: Optional[str],
|
||||||
is_allowed: bool,
|
is_allowed: bool,
|
||||||
scope_desc: str,
|
scope_desc: str,
|
||||||
) -> None:
|
) -> None:
|
||||||
|
|
@ -244,7 +231,7 @@ class ConfigPermissionProcessor:
|
||||||
if db_name == parent:
|
if db_name == parent:
|
||||||
self.collector.add(db_name, table_name, False, reason)
|
self.collector.add(db_name, table_name, False, reason)
|
||||||
|
|
||||||
def process(self) -> PermissionSQL | None:
|
def process(self) -> Optional[PermissionSQL]:
|
||||||
"""Process all config rules and return combined PermissionSQL."""
|
"""Process all config rules and return combined PermissionSQL."""
|
||||||
self._process_root_permissions()
|
self._process_root_permissions()
|
||||||
self._process_databases()
|
self._process_databases()
|
||||||
|
|
@ -434,10 +421,10 @@ class ConfigPermissionProcessor:
|
||||||
|
|
||||||
@hookimpl(specname="permission_resources_sql")
|
@hookimpl(specname="permission_resources_sql")
|
||||||
async def config_permissions_sql(
|
async def config_permissions_sql(
|
||||||
datasette: Datasette,
|
datasette: "Datasette",
|
||||||
actor: dict | None,
|
actor: Optional[dict],
|
||||||
action: str,
|
action: str,
|
||||||
) -> list[PermissionSQL] | None:
|
) -> Optional[List[PermissionSQL]]:
|
||||||
"""
|
"""
|
||||||
Apply permission rules from datasette.yaml configuration.
|
Apply permission rules from datasette.yaml configuration.
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ Provides default allow rules for standard view/execute actions.
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from typing import TYPE_CHECKING
|
from typing import TYPE_CHECKING, Optional
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from datasette.app import Datasette
|
from datasette.app import Datasette
|
||||||
|
|
@ -29,17 +29,18 @@ DEFAULT_ALLOW_ACTIONS = frozenset(
|
||||||
|
|
||||||
@hookimpl(specname="permission_resources_sql")
|
@hookimpl(specname="permission_resources_sql")
|
||||||
async def default_allow_sql_check(
|
async def default_allow_sql_check(
|
||||||
datasette: Datasette,
|
datasette: "Datasette",
|
||||||
actor: dict | None,
|
actor: Optional[dict],
|
||||||
action: str,
|
action: str,
|
||||||
) -> PermissionSQL | None:
|
) -> Optional[PermissionSQL]:
|
||||||
"""
|
"""
|
||||||
Enforce the default_allow_sql setting.
|
Enforce the default_allow_sql setting.
|
||||||
|
|
||||||
When default_allow_sql is false (the default), execute-sql is denied
|
When default_allow_sql is false (the default), execute-sql is denied
|
||||||
unless explicitly allowed by config or other rules.
|
unless explicitly allowed by config or other rules.
|
||||||
"""
|
"""
|
||||||
if action == "execute-sql" and not datasette.setting("default_allow_sql"):
|
if action == "execute-sql":
|
||||||
|
if not datasette.setting("default_allow_sql"):
|
||||||
return PermissionSQL.deny(reason="default_allow_sql is false")
|
return PermissionSQL.deny(reason="default_allow_sql is false")
|
||||||
|
|
||||||
return None
|
return None
|
||||||
|
|
@ -47,10 +48,10 @@ async def default_allow_sql_check(
|
||||||
|
|
||||||
@hookimpl(specname="permission_resources_sql")
|
@hookimpl(specname="permission_resources_sql")
|
||||||
async def default_action_permissions_sql(
|
async def default_action_permissions_sql(
|
||||||
datasette: Datasette,
|
datasette: "Datasette",
|
||||||
actor: dict | None,
|
actor: Optional[dict],
|
||||||
action: str,
|
action: str,
|
||||||
) -> PermissionSQL | None:
|
) -> Optional[PermissionSQL]:
|
||||||
"""
|
"""
|
||||||
Provide default allow rules for standard view/execute actions.
|
Provide default allow rules for standard view/execute actions.
|
||||||
|
|
||||||
|
|
@ -70,10 +71,10 @@ async def default_action_permissions_sql(
|
||||||
|
|
||||||
@hookimpl(specname="permission_resources_sql")
|
@hookimpl(specname="permission_resources_sql")
|
||||||
async def default_query_permissions_sql(
|
async def default_query_permissions_sql(
|
||||||
datasette: Datasette,
|
datasette: "Datasette",
|
||||||
actor: dict | None,
|
actor: Optional[dict],
|
||||||
action: str,
|
action: str,
|
||||||
) -> PermissionSQL | None:
|
) -> Optional[PermissionSQL]:
|
||||||
actor_id = actor.get("id") if isinstance(actor, dict) else None
|
actor_id = actor.get("id") if isinstance(actor, dict) else None
|
||||||
|
|
||||||
if action not in {"view-query", "update-query", "delete-query"}:
|
if action not in {"view-query", "update-query", "delete-query"}:
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@ Shared helper utilities for default permission implementations.
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from typing import TYPE_CHECKING
|
from typing import TYPE_CHECKING, List, Optional, Set
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from datasette.app import Datasette
|
from datasette.app import Datasette
|
||||||
|
|
@ -13,7 +13,7 @@ if TYPE_CHECKING:
|
||||||
from datasette.permissions import PermissionSQL
|
from datasette.permissions import PermissionSQL
|
||||||
|
|
||||||
|
|
||||||
def get_action_name_variants(datasette: Datasette, action: str) -> set[str]:
|
def get_action_name_variants(datasette: "Datasette", action: str) -> Set[str]:
|
||||||
"""
|
"""
|
||||||
Get all name variants for an action (full name and abbreviation).
|
Get all name variants for an action (full name and abbreviation).
|
||||||
|
|
||||||
|
|
@ -27,7 +27,7 @@ def get_action_name_variants(datasette: Datasette, action: str) -> set[str]:
|
||||||
return variants
|
return variants
|
||||||
|
|
||||||
|
|
||||||
def action_in_list(datasette: Datasette, action: str, action_list: list) -> bool:
|
def action_in_list(datasette: "Datasette", action: str, action_list: list) -> bool:
|
||||||
"""Check if an action (or its abbreviation) is in a list."""
|
"""Check if an action (or its abbreviation) is in a list."""
|
||||||
return bool(get_action_name_variants(datasette, action).intersection(action_list))
|
return bool(get_action_name_variants(datasette, action).intersection(action_list))
|
||||||
|
|
||||||
|
|
@ -36,8 +36,8 @@ def action_in_list(datasette: Datasette, action: str, action_list: list) -> bool
|
||||||
class PermissionRow:
|
class PermissionRow:
|
||||||
"""A single permission rule row."""
|
"""A single permission rule row."""
|
||||||
|
|
||||||
parent: str | None
|
parent: Optional[str]
|
||||||
child: str | None
|
child: Optional[str]
|
||||||
allow: bool
|
allow: bool
|
||||||
reason: str
|
reason: str
|
||||||
|
|
||||||
|
|
@ -46,14 +46,14 @@ class PermissionRowCollector:
|
||||||
"""Collects permission rows and converts them to PermissionSQL."""
|
"""Collects permission rows and converts them to PermissionSQL."""
|
||||||
|
|
||||||
def __init__(self, prefix: str = "row"):
|
def __init__(self, prefix: str = "row"):
|
||||||
self.rows: list[PermissionRow] = []
|
self.rows: List[PermissionRow] = []
|
||||||
self.prefix = prefix
|
self.prefix = prefix
|
||||||
|
|
||||||
def add(
|
def add(
|
||||||
self,
|
self,
|
||||||
parent: str | None,
|
parent: Optional[str],
|
||||||
child: str | None,
|
child: Optional[str],
|
||||||
allow: bool | None,
|
allow: Optional[bool],
|
||||||
reason: str,
|
reason: str,
|
||||||
if_not_none: bool = False,
|
if_not_none: bool = False,
|
||||||
) -> None:
|
) -> None:
|
||||||
|
|
@ -62,7 +62,7 @@ class PermissionRowCollector:
|
||||||
return
|
return
|
||||||
self.rows.append(PermissionRow(parent, child, allow, reason))
|
self.rows.append(PermissionRow(parent, child, allow, reason))
|
||||||
|
|
||||||
def to_permission_sql(self) -> PermissionSQL | None:
|
def to_permission_sql(self) -> Optional[PermissionSQL]:
|
||||||
"""Convert collected rows to a PermissionSQL object."""
|
"""Convert collected rows to a PermissionSQL object."""
|
||||||
if not self.rows:
|
if not self.rows:
|
||||||
return None
|
return None
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@ contains allowlists of resources the actor can access.
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from typing import TYPE_CHECKING
|
from typing import TYPE_CHECKING, List, Optional, Set, Tuple
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from datasette.app import Datasette
|
from datasette.app import Datasette
|
||||||
|
|
@ -23,12 +23,12 @@ from .helpers import action_in_list, get_action_name_variants
|
||||||
class ActorRestrictions:
|
class ActorRestrictions:
|
||||||
"""Parsed actor restrictions from the _r key."""
|
"""Parsed actor restrictions from the _r key."""
|
||||||
|
|
||||||
global_actions: list[str] # _r.a - globally allowed actions
|
global_actions: List[str] # _r.a - globally allowed actions
|
||||||
database_actions: dict # _r.d - {db_name: [actions]}
|
database_actions: dict # _r.d - {db_name: [actions]}
|
||||||
table_actions: dict # _r.r - {db_name: {table: [actions]}}
|
table_actions: dict # _r.r - {db_name: {table: [actions]}}
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_actor(cls, actor: dict | None) -> ActorRestrictions | None:
|
def from_actor(cls, actor: Optional[dict]) -> Optional["ActorRestrictions"]:
|
||||||
"""Parse restrictions from actor dict. Returns None if no restrictions."""
|
"""Parse restrictions from actor dict. Returns None if no restrictions."""
|
||||||
if not actor:
|
if not actor:
|
||||||
return None
|
return None
|
||||||
|
|
@ -44,11 +44,11 @@ class ActorRestrictions:
|
||||||
table_actions=restrictions.get("r", {}),
|
table_actions=restrictions.get("r", {}),
|
||||||
)
|
)
|
||||||
|
|
||||||
def is_action_globally_allowed(self, datasette: Datasette, action: str) -> bool:
|
def is_action_globally_allowed(self, datasette: "Datasette", action: str) -> bool:
|
||||||
"""Check if action is in the global allowlist."""
|
"""Check if action is in the global allowlist."""
|
||||||
return action_in_list(datasette, action, self.global_actions)
|
return action_in_list(datasette, action, self.global_actions)
|
||||||
|
|
||||||
def get_allowed_databases(self, datasette: Datasette, action: str) -> set[str]:
|
def get_allowed_databases(self, datasette: "Datasette", action: str) -> Set[str]:
|
||||||
"""Get database names where this action is allowed."""
|
"""Get database names where this action is allowed."""
|
||||||
allowed = set()
|
allowed = set()
|
||||||
for db_name, db_actions in self.database_actions.items():
|
for db_name, db_actions in self.database_actions.items():
|
||||||
|
|
@ -57,8 +57,8 @@ class ActorRestrictions:
|
||||||
return allowed
|
return allowed
|
||||||
|
|
||||||
def get_allowed_tables(
|
def get_allowed_tables(
|
||||||
self, datasette: Datasette, action: str
|
self, datasette: "Datasette", action: str
|
||||||
) -> set[tuple[str, str]]:
|
) -> Set[Tuple[str, str]]:
|
||||||
"""Get (database, table) pairs where this action is allowed."""
|
"""Get (database, table) pairs where this action is allowed."""
|
||||||
allowed = set()
|
allowed = set()
|
||||||
for db_name, tables in self.table_actions.items():
|
for db_name, tables in self.table_actions.items():
|
||||||
|
|
@ -70,10 +70,10 @@ class ActorRestrictions:
|
||||||
|
|
||||||
@hookimpl(specname="permission_resources_sql")
|
@hookimpl(specname="permission_resources_sql")
|
||||||
async def actor_restrictions_sql(
|
async def actor_restrictions_sql(
|
||||||
datasette: Datasette,
|
datasette: "Datasette",
|
||||||
actor: dict | None,
|
actor: Optional[dict],
|
||||||
action: str,
|
action: str,
|
||||||
) -> list[PermissionSQL] | None:
|
) -> Optional[List[PermissionSQL]]:
|
||||||
"""
|
"""
|
||||||
Handle actor restriction-based permission rules.
|
Handle actor restriction-based permission rules.
|
||||||
|
|
||||||
|
|
@ -140,10 +140,10 @@ async def actor_restrictions_sql(
|
||||||
|
|
||||||
|
|
||||||
def restrictions_allow_action(
|
def restrictions_allow_action(
|
||||||
datasette: Datasette,
|
datasette: "Datasette",
|
||||||
restrictions: dict,
|
restrictions: dict,
|
||||||
action: str,
|
action: str,
|
||||||
resource: str | tuple[str, str] | None,
|
resource: Optional[str | Tuple[str, str]],
|
||||||
) -> bool:
|
) -> bool:
|
||||||
"""
|
"""
|
||||||
Check if restrictions allow the requested action on the requested resource.
|
Check if restrictions allow the requested action on the requested resource.
|
||||||
|
|
@ -185,12 +185,8 @@ def restrictions_allow_action(
|
||||||
# Check table/resource level
|
# Check table/resource level
|
||||||
if resource is not None and not isinstance(resource, str) and len(resource) == 2:
|
if resource is not None and not isinstance(resource, str) and len(resource) == 2:
|
||||||
database, table = resource
|
database, table = resource
|
||||||
action_obj = datasette.actions.get(action)
|
table_allowed = restrictions.get("r", {}).get(database, {}).get(table)
|
||||||
normalize = action_obj.normalize_child if action_obj else lambda name: name
|
if table_allowed is not None:
|
||||||
for table_name, table_allowed in (
|
|
||||||
restrictions.get("r", {}).get(database, {}).items()
|
|
||||||
):
|
|
||||||
if normalize(table_name) == normalize(table):
|
|
||||||
assert isinstance(table_allowed, list)
|
assert isinstance(table_allowed, list)
|
||||||
if to_check.intersection(table_allowed):
|
if to_check.intersection(table_allowed):
|
||||||
return True
|
return True
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ Grants full permissions to the root user when --root flag is used.
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from typing import TYPE_CHECKING
|
from typing import TYPE_CHECKING, Optional
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from datasette.app import Datasette
|
from datasette.app import Datasette
|
||||||
|
|
@ -17,9 +17,9 @@ from datasette.permissions import PermissionSQL
|
||||||
|
|
||||||
@hookimpl(specname="permission_resources_sql")
|
@hookimpl(specname="permission_resources_sql")
|
||||||
async def root_user_permissions_sql(
|
async def root_user_permissions_sql(
|
||||||
datasette: Datasette,
|
datasette: "Datasette",
|
||||||
actor: dict | None,
|
actor: Optional[dict],
|
||||||
) -> PermissionSQL | None:
|
) -> Optional[PermissionSQL]:
|
||||||
"""
|
"""
|
||||||
Grant root user full permissions when --root flag is used.
|
Grant root user full permissions when --root flag is used.
|
||||||
"""
|
"""
|
||||||
|
|
|
||||||
|
|
@ -1,25 +0,0 @@
|
||||||
"""Default table-access policy for SQLite optimizer statistics."""
|
|
||||||
|
|
||||||
import json
|
|
||||||
|
|
||||||
from datasette import hookimpl
|
|
||||||
from datasette.permissions import PermissionSQL
|
|
||||||
|
|
||||||
|
|
||||||
@hookimpl
|
|
||||||
def permission_resources_sql(action):
|
|
||||||
if action != "view-table":
|
|
||||||
return None
|
|
||||||
return PermissionSQL(
|
|
||||||
sql="""
|
|
||||||
SELECT database_name AS parent, value AS child, 0 AS allow,
|
|
||||||
'SQLite statistics tables are denied by default' AS reason
|
|
||||||
FROM catalog_databases
|
|
||||||
CROSS JOIN json_each(:sqlite_statistics_names)
|
|
||||||
""",
|
|
||||||
params={
|
|
||||||
"sqlite_statistics_names": json.dumps(
|
|
||||||
["sqlite_stat1", "sqlite_stat2", "sqlite_stat3", "sqlite_stat4"]
|
|
||||||
)
|
|
||||||
},
|
|
||||||
)
|
|
||||||
|
|
@ -7,7 +7,7 @@ to datasette.verify_token() so all registered handlers are tried.
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from typing import TYPE_CHECKING
|
from typing import TYPE_CHECKING, Optional
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from datasette.app import Datasette
|
from datasette.app import Datasette
|
||||||
|
|
@ -17,13 +17,15 @@ from datasette.tokens import SignedTokenHandler
|
||||||
|
|
||||||
|
|
||||||
@hookimpl
|
@hookimpl
|
||||||
def register_token_handler(datasette: Datasette):
|
def register_token_handler(datasette: "Datasette"):
|
||||||
"""Register the default signed token handler."""
|
"""Register the default signed token handler."""
|
||||||
return SignedTokenHandler()
|
return SignedTokenHandler()
|
||||||
|
|
||||||
|
|
||||||
@hookimpl(specname="actor_from_request")
|
@hookimpl(specname="actor_from_request")
|
||||||
async def actor_from_signed_api_token(datasette: Datasette, request) -> dict | None:
|
async def actor_from_signed_api_token(
|
||||||
|
datasette: "Datasette", request
|
||||||
|
) -> Optional[dict]:
|
||||||
"""
|
"""
|
||||||
Authenticate requests using API tokens by delegating to all registered
|
Authenticate requests using API tokens by delegating to all registered
|
||||||
token handlers via datasette.verify_token().
|
token handlers via datasette.verify_token().
|
||||||
|
|
|
||||||
|
|
@ -1,29 +0,0 @@
|
||||||
from datasette import hookimpl
|
|
||||||
from datasette.resources import TableResource
|
|
||||||
|
|
||||||
|
|
||||||
@hookimpl
|
|
||||||
def table_actions(datasette, actor, database, table, request):
|
|
||||||
async def inner():
|
|
||||||
db = datasette.get_database(database)
|
|
||||||
if not db.is_mutable:
|
|
||||||
return []
|
|
||||||
if not await datasette.allowed(
|
|
||||||
action="alter-table",
|
|
||||||
resource=TableResource(database=database, table=table),
|
|
||||||
actor=actor,
|
|
||||||
):
|
|
||||||
return []
|
|
||||||
return [
|
|
||||||
{
|
|
||||||
"type": "button",
|
|
||||||
"label": "Alter table",
|
|
||||||
"description": "Change columns and primary key for this table.",
|
|
||||||
"attrs": {
|
|
||||||
"aria-label": f"Alter table {table}",
|
|
||||||
"data-table-action": "alter-table",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
]
|
|
||||||
|
|
||||||
return inner
|
|
||||||
|
|
@ -1,8 +1,7 @@
|
||||||
from abc import ABC, abstractproperty
|
from abc import ABC, abstractproperty
|
||||||
from dataclasses import asdict, dataclass, field
|
from dataclasses import asdict, dataclass, field
|
||||||
from datetime import datetime, timezone
|
|
||||||
|
|
||||||
from datasette.hookspecs import hookimpl
|
from datasette.hookspecs import hookimpl
|
||||||
|
from datetime import datetime, timezone
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
|
|
|
||||||
|
|
@ -5,8 +5,6 @@ from typing import ClassVar
|
||||||
|
|
||||||
from asyncinject import Registry
|
from asyncinject import Registry
|
||||||
|
|
||||||
from datasette.utils.asgi import BadRequest
|
|
||||||
|
|
||||||
|
|
||||||
def extra_names_from_request(request):
|
def extra_names_from_request(request):
|
||||||
extra_bits = request.args.getlist("_extra")
|
extra_bits = request.args.getlist("_extra")
|
||||||
|
|
@ -83,16 +81,6 @@ class ExtraRegistry:
|
||||||
def public_classes_for_scope(self, scope):
|
def public_classes_for_scope(self, scope):
|
||||||
return self.classes_for_scope(scope, include_internal=False)
|
return self.classes_for_scope(scope, include_internal=False)
|
||||||
|
|
||||||
def internal_classes_for_scope(self, scope):
|
|
||||||
# Extras that are available to HTML templates but excluded from
|
|
||||||
# JSON responses - plain Providers are dependency plumbing and
|
|
||||||
# never surface as keys, so they are not included
|
|
||||||
return [
|
|
||||||
cls
|
|
||||||
for cls in self.classes_for_scope(scope)
|
|
||||||
if issubclass(cls, Extra) and not cls.public
|
|
||||||
]
|
|
||||||
|
|
||||||
def _registry_for_scope(self, scope):
|
def _registry_for_scope(self, scope):
|
||||||
registry = self._scope_registries.get(scope)
|
registry = self._scope_registries.get(scope)
|
||||||
if registry is None:
|
if registry is None:
|
||||||
|
|
@ -115,17 +103,6 @@ class ExtraRegistry:
|
||||||
self._allowed_names[key] = names
|
self._allowed_names[key] = names
|
||||||
return names
|
return names
|
||||||
|
|
||||||
def validate_requested(self, requested, scope):
|
|
||||||
"""
|
|
||||||
Raise BadRequest if any requested extra name is not a public extra
|
|
||||||
for this scope. Used by data formats such as .json - HTML pages
|
|
||||||
silently ignore unknown names instead.
|
|
||||||
"""
|
|
||||||
allowed = self._allowed_names_for_scope(scope, include_internal=False)
|
|
||||||
unknown = sorted(name for name in requested if name not in allowed)
|
|
||||||
if unknown:
|
|
||||||
raise BadRequest("Unknown _extra: {}".format(", ".join(unknown)))
|
|
||||||
|
|
||||||
async def resolve(self, requested, context, scope, include_internal=False):
|
async def resolve(self, requested, context, scope, include_internal=False):
|
||||||
allowed_names = self._allowed_names_for_scope(scope, include_internal)
|
allowed_names = self._allowed_names_for_scope(scope, include_internal)
|
||||||
requested_names = [name for name in requested if name in allowed_names]
|
requested_names = [name for name in requested if name in allowed_names]
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,12 @@
|
||||||
import json
|
import json
|
||||||
import urllib
|
import urllib
|
||||||
|
|
||||||
from datasette import hookimpl
|
from datasette import hookimpl
|
||||||
from datasette.database import QueryInterrupted
|
from datasette.database import QueryInterrupted
|
||||||
from datasette.utils import (
|
from datasette.utils import (
|
||||||
detect_json1,
|
|
||||||
escape_sqlite,
|
escape_sqlite,
|
||||||
path_with_added_args,
|
path_with_added_args,
|
||||||
path_with_removed_args,
|
path_with_removed_args,
|
||||||
|
detect_json1,
|
||||||
sqlite3,
|
sqlite3,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -31,7 +30,7 @@ def load_facet_configs(request, table_config):
|
||||||
assert (
|
assert (
|
||||||
len(facet_config.values()) == 1
|
len(facet_config.values()) == 1
|
||||||
), "Metadata config dicts should be {type: config}"
|
), "Metadata config dicts should be {type: config}"
|
||||||
type, facet_config = next(iter(facet_config.items()))
|
type, facet_config = list(facet_config.items())[0]
|
||||||
if isinstance(facet_config, str):
|
if isinstance(facet_config, str):
|
||||||
facet_config = {"simple": facet_config}
|
facet_config = {"simple": facet_config}
|
||||||
facet_configs.setdefault(type, []).append(
|
facet_configs.setdefault(type, []).append(
|
||||||
|
|
@ -86,7 +85,7 @@ class Facet:
|
||||||
self.database = database
|
self.database = database
|
||||||
# For foreign key expansion. Can be None for e.g. stored SQL queries:
|
# For foreign key expansion. Can be None for e.g. stored SQL queries:
|
||||||
self.table = table
|
self.table = table
|
||||||
self.sql = sql or f"select * from {escape_sqlite(table)}"
|
self.sql = sql or f"select * from [{table}]"
|
||||||
self.params = params or []
|
self.params = params or []
|
||||||
self.table_config = table_config
|
self.table_config = table_config
|
||||||
# row_count can be None, in which case we calculate it ourselves:
|
# row_count can be None, in which case we calculate it ourselves:
|
||||||
|
|
@ -161,13 +160,18 @@ class ColumnFacet(Facet):
|
||||||
for column in columns:
|
for column in columns:
|
||||||
if column in already_enabled:
|
if column in already_enabled:
|
||||||
continue
|
continue
|
||||||
suggested_facet_sql = f"""
|
suggested_facet_sql = """
|
||||||
with limited as (select * from ({self.sql}) limit {self.suggest_consider})
|
with limited as (select * from ({sql}) limit {suggest_consider})
|
||||||
select {escape_sqlite(column)} as value, count(*) as n from limited
|
select {column} as value, count(*) as n from limited
|
||||||
where value is not null
|
where value is not null
|
||||||
group by value
|
group by value
|
||||||
limit {facet_size + 1}
|
limit {limit}
|
||||||
"""
|
""".format(
|
||||||
|
column=escape_sqlite(column),
|
||||||
|
sql=self.sql,
|
||||||
|
limit=facet_size + 1,
|
||||||
|
suggest_consider=self.suggest_consider,
|
||||||
|
)
|
||||||
distinct_values = None
|
distinct_values = None
|
||||||
try:
|
try:
|
||||||
distinct_values = await self.ds.execute(
|
distinct_values = await self.ds.execute(
|
||||||
|
|
@ -263,16 +267,11 @@ class ColumnFacet(Facet):
|
||||||
for row in facet_rows:
|
for row in facet_rows:
|
||||||
column_qs = column
|
column_qs = column
|
||||||
if column.startswith("_"):
|
if column.startswith("_"):
|
||||||
column_qs = f"{column}__exact"
|
column_qs = "{}__exact".format(column)
|
||||||
selected_args = {
|
selected = (column_qs, str(row["value"])) in qs_pairs
|
||||||
key: str(row["value"])
|
|
||||||
for key in (column_qs, f"{column}__exact")
|
|
||||||
if (key, str(row["value"])) in qs_pairs
|
|
||||||
}
|
|
||||||
selected = bool(selected_args)
|
|
||||||
if selected:
|
if selected:
|
||||||
toggle_path = path_with_removed_args(
|
toggle_path = path_with_removed_args(
|
||||||
self.request, selected_args
|
self.request, {column_qs: str(row["value"])}
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
toggle_path = path_with_added_args(
|
toggle_path = path_with_added_args(
|
||||||
|
|
@ -343,12 +342,12 @@ class ArrayFacet(Facet):
|
||||||
for v in await self.ds.execute(
|
for v in await self.ds.execute(
|
||||||
self.database,
|
self.database,
|
||||||
(
|
(
|
||||||
f"select {escape_sqlite(column)} from ({self.sql}) "
|
"select {column} from ({sql}) "
|
||||||
f"where {escape_sqlite(column)} is not null "
|
"where {column} is not null "
|
||||||
f"and {escape_sqlite(column)} != '' "
|
"and {column} != '' "
|
||||||
f"and json_array_length({escape_sqlite(column)}) > 0 "
|
"and json_array_length({column}) > 0 "
|
||||||
"limit 100"
|
"limit 100"
|
||||||
),
|
).format(column=escape_sqlite(column), sql=self.sql),
|
||||||
self.params,
|
self.params,
|
||||||
truncate=False,
|
truncate=False,
|
||||||
custom_time_limit=self.ds.setting(
|
custom_time_limit=self.ds.setting(
|
||||||
|
|
@ -389,14 +388,14 @@ class ArrayFacet(Facet):
|
||||||
source = source_and_config["source"]
|
source = source_and_config["source"]
|
||||||
column = config.get("column") or config["simple"]
|
column = config.get("column") or config["simple"]
|
||||||
# https://github.com/simonw/datasette/issues/448
|
# https://github.com/simonw/datasette/issues/448
|
||||||
facet_sql = f"""
|
facet_sql = """
|
||||||
with inner as ({self.sql}),
|
with inner as ({sql}),
|
||||||
deduped_array_items as (
|
deduped_array_items as (
|
||||||
select
|
select
|
||||||
distinct j.value,
|
distinct j.value,
|
||||||
inner.*
|
inner.*
|
||||||
from
|
from
|
||||||
json_each([inner].{escape_sqlite(column)}) j
|
json_each([inner].{col}) j
|
||||||
join inner
|
join inner
|
||||||
)
|
)
|
||||||
select
|
select
|
||||||
|
|
@ -407,8 +406,12 @@ class ArrayFacet(Facet):
|
||||||
group by
|
group by
|
||||||
value
|
value
|
||||||
order by
|
order by
|
||||||
count(*) desc, value limit {facet_size + 1}
|
count(*) desc, value limit {limit}
|
||||||
"""
|
""".format(
|
||||||
|
col=escape_sqlite(column),
|
||||||
|
sql=self.sql,
|
||||||
|
limit=facet_size + 1,
|
||||||
|
)
|
||||||
try:
|
try:
|
||||||
facet_rows_results = await self.ds.execute(
|
facet_rows_results = await self.ds.execute(
|
||||||
self.database,
|
self.database,
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,8 @@
|
||||||
import json
|
|
||||||
import math
|
|
||||||
from typing import ClassVar
|
|
||||||
|
|
||||||
from datasette import hookimpl
|
from datasette import hookimpl
|
||||||
from datasette.resources import DatabaseResource, TableResource
|
from datasette.resources import DatabaseResource
|
||||||
from datasette.utils.asgi import BadRequest
|
|
||||||
from datasette.views.base import DatasetteError
|
from datasette.views.base import DatasetteError
|
||||||
|
from datasette.utils.asgi import BadRequest
|
||||||
|
import json
|
||||||
from .utils import detect_json1, escape_sqlite, path_with_removed_args
|
from .utils import detect_json1, escape_sqlite, path_with_removed_args
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -52,20 +48,13 @@ def search_filters(request, database, table, datasette):
|
||||||
human_descriptions = []
|
human_descriptions = []
|
||||||
extra_context = {}
|
extra_context = {}
|
||||||
|
|
||||||
# Figure out which trusted fts_table to use. Query string parameters can
|
# Figure out which fts_table to use
|
||||||
# repeat this mapping (for backwards compatibility), but must not select
|
|
||||||
# a different table or primary key.
|
|
||||||
table_metadata = await datasette.table_config(database, table)
|
table_metadata = await datasette.table_config(database, table)
|
||||||
db = datasette.get_database(database)
|
db = datasette.get_database(database)
|
||||||
fts_table = table_metadata.get("fts_table")
|
fts_table = request.args.get("_fts_table")
|
||||||
|
fts_table = fts_table or table_metadata.get("fts_table")
|
||||||
fts_table = fts_table or await db.fts_table(table)
|
fts_table = fts_table or await db.fts_table(table)
|
||||||
fts_pk = table_metadata.get("fts_pk", "rowid")
|
fts_pk = request.args.get("_fts_pk", table_metadata.get("fts_pk", "rowid"))
|
||||||
requested_fts_table = request.args.get("_fts_table")
|
|
||||||
requested_fts_pk = request.args.get("_fts_pk")
|
|
||||||
if (requested_fts_table and requested_fts_table != fts_table) or (
|
|
||||||
requested_fts_pk and requested_fts_pk != fts_pk
|
|
||||||
):
|
|
||||||
raise BadRequest("Invalid _fts_table or _fts_pk")
|
|
||||||
search_args = {
|
search_args = {
|
||||||
key: request.args[key]
|
key: request.args[key]
|
||||||
for key in request.args
|
for key in request.args
|
||||||
|
|
@ -83,11 +72,6 @@ def search_filters(request, database, table, datasette):
|
||||||
extra_context["supports_search"] = bool(fts_table)
|
extra_context["supports_search"] = bool(fts_table)
|
||||||
|
|
||||||
if fts_table and search_args:
|
if fts_table and search_args:
|
||||||
await datasette.ensure_permission(
|
|
||||||
action="view-table",
|
|
||||||
resource=TableResource(database=database, table=fts_table),
|
|
||||||
actor=request.actor,
|
|
||||||
)
|
|
||||||
if "_search" in search_args:
|
if "_search" in search_args:
|
||||||
# Simple ?_search=xxx
|
# Simple ?_search=xxx
|
||||||
search = search_args["_search"]
|
search = search_args["_search"]
|
||||||
|
|
@ -115,9 +99,9 @@ def search_filters(request, database, table, datasette):
|
||||||
fts_table=escape_sqlite(fts_table),
|
fts_table=escape_sqlite(fts_table),
|
||||||
search_col=escape_sqlite(search_col),
|
search_col=escape_sqlite(search_col),
|
||||||
match_clause=(
|
match_clause=(
|
||||||
f":search_{i}"
|
":search_{}".format(i)
|
||||||
if search_mode_raw
|
if search_mode_raw
|
||||||
else f"escape_fts(:search_{i})"
|
else "escape_fts(:search_{})".format(i)
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
@ -148,18 +132,13 @@ def through_filters(request, database, table, datasette):
|
||||||
through_table = through_data["table"]
|
through_table = through_data["table"]
|
||||||
other_column = through_data["column"]
|
other_column = through_data["column"]
|
||||||
value = through_data["value"]
|
value = through_data["value"]
|
||||||
await datasette.ensure_permission(
|
|
||||||
action="view-table",
|
|
||||||
resource=TableResource(database=database, table=through_table),
|
|
||||||
actor=request.actor,
|
|
||||||
)
|
|
||||||
db = datasette.get_database(database)
|
db = datasette.get_database(database)
|
||||||
outgoing_foreign_keys = await db.foreign_keys_for_table(through_table)
|
outgoing_foreign_keys = await db.foreign_keys_for_table(through_table)
|
||||||
fk_to_us = next(
|
try:
|
||||||
(fk for fk in outgoing_foreign_keys if fk["other_table"] == table),
|
fk_to_us = [
|
||||||
None,
|
fk for fk in outgoing_foreign_keys if fk["other_table"] == table
|
||||||
)
|
][0]
|
||||||
if fk_to_us is None:
|
except IndexError:
|
||||||
raise DatasetteError(
|
raise DatasetteError(
|
||||||
"Invalid _through - could not find corresponding foreign key"
|
"Invalid _through - could not find corresponding foreign key"
|
||||||
)
|
)
|
||||||
|
|
@ -203,17 +182,6 @@ class Filter:
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
|
|
||||||
def _coerce_numeric_filter_value(value):
|
|
||||||
try:
|
|
||||||
return int(value)
|
|
||||||
except ValueError:
|
|
||||||
try:
|
|
||||||
converted = float(value)
|
|
||||||
except ValueError:
|
|
||||||
return value
|
|
||||||
return converted if math.isfinite(converted) else value
|
|
||||||
|
|
||||||
|
|
||||||
class TemplatedFilter(Filter):
|
class TemplatedFilter(Filter):
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
|
|
@ -235,17 +203,13 @@ class TemplatedFilter(Filter):
|
||||||
|
|
||||||
def where_clause(self, table, column, value, param_counter):
|
def where_clause(self, table, column, value, param_counter):
|
||||||
converted = self.format.format(value)
|
converted = self.format.format(value)
|
||||||
if self.numeric:
|
if self.numeric and converted.isdigit():
|
||||||
converted = _coerce_numeric_filter_value(converted)
|
converted = int(converted)
|
||||||
if self.no_argument:
|
if self.no_argument:
|
||||||
kwargs = {"c": _quote_sqlite_identifier(column)}
|
kwargs = {"c": column}
|
||||||
converted = None
|
converted = None
|
||||||
else:
|
else:
|
||||||
kwargs = {
|
kwargs = {"c": column, "p": f"p{param_counter}", "t": table}
|
||||||
"c": _quote_sqlite_identifier(column),
|
|
||||||
"p": f"p{param_counter}",
|
|
||||||
"t": _quote_sqlite_identifier(table),
|
|
||||||
}
|
|
||||||
return self.sql_template.format(**kwargs), converted
|
return self.sql_template.format(**kwargs), converted
|
||||||
|
|
||||||
def human_clause(self, column, value):
|
def human_clause(self, column, value):
|
||||||
|
|
@ -259,14 +223,6 @@ class TemplatedFilter(Filter):
|
||||||
return template.format(c=column, v=value)
|
return template.format(c=column, v=value)
|
||||||
|
|
||||||
|
|
||||||
def _quote_sqlite_identifier(identifier):
|
|
||||||
# Preserve the historic always-quoted SQL generated by TemplatedFilter.
|
|
||||||
escaped = escape_sqlite(identifier)
|
|
||||||
if escaped == identifier:
|
|
||||||
return f'"{identifier}"'
|
|
||||||
return escaped
|
|
||||||
|
|
||||||
|
|
||||||
class InFilter(Filter):
|
class InFilter(Filter):
|
||||||
key = "in"
|
key = "in"
|
||||||
display = "in"
|
display = "in"
|
||||||
|
|
@ -308,56 +264,56 @@ class Filters:
|
||||||
TemplatedFilter(
|
TemplatedFilter(
|
||||||
"exact",
|
"exact",
|
||||||
"=",
|
"=",
|
||||||
"{c} = :{p}",
|
'"{c}" = :{p}',
|
||||||
lambda c, v: "{c} = {v}" if v.isdigit() else '{c} = "{v}"',
|
lambda c, v: "{c} = {v}" if v.isdigit() else '{c} = "{v}"',
|
||||||
),
|
),
|
||||||
TemplatedFilter(
|
TemplatedFilter(
|
||||||
"not",
|
"not",
|
||||||
"!=",
|
"!=",
|
||||||
"{c} != :{p}",
|
'"{c}" != :{p}',
|
||||||
lambda c, v: "{c} != {v}" if v.isdigit() else '{c} != "{v}"',
|
lambda c, v: "{c} != {v}" if v.isdigit() else '{c} != "{v}"',
|
||||||
),
|
),
|
||||||
TemplatedFilter(
|
TemplatedFilter(
|
||||||
"contains",
|
"contains",
|
||||||
"contains",
|
"contains",
|
||||||
"{c} like :{p}",
|
'"{c}" like :{p}',
|
||||||
'{c} contains "{v}"',
|
'{c} contains "{v}"',
|
||||||
format="%{}%",
|
format="%{}%",
|
||||||
),
|
),
|
||||||
TemplatedFilter(
|
TemplatedFilter(
|
||||||
"notcontains",
|
"notcontains",
|
||||||
"does not contain",
|
"does not contain",
|
||||||
"{c} not like :{p}",
|
'"{c}" not like :{p}',
|
||||||
'{c} does not contain "{v}"',
|
'{c} does not contain "{v}"',
|
||||||
format="%{}%",
|
format="%{}%",
|
||||||
),
|
),
|
||||||
TemplatedFilter(
|
TemplatedFilter(
|
||||||
"endswith",
|
"endswith",
|
||||||
"ends with",
|
"ends with",
|
||||||
"{c} like :{p}",
|
'"{c}" like :{p}',
|
||||||
'{c} ends with "{v}"',
|
'{c} ends with "{v}"',
|
||||||
format="%{}",
|
format="%{}",
|
||||||
),
|
),
|
||||||
TemplatedFilter(
|
TemplatedFilter(
|
||||||
"startswith",
|
"startswith",
|
||||||
"starts with",
|
"starts with",
|
||||||
"{c} like :{p}",
|
'"{c}" like :{p}',
|
||||||
'{c} starts with "{v}"',
|
'{c} starts with "{v}"',
|
||||||
format="{}%",
|
format="{}%",
|
||||||
),
|
),
|
||||||
TemplatedFilter("gt", ">", "{c} > :{p}", "{c} > {v}", numeric=True),
|
TemplatedFilter("gt", ">", '"{c}" > :{p}', "{c} > {v}", numeric=True),
|
||||||
TemplatedFilter(
|
TemplatedFilter(
|
||||||
"gte", "\u2265", "{c} >= :{p}", "{c} \u2265 {v}", numeric=True
|
"gte", "\u2265", '"{c}" >= :{p}', "{c} \u2265 {v}", numeric=True
|
||||||
),
|
),
|
||||||
TemplatedFilter("lt", "<", "{c} < :{p}", "{c} < {v}", numeric=True),
|
TemplatedFilter("lt", "<", '"{c}" < :{p}', "{c} < {v}", numeric=True),
|
||||||
TemplatedFilter(
|
TemplatedFilter(
|
||||||
"lte", "\u2264", "{c} <= :{p}", "{c} \u2264 {v}", numeric=True
|
"lte", "\u2264", '"{c}" <= :{p}', "{c} \u2264 {v}", numeric=True
|
||||||
),
|
),
|
||||||
TemplatedFilter("like", "like", "{c} like :{p}", '{c} like "{v}"'),
|
TemplatedFilter("like", "like", '"{c}" like :{p}', '{c} like "{v}"'),
|
||||||
TemplatedFilter(
|
TemplatedFilter(
|
||||||
"notlike", "not like", "{c} not like :{p}", '{c} not like "{v}"'
|
"notlike", "not like", '"{c}" not like :{p}', '{c} not like "{v}"'
|
||||||
),
|
),
|
||||||
TemplatedFilter("glob", "glob", "{c} glob :{p}", '{c} glob "{v}"'),
|
TemplatedFilter("glob", "glob", '"{c}" glob :{p}', '{c} glob "{v}"'),
|
||||||
InFilter(),
|
InFilter(),
|
||||||
NotInFilter(),
|
NotInFilter(),
|
||||||
]
|
]
|
||||||
|
|
@ -366,13 +322,13 @@ class Filters:
|
||||||
TemplatedFilter(
|
TemplatedFilter(
|
||||||
"arraycontains",
|
"arraycontains",
|
||||||
"array contains",
|
"array contains",
|
||||||
""":{p} in (select value from json_each({t}.{c}))""",
|
""":{p} in (select value from json_each([{t}].[{c}]))""",
|
||||||
'{c} contains "{v}"',
|
'{c} contains "{v}"',
|
||||||
),
|
),
|
||||||
TemplatedFilter(
|
TemplatedFilter(
|
||||||
"arraynotcontains",
|
"arraynotcontains",
|
||||||
"array does not contain",
|
"array does not contain",
|
||||||
""":{p} not in (select value from json_each({t}.{c}))""",
|
""":{p} not in (select value from json_each([{t}].[{c}]))""",
|
||||||
'{c} does not contain "{v}"',
|
'{c} does not contain "{v}"',
|
||||||
),
|
),
|
||||||
]
|
]
|
||||||
|
|
@ -380,34 +336,36 @@ class Filters:
|
||||||
else []
|
else []
|
||||||
)
|
)
|
||||||
+ [
|
+ [
|
||||||
TemplatedFilter("date", "date", "date({c}) = :{p}", '"{c}" is on date {v}'),
|
|
||||||
TemplatedFilter(
|
TemplatedFilter(
|
||||||
"isnull", "is null", "{c} is null", "{c} is null", no_argument=True
|
"date", "date", 'date("{c}") = :{p}', '"{c}" is on date {v}'
|
||||||
|
),
|
||||||
|
TemplatedFilter(
|
||||||
|
"isnull", "is null", '"{c}" is null', "{c} is null", no_argument=True
|
||||||
),
|
),
|
||||||
TemplatedFilter(
|
TemplatedFilter(
|
||||||
"notnull",
|
"notnull",
|
||||||
"is not null",
|
"is not null",
|
||||||
"{c} is not null",
|
'"{c}" is not null',
|
||||||
"{c} is not null",
|
"{c} is not null",
|
||||||
no_argument=True,
|
no_argument=True,
|
||||||
),
|
),
|
||||||
TemplatedFilter(
|
TemplatedFilter(
|
||||||
"isblank",
|
"isblank",
|
||||||
"is blank",
|
"is blank",
|
||||||
"({c} is null or {c} = '')",
|
'("{c}" is null or "{c}" = "")',
|
||||||
"{c} is blank",
|
"{c} is blank",
|
||||||
no_argument=True,
|
no_argument=True,
|
||||||
),
|
),
|
||||||
TemplatedFilter(
|
TemplatedFilter(
|
||||||
"notblank",
|
"notblank",
|
||||||
"is not blank",
|
"is not blank",
|
||||||
"({c} is not null and {c} != '')",
|
'("{c}" is not null and "{c}" != "")',
|
||||||
"{c} is not blank",
|
"{c} is not blank",
|
||||||
no_argument=True,
|
no_argument=True,
|
||||||
),
|
),
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
_filters_by_key: ClassVar[dict[str, Filter]] = {f.key: f for f in _filters}
|
_filters_by_key = {f.key: f for f in _filters}
|
||||||
|
|
||||||
def __init__(self, pairs):
|
def __init__(self, pairs):
|
||||||
self.pairs = pairs
|
self.pairs = pairs
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,9 @@
|
||||||
|
from datasette.utils.sqlite import sqlite3
|
||||||
|
from datasette.utils import documented
|
||||||
import itertools
|
import itertools
|
||||||
import random
|
import random
|
||||||
import string
|
import string
|
||||||
|
|
||||||
from datasette.utils import documented
|
|
||||||
from datasette.utils.sqlite import sqlite3
|
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
"EXTRA_DATABASE_SQL",
|
"EXTRA_DATABASE_SQL",
|
||||||
"TABLES",
|
"TABLES",
|
||||||
|
|
@ -347,7 +346,9 @@ CREATE VIEW searchable_view_configured_by_metadata AS
|
||||||
+ '\nINSERT INTO no_primary_key VALUES ("RENDER_CELL_DEMO", "a202", "b202", "c202");\n'
|
+ '\nINSERT INTO no_primary_key VALUES ("RENDER_CELL_DEMO", "a202", "b202", "c202");\n'
|
||||||
+ "\n".join(
|
+ "\n".join(
|
||||||
[
|
[
|
||||||
f'INSERT INTO compound_three_primary_keys VALUES ("{a}", "{b}", "{c}", "{content}");'
|
'INSERT INTO compound_three_primary_keys VALUES ("{a}", "{b}", "{c}", "{content}");'.format(
|
||||||
|
a=a, b=b, c=c, content=content
|
||||||
|
)
|
||||||
for a, b, c, content in generate_compound_rows(1001)
|
for a, b, c, content in generate_compound_rows(1001)
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -1,20 +1,9 @@
|
||||||
from datasette import Response, hookimpl
|
from datasette import hookimpl, Response
|
||||||
|
|
||||||
from .utils import add_cors_headers
|
|
||||||
|
|
||||||
|
|
||||||
@hookimpl(trylast=True)
|
@hookimpl(trylast=True)
|
||||||
def forbidden(datasette, request, message):
|
def forbidden(datasette, request, message):
|
||||||
async def inner():
|
async def inner():
|
||||||
if (
|
|
||||||
request.path.split("?")[0].endswith(".json")
|
|
||||||
or "application/json" in (request.headers.get("accept") or "")
|
|
||||||
or request.headers.get("content-type") == "application/json"
|
|
||||||
):
|
|
||||||
headers = {}
|
|
||||||
if datasette.cors:
|
|
||||||
add_cors_headers(headers)
|
|
||||||
return Response.error(message, 403, headers=headers)
|
|
||||||
return Response.html(
|
return Response.html(
|
||||||
await datasette.render_template(
|
await datasette.render_template(
|
||||||
"error.html",
|
"error.html",
|
||||||
|
|
|
||||||
|
|
@ -1,21 +1,16 @@
|
||||||
import traceback
|
from datasette import hookimpl, Response
|
||||||
|
from .utils import add_cors_headers
|
||||||
from markupsafe import Markup
|
|
||||||
|
|
||||||
from datasette import Response, hookimpl
|
|
||||||
|
|
||||||
from .utils import add_cors_headers, error_body
|
|
||||||
from .utils.asgi import (
|
from .utils.asgi import (
|
||||||
Base400,
|
Base400,
|
||||||
)
|
)
|
||||||
from .views.base import DatasetteError
|
from .views.base import DatasetteError
|
||||||
|
from markupsafe import Markup
|
||||||
|
import traceback
|
||||||
|
|
||||||
# Debugger imports are deliberate - they back the "pdb" setting, which drops
|
|
||||||
# into a debugger on unhandled exceptions
|
|
||||||
try:
|
try:
|
||||||
import ipdb as pdb # noqa: T100
|
import ipdb as pdb
|
||||||
except ImportError:
|
except ImportError:
|
||||||
import pdb # noqa: T100
|
import pdb
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import rich
|
import rich
|
||||||
|
|
@ -33,7 +28,6 @@ def handle_exception(datasette, request, exception):
|
||||||
rich.get_console().print_exception(show_locals=True)
|
rich.get_console().print_exception(show_locals=True)
|
||||||
|
|
||||||
title = None
|
title = None
|
||||||
plain_message = None
|
|
||||||
if isinstance(exception, Base400):
|
if isinstance(exception, Base400):
|
||||||
status = exception.status
|
status = exception.status
|
||||||
info = {}
|
info = {}
|
||||||
|
|
@ -42,7 +36,6 @@ def handle_exception(datasette, request, exception):
|
||||||
status = exception.status
|
status = exception.status
|
||||||
info = exception.error_dict
|
info = exception.error_dict
|
||||||
message = exception.message
|
message = exception.message
|
||||||
plain_message = exception.plain_message
|
|
||||||
if exception.message_is_html:
|
if exception.message_is_html:
|
||||||
message = Markup(message)
|
message = Markup(message)
|
||||||
title = exception.title
|
title = exception.title
|
||||||
|
|
@ -52,17 +45,6 @@ def handle_exception(datasette, request, exception):
|
||||||
message = str(exception)
|
message = str(exception)
|
||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
templates = [f"{status}.html", "error.html"]
|
templates = [f"{status}.html", "error.html"]
|
||||||
headers = {}
|
|
||||||
if datasette.cors:
|
|
||||||
add_cors_headers(headers)
|
|
||||||
if request.path.split("?")[0].endswith(".json"):
|
|
||||||
body = dict(info)
|
|
||||||
body.update(error_body(plain_message or message, status))
|
|
||||||
return Response.json(body, status=status, headers=headers)
|
|
||||||
if request.path.split("?")[0].endswith(".csv"):
|
|
||||||
return Response.text(
|
|
||||||
plain_message or message, status=status, headers=headers
|
|
||||||
)
|
|
||||||
info.update(
|
info.update(
|
||||||
{
|
{
|
||||||
"ok": False,
|
"ok": False,
|
||||||
|
|
@ -71,15 +53,18 @@ def handle_exception(datasette, request, exception):
|
||||||
"title": title,
|
"title": title,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
environment = datasette.get_jinja_environment(request)
|
headers = {}
|
||||||
template = environment.select_template(templates)
|
if datasette.cors:
|
||||||
|
add_cors_headers(headers)
|
||||||
|
if request.path.split("?")[0].endswith(".json"):
|
||||||
|
return Response.json(info, status=status, headers=headers)
|
||||||
|
else:
|
||||||
return Response.html(
|
return Response.html(
|
||||||
await template.render_async(
|
await datasette.render_template(
|
||||||
dict(
|
templates,
|
||||||
info,
|
info,
|
||||||
urls=datasette.urls,
|
request=request,
|
||||||
menu_links=list,
|
view_name="error",
|
||||||
)
|
|
||||||
),
|
),
|
||||||
status=status,
|
status=status,
|
||||||
headers=headers,
|
headers=headers,
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
from pluggy import HookimplMarker, HookspecMarker
|
from pluggy import HookimplMarker
|
||||||
|
from pluggy import HookspecMarker
|
||||||
|
|
||||||
hookspec = HookspecMarker("datasette")
|
hookspec = HookspecMarker("datasette")
|
||||||
hookimpl = HookimplMarker("datasette")
|
hookimpl = HookimplMarker("datasette")
|
||||||
|
|
@ -9,11 +10,6 @@ def startup(datasette):
|
||||||
"""Fires directly after Datasette first starts running"""
|
"""Fires directly after Datasette first starts running"""
|
||||||
|
|
||||||
|
|
||||||
@hookspec
|
|
||||||
def shutdown(datasette):
|
|
||||||
"""Called once when the Datasette server is shutting down"""
|
|
||||||
|
|
||||||
|
|
||||||
@hookspec
|
@hookspec
|
||||||
def asgi_wrapper(datasette):
|
def asgi_wrapper(datasette):
|
||||||
"""Returns an ASGI middleware callable to wrap our ASGI application with"""
|
"""Returns an ASGI middleware callable to wrap our ASGI application with"""
|
||||||
|
|
@ -50,7 +46,7 @@ def extra_body_script(
|
||||||
def extra_template_vars(
|
def extra_template_vars(
|
||||||
template, database, table, columns, view_name, request, datasette
|
template, database, table, columns, view_name, request, datasette
|
||||||
):
|
):
|
||||||
"""Extra template variables to be made available to the template - can return dict, None, callable or awaitable"""
|
"""Extra template variables to be made available to the template - can return dict or callable or awaitable"""
|
||||||
|
|
||||||
|
|
||||||
@hookspec
|
@hookspec
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,13 @@
|
||||||
import hashlib
|
import hashlib
|
||||||
|
|
||||||
from .utils import (
|
from .utils import (
|
||||||
|
detect_spatialite,
|
||||||
detect_fts,
|
detect_fts,
|
||||||
detect_primary_keys,
|
detect_primary_keys,
|
||||||
detect_spatialite,
|
|
||||||
escape_sqlite,
|
escape_sqlite,
|
||||||
get_all_foreign_keys,
|
get_all_foreign_keys,
|
||||||
sqlite3,
|
|
||||||
table_columns,
|
table_columns,
|
||||||
|
sqlite3,
|
||||||
)
|
)
|
||||||
|
|
||||||
HASH_BLOCK_SIZE = 1024 * 1024
|
HASH_BLOCK_SIZE = 1024 * 1024
|
||||||
|
|
@ -95,10 +95,10 @@ def inspect_tables(conn, database_metadata):
|
||||||
""")
|
""")
|
||||||
]
|
]
|
||||||
|
|
||||||
for t, table_info in tables.items():
|
for t in tables.keys():
|
||||||
for hidden_table in hidden_tables:
|
for hidden_table in hidden_tables:
|
||||||
if t == hidden_table or t.startswith(hidden_table):
|
if t == hidden_table or t.startswith(hidden_table):
|
||||||
table_info["hidden"] = True
|
tables[t]["hidden"] = True
|
||||||
continue
|
continue
|
||||||
|
|
||||||
return tables
|
return tables
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,7 @@ class JumpSQL:
|
||||||
search_text: str | None = None,
|
search_text: str | None = None,
|
||||||
display_name: str | None = None,
|
display_name: str | None = None,
|
||||||
item_type: str = "menu",
|
item_type: str = "menu",
|
||||||
) -> JumpSQL:
|
) -> "JumpSQL":
|
||||||
if search_text is None:
|
if search_text is None:
|
||||||
search_text = " ".join(
|
search_text = " ".join(
|
||||||
text for text in (label, display_name, description) if text is not None
|
text for text in (label, display_name, description) if text is not None
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,7 @@
|
||||||
import contextvars
|
|
||||||
from abc import ABC, abstractmethod
|
from abc import ABC, abstractmethod
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from typing import Any, NamedTuple
|
from typing import Any, NamedTuple
|
||||||
|
import contextvars
|
||||||
_SQLITE_IDENTIFIER_CASE = str.maketrans(
|
|
||||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz"
|
|
||||||
)
|
|
||||||
|
|
||||||
# Context variable to track when permission checks should be skipped
|
# Context variable to track when permission checks should be skipped
|
||||||
_skip_permission_checks = contextvars.ContextVar(
|
_skip_permission_checks = contextvars.ContextVar(
|
||||||
|
|
@ -53,15 +49,6 @@ class Resource(ABC):
|
||||||
# Class-level metadata (subclasses must define these)
|
# Class-level metadata (subclasses must define these)
|
||||||
name: str = None # e.g., "table", "database", "model"
|
name: str = None # e.g., "table", "database", "model"
|
||||||
parent_class: type["Resource"] | None = None # e.g., DatabaseResource for tables
|
parent_class: type["Resource"] | None = None # e.g., DatabaseResource for tables
|
||||||
case_insensitive_child: bool = False
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def normalize_child(cls, child: str | None) -> str | None:
|
|
||||||
"""Return a comparison key without changing the resource's display name."""
|
|
||||||
if cls.case_insensitive_child and child is not None:
|
|
||||||
# Match SQLite NOCASE: fold ASCII only, not Unicode lower/casefold.
|
|
||||||
return child.translate(_SQLITE_IDENTIFIER_CASE)
|
|
||||||
return child
|
|
||||||
|
|
||||||
# Instance-level optional extra attributes
|
# Instance-level optional extra attributes
|
||||||
reasons: list[str] | None = None
|
reasons: list[str] | None = None
|
||||||
|
|
@ -85,8 +72,8 @@ class Resource(ABC):
|
||||||
)
|
)
|
||||||
|
|
||||||
def __repr__(self) -> str:
|
def __repr__(self) -> str:
|
||||||
return (
|
return "{}(parent={!r}, child={!r})".format(
|
||||||
f"{self.__class__.__name__}(parent={self.parent!r}, child={self.child!r})"
|
self.__class__.__name__, self.parent, self.child
|
||||||
)
|
)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
|
@ -142,6 +129,7 @@ class Resource(ABC):
|
||||||
|
|
||||||
Must return two columns: parent, child
|
Must return two columns: parent, child
|
||||||
"""
|
"""
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
class AllowedResource(NamedTuple):
|
class AllowedResource(NamedTuple):
|
||||||
|
|
@ -159,11 +147,6 @@ class Action:
|
||||||
resource_class: type[Resource] | None = None
|
resource_class: type[Resource] | None = None
|
||||||
also_requires: str | None = None # Optional action name that must also be allowed
|
also_requires: str | None = None # Optional action name that must also be allowed
|
||||||
|
|
||||||
def normalize_child(self, child: str | None) -> str | None:
|
|
||||||
if self.resource_class is None:
|
|
||||||
return child
|
|
||||||
return self.resource_class.normalize_child(child)
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def takes_parent(self) -> bool:
|
def takes_parent(self) -> bool:
|
||||||
"""
|
"""
|
||||||
|
|
|
||||||
|
|
@ -1,14 +1,20 @@
|
||||||
import importlib
|
import importlib
|
||||||
import importlib.metadata as importlib_metadata
|
|
||||||
import importlib.resources as importlib_resources
|
|
||||||
import os
|
import os
|
||||||
import sys
|
|
||||||
from pprint import pprint
|
|
||||||
|
|
||||||
import pluggy
|
import pluggy
|
||||||
|
from pprint import pprint
|
||||||
|
import sys
|
||||||
from . import hookspecs
|
from . import hookspecs
|
||||||
|
|
||||||
|
if sys.version_info >= (3, 9):
|
||||||
|
import importlib.resources as importlib_resources
|
||||||
|
else:
|
||||||
|
import importlib_resources
|
||||||
|
if sys.version_info >= (3, 10):
|
||||||
|
import importlib.metadata as importlib_metadata
|
||||||
|
else:
|
||||||
|
import importlib_metadata
|
||||||
|
|
||||||
|
|
||||||
DEFAULT_PLUGINS = (
|
DEFAULT_PLUGINS = (
|
||||||
"datasette.publish.heroku",
|
"datasette.publish.heroku",
|
||||||
"datasette.publish.cloudrun",
|
"datasette.publish.cloudrun",
|
||||||
|
|
@ -18,7 +24,6 @@ DEFAULT_PLUGINS = (
|
||||||
"datasette.actor_auth_cookie",
|
"datasette.actor_auth_cookie",
|
||||||
"datasette.default_permissions",
|
"datasette.default_permissions",
|
||||||
"datasette.default_permissions.tokens",
|
"datasette.default_permissions.tokens",
|
||||||
"datasette.default_permissions.sqlite_statistics",
|
|
||||||
"datasette.default_actions",
|
"datasette.default_actions",
|
||||||
"datasette.default_column_types",
|
"datasette.default_column_types",
|
||||||
"datasette.default_magic_parameters",
|
"datasette.default_magic_parameters",
|
||||||
|
|
@ -26,7 +31,6 @@ DEFAULT_PLUGINS = (
|
||||||
"datasette.default_debug_menu",
|
"datasette.default_debug_menu",
|
||||||
"datasette.default_jump_items",
|
"datasette.default_jump_items",
|
||||||
"datasette.default_database_actions",
|
"datasette.default_database_actions",
|
||||||
"datasette.default_table_actions",
|
|
||||||
"datasette.default_query_actions",
|
"datasette.default_query_actions",
|
||||||
"datasette.handle_exception",
|
"datasette.handle_exception",
|
||||||
"datasette.forbidden",
|
"datasette.forbidden",
|
||||||
|
|
@ -80,7 +84,7 @@ if DATASETTE_LOAD_PLUGINS is not None:
|
||||||
# Ensure name can be found in plugin_to_distinfo later:
|
# Ensure name can be found in plugin_to_distinfo later:
|
||||||
pm._plugin_distinfo.append((mod, distribution))
|
pm._plugin_distinfo.append((mod, distribution))
|
||||||
except importlib_metadata.PackageNotFoundError:
|
except importlib_metadata.PackageNotFoundError:
|
||||||
sys.stderr.write(f"Plugin {package_name} could not be found\n")
|
sys.stderr.write("Plugin {} could not be found\n".format(package_name))
|
||||||
|
|
||||||
|
|
||||||
# Load default plugins
|
# Load default plugins
|
||||||
|
|
|
||||||
|
|
@ -1,17 +1,15 @@
|
||||||
|
from datasette import hookimpl
|
||||||
|
import click
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
from subprocess import CalledProcessError, check_call, check_output
|
from subprocess import CalledProcessError, check_call, check_output
|
||||||
|
|
||||||
import click
|
|
||||||
|
|
||||||
from datasette import hookimpl
|
|
||||||
|
|
||||||
from ..utils import temporary_docker_directory
|
|
||||||
from .common import (
|
from .common import (
|
||||||
add_common_publish_arguments_and_options,
|
add_common_publish_arguments_and_options,
|
||||||
fail_if_publish_binary_not_installed,
|
fail_if_publish_binary_not_installed,
|
||||||
)
|
)
|
||||||
|
from ..utils import temporary_docker_directory
|
||||||
|
|
||||||
|
|
||||||
@hookimpl
|
@hookimpl
|
||||||
|
|
@ -221,7 +219,7 @@ def publish_subcommand(publish):
|
||||||
|
|
||||||
check_call(
|
check_call(
|
||||||
"gcloud builds submit --tag {}{}".format(
|
"gcloud builds submit --tag {}{}".format(
|
||||||
image_id, f" --timeout {timeout}" if timeout else ""
|
image_id, " --timeout {}".format(timeout) if timeout else ""
|
||||||
),
|
),
|
||||||
shell=True,
|
shell=True,
|
||||||
)
|
)
|
||||||
|
|
@ -233,7 +231,7 @@ def publish_subcommand(publish):
|
||||||
("--min-instances", min_instances),
|
("--min-instances", min_instances),
|
||||||
):
|
):
|
||||||
if value is not None:
|
if value is not None:
|
||||||
extra_deploy_options.append(f"{option} {value}")
|
extra_deploy_options.append("{} {}".format(option, value))
|
||||||
check_call(
|
check_call(
|
||||||
"gcloud run deploy --allow-unauthenticated --platform=managed --image {} {}{}".format(
|
"gcloud run deploy --allow-unauthenticated --platform=managed --image {} {}{}".format(
|
||||||
image_id,
|
image_id,
|
||||||
|
|
@ -260,16 +258,24 @@ def _ensure_artifact_registry(artifact_project, artifact_region, artifact_reposi
|
||||||
) from exc
|
) from exc
|
||||||
|
|
||||||
describe_cmd = (
|
describe_cmd = (
|
||||||
f"gcloud artifacts repositories describe {artifact_repository} --project {artifact_project} "
|
"gcloud artifacts repositories describe {repo} --project {project} "
|
||||||
f"--location {artifact_region} --quiet"
|
"--location {location} --quiet"
|
||||||
|
).format(
|
||||||
|
repo=artifact_repository,
|
||||||
|
project=artifact_project,
|
||||||
|
location=artifact_region,
|
||||||
)
|
)
|
||||||
try:
|
try:
|
||||||
check_call(describe_cmd, shell=True)
|
check_call(describe_cmd, shell=True)
|
||||||
return
|
return
|
||||||
except CalledProcessError:
|
except CalledProcessError:
|
||||||
create_cmd = (
|
create_cmd = (
|
||||||
f"gcloud artifacts repositories create {artifact_repository} --repository-format=docker "
|
"gcloud artifacts repositories create {repo} --repository-format=docker "
|
||||||
f'--location {artifact_region} --project {artifact_project} --description "Datasette Cloud Run images" --quiet'
|
'--location {location} --project {project} --description "Datasette Cloud Run images" --quiet'
|
||||||
|
).format(
|
||||||
|
repo=artifact_repository,
|
||||||
|
location=artifact_region,
|
||||||
|
project=artifact_project,
|
||||||
)
|
)
|
||||||
try:
|
try:
|
||||||
check_call(create_cmd, shell=True)
|
check_call(create_cmd, shell=True)
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,9 @@
|
||||||
|
from ..utils import StaticMount
|
||||||
|
import click
|
||||||
import os
|
import os
|
||||||
import shutil
|
import shutil
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
import click
|
|
||||||
|
|
||||||
from ..utils import StaticMount
|
|
||||||
|
|
||||||
|
|
||||||
def add_common_publish_arguments_and_options(subcommand):
|
def add_common_publish_arguments_and_options(subcommand):
|
||||||
for decorator in reversed(
|
for decorator in reversed(
|
||||||
|
|
@ -78,7 +76,9 @@ def fail_if_publish_binary_not_installed(binary, publish_target, install_link):
|
||||||
"""Exit (with error message) if ``binary` isn't installed"""
|
"""Exit (with error message) if ``binary` isn't installed"""
|
||||||
if not shutil.which(binary):
|
if not shutil.which(binary):
|
||||||
click.secho(
|
click.secho(
|
||||||
f"Publishing to {publish_target} requires {binary} to be installed and configured",
|
"Publishing to {publish_target} requires {binary} to be installed and configured".format(
|
||||||
|
publish_target=publish_target, binary=binary
|
||||||
|
),
|
||||||
bg="red",
|
bg="red",
|
||||||
fg="white",
|
fg="white",
|
||||||
bold=True,
|
bold=True,
|
||||||
|
|
|
||||||
|
|
@ -1,21 +1,19 @@
|
||||||
|
from contextlib import contextmanager
|
||||||
|
from datasette import hookimpl
|
||||||
|
import click
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
import pathlib
|
import pathlib
|
||||||
import shlex
|
import shlex
|
||||||
import shutil
|
import shutil
|
||||||
import tempfile
|
|
||||||
from contextlib import contextmanager
|
|
||||||
from subprocess import call, check_output
|
from subprocess import call, check_output
|
||||||
|
import tempfile
|
||||||
import click
|
|
||||||
|
|
||||||
from datasette import hookimpl
|
|
||||||
from datasette.utils import link_or_copy, link_or_copy_directory, parse_metadata
|
|
||||||
|
|
||||||
from .common import (
|
from .common import (
|
||||||
add_common_publish_arguments_and_options,
|
add_common_publish_arguments_and_options,
|
||||||
fail_if_publish_binary_not_installed,
|
fail_if_publish_binary_not_installed,
|
||||||
)
|
)
|
||||||
|
from datasette.utils import link_or_copy, link_or_copy_directory, parse_metadata
|
||||||
|
|
||||||
|
|
||||||
@hookimpl
|
@hookimpl
|
||||||
|
|
@ -236,7 +234,7 @@ def temporary_heroku_directory(
|
||||||
extras.extend(["--static", f"{mount_point}:{mount_point}"])
|
extras.extend(["--static", f"{mount_point}:{mount_point}"])
|
||||||
|
|
||||||
quoted_files = " ".join(
|
quoted_files = " ".join(
|
||||||
[f"-i {shlex.quote(file_name)}" for file_name in file_names]
|
["-i {}".format(shlex.quote(file_name)) for file_name in file_names]
|
||||||
)
|
)
|
||||||
procfile_cmd = "web: datasette serve --host 0.0.0.0 {quoted_files} --cors --port $PORT --inspect-file inspect-data.json {extras}".format(
|
procfile_cmd = "web: datasette serve --host 0.0.0.0 {quoted_files} --cors --port $PORT --inspect-file inspect-data.json {extras}".format(
|
||||||
quoted_files=quoted_files, extras=" ".join(extras)
|
quoted_files=quoted_files, extras=" ".join(extras)
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,11 @@
|
||||||
import json
|
import json
|
||||||
|
|
||||||
from datasette.extras import extra_names_from_request
|
from datasette.extras import extra_names_from_request
|
||||||
from datasette.utils import (
|
from datasette.utils import (
|
||||||
CustomJSONEncoder,
|
|
||||||
error_body,
|
|
||||||
path_from_row_pks,
|
|
||||||
remove_infinites,
|
|
||||||
sqlite3,
|
|
||||||
value_as_boolean,
|
value_as_boolean,
|
||||||
|
remove_infinites,
|
||||||
|
CustomJSONEncoder,
|
||||||
|
path_from_row_pks,
|
||||||
|
sqlite3,
|
||||||
)
|
)
|
||||||
from datasette.utils.asgi import Response
|
from datasette.utils.asgi import Response
|
||||||
|
|
||||||
|
|
@ -54,7 +52,8 @@ def json_renderer(request, args, data, error, truncated=None):
|
||||||
if error:
|
if error:
|
||||||
shape = "objects"
|
shape = "objects"
|
||||||
status_code = 400
|
status_code = 400
|
||||||
data.update(error_body(error, status_code))
|
data["error"] = error
|
||||||
|
data["ok"] = False
|
||||||
|
|
||||||
if truncated is not None:
|
if truncated is not None:
|
||||||
data["truncated"] = truncated
|
data["truncated"] = truncated
|
||||||
|
|
@ -88,8 +87,7 @@ def json_renderer(request, args, data, error, truncated=None):
|
||||||
object_rows[pk_string] = row
|
object_rows[pk_string] = row
|
||||||
data = object_rows
|
data = object_rows
|
||||||
if shape_error:
|
if shape_error:
|
||||||
status_code = 400
|
data = {"ok": False, "error": shape_error}
|
||||||
data = error_body(shape_error, status_code)
|
|
||||||
elif shape == "array":
|
elif shape == "array":
|
||||||
data = data["rows"]
|
data = data["rows"]
|
||||||
|
|
||||||
|
|
@ -102,7 +100,12 @@ def json_renderer(request, args, data, error, truncated=None):
|
||||||
data["rows"] = [list(row.values()) for row in data["rows"]]
|
data["rows"] = [list(row.values()) for row in data["rows"]]
|
||||||
else:
|
else:
|
||||||
status_code = 400
|
status_code = 400
|
||||||
data = error_body(f"Invalid _shape: {shape}", status_code)
|
data = {
|
||||||
|
"ok": False,
|
||||||
|
"error": f"Invalid _shape: {shape}",
|
||||||
|
"status": 400,
|
||||||
|
"title": None,
|
||||||
|
}
|
||||||
|
|
||||||
# Don't include "columns" in output
|
# Don't include "columns" in output
|
||||||
# https://github.com/simonw/datasette/issues/2136
|
# https://github.com/simonw/datasette/issues/2136
|
||||||
|
|
|
||||||
|
|
@ -25,7 +25,6 @@ class TableResource(Resource):
|
||||||
|
|
||||||
name = "table"
|
name = "table"
|
||||||
parent_class = DatabaseResource
|
parent_class = DatabaseResource
|
||||||
case_insensitive_child = True
|
|
||||||
|
|
||||||
def __init__(self, database: str, table: str):
|
def __init__(self, database: str, table: str):
|
||||||
super().__init__(parent=database, child=table)
|
super().__init__(parent=database, child=table)
|
||||||
|
|
|
||||||
File diff suppressed because it is too large
Load diff
|
|
@ -472,13 +472,11 @@ class ColumnChooser extends HTMLElement {
|
||||||
<span class="drag-item-check">
|
<span class="drag-item-check">
|
||||||
<input type="checkbox" ${this._checked.has(col) ? "checked" : ""}>
|
<input type="checkbox" ${this._checked.has(col) ? "checked" : ""}>
|
||||||
</span>
|
</span>
|
||||||
<span class="drag-item-label"></span>
|
<span class="drag-item-label">${col}</span>
|
||||||
</label>
|
</label>
|
||||||
<div class="drop-indicator"></div>
|
<div class="drop-indicator"></div>
|
||||||
`;
|
`;
|
||||||
|
|
||||||
li.querySelector(".drag-item-label").textContent = col;
|
|
||||||
|
|
||||||
li.querySelector("input").addEventListener("change", (e) => {
|
li.querySelector("input").addEventListener("change", (e) => {
|
||||||
e.target.checked ? this._checked.add(col) : this._checked.delete(col);
|
e.target.checked ? this._checked.add(col) : this._checked.delete(col);
|
||||||
this._updateCounts();
|
this._updateCounts();
|
||||||
|
|
|
||||||
|
|
@ -25,6 +25,29 @@ const DOM_SELECTORS = {
|
||||||
facetResults: ".facet-results [data-column]",
|
facetResults: ".facet-results [data-column]",
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let editToolsPromise = null;
|
||||||
|
let autocompletePromise = null;
|
||||||
|
|
||||||
|
function datasettePath(path) {
|
||||||
|
const baseUrl = window.datasetteBaseUrl || "/";
|
||||||
|
return baseUrl.replace(/\/?$/, "/") + String(path || "").replace(/^\/+/, "");
|
||||||
|
}
|
||||||
|
|
||||||
|
function parseInsertDialogRow(value) {
|
||||||
|
if (!value) {
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const row = JSON.parse(value);
|
||||||
|
if (!row || typeof row !== "object" || Array.isArray(row)) {
|
||||||
|
throw new Error("row must be an object");
|
||||||
|
}
|
||||||
|
return row;
|
||||||
|
} catch (error) {
|
||||||
|
throw new Error("Invalid insert dialog row: " + error.message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Monolith class for interacting with Datasette JS API
|
* Monolith class for interacting with Datasette JS API
|
||||||
* Imported with DEFER, runs after main document parsed
|
* Imported with DEFER, runs after main document parsed
|
||||||
|
|
@ -39,6 +62,62 @@ const datasetteManager = {
|
||||||
// Does pluginMetadata need to be serializable, or can we let it be stateful / have functions?
|
// Does pluginMetadata need to be serializable, or can we let it be stateful / have functions?
|
||||||
plugins: new Map(),
|
plugins: new Map(),
|
||||||
|
|
||||||
|
path: datasettePath,
|
||||||
|
|
||||||
|
loadAutocomplete: () => {
|
||||||
|
if (!window.customElements) {
|
||||||
|
return Promise.resolve(null);
|
||||||
|
}
|
||||||
|
if (customElements.get("datasette-autocomplete")) {
|
||||||
|
return Promise.resolve(customElements.get("datasette-autocomplete"));
|
||||||
|
}
|
||||||
|
if (!autocompletePromise) {
|
||||||
|
const url =
|
||||||
|
window.datasetteAutocompleteUrl ||
|
||||||
|
datasettePath("/-/static/autocomplete.js");
|
||||||
|
autocompletePromise = import(url).then(() =>
|
||||||
|
customElements.get("datasette-autocomplete"),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return autocompletePromise;
|
||||||
|
},
|
||||||
|
|
||||||
|
loadEditTools: () => {
|
||||||
|
if (window.__DATASETTE_EDIT_TOOLS__) {
|
||||||
|
if (window.__DATASETTE_EDIT_TOOLS__.install) {
|
||||||
|
window.__DATASETTE_EDIT_TOOLS__.install(datasetteManager);
|
||||||
|
}
|
||||||
|
return Promise.resolve(window.__DATASETTE_EDIT_TOOLS__);
|
||||||
|
}
|
||||||
|
if (!editToolsPromise) {
|
||||||
|
const url =
|
||||||
|
window.datasetteEditToolsUrl ||
|
||||||
|
datasettePath("/-/static/edit-tools.js");
|
||||||
|
editToolsPromise = import(url).then(() => {
|
||||||
|
if (!window.__DATASETTE_EDIT_TOOLS__) {
|
||||||
|
throw new Error("edit-tools.js did not register its API");
|
||||||
|
}
|
||||||
|
if (window.__DATASETTE_EDIT_TOOLS__.install) {
|
||||||
|
window.__DATASETTE_EDIT_TOOLS__.install(datasetteManager);
|
||||||
|
}
|
||||||
|
return window.__DATASETTE_EDIT_TOOLS__;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return editToolsPromise;
|
||||||
|
},
|
||||||
|
|
||||||
|
insertDialog: async (database, table, row, message, options) => {
|
||||||
|
const editTools = await datasetteManager.loadEditTools();
|
||||||
|
return editTools.insertDialog(
|
||||||
|
datasetteManager,
|
||||||
|
database,
|
||||||
|
table,
|
||||||
|
row,
|
||||||
|
message,
|
||||||
|
options,
|
||||||
|
);
|
||||||
|
},
|
||||||
|
|
||||||
registerPlugin: (name, pluginMetadata) => {
|
registerPlugin: (name, pluginMetadata) => {
|
||||||
if (datasetteManager.plugins.has(name)) {
|
if (datasetteManager.plugins.has(name)) {
|
||||||
console.warn(`Warning -> plugin ${name} was redefined`);
|
console.warn(`Warning -> plugin ${name} was redefined`);
|
||||||
|
|
@ -230,9 +309,7 @@ const datasetteManager = {
|
||||||
};
|
};
|
||||||
|
|
||||||
const initializeDatasette = () => {
|
const initializeDatasette = () => {
|
||||||
// Hide the global behind __ prefix. Ideally they should be listening for the
|
window.datasette = datasetteManager;
|
||||||
// DATASETTE_EVENTS.INIT event to avoid the habit of reading from the window.
|
|
||||||
|
|
||||||
window.__DATASETTE__ = datasetteManager;
|
window.__DATASETTE__ = datasetteManager;
|
||||||
|
|
||||||
const initDatasetteEvent = new CustomEvent(DATASETTE_EVENTS.INIT, {
|
const initDatasetteEvent = new CustomEvent(DATASETTE_EVENTS.INIT, {
|
||||||
|
|
@ -240,6 +317,43 @@ const initializeDatasette = () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
document.dispatchEvent(initDatasetteEvent);
|
document.dispatchEvent(initDatasetteEvent);
|
||||||
|
|
||||||
|
document.addEventListener("click", function (event) {
|
||||||
|
const button = event.target.closest("[data-insert-dialog]");
|
||||||
|
if (!button) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
event.preventDefault();
|
||||||
|
let row;
|
||||||
|
try {
|
||||||
|
row = parseInsertDialogRow(button.dataset.row || "{}");
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const reloadOnInsert = button.hasAttribute("data-insert-dialog-reload");
|
||||||
|
datasetteManager
|
||||||
|
.insertDialog(
|
||||||
|
button.dataset.database,
|
||||||
|
button.dataset.table,
|
||||||
|
row,
|
||||||
|
button.dataset.message || "",
|
||||||
|
{ flashMessage: reloadOnInsert },
|
||||||
|
)
|
||||||
|
.then((result) => {
|
||||||
|
if (
|
||||||
|
reloadOnInsert &&
|
||||||
|
result &&
|
||||||
|
result.ok &&
|
||||||
|
result.status === "inserted"
|
||||||
|
) {
|
||||||
|
window.location.reload();
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch((error) => {
|
||||||
|
console.error(error);
|
||||||
|
});
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
File diff suppressed because it is too large
Load diff
56
datasette/static/json-format-highlight-1.0.1.js
Normal file
56
datasette/static/json-format-highlight-1.0.1.js
Normal file
|
|
@ -0,0 +1,56 @@
|
||||||
|
/*
|
||||||
|
https://github.com/luyilin/json-format-highlight
|
||||||
|
From https://unpkg.com/json-format-highlight@1.0.1/dist/json-format-highlight.js
|
||||||
|
MIT Licensed
|
||||||
|
*/
|
||||||
|
(function (global, factory) {
|
||||||
|
typeof exports === "object" && typeof module !== "undefined"
|
||||||
|
? (module.exports = factory())
|
||||||
|
: typeof define === "function" && define.amd
|
||||||
|
? define(factory)
|
||||||
|
: (global.jsonFormatHighlight = factory());
|
||||||
|
})(this, function () {
|
||||||
|
"use strict";
|
||||||
|
|
||||||
|
var defaultColors = {
|
||||||
|
keyColor: "dimgray",
|
||||||
|
numberColor: "lightskyblue",
|
||||||
|
stringColor: "lightcoral",
|
||||||
|
trueColor: "lightseagreen",
|
||||||
|
falseColor: "#f66578",
|
||||||
|
nullColor: "cornflowerblue",
|
||||||
|
};
|
||||||
|
|
||||||
|
function index(json, colorOptions) {
|
||||||
|
if (colorOptions === void 0) colorOptions = {};
|
||||||
|
|
||||||
|
if (!json) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (typeof json !== "string") {
|
||||||
|
json = JSON.stringify(json, null, 2);
|
||||||
|
}
|
||||||
|
var colors = Object.assign({}, defaultColors, colorOptions);
|
||||||
|
json = json.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
|
||||||
|
return json.replace(
|
||||||
|
/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+]?\d+)?)/g,
|
||||||
|
function (match) {
|
||||||
|
var color = colors.numberColor;
|
||||||
|
if (/^"/.test(match)) {
|
||||||
|
color = /:$/.test(match) ? colors.keyColor : colors.stringColor;
|
||||||
|
} else {
|
||||||
|
color = /true/.test(match)
|
||||||
|
? colors.trueColor
|
||||||
|
: /false/.test(match)
|
||||||
|
? colors.falseColor
|
||||||
|
: /null/.test(match)
|
||||||
|
? colors.nullColor
|
||||||
|
: color;
|
||||||
|
}
|
||||||
|
return '<span style="color: ' + color + '">' + match + "</span>";
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return index;
|
||||||
|
});
|
||||||
|
|
@ -633,151 +633,32 @@ const initDatasetteTable = function (manager) {
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
function filterRowSelector(manager) {
|
/* Add x buttons to the filter rows */
|
||||||
return manager.selectors.filterRows || manager.selectors.filterRow;
|
function addButtonsToFilterRows(manager) {
|
||||||
}
|
var x = "✖";
|
||||||
|
var rows = Array.from(
|
||||||
function filterRowsWithControls(manager) {
|
document.querySelectorAll(manager.selectors.filterRow),
|
||||||
return Array.from(
|
|
||||||
document.querySelectorAll(filterRowSelector(manager)),
|
|
||||||
).filter((el) => el.querySelector(".filter-op"));
|
).filter((el) => el.querySelector(".filter-op"));
|
||||||
}
|
rows.forEach((row) => {
|
||||||
|
var a = document.createElement("a");
|
||||||
function filterRowNumberFromName(name) {
|
a.setAttribute("href", "#");
|
||||||
var match = name && name.match(/^_filter_column_(\d+)$/);
|
a.setAttribute("aria-label", "Remove this filter");
|
||||||
return match ? parseInt(match[1], 10) : 0;
|
a.style.textDecoration = "none";
|
||||||
}
|
a.innerText = x;
|
||||||
|
a.addEventListener("click", (ev) => {
|
||||||
function nextFilterRowNumber(manager) {
|
ev.preventDefault();
|
||||||
return filterRowsWithControls(manager).reduce((max, row) => {
|
let row = ev.target.closest("div");
|
||||||
var column = row.querySelector("select");
|
|
||||||
return Math.max(max, filterRowNumberFromName(column && column.name));
|
|
||||||
}, 0) + 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
function setFilterRowNumber(row, number) {
|
|
||||||
row.querySelector("select").name = `_filter_column_${number}`;
|
|
||||||
row.querySelector(".filter-op select").name = `_filter_op_${number}`;
|
|
||||||
row.querySelector("input.filter-value").name = `_filter_value_${number}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
function resetFilterRow(row) {
|
|
||||||
row.querySelector("select").value = "";
|
row.querySelector("select").value = "";
|
||||||
row.querySelector(".filter-op select").value = "exact";
|
row.querySelector(".filter-op select").value = "exact";
|
||||||
row.querySelector("input.filter-value").value = "";
|
row.querySelector("input.filter-value").value = "";
|
||||||
}
|
ev.target.closest("a").style.display = "none";
|
||||||
|
});
|
||||||
function updateFilterRowButtons(manager) {
|
row.appendChild(a);
|
||||||
var rows = filterRowsWithControls(manager);
|
|
||||||
rows.forEach((row, index) => {
|
|
||||||
var removeButton = row.querySelector(".filter-row-remove");
|
|
||||||
var addButton = row.querySelector(".filter-row-add");
|
|
||||||
var column = row.querySelector("select");
|
var column = row.querySelector("select");
|
||||||
if (removeButton) {
|
if (!column.value) {
|
||||||
removeButton.hidden = index === 0;
|
a.style.display = "none";
|
||||||
}
|
|
||||||
if (addButton) {
|
|
||||||
addButton.hidden = index !== rows.length - 1 || !column.value;
|
|
||||||
}
|
|
||||||
var visibleButtonCount = [removeButton, addButton].filter(function (button) {
|
|
||||||
return button && !button.hidden;
|
|
||||||
}).length;
|
|
||||||
row.classList.toggle(
|
|
||||||
"filter-controls-row-has-buttons",
|
|
||||||
visibleButtonCount > 0,
|
|
||||||
);
|
|
||||||
row.classList.toggle(
|
|
||||||
"filter-controls-row-one-button",
|
|
||||||
visibleButtonCount === 1,
|
|
||||||
);
|
|
||||||
row.classList.toggle(
|
|
||||||
"filter-controls-row-two-buttons",
|
|
||||||
visibleButtonCount === 2,
|
|
||||||
);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function cloneFilterRow(row) {
|
|
||||||
var clone = row.cloneNode(true);
|
|
||||||
clone.querySelector("select").name = "_filter_column";
|
|
||||||
clone.querySelector(".filter-op select").name = "_filter_op";
|
|
||||||
clone.querySelector("input.filter-value").name = "_filter_value";
|
|
||||||
resetFilterRow(clone);
|
|
||||||
clone.querySelectorAll(".filter-row-icon").forEach((button) => button.remove());
|
|
||||||
return clone;
|
|
||||||
}
|
|
||||||
|
|
||||||
var FILTER_REMOVE_ICON_SVG = `<svg class="filter-row-remove-icon" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.1" stroke-linecap="round" stroke-linejoin="round">
|
|
||||||
<path d="M3 6h18"></path>
|
|
||||||
<path d="M8 6V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"></path>
|
|
||||||
<path d="M19 6l-1 14a2 2 0 0 1-2 2H8a2 2 0 0 1-2-2L5 6"></path>
|
|
||||||
<path d="M10 11v6"></path>
|
|
||||||
<path d="M14 11v6"></path>
|
|
||||||
</svg>`;
|
|
||||||
|
|
||||||
var FILTER_ADD_ICON_SVG = `<svg class="filter-row-add-icon" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="3" stroke-linecap="round" stroke-linejoin="round">
|
|
||||||
<path d="M5 12h14"></path>
|
|
||||||
<path d="M12 5v14"></path>
|
|
||||||
</svg>`;
|
|
||||||
|
|
||||||
function addFilterRowButtons(row, manager) {
|
|
||||||
var removeButton = document.createElement("button");
|
|
||||||
removeButton.type = "button";
|
|
||||||
removeButton.className = "filter-row-icon filter-row-remove";
|
|
||||||
removeButton.setAttribute("aria-label", "Remove this filter");
|
|
||||||
removeButton.title = "Remove this filter";
|
|
||||||
removeButton.tabIndex = 0;
|
|
||||||
removeButton.innerHTML = FILTER_REMOVE_ICON_SVG;
|
|
||||||
removeButton.addEventListener("click", (ev) => {
|
|
||||||
var row = ev.currentTarget.closest(filterRowSelector(manager));
|
|
||||||
var rows = filterRowsWithControls(manager);
|
|
||||||
var rowIndex = rows.indexOf(row);
|
|
||||||
var focusRow = rows[rowIndex + 1] || rows[rowIndex - 1] || null;
|
|
||||||
row.remove();
|
|
||||||
updateFilterRowButtons(manager);
|
|
||||||
if (focusRow) {
|
|
||||||
var focusTarget =
|
|
||||||
focusRow.querySelector(".filter-row-add:not([hidden])") ||
|
|
||||||
focusRow.querySelector("select");
|
|
||||||
if (focusTarget) {
|
|
||||||
focusTarget.focus();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
row.appendChild(removeButton);
|
|
||||||
|
|
||||||
var addButton = document.createElement("button");
|
|
||||||
addButton.type = "button";
|
|
||||||
addButton.className = "filter-row-icon filter-row-add";
|
|
||||||
addButton.setAttribute("aria-label", "Add another filter");
|
|
||||||
addButton.title = "Add another filter";
|
|
||||||
addButton.tabIndex = 0;
|
|
||||||
addButton.innerHTML = FILTER_ADD_ICON_SVG;
|
|
||||||
addButton.addEventListener("click", (ev) => {
|
|
||||||
var row = ev.currentTarget.closest(filterRowSelector(manager));
|
|
||||||
if (row.querySelector("select").name === "_filter_column") {
|
|
||||||
setFilterRowNumber(row, nextFilterRowNumber(manager));
|
|
||||||
}
|
|
||||||
var clone = cloneFilterRow(row);
|
|
||||||
addFilterRowButtons(clone, manager);
|
|
||||||
row.parentNode.insertBefore(clone, row.nextSibling);
|
|
||||||
updateFilterRowButtons(manager);
|
|
||||||
clone.querySelector("select").focus();
|
|
||||||
});
|
|
||||||
row.appendChild(addButton);
|
|
||||||
|
|
||||||
row.querySelector("select").addEventListener("change", () => {
|
|
||||||
updateFilterRowButtons(manager);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Add buttons to the filter rows */
|
|
||||||
function addButtonsToFilterRows(manager) {
|
|
||||||
var rows = filterRowsWithControls(manager);
|
|
||||||
rows.forEach((row) => {
|
|
||||||
addFilterRowButtons(row, manager);
|
|
||||||
});
|
|
||||||
updateFilterRowButtons(manager);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Set up datalist autocomplete for filter values */
|
/* Set up datalist autocomplete for filter values */
|
||||||
|
|
@ -806,11 +687,11 @@ function initAutocompleteForFilterValues(manager) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
createDataLists();
|
createDataLists();
|
||||||
// When any filter column select changes, update the datalist
|
// When any select with name=_filter_column changes, update the datalist
|
||||||
document.body.addEventListener("change", function (event) {
|
document.body.addEventListener("change", function (event) {
|
||||||
if (event.target.name && event.target.name.startsWith("_filter_column")) {
|
if (event.target.name === "_filter_column") {
|
||||||
event.target
|
event.target
|
||||||
.closest(filterRowSelector(manager))
|
.closest(manager.selectors.filterRow)
|
||||||
.querySelector(".filter-value")
|
.querySelector(".filter-value")
|
||||||
.setAttribute("list", "datalist-" + event.target.value);
|
.setAttribute("list", "datalist-" + event.target.value);
|
||||||
}
|
}
|
||||||
|
|
@ -860,45 +741,10 @@ function openColumnChooser() {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function initCountAll() {
|
|
||||||
var button = document.querySelector(".count-all");
|
|
||||||
if (!button) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
button.addEventListener("click", async function () {
|
|
||||||
var count = document.querySelector(".table-count");
|
|
||||||
var error = document.querySelector(".count-error");
|
|
||||||
button.disabled = true;
|
|
||||||
button.textContent = "Counting…";
|
|
||||||
error.textContent = "";
|
|
||||||
try {
|
|
||||||
var response = await fetch(button.dataset.countUrl + location.search, {
|
|
||||||
method: "POST",
|
|
||||||
headers: {
|
|
||||||
Accept: "application/json",
|
|
||||||
},
|
|
||||||
});
|
|
||||||
var data = await response.json();
|
|
||||||
if (!response.ok || !data.ok) {
|
|
||||||
throw new Error((data.errors || ["Count failed"]).join(" "));
|
|
||||||
}
|
|
||||||
count.textContent =
|
|
||||||
data.count.toLocaleString("en-US") +
|
|
||||||
(data.count === 1 ? " row" : " rows");
|
|
||||||
button.remove();
|
|
||||||
} catch (ex) {
|
|
||||||
error.textContent = ex.message || "Count failed";
|
|
||||||
button.disabled = false;
|
|
||||||
button.textContent = "count all";
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ensures Table UI is initialized only after the Manager is ready.
|
// Ensures Table UI is initialized only after the Manager is ready.
|
||||||
document.addEventListener("datasette_init", function (evt) {
|
document.addEventListener("datasette_init", function (evt) {
|
||||||
const { detail: manager } = evt;
|
const { detail: manager } = evt;
|
||||||
|
|
||||||
initCountAll();
|
|
||||||
initializeColumnActions(manager);
|
initializeColumnActions(manager);
|
||||||
|
|
||||||
// Main table
|
// Main table
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,8 @@
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import json
|
|
||||||
from collections.abc import Iterable
|
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from typing import Any
|
import json
|
||||||
|
from typing import Any, Iterable
|
||||||
|
|
||||||
from .utils import tilde_encode, urlsafe_components
|
from .utils import tilde_encode, urlsafe_components
|
||||||
|
|
||||||
|
|
@ -63,6 +62,7 @@ def stored_query_to_dict(query: StoredQuery) -> dict[str, Any]:
|
||||||
"description_html": query.description_html,
|
"description_html": query.description_html,
|
||||||
"hide_sql": query.hide_sql,
|
"hide_sql": query.hide_sql,
|
||||||
"fragment": query.fragment,
|
"fragment": query.fragment,
|
||||||
|
"params": list(query.parameters),
|
||||||
"parameters": list(query.parameters),
|
"parameters": list(query.parameters),
|
||||||
"is_write": query.is_write,
|
"is_write": query.is_write,
|
||||||
"is_private": query.is_private,
|
"is_private": query.is_private,
|
||||||
|
|
@ -84,6 +84,7 @@ def stored_query_page_to_dict(page: StoredQueryPage) -> dict[str, Any]:
|
||||||
return {
|
return {
|
||||||
"queries": [stored_query_to_dict(query) for query in page.queries],
|
"queries": [stored_query_to_dict(query) for query in page.queries],
|
||||||
"next": page.next,
|
"next": page.next,
|
||||||
|
"has_more": page.has_more,
|
||||||
"limit": page.limit,
|
"limit": page.limit,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -387,7 +388,7 @@ async def count_queries(
|
||||||
OR q.sql LIKE :query_search
|
OR q.sql LIKE :query_search
|
||||||
)
|
)
|
||||||
""")
|
""")
|
||||||
params["query_search"] = f"%{q}%"
|
params["query_search"] = "%{}%".format(q)
|
||||||
if is_write is not None:
|
if is_write is not None:
|
||||||
where_clauses.append("q.is_write = :query_is_write")
|
where_clauses.append("q.is_write = :query_is_write")
|
||||||
params["query_is_write"] = int(bool(is_write))
|
params["query_is_write"] = int(bool(is_write))
|
||||||
|
|
@ -463,7 +464,7 @@ async def list_queries(
|
||||||
except ValueError:
|
except ValueError:
|
||||||
components = []
|
components = []
|
||||||
if database is None and len(components) == 3:
|
if database is None and len(components) == 3:
|
||||||
where_clauses.append(f"""
|
where_clauses.append("""
|
||||||
(
|
(
|
||||||
q.database_name > :cursor_database
|
q.database_name > :cursor_database
|
||||||
OR (
|
OR (
|
||||||
|
|
@ -477,12 +478,12 @@ async def list_queries(
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
""")
|
""".format(sort_key_sql=sort_key_sql))
|
||||||
params["cursor_database"] = components[0]
|
params["cursor_database"] = components[0]
|
||||||
params["cursor_sort_key"] = components[1]
|
params["cursor_sort_key"] = components[1]
|
||||||
params["cursor_name"] = components[2]
|
params["cursor_name"] = components[2]
|
||||||
elif database is not None and len(components) == 2:
|
elif database is not None and len(components) == 2:
|
||||||
where_clauses.append(f"""
|
where_clauses.append("""
|
||||||
(
|
(
|
||||||
{sort_key_sql} > :cursor_sort_key
|
{sort_key_sql} > :cursor_sort_key
|
||||||
OR (
|
OR (
|
||||||
|
|
@ -490,7 +491,7 @@ async def list_queries(
|
||||||
AND q.name > :cursor_name
|
AND q.name > :cursor_name
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
""")
|
""".format(sort_key_sql=sort_key_sql))
|
||||||
params["cursor_sort_key"] = components[0]
|
params["cursor_sort_key"] = components[0]
|
||||||
params["cursor_name"] = components[1]
|
params["cursor_name"] = components[1]
|
||||||
|
|
||||||
|
|
@ -503,7 +504,7 @@ async def list_queries(
|
||||||
OR q.sql LIKE :query_search
|
OR q.sql LIKE :query_search
|
||||||
)
|
)
|
||||||
""")
|
""")
|
||||||
params["query_search"] = f"%{q}%"
|
params["query_search"] = "%{}%".format(q)
|
||||||
if is_write is not None:
|
if is_write is not None:
|
||||||
where_clauses.append("q.is_write = :query_is_write")
|
where_clauses.append("q.is_write = :query_is_write")
|
||||||
params["query_is_write"] = int(bool(is_write))
|
params["query_is_write"] = int(bool(is_write))
|
||||||
|
|
|
||||||
|
|
@ -1,40 +0,0 @@
|
||||||
"""
|
|
||||||
Index of the documented template contexts for Datasette's core HTML pages.
|
|
||||||
|
|
||||||
This module deliberately contains no documentation strings of its own -
|
|
||||||
the documentation lives next to the code it describes:
|
|
||||||
|
|
||||||
- Every page renders a Context dataclass defined in its view module
|
|
||||||
(DatabaseContext, QueryContext in views/database.py, TableContext in
|
|
||||||
views/table.py, RowContext in views/row.py). Fields added by view code
|
|
||||||
carry ``help`` metadata; fields declared with from_extra() take their
|
|
||||||
documentation from the description on the matching Extra class in
|
|
||||||
views/table_extras.py.
|
|
||||||
- The keys render_template() adds to every page are documented in
|
|
||||||
TEMPLATE_BASE_CONTEXT in datasette/app.py, next to the code that adds
|
|
||||||
them.
|
|
||||||
|
|
||||||
The contract tests in tests/test_template_context.py assert that the real
|
|
||||||
rendered context for each page exactly matches what is documented, and
|
|
||||||
docs/template_context_doc.py generates docs/template_context.rst from the
|
|
||||||
same classes.
|
|
||||||
"""
|
|
||||||
|
|
||||||
from datasette.app import TEMPLATE_BASE_CONTEXT
|
|
||||||
from datasette.views.database import DatabaseContext, QueryContext
|
|
||||||
from datasette.views.row import RowContext
|
|
||||||
from datasette.views.table import TableContext
|
|
||||||
|
|
||||||
PAGES = {
|
|
||||||
"database": DatabaseContext,
|
|
||||||
"query": QueryContext,
|
|
||||||
"table": TableContext,
|
|
||||||
"row": RowContext,
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
def documented_context_keys(page_name):
|
|
||||||
"Set of every documented key for the named page, including base context keys"
|
|
||||||
return set(TEMPLATE_BASE_CONTEXT) | {
|
|
||||||
f.name for f in PAGES[page_name].documented_fields()
|
|
||||||
}
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
<script src="{{ static('sql-formatter-2.3.3.min.js') }}" defer></script>
|
<script src="{{ base_url }}-/static/sql-formatter-2.3.3.min.js" defer></script>
|
||||||
<script src="{{ static('cm-editor-6.0.1.bundle.js') }}"></script>
|
<script src="{{ base_url }}-/static/cm-editor-6.0.1.bundle.js"></script>
|
||||||
<style>
|
<style>
|
||||||
.cm-editor {
|
.cm-editor {
|
||||||
resize: both;
|
resize: both;
|
||||||
|
|
|
||||||
|
|
@ -6,20 +6,8 @@
|
||||||
padding: 1.5em;
|
padding: 1.5em;
|
||||||
margin-bottom: 2em;
|
margin-bottom: 2em;
|
||||||
}
|
}
|
||||||
.permission-form form {
|
|
||||||
max-width: 60rem;
|
|
||||||
}
|
|
||||||
.permission-form-grid {
|
|
||||||
display: grid;
|
|
||||||
gap: 1.5rem;
|
|
||||||
grid-template-columns: repeat(2, minmax(0, 1fr));
|
|
||||||
}
|
|
||||||
.permission-form-result {
|
|
||||||
margin-top: 1rem;
|
|
||||||
max-width: 60rem;
|
|
||||||
}
|
|
||||||
.form-section {
|
.form-section {
|
||||||
margin-bottom: 1.25em;
|
margin-bottom: 1em;
|
||||||
}
|
}
|
||||||
.form-section label {
|
.form-section label {
|
||||||
display: block;
|
display: block;
|
||||||
|
|
@ -27,51 +15,22 @@
|
||||||
font-weight: bold;
|
font-weight: bold;
|
||||||
}
|
}
|
||||||
.form-section input[type="text"],
|
.form-section input[type="text"],
|
||||||
.form-section input[type="number"],
|
|
||||||
.form-section select,
|
|
||||||
.permission-textarea {
|
|
||||||
background-color: #fff;
|
|
||||||
border: 1px solid #aaa;
|
|
||||||
border-radius: 4px;
|
|
||||||
box-sizing: border-box;
|
|
||||||
box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.08);
|
|
||||||
color: #222;
|
|
||||||
font-family: inherit;
|
|
||||||
font-size: 1rem;
|
|
||||||
line-height: 1.4;
|
|
||||||
max-width: none;
|
|
||||||
width: 100%;
|
|
||||||
}
|
|
||||||
.form-section input[type="text"] {
|
|
||||||
height: 3rem;
|
|
||||||
padding: 0.6rem 0.75rem;
|
|
||||||
}
|
|
||||||
.form-section input[type="number"] {
|
|
||||||
height: 3rem;
|
|
||||||
max-width: 7rem;
|
|
||||||
padding: 0.6rem 0.75rem;
|
|
||||||
}
|
|
||||||
.form-section select {
|
.form-section select {
|
||||||
height: 3rem;
|
width: 100%;
|
||||||
padding: 0.6rem 0.75rem;
|
max-width: 500px;
|
||||||
}
|
padding: 0.5em;
|
||||||
.permission-textarea {
|
box-sizing: border-box;
|
||||||
font-family: monospace;
|
border: 1px solid #ccc;
|
||||||
min-height: 12rem;
|
border-radius: 3px;
|
||||||
padding: 0.75rem;
|
|
||||||
resize: vertical;
|
|
||||||
}
|
}
|
||||||
.form-section input[type="text"]:focus,
|
.form-section input[type="text"]:focus,
|
||||||
.form-section input[type="number"]:focus,
|
.form-section select:focus {
|
||||||
.form-section select:focus,
|
outline: 2px solid #0066cc;
|
||||||
.permission-textarea:focus {
|
|
||||||
border-color: #0066cc;
|
border-color: #0066cc;
|
||||||
box-shadow: 0 0 0 3px rgba(0, 102, 204, 0.18);
|
|
||||||
outline: none;
|
|
||||||
}
|
}
|
||||||
.form-section small {
|
.form-section small {
|
||||||
display: block;
|
display: block;
|
||||||
margin-top: 0.45em;
|
margin-top: 0.3em;
|
||||||
color: #666;
|
color: #666;
|
||||||
}
|
}
|
||||||
.form-actions {
|
.form-actions {
|
||||||
|
|
@ -183,9 +142,4 @@
|
||||||
text-align: center;
|
text-align: center;
|
||||||
color: #666;
|
color: #666;
|
||||||
}
|
}
|
||||||
@media only screen and (max-width: 576px) {
|
|
||||||
.permission-form-grid {
|
|
||||||
grid-template-columns: minmax(0, 1fr);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
</style>
|
</style>
|
||||||
|
|
|
||||||
|
|
@ -44,10 +44,10 @@
|
||||||
</style>
|
</style>
|
||||||
|
|
||||||
<nav class="permissions-debug-tabs">
|
<nav class="permissions-debug-tabs">
|
||||||
<a href="{{ urls.path('-/check') }}{{ query_string }}" {% if current_tab == "check" %}class="active"{% endif %}>Explain</a>
|
<a href="{{ urls.path('-/permissions') }}" {% if current_tab == "permissions" %}class="active"{% endif %}>Playground</a>
|
||||||
<a href="{{ urls.path('-/allowed') }}{{ query_string }}" {% if current_tab == "allowed" %}class="active"{% endif %}>Access map</a>
|
<a href="{{ urls.path('-/check') }}{{ query_string }}" {% if current_tab == "check" %}class="active"{% endif %}>Check</a>
|
||||||
<a href="{{ urls.path('-/rules') }}{{ query_string }}" {% if current_tab == "rules" %}class="active"{% endif %}>Rule explorer</a>
|
<a href="{{ urls.path('-/allowed') }}{{ query_string }}" {% if current_tab == "allowed" %}class="active"{% endif %}>Allowed</a>
|
||||||
<a href="{{ urls.path('-/permissions') }}" {% if current_tab == "permissions" %}class="active"{% endif %}>Activity</a>
|
<a href="{{ urls.path('-/rules') }}{{ query_string }}" {% if current_tab == "rules" %}class="active"{% endif %}>Rules</a>
|
||||||
<a href="{{ urls.path('-/actions') }}" {% if current_tab == "actions" %}class="active"{% endif %}>Actions</a>
|
<a href="{{ urls.path('-/actions') }}" {% if current_tab == "actions" %}class="active"{% endif %}>Actions</a>
|
||||||
<a href="{{ urls.path('-/allow-debug') }}" {% if current_tab == "allow_debug" %}class="active"{% endif %}>Allow debug</a>
|
<a href="{{ urls.path('-/allow-debug') }}" {% if current_tab == "allow_debug" %}class="active"{% endif %}>Allow debug</a>
|
||||||
</nav>
|
</nav>
|
||||||
|
|
|
||||||
|
|
@ -3,11 +3,29 @@
|
||||||
{% block title %}Debug allow rules{% endblock %}
|
{% block title %}Debug allow rules{% endblock %}
|
||||||
|
|
||||||
{% block extra_head %}
|
{% block extra_head %}
|
||||||
{% include "_permission_ui_styles.html" %}
|
|
||||||
<style>
|
<style>
|
||||||
|
textarea {
|
||||||
|
height: 10em;
|
||||||
|
width: 95%;
|
||||||
|
box-sizing: border-box;
|
||||||
|
padding: 0.5em;
|
||||||
|
border: 2px dotted black;
|
||||||
|
}
|
||||||
|
.two-col {
|
||||||
|
display: inline-block;
|
||||||
|
width: 48%;
|
||||||
|
}
|
||||||
|
.two-col label {
|
||||||
|
width: 48%;
|
||||||
|
}
|
||||||
p.message-warning {
|
p.message-warning {
|
||||||
white-space: pre-wrap;
|
white-space: pre-wrap;
|
||||||
}
|
}
|
||||||
|
@media only screen and (max-width: 576px) {
|
||||||
|
.two-col {
|
||||||
|
width: 100%;
|
||||||
|
}
|
||||||
|
}
|
||||||
</style>
|
</style>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
|
|
@ -20,28 +38,24 @@ p.message-warning {
|
||||||
|
|
||||||
<p>Use this tool to try out different actor and allow combinations. See <a href="https://docs.datasette.io/en/stable/authentication.html#defining-permissions-with-allow-blocks">Defining permissions with "allow" blocks</a> for documentation.</p>
|
<p>Use this tool to try out different actor and allow combinations. See <a href="https://docs.datasette.io/en/stable/authentication.html#defining-permissions-with-allow-blocks">Defining permissions with "allow" blocks</a> for documentation.</p>
|
||||||
|
|
||||||
<div class="permission-form">
|
<form class="core" action="{{ urls.path('-/allow-debug') }}" method="get" style="margin-bottom: 1em">
|
||||||
<form class="core" action="{{ urls.path('-/allow-debug') }}" method="get">
|
<div class="two-col">
|
||||||
<div class="permission-form-grid">
|
<p><label>Allow block</label></p>
|
||||||
<div class="form-section">
|
<textarea name="allow">{{ allow_input }}</textarea>
|
||||||
<label for="allow-block">Allow block</label>
|
|
||||||
<textarea class="permission-textarea" id="allow-block" name="allow">{{ allow_input }}</textarea>
|
|
||||||
</div>
|
</div>
|
||||||
<div class="form-section">
|
<div class="two-col">
|
||||||
<label for="allow-actor">Actor</label>
|
<p><label>Actor</label></p>
|
||||||
<textarea class="permission-textarea" id="allow-actor" name="actor">{{ actor_input }}</textarea>
|
<textarea name="actor">{{ actor_input }}</textarea>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
<div style="margin-top: 1em;">
|
||||||
<div class="form-actions">
|
<input type="submit" value="Apply allow block to actor">
|
||||||
<button type="submit" class="submit-btn">Apply allow block to actor</button>
|
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
{% if error %}<p class="message-warning permission-form-result">{{ error }}</p>{% endif %}
|
{% if error %}<p class="message-warning">{{ error }}</p>{% endif %}
|
||||||
|
|
||||||
{% if result == "True" %}<p class="message-info permission-form-result">Result: allow</p>{% endif %}
|
{% if result == "True" %}<p class="message-info">Result: allow</p>{% endif %}
|
||||||
|
|
||||||
{% if result == "False" %}<p class="message-error permission-form-result">Result: deny</p>{% endif %}
|
{% if result == "False" %}<p class="message-error">Result: deny</p>{% endif %}
|
||||||
</div>
|
|
||||||
|
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@
|
||||||
{% block title %}API Explorer{% endblock %}
|
{% block title %}API Explorer{% endblock %}
|
||||||
|
|
||||||
{% block extra_head %}
|
{% block extra_head %}
|
||||||
|
<script src="{{ base_url }}-/static/json-format-highlight-1.0.1.js"></script>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
{% block content %}
|
{% block content %}
|
||||||
|
|
@ -125,7 +126,7 @@ getForm.addEventListener("submit", (ev) => {
|
||||||
document.getElementById('response-status').textContent = response.status;
|
document.getElementById('response-status').textContent = response.status;
|
||||||
return response.json();
|
return response.json();
|
||||||
}).then((data) => {
|
}).then((data) => {
|
||||||
output.querySelector('pre').textContent = JSON.stringify(data, null, 2);
|
output.querySelector('pre').innerHTML = jsonFormatHighlight(data);
|
||||||
errorList.style.display = 'none';
|
errorList.style.display = 'none';
|
||||||
}).catch((error) => {
|
}).catch((error) => {
|
||||||
alert(error);
|
alert(error);
|
||||||
|
|
@ -173,7 +174,7 @@ postForm.addEventListener("submit", (ev) => {
|
||||||
} else {
|
} else {
|
||||||
errorList.style.display = 'none';
|
errorList.style.display = 'none';
|
||||||
}
|
}
|
||||||
output.querySelector('pre').textContent = JSON.stringify(data, null, 2);
|
output.querySelector('pre').innerHTML = jsonFormatHighlight(data);
|
||||||
output.style.display = 'block';
|
output.style.display = 'block';
|
||||||
}).catch(err => {
|
}).catch(err => {
|
||||||
alert("Error: " + err);
|
alert("Error: " + err);
|
||||||
|
|
|
||||||
|
|
@ -2,13 +2,18 @@
|
||||||
<html lang="en">
|
<html lang="en">
|
||||||
<head>
|
<head>
|
||||||
<title>{% block title %}{% endblock %}</title>
|
<title>{% block title %}{% endblock %}</title>
|
||||||
<link rel="stylesheet" href="{{ static('app.css') }}">
|
<link rel="stylesheet" href="{{ urls.static('app.css') }}?{{ app_css_hash }}">
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
|
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
|
||||||
{% for url in extra_css_urls %}
|
{% for url in extra_css_urls %}
|
||||||
<link rel="stylesheet" href="{{ url.url }}"{% if url.get("sri") %} integrity="{{ url.sri }}" crossorigin="anonymous"{% endif %}>
|
<link rel="stylesheet" href="{{ url.url }}"{% if url.get("sri") %} integrity="{{ url.sri }}" crossorigin="anonymous"{% endif %}>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
<script>window.datasetteVersion = '{{ datasette_version }}';</script>
|
<script>
|
||||||
<script src="{{ static('datasette-manager.js') }}" defer></script>
|
window.datasetteVersion = {{ datasette_version|tojson }};
|
||||||
|
window.datasetteBaseUrl = {{ base_url|tojson }};
|
||||||
|
window.datasetteEditToolsUrl = {{ (urls.static('edit-tools.js') ~ '?hash=' ~ edit_tools_js_hash)|tojson }};
|
||||||
|
window.datasetteAutocompleteUrl = {{ urls.static('autocomplete.js')|tojson }};
|
||||||
|
</script>
|
||||||
|
<script src="{{ urls.static('datasette-manager.js') }}" defer></script>
|
||||||
{% for url in extra_js_urls %}
|
{% for url in extra_js_urls %}
|
||||||
<script {% if url.module %}type="module" {% endif %}src="{{ url.url }}"{% if url.get("sri") %} integrity="{{ url.sri }}" crossorigin="anonymous"{% endif %}></script>
|
<script {% if url.module %}type="module" {% endif %}src="{{ url.url }}"{% if url.get("sri") %} integrity="{{ url.sri }}" crossorigin="anonymous"{% endif %}></script>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
|
@ -70,7 +75,7 @@
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
|
||||||
{% if select_templates %}<!-- Templates considered: {{ select_templates|join(", ") }} -->{% endif %}
|
{% if select_templates %}<!-- Templates considered: {{ select_templates|join(", ") }} -->{% endif %}
|
||||||
<script src="{{ static('navigation-search.js') }}" defer></script>
|
<script src="{{ urls.static('navigation-search.js') }}" defer></script>
|
||||||
<navigation-search url="{{ urls.path("/-/jump") }}"></navigation-search>
|
<navigation-search url="{{ urls.path("/-/jump") }}"></navigation-search>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
|
||||||
|
|
@ -6,10 +6,6 @@
|
||||||
{{- super() -}}
|
{{- super() -}}
|
||||||
{% include "_codemirror.html" %}
|
{% include "_codemirror.html" %}
|
||||||
{% include "_sql_parameter_styles.html" %}
|
{% include "_sql_parameter_styles.html" %}
|
||||||
{% if database_page_data.createTable %}
|
|
||||||
<script>window._datasetteDatabaseData = {{ database_page_data|tojson }};</script>
|
|
||||||
<script src="{{ static('edit-tools.js') }}" defer></script>
|
|
||||||
{% endif %}
|
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
{% block body_class %}db db-{{ database|to_css_class }}{% endblock %}
|
{% block body_class %}db db-{{ database|to_css_class }}{% endblock %}
|
||||||
|
|
@ -76,7 +72,7 @@
|
||||||
<div class="db-table">
|
<div class="db-table">
|
||||||
<h3><a href="{{ urls.table(database, table.name) }}">{{ table.name }}</a>{% if table.private %} 🔒{% endif %}{% if table.hidden %}<em> (hidden)</em>{% endif %}</h3>
|
<h3><a href="{{ urls.table(database, table.name) }}">{{ table.name }}</a>{% if table.private %} 🔒{% endif %}{% if table.hidden %}<em> (hidden)</em>{% endif %}</h3>
|
||||||
<p><em>{% for column in table.columns %}{{ column }}{% if not loop.last %}, {% endif %}{% endfor %}</em></p>
|
<p><em>{% for column in table.columns %}{{ column }}{% if not loop.last %}, {% endif %}{% endfor %}</em></p>
|
||||||
<p>{% if table.count is none %}Many rows{% elif table.count_truncated %}>{{ "{:,}".format(table.count - 1) }} rows{% else %}{{ "{:,}".format(table.count) }} row{% if table.count == 1 %}{% else %}s{% endif %}{% endif %}</p>
|
<p>{% if table.count is none %}Many rows{% elif table.count == count_limit + 1 %}>{{ "{:,}".format(count_limit) }} rows{% else %}{{ "{:,}".format(table.count) }} row{% if table.count == 1 %}{% else %}s{% endif %}{% endif %}</p>
|
||||||
</div>
|
</div>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@
|
||||||
{% include "_permissions_debug_tabs.html" %}
|
{% include "_permissions_debug_tabs.html" %}
|
||||||
|
|
||||||
<p style="margin-bottom: 2em;">
|
<p style="margin-bottom: 2em;">
|
||||||
This Datasette instance has registered {{ data.actions|length }} action{{ data.actions|length != 1 and "s" or "" }}.
|
This Datasette instance has registered {{ data|length }} action{{ data|length != 1 and "s" or "" }}.
|
||||||
Actions are used by the permission system to control access to different features.
|
Actions are used by the permission system to control access to different features.
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
|
|
@ -26,7 +26,7 @@
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
{% for action in data.actions %}
|
{% for action in data %}
|
||||||
<tr>
|
<tr>
|
||||||
<td><strong>{{ action.name }}</strong></td>
|
<td><strong>{{ action.name }}</strong></td>
|
||||||
<td>{% if action.abbr %}<code>{{ action.abbr }}</code>{% endif %}</td>
|
<td>{% if action.abbr %}<code>{{ action.abbr }}</code>{% endif %}</td>
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@
|
||||||
{% block title %}Allowed Resources{% endblock %}
|
{% block title %}Allowed Resources{% endblock %}
|
||||||
|
|
||||||
{% block extra_head %}
|
{% block extra_head %}
|
||||||
|
<script src="{{ base_url }}-/static/json-format-highlight-1.0.1.js"></script>
|
||||||
{% include "_permission_ui_styles.html" %}
|
{% include "_permission_ui_styles.html" %}
|
||||||
{% include "_debug_common_functions.html" %}
|
{% include "_debug_common_functions.html" %}
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
@ -48,7 +49,7 @@
|
||||||
|
|
||||||
<div class="form-section">
|
<div class="form-section">
|
||||||
<label for="page_size">Page size:</label>
|
<label for="page_size">Page size:</label>
|
||||||
<input type="number" id="page_size" name="_size" value="50" min="1" max="200">
|
<input type="number" id="page_size" name="page_size" value="50" min="1" max="200" style="max-width: 100px;">
|
||||||
<small>Number of results per page (max 200)</small>
|
<small>Number of results per page (max 200)</small>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|
@ -87,7 +88,7 @@ const hasDebugPermission = {{ 'true' if has_debug_permission else 'false' }};
|
||||||
(function() {
|
(function() {
|
||||||
const params = populateFormFromURL();
|
const params = populateFormFromURL();
|
||||||
const action = params.get('action');
|
const action = params.get('action');
|
||||||
const page = params.get('_page');
|
const page = params.get('page');
|
||||||
if (action) {
|
if (action) {
|
||||||
fetchResults(page ? parseInt(page) : 1);
|
fetchResults(page ? parseInt(page) : 1);
|
||||||
}
|
}
|
||||||
|
|
@ -101,14 +102,14 @@ async function fetchResults(page = 1) {
|
||||||
const params = new URLSearchParams();
|
const params = new URLSearchParams();
|
||||||
|
|
||||||
for (const [key, value] of formData.entries()) {
|
for (const [key, value] of formData.entries()) {
|
||||||
if (value && key !== '_size' && key !== '_page') {
|
if (value && key !== 'page_size') {
|
||||||
params.append(key, value);
|
params.append(key, value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const pageSize = document.getElementById('page_size').value || '50';
|
const pageSize = document.getElementById('page_size').value || '50';
|
||||||
params.append('_page', page.toString());
|
params.append('page', page.toString());
|
||||||
params.append('_size', pageSize);
|
params.append('page_size', pageSize);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await fetch('{{ urls.path("-/allowed.json") }}?' + params.toString(), {
|
const response = await fetch('{{ urls.path("-/allowed.json") }}?' + params.toString(), {
|
||||||
|
|
@ -197,7 +198,7 @@ function displayResults(data) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update raw JSON
|
// Update raw JSON
|
||||||
document.getElementById('raw-json').textContent = JSON.stringify(data, null, 2);
|
document.getElementById('raw-json').innerHTML = jsonFormatHighlight(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
function displayError(data) {
|
function displayError(data) {
|
||||||
|
|
@ -207,7 +208,7 @@ function displayError(data) {
|
||||||
|
|
||||||
resultsContent.innerHTML = `<div class="error-message">Error: ${escapeHtml(data.error || 'Unknown error')}</div>`;
|
resultsContent.innerHTML = `<div class="error-message">Error: ${escapeHtml(data.error || 'Unknown error')}</div>`;
|
||||||
|
|
||||||
document.getElementById('raw-json').textContent = JSON.stringify(data, null, 2);
|
document.getElementById('raw-json').innerHTML = jsonFormatHighlight(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Disable child input if parent is empty
|
// Disable child input if parent is empty
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@
|
||||||
|
|
||||||
{% block extra_head %}
|
{% block extra_head %}
|
||||||
{{ super() }}
|
{{ super() }}
|
||||||
<script src="{{ static('autocomplete.js') }}" defer></script>
|
<script src="{{ urls.static('autocomplete.js') }}" defer></script>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
{% block content %}
|
{% block content %}
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,9 @@
|
||||||
{% extends "base.html" %}
|
{% extends "base.html" %}
|
||||||
|
|
||||||
{% block title %}Explain a permission decision{% endblock %}
|
{% block title %}Permission Check{% endblock %}
|
||||||
|
|
||||||
{% block extra_head %}
|
{% block extra_head %}
|
||||||
|
<script src="{{ base_url }}-/static/json-format-highlight-1.0.1.js"></script>
|
||||||
{% include "_permission_ui_styles.html" %}
|
{% include "_permission_ui_styles.html" %}
|
||||||
{% include "_debug_common_functions.html" %}
|
{% include "_debug_common_functions.html" %}
|
||||||
<style>
|
<style>
|
||||||
|
|
@ -12,35 +13,29 @@
|
||||||
border-radius: 5px;
|
border-radius: 5px;
|
||||||
}
|
}
|
||||||
#output.allowed {
|
#output.allowed {
|
||||||
background-color: #f3fbf4;
|
background-color: #e8f5e9;
|
||||||
border: 2px solid #4caf50;
|
border: 2px solid #4caf50;
|
||||||
}
|
}
|
||||||
#output.denied {
|
#output.denied {
|
||||||
background-color: #fff7f7;
|
background-color: #ffebee;
|
||||||
border: 2px solid #f44336;
|
border: 2px solid #f44336;
|
||||||
}
|
}
|
||||||
#output h2 {
|
#output h2 {
|
||||||
margin-top: 0;
|
margin-top: 0;
|
||||||
}
|
}
|
||||||
#output h3 {
|
#output .result-badge {
|
||||||
margin-bottom: 0.5em;
|
|
||||||
}
|
|
||||||
#output .result-badge,
|
|
||||||
.effect-badge,
|
|
||||||
.rule-status {
|
|
||||||
display: inline-block;
|
display: inline-block;
|
||||||
padding: 0.2em 0.5em;
|
padding: 0.3em 0.8em;
|
||||||
border-radius: 3px;
|
border-radius: 3px;
|
||||||
font-weight: bold;
|
font-weight: bold;
|
||||||
|
font-size: 1.1em;
|
||||||
}
|
}
|
||||||
#output .allowed-badge,
|
#output .allowed-badge {
|
||||||
.effect-allow {
|
background-color: #4caf50;
|
||||||
background-color: #2e7d32;
|
|
||||||
color: white;
|
color: white;
|
||||||
}
|
}
|
||||||
#output .denied-badge,
|
#output .denied-badge {
|
||||||
.effect-deny {
|
background-color: #f44336;
|
||||||
background-color: #c62828;
|
|
||||||
color: white;
|
color: white;
|
||||||
}
|
}
|
||||||
.details-section {
|
.details-section {
|
||||||
|
|
@ -53,130 +48,70 @@
|
||||||
.details-section dd {
|
.details-section dd {
|
||||||
margin-left: 1em;
|
margin-left: 1em;
|
||||||
}
|
}
|
||||||
.explanation-section {
|
|
||||||
background: rgba(255, 255, 255, 0.75);
|
|
||||||
border: 1px solid #ddd;
|
|
||||||
border-radius: 4px;
|
|
||||||
margin-top: 1em;
|
|
||||||
padding: 0 1em 1em;
|
|
||||||
}
|
|
||||||
.rules-table {
|
|
||||||
border-collapse: collapse;
|
|
||||||
width: 100%;
|
|
||||||
}
|
|
||||||
.rules-table th,
|
|
||||||
.rules-table td {
|
|
||||||
border-bottom: 1px solid #ddd;
|
|
||||||
padding: 0.5em;
|
|
||||||
text-align: left;
|
|
||||||
vertical-align: top;
|
|
||||||
}
|
|
||||||
.rule-status {
|
|
||||||
background: #e8f5e9;
|
|
||||||
color: #1b5e20;
|
|
||||||
}
|
|
||||||
.rule-ignored {
|
|
||||||
background: #eee;
|
|
||||||
color: #555;
|
|
||||||
font-weight: normal;
|
|
||||||
}
|
|
||||||
.requirement-allowed {
|
|
||||||
color: #1b5e20;
|
|
||||||
}
|
|
||||||
.requirement-denied {
|
|
||||||
color: #b71c1c;
|
|
||||||
}
|
|
||||||
@media only screen and (max-width: 576px) {
|
|
||||||
.rules-table,
|
|
||||||
.rules-table tbody,
|
|
||||||
.rules-table tr,
|
|
||||||
.rules-table td {
|
|
||||||
display: block;
|
|
||||||
}
|
|
||||||
.rules-table thead {
|
|
||||||
display: none;
|
|
||||||
}
|
|
||||||
.rules-table td::before {
|
|
||||||
content: attr(data-label) ": ";
|
|
||||||
font-weight: bold;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
</style>
|
</style>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
{% block content %}
|
{% block content %}
|
||||||
<h1>Explain a permission decision</h1>
|
<h1>Permission check</h1>
|
||||||
|
|
||||||
{% set current_tab = "check" %}
|
{% set current_tab = "check" %}
|
||||||
{% include "_permissions_debug_tabs.html" %}
|
{% include "_permissions_debug_tabs.html" %}
|
||||||
|
|
||||||
<p>Test an actor, action and resource. The result explains which rules matched, which specificity level won, and whether actor restrictions or required actions changed the verdict.</p>
|
<p>Use this tool to test permission checks for the current actor. It queries the <code>/-/check.json</code> API endpoint.</p>
|
||||||
|
|
||||||
|
{% if request.actor %}
|
||||||
|
<p>Current actor: <strong>{{ request.actor.get("id", "anonymous") }}</strong></p>
|
||||||
|
{% else %}
|
||||||
|
<p>Current actor: <strong>anonymous (not logged in)</strong></p>
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
<div class="permission-form">
|
<div class="permission-form">
|
||||||
<form id="check-form" method="get" action="{{ urls.path('-/check') }}">
|
<form id="check-form" method="get" action="{{ urls.path("-/check") }}">
|
||||||
<div class="form-section">
|
<div class="form-section">
|
||||||
<label for="actor">Actor JSON:</label>
|
<label for="action">Action (permission name):</label>
|
||||||
<textarea class="permission-textarea" id="actor" name="actor">{{ actor_json }}</textarea>
|
|
||||||
<small>Use <code>null</code> for an anonymous actor. This actor is simulated; it does not change who you are signed in as.</small>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="form-section">
|
|
||||||
<label for="action">Action:</label>
|
|
||||||
<select id="action" name="action" required>
|
<select id="action" name="action" required>
|
||||||
<option value="">Select an action...</option>
|
<option value="">Select an action...</option>
|
||||||
{% for action in actions %}
|
{% for action_name in sorted_actions %}
|
||||||
<option value="{{ action.name }}">{{ action.name }}{% if action.description %} — {{ action.description }}{% endif %}</option>
|
<option value="{{ action_name }}">{{ action_name }}</option>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</select>
|
</select>
|
||||||
<small id="action-help">The operation to evaluate</small>
|
<small>The permission action to check</small>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="form-section" id="parent-section">
|
<div class="form-section">
|
||||||
<label for="parent">Parent resource:</label>
|
<label for="parent">Parent resource (optional):</label>
|
||||||
<input type="text" id="parent" name="parent" placeholder="e.g., database name">
|
<input type="text" id="parent" name="parent" placeholder="e.g., database name">
|
||||||
<small>The database or other parent resource</small>
|
<small>For database-level permissions, specify the database name</small>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="form-section" id="child-section">
|
<div class="form-section">
|
||||||
<label for="child">Child resource:</label>
|
<label for="child">Child resource (optional):</label>
|
||||||
<input type="text" id="child" name="child" placeholder="e.g., table or query name">
|
<input type="text" id="child" name="child" placeholder="e.g., table name">
|
||||||
<small>The table, query or other child resource</small>
|
<small>For table-level permissions, specify the table name (requires parent)</small>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="form-actions">
|
<div class="form-actions">
|
||||||
<button type="submit" class="submit-btn" id="submit-btn">Explain decision</button>
|
<button type="submit" class="submit-btn" id="submit-btn">Check Permission</button>
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div id="output" style="display: none;">
|
<div id="output" style="display: none;">
|
||||||
<h2>Result: <span class="result-badge" id="result-badge"></span></h2>
|
<h2>Result: <span class="result-badge" id="result-badge"></span></h2>
|
||||||
<p id="result-summary"></p>
|
|
||||||
|
|
||||||
<dl class="details-section">
|
<dl class="details-section">
|
||||||
<dt>Actor:</dt>
|
|
||||||
<dd><code id="result-actor"></code></dd>
|
|
||||||
<dt>Action:</dt>
|
<dt>Action:</dt>
|
||||||
<dd><code id="result-action"></code></dd>
|
<dd id="result-action"></dd>
|
||||||
<dt>Resource:</dt>
|
|
||||||
<dd><code id="result-resource"></code></dd>
|
<dt>Resource Path:</dt>
|
||||||
|
<dd id="result-resource"></dd>
|
||||||
|
|
||||||
|
<dt>Actor ID:</dt>
|
||||||
|
<dd id="result-actor"></dd>
|
||||||
|
|
||||||
|
<div id="additional-details"></div>
|
||||||
</dl>
|
</dl>
|
||||||
|
|
||||||
<section class="explanation-section">
|
|
||||||
<h3>Matching rules</h3>
|
|
||||||
<div id="matching-rules"></div>
|
|
||||||
</section>
|
|
||||||
|
|
||||||
<section class="explanation-section" id="restrictions-section">
|
|
||||||
<h3>Actor restrictions</h3>
|
|
||||||
<div id="restriction-results"></div>
|
|
||||||
</section>
|
|
||||||
|
|
||||||
<section class="explanation-section" id="requirements-section">
|
|
||||||
<h3>Required actions</h3>
|
|
||||||
<div id="requirement-results"></div>
|
|
||||||
</section>
|
|
||||||
|
|
||||||
<details style="margin-top: 1em;">
|
<details style="margin-top: 1em;">
|
||||||
<summary style="cursor: pointer; font-weight: bold;">Raw JSON response</summary>
|
<summary style="cursor: pointer; font-weight: bold;">Raw JSON response</summary>
|
||||||
<pre id="raw-json" style="margin-top: 1em; padding: 1em; background-color: #f5f5f5; border: 1px solid #ddd; border-radius: 3px; overflow-x: auto;"></pre>
|
<pre id="raw-json" style="margin-top: 1em; padding: 1em; background-color: #f5f5f5; border: 1px solid #ddd; border-radius: 3px; overflow-x: auto;"></pre>
|
||||||
|
|
@ -184,134 +119,152 @@
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
const actions = Object.fromEntries({{ actions|tojson }}.map(action => [action.name, action]));
|
|
||||||
const form = document.getElementById('check-form');
|
const form = document.getElementById('check-form');
|
||||||
const output = document.getElementById('output');
|
const output = document.getElementById('output');
|
||||||
const submitBtn = document.getElementById('submit-btn');
|
const submitBtn = document.getElementById('submit-btn');
|
||||||
const actionSelect = document.getElementById('action');
|
|
||||||
|
|
||||||
function updateResourceFields() {
|
|
||||||
const action = actions[actionSelect.value];
|
|
||||||
document.getElementById('parent-section').style.display = action && action.takes_parent ? 'block' : 'none';
|
|
||||||
document.getElementById('child-section').style.display = action && action.takes_child ? 'block' : 'none';
|
|
||||||
let help = action && action.description ? action.description : 'The operation to evaluate';
|
|
||||||
if (action && action.also_requires) {
|
|
||||||
help += `; also requires ${action.also_requires}`;
|
|
||||||
}
|
|
||||||
document.getElementById('action-help').textContent = help;
|
|
||||||
}
|
|
||||||
|
|
||||||
async function performCheck() {
|
async function performCheck() {
|
||||||
submitBtn.disabled = true;
|
submitBtn.disabled = true;
|
||||||
submitBtn.textContent = 'Explaining...';
|
submitBtn.textContent = 'Checking...';
|
||||||
const params = new URLSearchParams(new FormData(form));
|
|
||||||
|
const formData = new FormData(form);
|
||||||
|
const params = new URLSearchParams();
|
||||||
|
|
||||||
|
for (const [key, value] of formData.entries()) {
|
||||||
|
if (value) {
|
||||||
|
params.append(key, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await fetch('{{ urls.path("-/check.json") }}?' + params.toString(), {
|
const response = await fetch('{{ urls.path("-/check.json") }}?' + params.toString(), {
|
||||||
headers: {'Accept': 'application/json'}
|
method: 'GET',
|
||||||
|
headers: {
|
||||||
|
'Accept': 'application/json',
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
|
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
displayResult(data);
|
displayResult(data);
|
||||||
} else {
|
} else {
|
||||||
displayError(data);
|
displayError(data);
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
displayError({error: error.message});
|
alert('Error: ' + error.message);
|
||||||
} finally {
|
} finally {
|
||||||
submitBtn.disabled = false;
|
submitBtn.disabled = false;
|
||||||
submitBtn.textContent = 'Explain decision';
|
submitBtn.textContent = 'Check Permission';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Populate form on initial load
|
||||||
|
(function() {
|
||||||
|
const params = populateFormFromURL();
|
||||||
|
const action = params.get('action');
|
||||||
|
if (action) {
|
||||||
|
performCheck();
|
||||||
|
}
|
||||||
|
})();
|
||||||
|
|
||||||
function displayResult(data) {
|
function displayResult(data) {
|
||||||
output.style.display = 'block';
|
output.style.display = 'block';
|
||||||
|
|
||||||
|
// Set badge and styling
|
||||||
const resultBadge = document.getElementById('result-badge');
|
const resultBadge = document.getElementById('result-badge');
|
||||||
output.className = data.allowed ? 'allowed' : 'denied';
|
if (data.allowed) {
|
||||||
resultBadge.className = `result-badge ${data.allowed ? 'allowed-badge' : 'denied-badge'}`;
|
output.className = 'allowed';
|
||||||
resultBadge.textContent = data.allowed ? 'ALLOWED ✓' : 'DENIED ✗';
|
resultBadge.className = 'result-badge allowed-badge';
|
||||||
document.getElementById('result-summary').textContent = data.explanation.summary;
|
resultBadge.textContent = 'ALLOWED ✓';
|
||||||
document.getElementById('result-actor').textContent = data.actor === null ? 'anonymous' : JSON.stringify(data.actor);
|
} else {
|
||||||
document.getElementById('result-action').textContent = data.action;
|
output.className = 'denied';
|
||||||
document.getElementById('result-resource').textContent = data.resource.path;
|
resultBadge.className = 'result-badge denied-badge';
|
||||||
displayRules(data.explanation);
|
resultBadge.textContent = 'DENIED ✗';
|
||||||
displayRestrictions(data.explanation.restrictions);
|
|
||||||
displayRequirements(data.explanation.required_actions);
|
|
||||||
document.getElementById('raw-json').textContent = JSON.stringify(data, null, 2);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function displayRules(explanation) {
|
// Basic details
|
||||||
const container = document.getElementById('matching-rules');
|
document.getElementById('result-action').textContent = data.action || 'N/A';
|
||||||
if (!explanation.matched_rules.length) {
|
document.getElementById('result-resource').textContent = data.resource?.path || '/';
|
||||||
container.innerHTML = '<p>No rules matched. Datasette denies access when there is no matching rule.</p>';
|
document.getElementById('result-actor').textContent = data.actor_id || 'anonymous';
|
||||||
return;
|
|
||||||
}
|
// Additional details
|
||||||
let html = '<table class="rules-table"><thead><tr><th>Effect</th><th>Scope</th><th>Source</th><th>Reason</th><th>Role in decision</th></tr></thead><tbody>';
|
const additionalDetails = document.getElementById('additional-details');
|
||||||
for (const rule of explanation.matched_rules) {
|
additionalDetails.innerHTML = '';
|
||||||
const status = rule.decisive
|
|
||||||
? '<span class="rule-status">Decisive</span>'
|
if (data.reason !== undefined) {
|
||||||
: `<span class="rule-status rule-ignored">${escapeHtml(rule.ignored_because)}</span>`;
|
const dt = document.createElement('dt');
|
||||||
html += '<tr>';
|
dt.textContent = 'Reason:';
|
||||||
html += `<td data-label="Effect"><span class="effect-badge effect-${rule.effect}">${rule.effect.toUpperCase()}</span></td>`;
|
const dd = document.createElement('dd');
|
||||||
html += `<td data-label="Scope">${escapeHtml(rule.scope)}</td>`;
|
dd.textContent = data.reason || 'N/A';
|
||||||
html += `<td data-label="Source"><code>${escapeHtml(rule.source || 'unknown')}</code></td>`;
|
additionalDetails.appendChild(dt);
|
||||||
html += `<td data-label="Reason">${escapeHtml(rule.reason || 'No reason supplied')}</td>`;
|
additionalDetails.appendChild(dd);
|
||||||
html += `<td data-label="Role in decision">${status}</td>`;
|
|
||||||
html += '</tr>';
|
|
||||||
}
|
|
||||||
container.innerHTML = html + '</tbody></table>';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function displayRestrictions(restrictions) {
|
if (data.source_plugin !== undefined) {
|
||||||
const section = document.getElementById('restrictions-section');
|
const dt = document.createElement('dt');
|
||||||
const container = document.getElementById('restriction-results');
|
dt.textContent = 'Source Plugin:';
|
||||||
section.style.display = restrictions.length ? 'block' : 'none';
|
const dd = document.createElement('dd');
|
||||||
container.innerHTML = restrictions.map(restriction => {
|
dd.textContent = data.source_plugin || 'N/A';
|
||||||
const className = restriction.allowed ? 'requirement-allowed' : 'requirement-denied';
|
additionalDetails.appendChild(dt);
|
||||||
const verdict = restriction.allowed ? 'INCLUDED ✓' : 'EXCLUDED ✗';
|
additionalDetails.appendChild(dd);
|
||||||
return `<p class="${className}"><strong>${verdict}</strong> by <code>${escapeHtml(restriction.source || 'unknown')}</code>: ${escapeHtml(restriction.reason)}</p>`;
|
|
||||||
}).join('');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function displayRequirements(requirements) {
|
if (data.used_default !== undefined) {
|
||||||
const section = document.getElementById('requirements-section');
|
const dt = document.createElement('dt');
|
||||||
const container = document.getElementById('requirement-results');
|
dt.textContent = 'Used Default:';
|
||||||
section.style.display = requirements.length ? 'block' : 'none';
|
const dd = document.createElement('dd');
|
||||||
container.innerHTML = requirements.map(requirement => {
|
dd.textContent = data.used_default ? 'Yes' : 'No';
|
||||||
const className = requirement.allowed ? 'requirement-allowed' : 'requirement-denied';
|
additionalDetails.appendChild(dt);
|
||||||
const verdict = requirement.allowed ? 'ALLOWED ✓' : 'DENIED ✗';
|
additionalDetails.appendChild(dd);
|
||||||
return `<p class="${className}"><strong>${escapeHtml(requirement.action)}: ${verdict}</strong> — ${escapeHtml(requirement.summary)}</p>`;
|
}
|
||||||
}).join('');
|
|
||||||
|
if (data.depth !== undefined) {
|
||||||
|
const dt = document.createElement('dt');
|
||||||
|
dt.textContent = 'Depth:';
|
||||||
|
const dd = document.createElement('dd');
|
||||||
|
dd.textContent = data.depth;
|
||||||
|
additionalDetails.appendChild(dt);
|
||||||
|
additionalDetails.appendChild(dd);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Raw JSON
|
||||||
|
document.getElementById('raw-json').innerHTML = jsonFormatHighlight(data);
|
||||||
|
|
||||||
|
// Scroll to output
|
||||||
|
output.scrollIntoView({ behavior: 'smooth', block: 'nearest' });
|
||||||
}
|
}
|
||||||
|
|
||||||
function displayError(data) {
|
function displayError(data) {
|
||||||
output.style.display = 'block';
|
output.style.display = 'block';
|
||||||
output.className = 'denied';
|
output.className = 'denied';
|
||||||
|
|
||||||
const resultBadge = document.getElementById('result-badge');
|
const resultBadge = document.getElementById('result-badge');
|
||||||
resultBadge.className = 'result-badge denied-badge';
|
resultBadge.className = 'result-badge denied-badge';
|
||||||
resultBadge.textContent = 'ERROR';
|
resultBadge.textContent = 'ERROR';
|
||||||
document.getElementById('result-summary').textContent = data.error || 'Unknown error';
|
|
||||||
document.getElementById('result-actor').textContent = '—';
|
document.getElementById('result-action').textContent = 'N/A';
|
||||||
document.getElementById('result-action').textContent = '—';
|
document.getElementById('result-resource').textContent = 'N/A';
|
||||||
document.getElementById('result-resource').textContent = '—';
|
document.getElementById('result-actor').textContent = 'N/A';
|
||||||
document.getElementById('matching-rules').innerHTML = '';
|
|
||||||
document.getElementById('restrictions-section').style.display = 'none';
|
const additionalDetails = document.getElementById('additional-details');
|
||||||
document.getElementById('requirements-section').style.display = 'none';
|
additionalDetails.innerHTML = '<dt>Error:</dt><dd>' + (data.error || 'Unknown error') + '</dd>';
|
||||||
document.getElementById('raw-json').textContent = JSON.stringify(data, null, 2);
|
|
||||||
|
document.getElementById('raw-json').innerHTML = jsonFormatHighlight(data);
|
||||||
|
|
||||||
|
output.scrollIntoView({ behavior: 'smooth', block: 'nearest' });
|
||||||
}
|
}
|
||||||
|
|
||||||
form.addEventListener('submit', event => {
|
// Disable child input if parent is empty
|
||||||
event.preventDefault();
|
const parentInput = document.getElementById('parent');
|
||||||
performCheck();
|
const childInput = document.getElementById('child');
|
||||||
|
|
||||||
|
childInput.addEventListener('focus', () => {
|
||||||
|
if (!parentInput.value) {
|
||||||
|
alert('Please specify a parent resource first before adding a child resource.');
|
||||||
|
parentInput.focus();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
actionSelect.addEventListener('change', updateResourceFields);
|
|
||||||
|
|
||||||
(function initializeFromUrl() {
|
|
||||||
const params = populateFormFromURL();
|
|
||||||
updateResourceFields();
|
|
||||||
if (params.get('action')) {
|
|
||||||
performCheck();
|
|
||||||
}
|
|
||||||
})();
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
{% extends "base.html" %}
|
{% extends "base.html" %}
|
||||||
|
|
||||||
{% block title %}Permission activity{% endblock %}
|
{% block title %}Debug permissions{% endblock %}
|
||||||
|
|
||||||
{% block extra_head %}
|
{% block extra_head %}
|
||||||
{% include "_permission_ui_styles.html" %}
|
{% include "_permission_ui_styles.html" %}
|
||||||
|
|
@ -20,29 +20,45 @@
|
||||||
.check-action, .check-when, .check-result {
|
.check-action, .check-when, .check-result {
|
||||||
font-size: 1.3em;
|
font-size: 1.3em;
|
||||||
}
|
}
|
||||||
|
textarea {
|
||||||
|
height: 10em;
|
||||||
|
width: 95%;
|
||||||
|
box-sizing: border-box;
|
||||||
|
padding: 0.5em;
|
||||||
|
border: 2px dotted black;
|
||||||
|
}
|
||||||
|
.two-col {
|
||||||
|
display: inline-block;
|
||||||
|
width: 48%;
|
||||||
|
}
|
||||||
|
.two-col label {
|
||||||
|
width: 48%;
|
||||||
|
}
|
||||||
|
@media only screen and (max-width: 576px) {
|
||||||
|
.two-col {
|
||||||
|
width: 100%;
|
||||||
|
}
|
||||||
|
}
|
||||||
</style>
|
</style>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
{% block content %}
|
{% block content %}
|
||||||
<h1>Permission activity</h1>
|
<h1>Permission playground</h1>
|
||||||
|
|
||||||
{% set current_tab = "permissions" %}
|
{% set current_tab = "permissions" %}
|
||||||
{% include "_permissions_debug_tabs.html" %}
|
{% include "_permissions_debug_tabs.html" %}
|
||||||
|
|
||||||
<h2>Raw simulator</h2>
|
<p>This tool lets you simulate an actor and a permission check for that actor.</p>
|
||||||
|
|
||||||
<p>This form runs a hypothetical permission check and returns its raw explanation JSON. Use the <a href="{{ urls.path('-/check') }}">Explain tool</a> for a visual explanation of the same decision.</p>
|
|
||||||
|
|
||||||
<div class="permission-form">
|
<div class="permission-form">
|
||||||
<form action="{{ urls.path('-/permissions') }}" id="debug-post" method="post">
|
<form action="{{ urls.path('-/permissions') }}" id="debug-post" method="post">
|
||||||
<div class="permission-form-grid">
|
<div class="two-col">
|
||||||
<div>
|
|
||||||
<div class="form-section">
|
<div class="form-section">
|
||||||
<label for="activity-actor">Actor</label>
|
<label>Actor</label>
|
||||||
<textarea class="permission-textarea" id="activity-actor" name="actor">{% if actor_input %}{{ actor_input }}{% else %}{"id": "root"}{% endif %}</textarea>
|
<textarea name="actor">{% if actor_input %}{{ actor_input }}{% else %}{"id": "root"}{% endif %}</textarea>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div class="two-col" style="vertical-align: top">
|
||||||
<div class="form-section">
|
<div class="form-section">
|
||||||
<label for="permission">Action</label>
|
<label for="permission">Action</label>
|
||||||
<select name="permission" id="permission">
|
<select name="permission" id="permission">
|
||||||
|
|
@ -60,7 +76,6 @@
|
||||||
<input type="text" id="resource_2" name="resource_2" placeholder="e.g., table name">
|
<input type="text" id="resource_2" name="resource_2" placeholder="e.g., table name">
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
|
||||||
<div class="form-actions">
|
<div class="form-actions">
|
||||||
<button type="submit" class="submit-btn">Simulate permission check</button>
|
<button type="submit" class="submit-btn">Simulate permission check</button>
|
||||||
</div>
|
</div>
|
||||||
|
|
@ -110,7 +125,7 @@ debugPost.addEventListener('submit', function(ev) {
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<h2>Recent permission checks</h2>
|
<h1>Recent permissions checks</h1>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
{% if filter != "all" %}<a href="?filter=all">All</a>{% else %}<strong>All</strong>{% endif %},
|
{% if filter != "all" %}<a href="?filter=all">All</a>{% else %}<strong>All</strong>{% endif %},
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@
|
||||||
{% block title %}Permission Rules{% endblock %}
|
{% block title %}Permission Rules{% endblock %}
|
||||||
|
|
||||||
{% block extra_head %}
|
{% block extra_head %}
|
||||||
|
<script src="{{ base_url }}-/static/json-format-highlight-1.0.1.js"></script>
|
||||||
{% include "_permission_ui_styles.html" %}
|
{% include "_permission_ui_styles.html" %}
|
||||||
{% include "_debug_common_functions.html" %}
|
{% include "_debug_common_functions.html" %}
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
@ -36,7 +37,7 @@
|
||||||
|
|
||||||
<div class="form-section">
|
<div class="form-section">
|
||||||
<label for="page_size">Page size:</label>
|
<label for="page_size">Page size:</label>
|
||||||
<input type="number" id="page_size" name="_size" value="50" min="1" max="200">
|
<input type="number" id="page_size" name="page_size" value="50" min="1" max="200" style="max-width: 100px;">
|
||||||
<small>Number of results per page (max 200)</small>
|
<small>Number of results per page (max 200)</small>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|
@ -74,7 +75,7 @@ const submitBtn = document.getElementById('submit-btn');
|
||||||
(function() {
|
(function() {
|
||||||
const params = populateFormFromURL();
|
const params = populateFormFromURL();
|
||||||
const action = params.get('action');
|
const action = params.get('action');
|
||||||
const page = params.get('_page');
|
const page = params.get('page');
|
||||||
if (action) {
|
if (action) {
|
||||||
fetchResults(page ? parseInt(page) : 1);
|
fetchResults(page ? parseInt(page) : 1);
|
||||||
}
|
}
|
||||||
|
|
@ -88,14 +89,14 @@ async function fetchResults(page = 1) {
|
||||||
const params = new URLSearchParams();
|
const params = new URLSearchParams();
|
||||||
|
|
||||||
for (const [key, value] of formData.entries()) {
|
for (const [key, value] of formData.entries()) {
|
||||||
if (value && key !== '_size' && key !== '_page') {
|
if (value && key !== 'page_size') {
|
||||||
params.append(key, value);
|
params.append(key, value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const pageSize = document.getElementById('page_size').value || '50';
|
const pageSize = document.getElementById('page_size').value || '50';
|
||||||
params.append('_page', page.toString());
|
params.append('page', page.toString());
|
||||||
params.append('_size', pageSize);
|
params.append('page_size', pageSize);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await fetch('{{ urls.path("-/rules.json") }}?' + params.toString(), {
|
const response = await fetch('{{ urls.path("-/rules.json") }}?' + params.toString(), {
|
||||||
|
|
@ -184,7 +185,7 @@ function displayResults(data) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update raw JSON
|
// Update raw JSON
|
||||||
document.getElementById('raw-json').textContent = JSON.stringify(data, null, 2);
|
document.getElementById('raw-json').innerHTML = jsonFormatHighlight(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
function displayError(data) {
|
function displayError(data) {
|
||||||
|
|
@ -194,7 +195,7 @@ function displayError(data) {
|
||||||
|
|
||||||
resultsContent.innerHTML = `<div class="error-message">Error: ${escapeHtml(data.error || 'Unknown error')}</div>`;
|
resultsContent.innerHTML = `<div class="error-message">Error: ${escapeHtml(data.error || 'Unknown error')}</div>`;
|
||||||
|
|
||||||
document.getElementById('raw-json').textContent = JSON.stringify(data, null, 2);
|
document.getElementById('raw-json').innerHTML = jsonFormatHighlight(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@
|
||||||
<html lang="en">
|
<html lang="en">
|
||||||
<head>
|
<head>
|
||||||
<title>Datasette: Pattern Portfolio</title>
|
<title>Datasette: Pattern Portfolio</title>
|
||||||
<link rel="stylesheet" href="{{ static('app.css') }}">
|
<link rel="stylesheet" href="{{ base_url }}-/static/app.css?{{ app_css_hash }}">
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
|
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
|
||||||
<meta name="robots" content="noindex">
|
<meta name="robots" content="noindex">
|
||||||
<style></style>
|
<style></style>
|
||||||
|
|
@ -202,9 +202,9 @@
|
||||||
<h3>3 rows
|
<h3>3 rows
|
||||||
where characteristic_id = 2
|
where characteristic_id = 2
|
||||||
</h3>
|
</h3>
|
||||||
<form class="core filters" action="{{ base_url }}fixtures/roadside_attraction_characteristics" method="get">
|
<form class="filters" action="{{ base_url }}fixtures/roadside_attraction_characteristics" method="get">
|
||||||
<div class="search-row"><label for="_search">Search:</label><input id="_search" type="search" name="_search" value=""></div>
|
<div class="search-row"><label for="_search">Search:</label><input id="_search" type="search" name="_search" value=""></div>
|
||||||
<div class="filter-row filter-controls-row">
|
<div class="filter-row">
|
||||||
<div class="select-wrapper">
|
<div class="select-wrapper">
|
||||||
<select name="_filter_column_1">
|
<select name="_filter_column_1">
|
||||||
<option value="">- remove filter -</option>
|
<option value="">- remove filter -</option>
|
||||||
|
|
@ -238,7 +238,7 @@
|
||||||
</select>
|
</select>
|
||||||
</div><input type="text" name="_filter_value_1" class="filter-value" value="2">
|
</div><input type="text" name="_filter_value_1" class="filter-value" value="2">
|
||||||
</div>
|
</div>
|
||||||
<div class="filter-row filter-controls-row">
|
<div class="filter-row">
|
||||||
<div class="select-wrapper">
|
<div class="select-wrapper">
|
||||||
<select name="_filter_column">
|
<select name="_filter_column">
|
||||||
<option value="">- column -</option>
|
<option value="">- column -</option>
|
||||||
|
|
@ -272,8 +272,8 @@
|
||||||
</select>
|
</select>
|
||||||
</div><input type="text" name="_filter_value" class="filter-value">
|
</div><input type="text" name="_filter_value" class="filter-value">
|
||||||
</div>
|
</div>
|
||||||
<div class="filter-row filter-actions-row">
|
<div class="filter-row">
|
||||||
<div class="select-wrapper">
|
<div class="select-wrapper small-screen-only">
|
||||||
<select name="_sort" id="sort_by">
|
<select name="_sort" id="sort_by">
|
||||||
<option value="">Sort...</option>
|
<option value="">Sort...</option>
|
||||||
<option value="rowid" selected>Sort by rowid</option>
|
<option value="rowid" selected>Sort by rowid</option>
|
||||||
|
|
@ -281,8 +281,8 @@
|
||||||
<option value="characteristic_id">Sort by characteristic_id</option>
|
<option value="characteristic_id">Sort by characteristic_id</option>
|
||||||
</select>
|
</select>
|
||||||
</div>
|
</div>
|
||||||
<label class="sort_by_desc"><input type="checkbox" name="_sort_by_desc"> descending</label>
|
<label class="sort_by_desc small-screen-only"><input type="checkbox" name="_sort_by_desc"> descending</label>
|
||||||
<input type="submit" value="Apply filters">
|
<input type="submit" value="Apply">
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -7,9 +7,9 @@
|
||||||
{% if row_mutation_ui %}
|
{% if row_mutation_ui %}
|
||||||
<script>window._datasetteTableData = {{ table_page_data|tojson }};</script>
|
<script>window._datasetteTableData = {{ table_page_data|tojson }};</script>
|
||||||
{% if table_page_data.foreignKeys %}
|
{% if table_page_data.foreignKeys %}
|
||||||
<script src="{{ static('autocomplete.js') }}" defer></script>
|
<script src="{{ urls.static('autocomplete.js') }}" defer></script>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
<script src="{{ static('edit-tools.js') }}" defer></script>
|
<script src="{{ urls.static('edit-tools.js') }}?hash={{ edit_tools_js_hash }}" defer></script>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
<style>
|
<style>
|
||||||
@media only screen and (max-width: 576px) {
|
@media only screen and (max-width: 576px) {
|
||||||
|
|
@ -42,12 +42,24 @@
|
||||||
|
|
||||||
{% if foreign_key_tables %}
|
{% if foreign_key_tables %}
|
||||||
<h2>Links from other tables</h2>
|
<h2>Links from other tables</h2>
|
||||||
<ul>
|
<ul class="row-foreign-key-tables">
|
||||||
{% for other in foreign_key_tables %}
|
{% for other in foreign_key_tables %}
|
||||||
<li>
|
<li>
|
||||||
<a href="{{ other.link }}">
|
<a href="{{ other.link }}">
|
||||||
{{ "{:,}".format(other.count) }} row{% if other.count == 1 %}{% else %}s{% endif %}</a>
|
{{ "{:,}".format(other.count) }} row{% if other.count == 1 %}{% else %}s{% endif %}</a>
|
||||||
from {{ other.other_column }} in {{ other.other_table }}
|
from {{ other.other_column }} in {{ other.other_table }}
|
||||||
|
{% if other.can_insert %}
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
class="core row-foreign-key-insert"
|
||||||
|
data-insert-dialog
|
||||||
|
data-insert-dialog-reload
|
||||||
|
data-database="{{ database }}"
|
||||||
|
data-table="{{ other.other_table }}"
|
||||||
|
data-row="{{ other.insert_row|tojson|forceescape }}"
|
||||||
|
data-message="{{ other.insert_message }}"
|
||||||
|
>Insert</button>
|
||||||
|
{% endif %}
|
||||||
</li>
|
</li>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</ul>
|
</ul>
|
||||||
|
|
|
||||||
|
|
@ -1,17 +1,17 @@
|
||||||
{% extends "base.html" %}
|
{% extends "base.html" %}
|
||||||
|
|
||||||
{% block title %}{{ database }}: {{ table }}: {% if count_truncated %}{{ "{:,}".format(count - 1) }}+ rows{% elif count or count == 0 %}{{ "{:,}".format(count) }} row{% if count == 1 %}{% else %}s{% endif %}{% endif %}{% if human_description_en %} {{ human_description_en }}{% endif %}{% endblock %}
|
{% block title %}{{ database }}: {{ table }}: {% if count or count == 0 %}{{ "{:,}".format(count) }} row{% if count == 1 %}{% else %}s{% endif %}{% endif %}{% if human_description_en %} {{ human_description_en }}{% endif %}{% endblock %}
|
||||||
|
|
||||||
{% block extra_head %}
|
{% block extra_head %}
|
||||||
{{- super() -}}
|
{{- super() -}}
|
||||||
<script>window._datasetteTableData = {{ table_page_data|tojson }};</script>
|
<script>window._datasetteTableData = {{ table_page_data|tojson }};</script>
|
||||||
<script src="{{ static('column-chooser.js') }}" defer></script>
|
<script src="{{ urls.static('column-chooser.js') }}" defer></script>
|
||||||
{% if table_page_data.foreignKeys %}
|
{% if table_page_data.foreignKeys %}
|
||||||
<script src="{{ static('autocomplete.js') }}" defer></script>
|
<script src="{{ urls.static('autocomplete.js') }}" defer></script>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
<script src="{{ static('edit-tools.js') }}" defer></script>
|
<script src="{{ urls.static('edit-tools.js') }}?hash={{ edit_tools_js_hash }}" defer></script>
|
||||||
<script src="{{ static('table.js') }}" defer></script>
|
<script src="{{ urls.static('table.js') }}?hash={{ table_js_hash }}" defer></script>
|
||||||
<script src="{{ static('mobile-column-actions.js') }}" defer></script>
|
<script src="{{ urls.static('mobile-column-actions.js') }}" defer></script>
|
||||||
<script>DATASETTE_ALLOW_FACET = {{ datasette_allow_facet }};</script>
|
<script>DATASETTE_ALLOW_FACET = {{ datasette_allow_facet }};</script>
|
||||||
<style>
|
<style>
|
||||||
@media only screen and (max-width: 576px) {
|
@media only screen and (max-width: 576px) {
|
||||||
|
|
@ -47,21 +47,20 @@
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
{% if count or human_description_en %}
|
{% if count or human_description_en %}
|
||||||
<h3 class="table-summary">
|
<h3>
|
||||||
{% if count_truncated %}<span class="table-count" aria-live="polite">{{ "{:,}".format(count - 1) }}+ rows</span>
|
{% if count == count_limit + 1 %}>{{ "{:,}".format(count_limit) }} rows
|
||||||
<button type="button" class="count-all" data-count-url="{{ urls.table(database, table) }}/-/count">count all</button>
|
{% if allow_execute_sql and query.sql %} <a class="count-sql" style="font-size: 0.8em;" href="{{ urls.database_query(database, count_sql) }}">count all</a>{% endif %}
|
||||||
<span class="count-error" role="alert"></span>
|
|
||||||
{% elif count or count == 0 %}{{ "{:,}".format(count) }} row{% if count == 1 %}{% else %}s{% endif %}{% endif %}
|
{% elif count or count == 0 %}{{ "{:,}".format(count) }} row{% if count == 1 %}{% else %}s{% endif %}{% endif %}
|
||||||
{% if human_description_en %}<span class="table-summary-description">{{ human_description_en }}</span>{% endif %}
|
{% if human_description_en %}{{ human_description_en }}{% endif %}
|
||||||
</h3>
|
</h3>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
<form class="core filters" action="{{ urls.table(database, table) }}" method="get">
|
<form class="core" class="filters" action="{{ urls.table(database, table) }}" method="get">
|
||||||
{% if supports_search %}
|
{% if supports_search %}
|
||||||
<div class="search-row"><label for="_search">Search:</label><input id="_search" type="search" name="_search" value="{{ search }}"></div>
|
<div class="search-row"><label for="_search">Search:</label><input id="_search" type="search" name="_search" value="{{ search }}"></div>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% for column, lookup, value in filters.selections() %}
|
{% for column, lookup, value in filters.selections() %}
|
||||||
<div class="filter-row filter-controls-row">
|
<div class="filter-row">
|
||||||
<div class="select-wrapper">
|
<div class="select-wrapper">
|
||||||
<select name="_filter_column_{{ loop.index }}">
|
<select name="_filter_column_{{ loop.index }}">
|
||||||
<option value="">- remove filter -</option>
|
<option value="">- remove filter -</option>
|
||||||
|
|
@ -78,7 +77,7 @@
|
||||||
</div><input type="text" name="_filter_value_{{ loop.index }}" class="filter-value" value="{{ value }}">
|
</div><input type="text" name="_filter_value_{{ loop.index }}" class="filter-value" value="{{ value }}">
|
||||||
</div>
|
</div>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
<div class="filter-row filter-controls-row">
|
<div class="filter-row">
|
||||||
<div class="select-wrapper">
|
<div class="select-wrapper">
|
||||||
<select name="_filter_column">
|
<select name="_filter_column">
|
||||||
<option value="">- column -</option>
|
<option value="">- column -</option>
|
||||||
|
|
@ -94,9 +93,9 @@
|
||||||
</select>
|
</select>
|
||||||
</div><input type="text" name="_filter_value" class="filter-value">
|
</div><input type="text" name="_filter_value" class="filter-value">
|
||||||
</div>
|
</div>
|
||||||
<div class="filter-row filter-actions-row">
|
<div class="filter-row">
|
||||||
{% if is_sortable %}
|
{% if is_sortable %}
|
||||||
<div class="select-wrapper">
|
<div class="select-wrapper small-screen-only">
|
||||||
<select name="_sort" id="sort_by">
|
<select name="_sort" id="sort_by">
|
||||||
<option value="">Sort...</option>
|
<option value="">Sort...</option>
|
||||||
{% for column in display_columns %}
|
{% for column in display_columns %}
|
||||||
|
|
@ -106,12 +105,12 @@
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</select>
|
</select>
|
||||||
</div>
|
</div>
|
||||||
<label class="sort_by_desc"><input type="checkbox" name="_sort_by_desc" tabindex="0"{% if sort_desc %} checked{% endif %}> descending</label>
|
<label class="sort_by_desc small-screen-only"><input type="checkbox" name="_sort_by_desc"{% if sort_desc %} checked{% endif %}> descending</label>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% for key, value in form_hidden_args %}
|
{% for key, value in form_hidden_args %}
|
||||||
<input type="hidden" name="{{ key }}" value="{{ value }}">
|
<input type="hidden" name="{{ key }}" value="{{ value }}">
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
<input type="submit" value="Apply filters" tabindex="0">
|
<input type="submit" value="Apply">
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@ from __future__ import annotations
|
||||||
|
|
||||||
import dataclasses
|
import dataclasses
|
||||||
import time
|
import time
|
||||||
from typing import TYPE_CHECKING
|
from typing import TYPE_CHECKING, Optional
|
||||||
|
|
||||||
import itsdangerous
|
import itsdangerous
|
||||||
|
|
||||||
|
|
@ -18,21 +18,6 @@ if TYPE_CHECKING:
|
||||||
from datasette.app import Datasette
|
from datasette.app import Datasette
|
||||||
|
|
||||||
|
|
||||||
class TokenInvalid(Exception):
|
|
||||||
"""
|
|
||||||
Raised by a TokenHandler when a token it recognizes is invalid -
|
|
||||||
for example a bad signature, malformed payload or expired token.
|
|
||||||
|
|
||||||
Datasette responds to this with an HTTP 401 error. Handlers should
|
|
||||||
return None instead for tokens they do not recognize at all, so that
|
|
||||||
other registered handlers get a chance to verify them.
|
|
||||||
"""
|
|
||||||
|
|
||||||
def __init__(self, message="Invalid token"):
|
|
||||||
self.message = message
|
|
||||||
super().__init__(message)
|
|
||||||
|
|
||||||
|
|
||||||
@dataclasses.dataclass
|
@dataclasses.dataclass
|
||||||
class TokenRestrictions:
|
class TokenRestrictions:
|
||||||
"""
|
"""
|
||||||
|
|
@ -50,24 +35,24 @@ class TokenRestrictions:
|
||||||
database: dict[str, list[str]] = dataclasses.field(default_factory=dict)
|
database: dict[str, list[str]] = dataclasses.field(default_factory=dict)
|
||||||
resource: dict[str, dict[str, list[str]]] = dataclasses.field(default_factory=dict)
|
resource: dict[str, dict[str, list[str]]] = dataclasses.field(default_factory=dict)
|
||||||
|
|
||||||
def allow_all(self, action: str) -> TokenRestrictions:
|
def allow_all(self, action: str) -> "TokenRestrictions":
|
||||||
"""Allow an action across all databases and resources."""
|
"""Allow an action across all databases and resources."""
|
||||||
self.all.append(action)
|
self.all.append(action)
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def allow_database(self, database: str, action: str) -> TokenRestrictions:
|
def allow_database(self, database: str, action: str) -> "TokenRestrictions":
|
||||||
"""Allow an action on a specific database."""
|
"""Allow an action on a specific database."""
|
||||||
self.database.setdefault(database, []).append(action)
|
self.database.setdefault(database, []).append(action)
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def allow_resource(
|
def allow_resource(
|
||||||
self, database: str, resource: str, action: str
|
self, database: str, resource: str, action: str
|
||||||
) -> TokenRestrictions:
|
) -> "TokenRestrictions":
|
||||||
"""Allow an action on a specific resource within a database."""
|
"""Allow an action on a specific resource within a database."""
|
||||||
self.resource.setdefault(database, {}).setdefault(resource, []).append(action)
|
self.resource.setdefault(database, {}).setdefault(resource, []).append(action)
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def abbreviated(self, datasette: Datasette) -> dict | None:
|
def abbreviated(self, datasette: "Datasette") -> Optional[dict]:
|
||||||
"""
|
"""
|
||||||
Return the abbreviated ``_r`` dictionary shape for this set of
|
Return the abbreviated ``_r`` dictionary shape for this set of
|
||||||
restrictions, using action abbreviations registered with ``datasette``.
|
restrictions, using action abbreviations registered with ``datasette``.
|
||||||
|
|
@ -112,23 +97,19 @@ class TokenHandler:
|
||||||
|
|
||||||
async def create_token(
|
async def create_token(
|
||||||
self,
|
self,
|
||||||
datasette: Datasette,
|
datasette: "Datasette",
|
||||||
actor_id: str,
|
actor_id: str,
|
||||||
*,
|
*,
|
||||||
expires_after: int | None = None,
|
expires_after: Optional[int] = None,
|
||||||
restrictions: TokenRestrictions | None = None,
|
restrictions: Optional[TokenRestrictions] = None,
|
||||||
) -> str:
|
) -> str:
|
||||||
"""Create and return a token string for the given actor."""
|
"""Create and return a token string for the given actor."""
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
async def verify_token(self, datasette: Datasette, token: str) -> dict | None:
|
async def verify_token(self, datasette: "Datasette", token: str) -> Optional[dict]:
|
||||||
"""
|
"""
|
||||||
Verify a token and return an actor dict.
|
Verify a token and return an actor dict, or None if this handler
|
||||||
|
does not recognize the token.
|
||||||
Return None if this handler does not recognize the token at all,
|
|
||||||
so other handlers can try it. Raise TokenInvalid if the token is
|
|
||||||
recognized but invalid (bad signature, malformed, expired) - the
|
|
||||||
request will fail with a 401 error.
|
|
||||||
"""
|
"""
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
|
|
@ -142,11 +123,11 @@ class SignedTokenHandler(TokenHandler):
|
||||||
|
|
||||||
async def create_token(
|
async def create_token(
|
||||||
self,
|
self,
|
||||||
datasette: Datasette,
|
datasette: "Datasette",
|
||||||
actor_id: str,
|
actor_id: str,
|
||||||
*,
|
*,
|
||||||
expires_after: int | None = None,
|
expires_after: Optional[int] = None,
|
||||||
restrictions: TokenRestrictions | None = None,
|
restrictions: Optional[TokenRestrictions] = None,
|
||||||
) -> str:
|
) -> str:
|
||||||
if not datasette.setting("allow_signed_tokens"):
|
if not datasette.setting("allow_signed_tokens"):
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
|
|
@ -163,35 +144,32 @@ class SignedTokenHandler(TokenHandler):
|
||||||
token["_r"] = abbreviated
|
token["_r"] = abbreviated
|
||||||
return "dstok_{}".format(datasette.sign(token, namespace="token"))
|
return "dstok_{}".format(datasette.sign(token, namespace="token"))
|
||||||
|
|
||||||
async def verify_token(self, datasette: Datasette, token: str) -> dict | None:
|
async def verify_token(self, datasette: "Datasette", token: str) -> Optional[dict]:
|
||||||
prefix = "dstok_"
|
prefix = "dstok_"
|
||||||
|
|
||||||
if not token.startswith(prefix):
|
if not datasette.setting("allow_signed_tokens"):
|
||||||
# Not one of our tokens - leave it for other handlers
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
if not datasette.setting("allow_signed_tokens"):
|
|
||||||
raise TokenInvalid(
|
|
||||||
"Signed tokens are not enabled for this Datasette instance"
|
|
||||||
)
|
|
||||||
|
|
||||||
max_signed_tokens_ttl = datasette.setting("max_signed_tokens_ttl")
|
max_signed_tokens_ttl = datasette.setting("max_signed_tokens_ttl")
|
||||||
|
|
||||||
|
if not token.startswith(prefix):
|
||||||
|
return None
|
||||||
|
|
||||||
raw = token[len(prefix) :]
|
raw = token[len(prefix) :]
|
||||||
try:
|
try:
|
||||||
decoded = datasette.unsign(raw, namespace="token")
|
decoded = datasette.unsign(raw, namespace="token")
|
||||||
except itsdangerous.BadSignature:
|
except itsdangerous.BadSignature:
|
||||||
raise TokenInvalid("Invalid token signature")
|
return None
|
||||||
|
|
||||||
if "t" not in decoded:
|
if "t" not in decoded:
|
||||||
raise TokenInvalid("Invalid token: no timestamp")
|
return None
|
||||||
created = decoded["t"]
|
created = decoded["t"]
|
||||||
if not isinstance(created, int):
|
if not isinstance(created, int):
|
||||||
raise TokenInvalid("Invalid token: invalid timestamp")
|
return None
|
||||||
|
|
||||||
duration = decoded.get("d")
|
duration = decoded.get("d")
|
||||||
if duration is not None and not isinstance(duration, int):
|
if duration is not None and not isinstance(duration, int):
|
||||||
raise TokenInvalid("Invalid token: invalid duration")
|
return None
|
||||||
|
|
||||||
if (duration is None and max_signed_tokens_ttl) or (
|
if (duration is None and max_signed_tokens_ttl) or (
|
||||||
duration is not None
|
duration is not None
|
||||||
|
|
@ -200,8 +178,9 @@ class SignedTokenHandler(TokenHandler):
|
||||||
):
|
):
|
||||||
duration = max_signed_tokens_ttl
|
duration = max_signed_tokens_ttl
|
||||||
|
|
||||||
if duration and time.time() - created > duration:
|
if duration:
|
||||||
raise TokenInvalid("Token has expired")
|
if time.time() - created > duration:
|
||||||
|
return None
|
||||||
|
|
||||||
actor = {"id": decoded["a"], "token": "dstok"}
|
actor = {"id": decoded["a"], "token": "dstok"}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,10 @@
|
||||||
import asyncio
|
import asyncio
|
||||||
import json
|
|
||||||
import time
|
|
||||||
import traceback
|
|
||||||
from contextlib import contextmanager
|
from contextlib import contextmanager
|
||||||
from contextvars import ContextVar
|
from contextvars import ContextVar
|
||||||
|
|
||||||
from markupsafe import escape
|
from markupsafe import escape
|
||||||
|
import time
|
||||||
|
import json
|
||||||
|
import traceback
|
||||||
|
|
||||||
tracers = {}
|
tracers = {}
|
||||||
|
|
||||||
|
|
@ -133,17 +132,17 @@ class AsgiTracer:
|
||||||
"num_traces": len(traces),
|
"num_traces": len(traces),
|
||||||
"traces": traces,
|
"traces": traces,
|
||||||
}
|
}
|
||||||
content_type = next(
|
try:
|
||||||
(
|
content_type = [
|
||||||
v.decode("utf8")
|
v.decode("utf8")
|
||||||
for k, v in response_headers
|
for k, v in response_headers
|
||||||
if k.lower() == b"content-type"
|
if k.lower() == b"content-type"
|
||||||
),
|
][0]
|
||||||
"",
|
except IndexError:
|
||||||
)
|
content_type = ""
|
||||||
if "text/html" in content_type and b"</body>" in accumulated_body:
|
if "text/html" in content_type and b"</body>" in accumulated_body:
|
||||||
extra = escape(json.dumps(trace_info, indent=2))
|
extra = escape(json.dumps(trace_info, indent=2))
|
||||||
extra_html = f"<pre>{extra}</pre></body>".encode()
|
extra_html = f"<pre>{extra}</pre></body>".encode("utf8")
|
||||||
accumulated_body = accumulated_body.replace(b"</body>", extra_html)
|
accumulated_body = accumulated_body.replace(b"</body>", extra_html)
|
||||||
elif "json" in content_type and accumulated_body.startswith(b"{"):
|
elif "json" in content_type and accumulated_body.startswith(b"{"):
|
||||||
data = json.loads(accumulated_body.decode("utf8"))
|
data = json.loads(accumulated_body.decode("utf8"))
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
|
from .utils import tilde_encode, path_with_format, PrefixedUrlString
|
||||||
import urllib
|
import urllib
|
||||||
|
|
||||||
from .utils import PrefixedUrlString, path_with_format, tilde_encode
|
|
||||||
|
|
||||||
|
|
||||||
class Urls:
|
class Urls:
|
||||||
def __init__(self, ds):
|
def __init__(self, ds):
|
||||||
|
|
@ -9,7 +8,8 @@ class Urls:
|
||||||
|
|
||||||
def path(self, path, format=None):
|
def path(self, path, format=None):
|
||||||
if not isinstance(path, PrefixedUrlString):
|
if not isinstance(path, PrefixedUrlString):
|
||||||
path = path.removeprefix("/")
|
if path.startswith("/"):
|
||||||
|
path = path[1:]
|
||||||
path = self.ds.setting("base_url") + path
|
path = self.ds.setting("base_url") + path
|
||||||
if format is not None:
|
if format is not None:
|
||||||
path = path_with_format(path=path, format=format)
|
path = path_with_format(path=path, format=format)
|
||||||
|
|
@ -56,7 +56,6 @@ class Urls:
|
||||||
return PrefixedUrlString(path)
|
return PrefixedUrlString(path)
|
||||||
|
|
||||||
def row_blob(self, database, table, row_path, column):
|
def row_blob(self, database, table, row_path, column):
|
||||||
return (
|
return self.table(database, table) + "/{}.blob?_blob_column={}".format(
|
||||||
self.table(database, table)
|
row_path, urllib.parse.quote_plus(column)
|
||||||
+ f"/{row_path}.blob?_blob_column={urllib.parse.quote_plus(column)}"
|
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -1,31 +1,28 @@
|
||||||
import asyncio
|
import asyncio
|
||||||
import base64
|
from contextlib import contextmanager
|
||||||
import binascii
|
import aiofiles
|
||||||
|
import click
|
||||||
|
from collections import OrderedDict, namedtuple, Counter
|
||||||
import copy
|
import copy
|
||||||
import dataclasses
|
import dataclasses
|
||||||
|
import base64
|
||||||
import hashlib
|
import hashlib
|
||||||
import inspect
|
import inspect
|
||||||
import json
|
import json
|
||||||
import os
|
|
||||||
import re
|
|
||||||
import secrets
|
|
||||||
import shlex
|
|
||||||
import shutil
|
|
||||||
import tempfile
|
|
||||||
import time
|
|
||||||
import types
|
|
||||||
import typing
|
|
||||||
import urllib
|
|
||||||
from collections import Counter, OrderedDict, namedtuple
|
|
||||||
from collections.abc import Iterable
|
|
||||||
from contextlib import contextmanager
|
|
||||||
|
|
||||||
import aiofiles
|
|
||||||
import click
|
|
||||||
import markupsafe
|
import markupsafe
|
||||||
import mergedeep
|
import mergedeep
|
||||||
|
import os
|
||||||
|
import re
|
||||||
|
import shlex
|
||||||
|
import tempfile
|
||||||
|
import typing
|
||||||
|
import time
|
||||||
|
import types
|
||||||
|
import secrets
|
||||||
|
import shutil
|
||||||
|
from typing import Iterable, List, Tuple
|
||||||
|
import urllib
|
||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
from .shutil_backport import copytree
|
from .shutil_backport import copytree
|
||||||
from .sqlite import sqlite3, supports_table_xinfo
|
from .sqlite import sqlite3, supports_table_xinfo
|
||||||
|
|
||||||
|
|
@ -38,7 +35,7 @@ if typing.TYPE_CHECKING:
|
||||||
class PaginatedResources:
|
class PaginatedResources:
|
||||||
"""Paginated results from allowed_resources query."""
|
"""Paginated results from allowed_resources query."""
|
||||||
|
|
||||||
resources: list["Resource"]
|
resources: List["Resource"]
|
||||||
next: str | None # Keyset token for next page (None if no more results)
|
next: str | None # Keyset token for next page (None if no more results)
|
||||||
_datasette: typing.Any = dataclasses.field(default=None, repr=False)
|
_datasette: typing.Any = dataclasses.field(default=None, repr=False)
|
||||||
_action: str = dataclasses.field(default=None, repr=False)
|
_action: str = dataclasses.field(default=None, repr=False)
|
||||||
|
|
@ -85,132 +82,22 @@ class PaginatedResources:
|
||||||
|
|
||||||
|
|
||||||
# From https://www.sqlite.org/lang_keywords.html
|
# From https://www.sqlite.org/lang_keywords.html
|
||||||
reserved_words = {
|
reserved_words = set(
|
||||||
"abort",
|
(
|
||||||
"action",
|
"abort action add after all alter analyze and as asc attach autoincrement "
|
||||||
"add",
|
"before begin between by cascade case cast check collate column commit "
|
||||||
"after",
|
"conflict constraint create cross current_date current_time "
|
||||||
"all",
|
"current_timestamp database default deferrable deferred delete desc detach "
|
||||||
"alter",
|
"distinct drop each else end escape except exclusive exists explain fail "
|
||||||
"analyze",
|
"for foreign from full glob group having if ignore immediate in index "
|
||||||
"and",
|
"indexed initially inner insert instead intersect into is isnull join key "
|
||||||
"as",
|
"left like limit match natural no not notnull null of offset on or order "
|
||||||
"asc",
|
"outer plan pragma primary query raise recursive references regexp reindex "
|
||||||
"attach",
|
"release rename replace restrict right rollback row savepoint select set "
|
||||||
"autoincrement",
|
"table temp temporary then to transaction trigger union unique update using "
|
||||||
"before",
|
"vacuum values view virtual when where with without"
|
||||||
"begin",
|
).split()
|
||||||
"between",
|
)
|
||||||
"by",
|
|
||||||
"cascade",
|
|
||||||
"case",
|
|
||||||
"cast",
|
|
||||||
"check",
|
|
||||||
"collate",
|
|
||||||
"column",
|
|
||||||
"commit",
|
|
||||||
"conflict",
|
|
||||||
"constraint",
|
|
||||||
"create",
|
|
||||||
"cross",
|
|
||||||
"current_date",
|
|
||||||
"current_time",
|
|
||||||
"current_timestamp",
|
|
||||||
"database",
|
|
||||||
"default",
|
|
||||||
"deferrable",
|
|
||||||
"deferred",
|
|
||||||
"delete",
|
|
||||||
"desc",
|
|
||||||
"detach",
|
|
||||||
"distinct",
|
|
||||||
"drop",
|
|
||||||
"each",
|
|
||||||
"else",
|
|
||||||
"end",
|
|
||||||
"escape",
|
|
||||||
"except",
|
|
||||||
"exclusive",
|
|
||||||
"exists",
|
|
||||||
"explain",
|
|
||||||
"fail",
|
|
||||||
"for",
|
|
||||||
"foreign",
|
|
||||||
"from",
|
|
||||||
"full",
|
|
||||||
"glob",
|
|
||||||
"group",
|
|
||||||
"having",
|
|
||||||
"if",
|
|
||||||
"ignore",
|
|
||||||
"immediate",
|
|
||||||
"in",
|
|
||||||
"index",
|
|
||||||
"indexed",
|
|
||||||
"initially",
|
|
||||||
"inner",
|
|
||||||
"insert",
|
|
||||||
"instead",
|
|
||||||
"intersect",
|
|
||||||
"into",
|
|
||||||
"is",
|
|
||||||
"isnull",
|
|
||||||
"join",
|
|
||||||
"key",
|
|
||||||
"left",
|
|
||||||
"like",
|
|
||||||
"limit",
|
|
||||||
"match",
|
|
||||||
"natural",
|
|
||||||
"no",
|
|
||||||
"not",
|
|
||||||
"notnull",
|
|
||||||
"null",
|
|
||||||
"of",
|
|
||||||
"offset",
|
|
||||||
"on",
|
|
||||||
"or",
|
|
||||||
"order",
|
|
||||||
"outer",
|
|
||||||
"plan",
|
|
||||||
"pragma",
|
|
||||||
"primary",
|
|
||||||
"query",
|
|
||||||
"raise",
|
|
||||||
"recursive",
|
|
||||||
"references",
|
|
||||||
"regexp",
|
|
||||||
"reindex",
|
|
||||||
"release",
|
|
||||||
"rename",
|
|
||||||
"replace",
|
|
||||||
"restrict",
|
|
||||||
"right",
|
|
||||||
"rollback",
|
|
||||||
"row",
|
|
||||||
"savepoint",
|
|
||||||
"select",
|
|
||||||
"set",
|
|
||||||
"table",
|
|
||||||
"temp",
|
|
||||||
"temporary",
|
|
||||||
"then",
|
|
||||||
"to",
|
|
||||||
"transaction",
|
|
||||||
"trigger",
|
|
||||||
"union",
|
|
||||||
"unique",
|
|
||||||
"update",
|
|
||||||
"using",
|
|
||||||
"vacuum",
|
|
||||||
"values",
|
|
||||||
"view",
|
|
||||||
"virtual",
|
|
||||||
"when",
|
|
||||||
"where",
|
|
||||||
"with",
|
|
||||||
"without",
|
|
||||||
}
|
|
||||||
|
|
||||||
APT_GET_DOCKERFILE_EXTRAS = r"""
|
APT_GET_DOCKERFILE_EXTRAS = r"""
|
||||||
RUN apt-get update && \
|
RUN apt-get update && \
|
||||||
|
|
@ -270,7 +157,7 @@ functions_marked_as_documented = []
|
||||||
|
|
||||||
def documented(fn=None, *, label=None):
|
def documented(fn=None, *, label=None):
|
||||||
def decorate(fn):
|
def decorate(fn):
|
||||||
fn._datasette_docs_label = label or f"internals_utils_{fn.__name__}"
|
fn._datasette_docs_label = label or "internals_utils_{}".format(fn.__name__)
|
||||||
functions_marked_as_documented.append(fn)
|
functions_marked_as_documented.append(fn)
|
||||||
return fn
|
return fn
|
||||||
|
|
||||||
|
|
@ -337,35 +224,17 @@ def compound_keys_after_sql(pks, start_index=0):
|
||||||
return "({})".format("\n or\n".join(or_clauses))
|
return "({})".format("\n or\n".join(or_clauses))
|
||||||
|
|
||||||
|
|
||||||
@documented
|
|
||||||
class CustomJSONEncoder(json.JSONEncoder):
|
class CustomJSONEncoder(json.JSONEncoder):
|
||||||
"""
|
|
||||||
The CustomJSONEncoder class handles serialization for objects commonly used by Datasette,
|
|
||||||
including SQLite cursors and binary blobs. Datasette uses it internally to serve .json endpoints,
|
|
||||||
and plugins that return JSON can use it to match Datasette's own handling.
|
|
||||||
|
|
||||||
Built-in types (text, numbers, lists, etc) are encoded the same as Python's built-in ``json`` module.
|
|
||||||
|
|
||||||
- ``sqlite3.Row`` becomes a tuple
|
|
||||||
- ``sqlite3.Cursor`` becomes a list
|
|
||||||
|
|
||||||
Binary blobs are encoded as an object, with the actual data base64-encoded,
|
|
||||||
like so: ::
|
|
||||||
|
|
||||||
{
|
|
||||||
"$base64": True,
|
|
||||||
"encoded": ...,
|
|
||||||
}
|
|
||||||
|
|
||||||
Example: https://latest.datasette.io/fixtures/binary_data.json
|
|
||||||
"""
|
|
||||||
|
|
||||||
def default(self, obj):
|
def default(self, obj):
|
||||||
if isinstance(obj, sqlite3.Row):
|
if isinstance(obj, sqlite3.Row):
|
||||||
return tuple(obj)
|
return tuple(obj)
|
||||||
if isinstance(obj, sqlite3.Cursor):
|
if isinstance(obj, sqlite3.Cursor):
|
||||||
return list(obj)
|
return list(obj)
|
||||||
if isinstance(obj, bytes):
|
if isinstance(obj, bytes):
|
||||||
|
# Does it encode to utf8?
|
||||||
|
try:
|
||||||
|
return obj.decode("utf8")
|
||||||
|
except UnicodeDecodeError:
|
||||||
return {
|
return {
|
||||||
"$base64": True,
|
"$base64": True,
|
||||||
"encoded": base64.b64encode(obj).decode("latin1"),
|
"encoded": base64.b64encode(obj).decode("latin1"),
|
||||||
|
|
@ -373,35 +242,6 @@ class CustomJSONEncoder(json.JSONEncoder):
|
||||||
return json.JSONEncoder.default(self, obj)
|
return json.JSONEncoder.default(self, obj)
|
||||||
|
|
||||||
|
|
||||||
class WriteJsonValueError(ValueError):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
def decode_write_json_cell(value):
|
|
||||||
if not isinstance(value, dict):
|
|
||||||
return value
|
|
||||||
keys = set(value.keys())
|
|
||||||
if keys == {"$raw"}:
|
|
||||||
return value["$raw"]
|
|
||||||
if keys == {"$base64", "encoded"} and value.get("$base64") is True:
|
|
||||||
encoded = value["encoded"]
|
|
||||||
if not isinstance(encoded, str):
|
|
||||||
raise WriteJsonValueError("$base64 encoded value must be a string")
|
|
||||||
try:
|
|
||||||
return base64.b64decode(encoded, validate=True)
|
|
||||||
except binascii.Error as ex:
|
|
||||||
raise WriteJsonValueError("Invalid $base64 encoded value") from ex
|
|
||||||
return value
|
|
||||||
|
|
||||||
|
|
||||||
def decode_write_json_row(row):
|
|
||||||
return {key: decode_write_json_cell(value) for key, value in row.items()}
|
|
||||||
|
|
||||||
|
|
||||||
def decode_write_json_rows(rows):
|
|
||||||
return [decode_write_json_row(row) for row in rows]
|
|
||||||
|
|
||||||
|
|
||||||
@contextmanager
|
@contextmanager
|
||||||
def sqlite_timelimit(conn, ms):
|
def sqlite_timelimit(conn, ms):
|
||||||
deadline = time.perf_counter() + (ms / 1000)
|
deadline = time.perf_counter() + (ms / 1000)
|
||||||
|
|
@ -472,7 +312,7 @@ disallawed_sql_res = [
|
||||||
(
|
(
|
||||||
re.compile(f"pragma(?!_({'|'.join(allowed_pragmas)}))"),
|
re.compile(f"pragma(?!_({'|'.join(allowed_pragmas)}))"),
|
||||||
"Statement contained a disallowed PRAGMA. Allowed pragma functions are {}".format(
|
"Statement contained a disallowed PRAGMA. Allowed pragma functions are {}".format(
|
||||||
", ".join(f"pragma_{pragma}()" for pragma in allowed_pragmas)
|
", ".join("pragma_{}()".format(pragma) for pragma in allowed_pragmas)
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
]
|
]
|
||||||
|
|
@ -568,9 +408,14 @@ def escape_css_string(s):
|
||||||
|
|
||||||
|
|
||||||
def escape_sqlite(s):
|
def escape_sqlite(s):
|
||||||
if _boring_keyword_re.fullmatch(s) and (s.lower() not in reserved_words):
|
if _boring_keyword_re.match(s) and (s.lower() not in reserved_words):
|
||||||
return s
|
return s
|
||||||
|
elif "]" in s:
|
||||||
|
# SQLite does not support escaping ] inside [bracket] quoting, so fall
|
||||||
|
# back to double-quote quoting (doubling any embedded ") - #2677
|
||||||
return '"{}"'.format(s.replace('"', '""'))
|
return '"{}"'.format(s.replace('"', '""'))
|
||||||
|
else:
|
||||||
|
return f"[{s}]"
|
||||||
|
|
||||||
|
|
||||||
def make_dockerfile(
|
def make_dockerfile(
|
||||||
|
|
@ -646,7 +491,10 @@ CMD {cmd}""".format(
|
||||||
else ""
|
else ""
|
||||||
),
|
),
|
||||||
environment_variables="\n".join(
|
environment_variables="\n".join(
|
||||||
[f"ENV {key} '{value}'" for key, value in environment_variables.items()]
|
[
|
||||||
|
"ENV {} '{}'".format(key, value)
|
||||||
|
for key, value in environment_variables.items()
|
||||||
|
]
|
||||||
),
|
),
|
||||||
install_from=" ".join(install),
|
install_from=" ".join(install),
|
||||||
files=" ".join(files),
|
files=" ".join(files),
|
||||||
|
|
@ -745,11 +593,11 @@ def detect_primary_keys(conn, table):
|
||||||
|
|
||||||
|
|
||||||
def get_outbound_foreign_keys(conn, table):
|
def get_outbound_foreign_keys(conn, table):
|
||||||
infos = conn.execute(f"PRAGMA foreign_key_list({escape_sqlite(table)})").fetchall()
|
infos = conn.execute(f"PRAGMA foreign_key_list([{table}])").fetchall()
|
||||||
fks = []
|
fks = []
|
||||||
for info in infos:
|
for info in infos:
|
||||||
if info is not None:
|
if info is not None:
|
||||||
id, seq, table_name, from_, to_, _on_update, _on_delete, _match = info
|
id, seq, table_name, from_, to_, on_update, on_delete, match = info
|
||||||
fks.append(
|
fks.append(
|
||||||
{
|
{
|
||||||
"column": from_,
|
"column": from_,
|
||||||
|
|
@ -820,8 +668,7 @@ def detect_spatialite(conn):
|
||||||
|
|
||||||
def detect_fts(conn, table):
|
def detect_fts(conn, table):
|
||||||
"""Detect if table has a corresponding FTS virtual table and return it"""
|
"""Detect if table has a corresponding FTS virtual table and return it"""
|
||||||
sql, params = detect_fts_sql(table)
|
rows = conn.execute(detect_fts_sql(table)).fetchall()
|
||||||
rows = conn.execute(sql, params).fetchall()
|
|
||||||
if len(rows) == 0:
|
if len(rows) == 0:
|
||||||
return None
|
return None
|
||||||
else:
|
else:
|
||||||
|
|
@ -829,26 +676,18 @@ def detect_fts(conn, table):
|
||||||
|
|
||||||
|
|
||||||
def detect_fts_sql(table):
|
def detect_fts_sql(table):
|
||||||
escaped_table = table.replace("\\", "\\\\").replace("%", "\\%").replace("_", "\\_")
|
return r"""
|
||||||
return (
|
|
||||||
r"""
|
|
||||||
select name from sqlite_master
|
select name from sqlite_master
|
||||||
where rootpage = 0
|
where rootpage = 0
|
||||||
and (
|
and (
|
||||||
sql like :fts_double_quoted escape char(92)
|
sql like '%VIRTUAL TABLE%USING FTS%content="{table}"%'
|
||||||
or sql like :fts_bracket_quoted escape char(92)
|
or sql like '%VIRTUAL TABLE%USING FTS%content=[{table}]%'
|
||||||
or (
|
or (
|
||||||
tbl_name = :table
|
tbl_name = "{table}"
|
||||||
and sql like '%VIRTUAL TABLE%USING FTS%'
|
and sql like '%VIRTUAL TABLE%USING FTS%'
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
""",
|
""".format(table=table.replace("'", "''"))
|
||||||
{
|
|
||||||
"fts_double_quoted": f'%VIRTUAL TABLE%USING FTS%content="{escaped_table}"%',
|
|
||||||
"fts_bracket_quoted": f"%VIRTUAL TABLE%USING FTS%content=[{escaped_table}]%",
|
|
||||||
"table": table,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def detect_json1(conn=None):
|
def detect_json1(conn=None):
|
||||||
|
|
@ -859,7 +698,7 @@ def detect_json1(conn=None):
|
||||||
try:
|
try:
|
||||||
conn.execute("SELECT json('{}')")
|
conn.execute("SELECT json('{}')")
|
||||||
return True
|
return True
|
||||||
except sqlite3.Error:
|
except Exception:
|
||||||
return False
|
return False
|
||||||
finally:
|
finally:
|
||||||
if close_conn:
|
if close_conn:
|
||||||
|
|
@ -939,7 +778,9 @@ def is_url(value):
|
||||||
if not value.startswith("http://") and not value.startswith("https://"):
|
if not value.startswith("http://") and not value.startswith("https://"):
|
||||||
return False
|
return False
|
||||||
# Any whitespace at all is invalid
|
# Any whitespace at all is invalid
|
||||||
return not whitespace_re.search(value)
|
if whitespace_re.search(value):
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
css_class_re = re.compile(r"^[a-zA-Z]+[_a-zA-Z0-9-]*$")
|
css_class_re = re.compile(r"^[a-zA-Z]+[_a-zA-Z0-9-]*$")
|
||||||
|
|
@ -992,9 +833,7 @@ def module_from_path(path, name):
|
||||||
mod.__file__ = path
|
mod.__file__ = path
|
||||||
with open(path, "r") as file:
|
with open(path, "r") as file:
|
||||||
code = compile(file.read(), path, "exec", dont_inherit=True)
|
code = compile(file.read(), path, "exec", dont_inherit=True)
|
||||||
# Executing the file is the whole point - this is how --plugins-dir loads
|
exec(code, mod.__dict__)
|
||||||
# plugins and how metadata/config .py files are evaluated
|
|
||||||
exec(code, mod.__dict__) # noqa: S102
|
|
||||||
return mod
|
return mod
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -1151,7 +990,9 @@ def escape_fts(query):
|
||||||
query += '"'
|
query += '"'
|
||||||
bits = _escape_fts_re.split(query)
|
bits = _escape_fts_re.split(query)
|
||||||
bits = [b for b in bits if b and b != '""']
|
bits = [b for b in bits if b and b != '""']
|
||||||
return " ".join(f'"{bit}"' if not bit.startswith('"') else bit for bit in bits)
|
return " ".join(
|
||||||
|
'"{}"'.format(bit) if not bit.startswith('"') else bit for bit in bits
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class MultiParams:
|
class MultiParams:
|
||||||
|
|
@ -1163,7 +1004,7 @@ class MultiParams:
|
||||||
data[key], (list, tuple)
|
data[key], (list, tuple)
|
||||||
), "dictionary data should be a dictionary of key => [list]"
|
), "dictionary data should be a dictionary of key => [list]"
|
||||||
self._data = data
|
self._data = data
|
||||||
elif isinstance(data, (list, tuple)):
|
elif isinstance(data, list) or isinstance(data, tuple):
|
||||||
new_data = {}
|
new_data = {}
|
||||||
for item in data:
|
for item in data:
|
||||||
assert (
|
assert (
|
||||||
|
|
@ -1253,7 +1094,9 @@ def _gather_arguments(fn, kwargs):
|
||||||
for parameter in parameters:
|
for parameter in parameters:
|
||||||
if parameter not in kwargs:
|
if parameter not in kwargs:
|
||||||
raise TypeError(
|
raise TypeError(
|
||||||
f"{fn} requires parameters {tuple(parameters)}, missing: {set(parameters) - set(kwargs.keys())}"
|
"{} requires parameters {}, missing: {}".format(
|
||||||
|
fn, tuple(parameters), set(parameters) - set(kwargs.keys())
|
||||||
|
)
|
||||||
)
|
)
|
||||||
call_with.append(kwargs[parameter])
|
call_with.append(kwargs[parameter])
|
||||||
return call_with
|
return call_with
|
||||||
|
|
@ -1322,9 +1165,9 @@ def resolve_env_secrets(config, environ):
|
||||||
"""Create copy that recursively replaces {"$env": "NAME"} with values from environ"""
|
"""Create copy that recursively replaces {"$env": "NAME"} with values from environ"""
|
||||||
if isinstance(config, dict):
|
if isinstance(config, dict):
|
||||||
if list(config.keys()) == ["$env"]:
|
if list(config.keys()) == ["$env"]:
|
||||||
return environ.get(next(iter(config.values())))
|
return environ.get(list(config.values())[0])
|
||||||
elif list(config.keys()) == ["$file"]:
|
elif list(config.keys()) == ["$file"]:
|
||||||
with open(next(iter(config.values()))) as fp:
|
with open(list(config.values())[0]) as fp:
|
||||||
return fp.read()
|
return fp.read()
|
||||||
else:
|
else:
|
||||||
return {
|
return {
|
||||||
|
|
@ -1402,38 +1245,29 @@ class StartupError(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
# Comments and string literals, matched in a single pass so that whichever
|
_single_line_comment_re = re.compile(r"--.*")
|
||||||
# construct starts first "wins" - this ensures a comment marker inside a string
|
_multi_line_comment_re = re.compile(r"/\*.*?\*/", re.DOTALL)
|
||||||
# literal (or a quote inside a comment) does not confuse the parameter scan.
|
_single_quote_re = re.compile(r"'(?:''|[^'])*'")
|
||||||
_comments_and_strings_re = re.compile(
|
_double_quote_re = re.compile(r'"(?:\"\"|[^"])*"')
|
||||||
r"""
|
|
||||||
--[^\n]* # single line comment
|
|
||||||
| /\*.*?(?:\*/|\Z) # multi line comment, possibly to end-of-input
|
|
||||||
| '(?:''|[^'])*' # single quoted string ('' escapes a quote)
|
|
||||||
| "(?:""|[^"])*" # double quoted identifier ("" escapes a quote)
|
|
||||||
| \[(?:[^\]])*\] # square-bracket quoted identifier
|
|
||||||
| `(?:``|[^`])*` # backtick quoted identifier
|
|
||||||
""",
|
|
||||||
re.DOTALL | re.VERBOSE,
|
|
||||||
)
|
|
||||||
_named_param_re = re.compile(r":(\w+)")
|
_named_param_re = re.compile(r":(\w+)")
|
||||||
|
|
||||||
|
|
||||||
@documented
|
@documented
|
||||||
def named_parameters(sql: str) -> list[str]:
|
def named_parameters(sql: str) -> List[str]:
|
||||||
"""
|
"""
|
||||||
Given a SQL statement, return a list of named parameters that are used in the statement
|
Given a SQL statement, return a list of named parameters that are used in the statement
|
||||||
|
|
||||||
e.g. for ``select * from foo where id=:id`` this would return ``["id"]``
|
e.g. for ``select * from foo where id=:id`` this would return ``["id"]``
|
||||||
"""
|
"""
|
||||||
# Strip comments and string literals first so that any ":name" sequences
|
sql = _single_line_comment_re.sub("", sql)
|
||||||
# inside them are not mistaken for named parameters
|
sql = _multi_line_comment_re.sub("", sql)
|
||||||
sql = _comments_and_strings_re.sub("", sql)
|
sql = _single_quote_re.sub("", sql)
|
||||||
|
sql = _double_quote_re.sub("", sql)
|
||||||
# Extract parameters from what is left
|
# Extract parameters from what is left
|
||||||
return _named_param_re.findall(sql)
|
return _named_param_re.findall(sql)
|
||||||
|
|
||||||
|
|
||||||
async def derive_named_parameters(db: "Database", sql: str) -> list[str]:
|
async def derive_named_parameters(db: "Database", sql: str) -> List[str]:
|
||||||
"""
|
"""
|
||||||
This undocumented but stable method exists for backwards compatibility
|
This undocumented but stable method exists for backwards compatibility
|
||||||
with plugins that were using it before it switched to named_parameters()
|
with plugins that were using it before it switched to named_parameters()
|
||||||
|
|
@ -1441,54 +1275,6 @@ async def derive_named_parameters(db: "Database", sql: str) -> list[str]:
|
||||||
return named_parameters(sql)
|
return named_parameters(sql)
|
||||||
|
|
||||||
|
|
||||||
def parse_size_limit(value, default, maximum, name="_size"):
|
|
||||||
"""
|
|
||||||
Parse a page-size parameter using the same semantics as the table
|
|
||||||
view's ?_size=: blank means default, "max" means maximum, integers
|
|
||||||
must be 0 or greater and no larger than maximum. Raises ValueError
|
|
||||||
with a message suitable for a 400 response.
|
|
||||||
"""
|
|
||||||
if value in (None, ""):
|
|
||||||
return default
|
|
||||||
if value == "max":
|
|
||||||
return maximum
|
|
||||||
try:
|
|
||||||
size = int(value)
|
|
||||||
if size < 0:
|
|
||||||
raise ValueError
|
|
||||||
except ValueError:
|
|
||||||
raise ValueError(f"{name} must be a positive integer")
|
|
||||||
if size > maximum:
|
|
||||||
raise ValueError(f"{name} must be <= {maximum}")
|
|
||||||
return size
|
|
||||||
|
|
||||||
|
|
||||||
UNSTABLE_API_MESSAGE = (
|
|
||||||
"This API is not part of Datasette's stable interface and may change at any time"
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def error_body(messages, status):
|
|
||||||
"""
|
|
||||||
The canonical JSON error body used by every Datasette JSON error response:
|
|
||||||
|
|
||||||
{"ok": False, "error": "...", "errors": ["...", ...], "status": 400}
|
|
||||||
|
|
||||||
"error" is all of the messages joined with "; ", "errors" is the full
|
|
||||||
list, "status" matches the HTTP status code. Callers may add extra
|
|
||||||
context keys to the returned dictionary but must not remove these four.
|
|
||||||
"""
|
|
||||||
if isinstance(messages, str):
|
|
||||||
messages = [messages]
|
|
||||||
messages = [str(message) for message in messages]
|
|
||||||
return {
|
|
||||||
"ok": False,
|
|
||||||
"error": "; ".join(messages),
|
|
||||||
"errors": messages,
|
|
||||||
"status": status,
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
def add_cors_headers(headers):
|
def add_cors_headers(headers):
|
||||||
headers["Access-Control-Allow-Origin"] = "*"
|
headers["Access-Control-Allow-Origin"] = "*"
|
||||||
headers["Access-Control-Allow-Headers"] = "Authorization, Content-Type"
|
headers["Access-Control-Allow-Headers"] = "Authorization, Content-Type"
|
||||||
|
|
@ -1517,7 +1303,7 @@ class TildeEncoder(dict):
|
||||||
elif b == _space:
|
elif b == _space:
|
||||||
res = "+"
|
res = "+"
|
||||||
else:
|
else:
|
||||||
res = f"~{b:02X}"
|
res = "~{:02X}".format(b)
|
||||||
self[b] = res
|
self[b] = res
|
||||||
return res
|
return res
|
||||||
|
|
||||||
|
|
@ -1566,13 +1352,7 @@ async def row_sql_params_pks(db, table, pk_values):
|
||||||
if use_rowid:
|
if use_rowid:
|
||||||
select = "rowid, *"
|
select = "rowid, *"
|
||||||
pks = ["rowid"]
|
pks = ["rowid"]
|
||||||
wheres = []
|
wheres = [f'"{pk}"=:p{i}' for i, pk in enumerate(pks)]
|
||||||
for i, pk in enumerate(pks):
|
|
||||||
escaped_pk = escape_sqlite(pk)
|
|
||||||
# Preserve the historic always-quoted SQL exposed by _extra=query
|
|
||||||
if escaped_pk == pk:
|
|
||||||
escaped_pk = f'"{pk}"'
|
|
||||||
wheres.append(f"{escaped_pk}=:p{i}")
|
|
||||||
sql = f"select {select} from {escape_sqlite(table)} where {' AND '.join(wheres)}"
|
sql = f"select {select} from {escape_sqlite(table)} where {' AND '.join(wheres)}"
|
||||||
params = {}
|
params = {}
|
||||||
for i, pk_value in enumerate(pk_values):
|
for i, pk_value in enumerate(pk_values):
|
||||||
|
|
@ -1618,7 +1398,7 @@ def _combine(base: dict, update: dict) -> dict:
|
||||||
return base
|
return base
|
||||||
|
|
||||||
|
|
||||||
def pairs_to_nested_config(pairs: list[tuple[str, typing.Any]]) -> dict:
|
def pairs_to_nested_config(pairs: typing.List[typing.Tuple[str, typing.Any]]) -> dict:
|
||||||
"""
|
"""
|
||||||
Parse a list of key-value pairs into a nested dictionary.
|
Parse a list of key-value pairs into a nested dictionary.
|
||||||
"""
|
"""
|
||||||
|
|
@ -1633,7 +1413,7 @@ def make_slot_function(name, datasette, request, **kwargs):
|
||||||
from datasette.plugins import pm
|
from datasette.plugins import pm
|
||||||
|
|
||||||
method = getattr(pm.hook, name, None)
|
method = getattr(pm.hook, name, None)
|
||||||
assert method is not None, f"No hook found for {name}"
|
assert method is not None, "No hook found for {}".format(name)
|
||||||
|
|
||||||
async def inner():
|
async def inner():
|
||||||
html_bits = []
|
html_bits = []
|
||||||
|
|
@ -1657,7 +1437,7 @@ def prune_empty_dicts(d: dict):
|
||||||
d.pop(key, None)
|
d.pop(key, None)
|
||||||
|
|
||||||
|
|
||||||
def move_plugins_and_allow(source: dict, destination: dict) -> tuple[dict, dict]:
|
def move_plugins_and_allow(source: dict, destination: dict) -> Tuple[dict, dict]:
|
||||||
"""
|
"""
|
||||||
Move 'plugins' and 'allow' keys from source to destination dictionary. Creates
|
Move 'plugins' and 'allow' keys from source to destination dictionary. Creates
|
||||||
hierarchy in destination if needed. After moving, recursively remove any keys
|
hierarchy in destination if needed. After moving, recursively remove any keys
|
||||||
|
|
@ -1744,7 +1524,7 @@ def redact_keys(original: dict, key_patterns: Iterable) -> dict:
|
||||||
return {
|
return {
|
||||||
k: (
|
k: (
|
||||||
redact(v)
|
redact(v)
|
||||||
if not any(pattern in k.casefold() for pattern in key_patterns)
|
if not any(pattern in k for pattern in key_patterns)
|
||||||
else "***"
|
else "***"
|
||||||
)
|
)
|
||||||
for k, v in data.items()
|
for k, v in data.items()
|
||||||
|
|
@ -1768,17 +1548,6 @@ def md5_not_usedforsecurity(s):
|
||||||
_etag_cache = {}
|
_etag_cache = {}
|
||||||
|
|
||||||
|
|
||||||
def sha256_file(filepath, chunk_size=4096):
|
|
||||||
hasher = hashlib.sha256()
|
|
||||||
with open(filepath, "rb") as fp:
|
|
||||||
while True:
|
|
||||||
chunk = fp.read(chunk_size)
|
|
||||||
if not chunk:
|
|
||||||
break
|
|
||||||
hasher.update(chunk)
|
|
||||||
return hasher.hexdigest()
|
|
||||||
|
|
||||||
|
|
||||||
async def calculate_etag(filepath, chunk_size=4096):
|
async def calculate_etag(filepath, chunk_size=4096):
|
||||||
if filepath in _etag_cache:
|
if filepath in _etag_cache:
|
||||||
return _etag_cache[filepath]
|
return _etag_cache[filepath]
|
||||||
|
|
|
||||||
|
|
@ -29,15 +29,6 @@ from datasette.utils.permissions import gather_permission_sql_from_hooks
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from datasette.app import Datasette
|
from datasette.app import Datasette
|
||||||
from datasette.permissions import Action
|
|
||||||
|
|
||||||
|
|
||||||
def _child_collation(action: "Action") -> str:
|
|
||||||
"""Match resource identity without changing the spelling returned by SQL."""
|
|
||||||
resource_class = action.resource_class
|
|
||||||
if resource_class is not None and resource_class.case_insensitive_child:
|
|
||||||
return "NOCASE"
|
|
||||||
return "BINARY"
|
|
||||||
|
|
||||||
|
|
||||||
async def build_allowed_resources_sql(
|
async def build_allowed_resources_sql(
|
||||||
|
|
@ -158,7 +149,6 @@ async def _build_single_action_sql(
|
||||||
raise ValueError(f"Unknown action: {action}")
|
raise ValueError(f"Unknown action: {action}")
|
||||||
|
|
||||||
# Get base resources SQL from the resource class
|
# Get base resources SQL from the resource class
|
||||||
child_collation = _child_collation(action_obj)
|
|
||||||
base_resources_sql = await action_obj.resource_class.resources_sql(
|
base_resources_sql = await action_obj.resource_class.resources_sql(
|
||||||
datasette, actor=actor
|
datasette, actor=actor
|
||||||
)
|
)
|
||||||
|
|
@ -195,7 +185,7 @@ async def _build_single_action_sql(
|
||||||
if permission_sql.sql is None:
|
if permission_sql.sql is None:
|
||||||
continue
|
continue
|
||||||
rule_sqls.append(f"""
|
rule_sqls.append(f"""
|
||||||
SELECT parent, child COLLATE {child_collation} AS child, allow, reason, '{permission_sql.source}' AS source_plugin FROM (
|
SELECT parent, child, allow, reason, '{permission_sql.source}' AS source_plugin FROM (
|
||||||
{permission_sql.sql}
|
{permission_sql.sql}
|
||||||
)
|
)
|
||||||
""".strip())
|
""".strip())
|
||||||
|
|
@ -262,62 +252,88 @@ async def _build_single_action_sql(
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
|
|
||||||
# Continue with the cascading logic.
|
# Continue with the cascading logic
|
||||||
# Aggregate the RULES by cascade level (small), rather than grouping
|
|
||||||
# base x rules (which scales with the number of resources).
|
|
||||||
def _agg(select_key, where, group_by):
|
|
||||||
parts = [
|
|
||||||
f" SELECT {select_key}",
|
|
||||||
" MAX(CASE WHEN allow = 0 THEN 1 ELSE 0 END) AS any_deny,",
|
|
||||||
" MAX(CASE WHEN allow = 1 THEN 1 ELSE 0 END) AS any_allow,",
|
|
||||||
" json_group_array(CASE WHEN allow = 0 THEN source_plugin || ': ' || reason END) AS deny_reasons,",
|
|
||||||
" json_group_array(CASE WHEN allow = 1 THEN source_plugin || ': ' || reason END) AS allow_reasons",
|
|
||||||
f" FROM all_rules WHERE {where}",
|
|
||||||
]
|
|
||||||
if group_by:
|
|
||||||
parts.append(f" GROUP BY {group_by}")
|
|
||||||
return parts
|
|
||||||
|
|
||||||
query_parts.extend(
|
query_parts.extend(
|
||||||
["child_agg AS ("]
|
[
|
||||||
+ _agg(
|
"child_lvl AS (",
|
||||||
"parent, child,",
|
" SELECT b.parent, b.child,",
|
||||||
"parent IS NOT NULL AND child IS NOT NULL",
|
" MAX(CASE WHEN ar.allow = 0 THEN 1 ELSE 0 END) AS any_deny,",
|
||||||
"parent, child",
|
" MAX(CASE WHEN ar.allow = 1 THEN 1 ELSE 0 END) AS any_allow,",
|
||||||
)
|
" json_group_array(CASE WHEN ar.allow = 0 THEN ar.source_plugin || ': ' || ar.reason END) AS deny_reasons,",
|
||||||
+ ["),", "parent_agg AS ("]
|
" json_group_array(CASE WHEN ar.allow = 1 THEN ar.source_plugin || ': ' || ar.reason END) AS allow_reasons",
|
||||||
+ _agg("parent,", "parent IS NOT NULL AND child IS NULL", "parent")
|
" FROM base b",
|
||||||
+ ["),", "global_agg AS ("]
|
" LEFT JOIN all_rules ar ON ar.parent = b.parent AND ar.child = b.child",
|
||||||
+ _agg("", "parent IS NULL AND child IS NULL", None)
|
" GROUP BY b.parent, b.child",
|
||||||
+ ["),"]
|
"),",
|
||||||
|
"parent_lvl AS (",
|
||||||
|
" SELECT b.parent, b.child,",
|
||||||
|
" MAX(CASE WHEN ar.allow = 0 THEN 1 ELSE 0 END) AS any_deny,",
|
||||||
|
" MAX(CASE WHEN ar.allow = 1 THEN 1 ELSE 0 END) AS any_allow,",
|
||||||
|
" json_group_array(CASE WHEN ar.allow = 0 THEN ar.source_plugin || ': ' || ar.reason END) AS deny_reasons,",
|
||||||
|
" json_group_array(CASE WHEN ar.allow = 1 THEN ar.source_plugin || ': ' || ar.reason END) AS allow_reasons",
|
||||||
|
" FROM base b",
|
||||||
|
" LEFT JOIN all_rules ar ON ar.parent = b.parent AND ar.child IS NULL",
|
||||||
|
" GROUP BY b.parent, b.child",
|
||||||
|
"),",
|
||||||
|
"global_lvl AS (",
|
||||||
|
" SELECT b.parent, b.child,",
|
||||||
|
" MAX(CASE WHEN ar.allow = 0 THEN 1 ELSE 0 END) AS any_deny,",
|
||||||
|
" MAX(CASE WHEN ar.allow = 1 THEN 1 ELSE 0 END) AS any_allow,",
|
||||||
|
" json_group_array(CASE WHEN ar.allow = 0 THEN ar.source_plugin || ': ' || ar.reason END) AS deny_reasons,",
|
||||||
|
" json_group_array(CASE WHEN ar.allow = 1 THEN ar.source_plugin || ': ' || ar.reason END) AS allow_reasons",
|
||||||
|
" FROM base b",
|
||||||
|
" LEFT JOIN all_rules ar ON ar.parent IS NULL AND ar.child IS NULL",
|
||||||
|
" GROUP BY b.parent, b.child",
|
||||||
|
"),",
|
||||||
|
]
|
||||||
)
|
)
|
||||||
|
|
||||||
# Add anonymous decision logic if needed
|
# Add anonymous decision logic if needed
|
||||||
if include_is_private:
|
if include_is_private:
|
||||||
|
|
||||||
def _anon_agg(select_key, where, group_by):
|
|
||||||
parts = [
|
|
||||||
f" SELECT {select_key}",
|
|
||||||
" MAX(CASE WHEN allow = 0 THEN 1 ELSE 0 END) AS any_deny,",
|
|
||||||
" MAX(CASE WHEN allow = 1 THEN 1 ELSE 0 END) AS any_allow",
|
|
||||||
f" FROM anon_rules WHERE {where}",
|
|
||||||
]
|
|
||||||
if group_by:
|
|
||||||
parts.append(f" GROUP BY {group_by}")
|
|
||||||
return parts
|
|
||||||
|
|
||||||
query_parts.extend(
|
query_parts.extend(
|
||||||
["anon_child_agg AS ("]
|
[
|
||||||
+ _anon_agg(
|
"anon_child_lvl AS (",
|
||||||
f"parent, child COLLATE {child_collation} AS child,",
|
" SELECT b.parent, b.child,",
|
||||||
"parent IS NOT NULL AND child IS NOT NULL",
|
" MAX(CASE WHEN ar.allow = 0 THEN 1 ELSE 0 END) AS any_deny,",
|
||||||
f"parent, child COLLATE {child_collation}",
|
" MAX(CASE WHEN ar.allow = 1 THEN 1 ELSE 0 END) AS any_allow",
|
||||||
)
|
" FROM base b",
|
||||||
+ ["),", "anon_parent_agg AS ("]
|
" LEFT JOIN anon_rules ar ON ar.parent = b.parent AND ar.child = b.child",
|
||||||
+ _anon_agg("parent,", "parent IS NOT NULL AND child IS NULL", "parent")
|
" GROUP BY b.parent, b.child",
|
||||||
+ ["),", "anon_global_agg AS ("]
|
"),",
|
||||||
+ _anon_agg("", "parent IS NULL AND child IS NULL", None)
|
"anon_parent_lvl AS (",
|
||||||
+ ["),"]
|
" SELECT b.parent, b.child,",
|
||||||
|
" MAX(CASE WHEN ar.allow = 0 THEN 1 ELSE 0 END) AS any_deny,",
|
||||||
|
" MAX(CASE WHEN ar.allow = 1 THEN 1 ELSE 0 END) AS any_allow",
|
||||||
|
" FROM base b",
|
||||||
|
" LEFT JOIN anon_rules ar ON ar.parent = b.parent AND ar.child IS NULL",
|
||||||
|
" GROUP BY b.parent, b.child",
|
||||||
|
"),",
|
||||||
|
"anon_global_lvl AS (",
|
||||||
|
" SELECT b.parent, b.child,",
|
||||||
|
" MAX(CASE WHEN ar.allow = 0 THEN 1 ELSE 0 END) AS any_deny,",
|
||||||
|
" MAX(CASE WHEN ar.allow = 1 THEN 1 ELSE 0 END) AS any_allow",
|
||||||
|
" FROM base b",
|
||||||
|
" LEFT JOIN anon_rules ar ON ar.parent IS NULL AND ar.child IS NULL",
|
||||||
|
" GROUP BY b.parent, b.child",
|
||||||
|
"),",
|
||||||
|
"anon_decisions AS (",
|
||||||
|
" SELECT",
|
||||||
|
" b.parent, b.child,",
|
||||||
|
" CASE",
|
||||||
|
" WHEN acl.any_deny = 1 THEN 0",
|
||||||
|
" WHEN acl.any_allow = 1 THEN 1",
|
||||||
|
" WHEN apl.any_deny = 1 THEN 0",
|
||||||
|
" WHEN apl.any_allow = 1 THEN 1",
|
||||||
|
" WHEN agl.any_deny = 1 THEN 0",
|
||||||
|
" WHEN agl.any_allow = 1 THEN 1",
|
||||||
|
" ELSE 0",
|
||||||
|
" END AS anon_is_allowed",
|
||||||
|
" FROM base b",
|
||||||
|
" JOIN anon_child_lvl acl ON b.parent = acl.parent AND (b.child = acl.child OR (b.child IS NULL AND acl.child IS NULL))",
|
||||||
|
" JOIN anon_parent_lvl apl ON b.parent = apl.parent AND (b.child = apl.child OR (b.child IS NULL AND apl.child IS NULL))",
|
||||||
|
" JOIN anon_global_lvl agl ON b.parent = agl.parent AND (b.child = agl.child OR (b.child IS NULL AND agl.child IS NULL))",
|
||||||
|
"),",
|
||||||
|
]
|
||||||
)
|
)
|
||||||
|
|
||||||
# Final decisions
|
# Final decisions
|
||||||
|
|
@ -326,28 +342,31 @@ async def _build_single_action_sql(
|
||||||
"decisions AS (",
|
"decisions AS (",
|
||||||
" SELECT",
|
" SELECT",
|
||||||
" b.parent, b.child,",
|
" b.parent, b.child,",
|
||||||
" -- Cascading permission logic: child -> parent -> global, DENY beats ALLOW at each level",
|
" -- Cascading permission logic: child → parent → global, DENY beats ALLOW at each level",
|
||||||
" -- Priority order:",
|
" -- Priority order:",
|
||||||
" -- 1. Child-level deny 2. Child-level allow",
|
" -- 1. Child-level deny (most specific, blocks access)",
|
||||||
" -- 3. Parent-level deny 4. Parent-level allow",
|
" -- 2. Child-level allow (most specific, grants access)",
|
||||||
" -- 5. Global-level deny 6. Global-level allow",
|
" -- 3. Parent-level deny (intermediate, blocks access)",
|
||||||
|
" -- 4. Parent-level allow (intermediate, grants access)",
|
||||||
|
" -- 5. Global-level deny (least specific, blocks access)",
|
||||||
|
" -- 6. Global-level allow (least specific, grants access)",
|
||||||
" -- 7. Default deny (no rules match)",
|
" -- 7. Default deny (no rules match)",
|
||||||
" CASE",
|
" CASE",
|
||||||
" WHEN ca.any_deny = 1 THEN 0",
|
" WHEN cl.any_deny = 1 THEN 0",
|
||||||
" WHEN ca.any_allow = 1 THEN 1",
|
" WHEN cl.any_allow = 1 THEN 1",
|
||||||
" WHEN pa.any_deny = 1 THEN 0",
|
" WHEN pl.any_deny = 1 THEN 0",
|
||||||
" WHEN pa.any_allow = 1 THEN 1",
|
" WHEN pl.any_allow = 1 THEN 1",
|
||||||
" WHEN ga.any_deny = 1 THEN 0",
|
" WHEN gl.any_deny = 1 THEN 0",
|
||||||
" WHEN ga.any_allow = 1 THEN 1",
|
" WHEN gl.any_allow = 1 THEN 1",
|
||||||
" ELSE 0",
|
" ELSE 0",
|
||||||
" END AS is_allowed,",
|
" END AS is_allowed,",
|
||||||
" CASE",
|
" CASE",
|
||||||
" WHEN ca.any_deny = 1 THEN ca.deny_reasons",
|
" WHEN cl.any_deny = 1 THEN cl.deny_reasons",
|
||||||
" WHEN ca.any_allow = 1 THEN ca.allow_reasons",
|
" WHEN cl.any_allow = 1 THEN cl.allow_reasons",
|
||||||
" WHEN pa.any_deny = 1 THEN pa.deny_reasons",
|
" WHEN pl.any_deny = 1 THEN pl.deny_reasons",
|
||||||
" WHEN pa.any_allow = 1 THEN pa.allow_reasons",
|
" WHEN pl.any_allow = 1 THEN pl.allow_reasons",
|
||||||
" WHEN ga.any_deny = 1 THEN ga.deny_reasons",
|
" WHEN gl.any_deny = 1 THEN gl.deny_reasons",
|
||||||
" WHEN ga.any_allow = 1 THEN ga.allow_reasons",
|
" WHEN gl.any_allow = 1 THEN gl.allow_reasons",
|
||||||
" ELSE '[]'",
|
" ELSE '[]'",
|
||||||
" END AS reason",
|
" END AS reason",
|
||||||
]
|
]
|
||||||
|
|
@ -355,34 +374,21 @@ async def _build_single_action_sql(
|
||||||
|
|
||||||
if include_is_private:
|
if include_is_private:
|
||||||
query_parts.append(
|
query_parts.append(
|
||||||
" , CASE WHEN ("
|
" , CASE WHEN ad.anon_is_allowed = 0 THEN 1 ELSE 0 END AS is_private"
|
||||||
"CASE"
|
|
||||||
" WHEN aca.any_deny = 1 THEN 0"
|
|
||||||
" WHEN aca.any_allow = 1 THEN 1"
|
|
||||||
" WHEN apa.any_deny = 1 THEN 0"
|
|
||||||
" WHEN apa.any_allow = 1 THEN 1"
|
|
||||||
" WHEN aga.any_deny = 1 THEN 0"
|
|
||||||
" WHEN aga.any_allow = 1 THEN 1"
|
|
||||||
" ELSE 0 END"
|
|
||||||
") = 0 THEN 1 ELSE 0 END AS is_private"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
query_parts.extend(
|
query_parts.extend(
|
||||||
[
|
[
|
||||||
" FROM base b",
|
" FROM base b",
|
||||||
" LEFT JOIN child_agg ca ON ca.parent = b.parent AND ca.child = b.child",
|
" JOIN child_lvl cl ON b.parent = cl.parent AND (b.child = cl.child OR (b.child IS NULL AND cl.child IS NULL))",
|
||||||
" LEFT JOIN parent_agg pa ON pa.parent = b.parent",
|
" JOIN parent_lvl pl ON b.parent = pl.parent AND (b.child = pl.child OR (b.child IS NULL AND pl.child IS NULL))",
|
||||||
" CROSS JOIN global_agg ga",
|
" JOIN global_lvl gl ON b.parent = gl.parent AND (b.child = gl.child OR (b.child IS NULL AND gl.child IS NULL))",
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
|
|
||||||
if include_is_private:
|
if include_is_private:
|
||||||
query_parts.extend(
|
query_parts.append(
|
||||||
[
|
" JOIN anon_decisions ad ON b.parent = ad.parent AND (b.child = ad.child OR (b.child IS NULL AND ad.child IS NULL))"
|
||||||
" LEFT JOIN anon_child_agg aca ON aca.parent = b.parent AND aca.child = b.child",
|
|
||||||
" LEFT JOIN anon_parent_agg apa ON apa.parent = b.parent",
|
|
||||||
" CROSS JOIN anon_global_agg aga",
|
|
||||||
]
|
|
||||||
)
|
)
|
||||||
|
|
||||||
query_parts.append(")")
|
query_parts.append(")")
|
||||||
|
|
@ -392,31 +398,10 @@ async def _build_single_action_sql(
|
||||||
# Wrap each restriction_sql in a subquery to avoid operator precedence issues
|
# Wrap each restriction_sql in a subquery to avoid operator precedence issues
|
||||||
# with UNION ALL inside the restriction SQL statements
|
# with UNION ALL inside the restriction SQL statements
|
||||||
restriction_intersect = "\nINTERSECT\n".join(
|
restriction_intersect = "\nINTERSECT\n".join(
|
||||||
f"SELECT parent, child COLLATE {child_collation} AS child FROM ({sql})"
|
f"SELECT * FROM ({sql})" for sql in restriction_sqls
|
||||||
for sql in restriction_sqls
|
|
||||||
)
|
)
|
||||||
# Decompose by NULL-pattern so the final filter can use pure-equality
|
|
||||||
# EXISTS lookups (satisfiable via automatic indexes) instead of a
|
|
||||||
# correlated OR-scan over the whole list.
|
|
||||||
query_parts.extend(
|
query_parts.extend(
|
||||||
[
|
[",", "restriction_list AS (", f" {restriction_intersect}", ")"]
|
||||||
",",
|
|
||||||
"restriction_list AS (",
|
|
||||||
f" {restriction_intersect}",
|
|
||||||
"),",
|
|
||||||
"restriction_exact AS (",
|
|
||||||
" SELECT parent, child FROM restriction_list WHERE parent IS NOT NULL AND child IS NOT NULL",
|
|
||||||
"),",
|
|
||||||
"restriction_parent_any AS (",
|
|
||||||
" SELECT DISTINCT parent FROM restriction_list WHERE parent IS NOT NULL AND child IS NULL",
|
|
||||||
"),",
|
|
||||||
"restriction_child_any AS (",
|
|
||||||
" SELECT DISTINCT child FROM restriction_list WHERE parent IS NULL AND child IS NOT NULL",
|
|
||||||
"),",
|
|
||||||
"restriction_all AS (",
|
|
||||||
" SELECT 1 AS matched FROM restriction_list WHERE parent IS NULL AND child IS NULL LIMIT 1",
|
|
||||||
")",
|
|
||||||
]
|
|
||||||
)
|
)
|
||||||
|
|
||||||
# Final SELECT
|
# Final SELECT
|
||||||
|
|
@ -431,11 +416,10 @@ async def _build_single_action_sql(
|
||||||
# Add restriction filter if there are restrictions
|
# Add restriction filter if there are restrictions
|
||||||
if restriction_sqls:
|
if restriction_sqls:
|
||||||
query_parts.append("""
|
query_parts.append("""
|
||||||
AND (
|
AND EXISTS (
|
||||||
EXISTS (SELECT 1 FROM restriction_all)
|
SELECT 1 FROM restriction_list r
|
||||||
OR EXISTS (SELECT 1 FROM restriction_parent_any r WHERE r.parent = decisions.parent)
|
WHERE (r.parent = decisions.parent OR r.parent IS NULL)
|
||||||
OR EXISTS (SELECT 1 FROM restriction_child_any r WHERE r.child = decisions.child)
|
AND (r.child = decisions.child OR r.child IS NULL)
|
||||||
OR EXISTS (SELECT 1 FROM restriction_exact r WHERE r.parent = decisions.parent AND r.child = decisions.child)
|
|
||||||
)""")
|
)""")
|
||||||
|
|
||||||
# Add parent filter if specified
|
# Add parent filter if specified
|
||||||
|
|
@ -491,7 +475,6 @@ async def build_permission_rules_sql(
|
||||||
union_parts = []
|
union_parts = []
|
||||||
all_params = {}
|
all_params = {}
|
||||||
restriction_sqls = []
|
restriction_sqls = []
|
||||||
child_collation = _child_collation(action_obj)
|
|
||||||
|
|
||||||
for permission_sql in permission_sqls:
|
for permission_sql in permission_sqls:
|
||||||
all_params.update(permission_sql.params or {})
|
all_params.update(permission_sql.params or {})
|
||||||
|
|
@ -505,7 +488,7 @@ async def build_permission_rules_sql(
|
||||||
continue
|
continue
|
||||||
|
|
||||||
union_parts.append(f"""
|
union_parts.append(f"""
|
||||||
SELECT parent, child COLLATE {child_collation} AS child, allow, reason, '{permission_sql.source}' AS source_plugin FROM (
|
SELECT parent, child, allow, reason, '{permission_sql.source}' AS source_plugin FROM (
|
||||||
{permission_sql.sql}
|
{permission_sql.sql}
|
||||||
)
|
)
|
||||||
""".strip())
|
""".strip())
|
||||||
|
|
@ -576,7 +559,6 @@ async def check_permissions_for_actions(
|
||||||
verdicts = {}
|
verdicts = {}
|
||||||
|
|
||||||
for i, (action, permission_sqls) in enumerate(zip(unique_actions, gathered)):
|
for i, (action, permission_sqls) in enumerate(zip(unique_actions, gathered)):
|
||||||
child_collation = _child_collation(datasette.actions[action])
|
|
||||||
prefix = f"a{i}_"
|
prefix = f"a{i}_"
|
||||||
rule_parts = []
|
rule_parts = []
|
||||||
restriction_parts = []
|
restriction_parts = []
|
||||||
|
|
@ -602,7 +584,7 @@ async def check_permissions_for_actions(
|
||||||
if sql is None:
|
if sql is None:
|
||||||
continue
|
continue
|
||||||
rule_parts.append(
|
rule_parts.append(
|
||||||
f"SELECT parent, child COLLATE {child_collation} AS child, allow, reason, '{permission_sql.source}' AS source_plugin FROM (\n{sql}\n)"
|
f"SELECT parent, child, allow, reason, '{permission_sql.source}' AS source_plugin FROM (\n{sql}\n)"
|
||||||
)
|
)
|
||||||
|
|
||||||
if not rule_parts:
|
if not rule_parts:
|
||||||
|
|
@ -636,8 +618,7 @@ async def check_permissions_for_actions(
|
||||||
if restriction_parts:
|
if restriction_parts:
|
||||||
# Database-level restrictions (parent, NULL) match all children
|
# Database-level restrictions (parent, NULL) match all children
|
||||||
restriction_intersect = "\nINTERSECT\n".join(
|
restriction_intersect = "\nINTERSECT\n".join(
|
||||||
f"SELECT parent, child COLLATE {child_collation} AS child FROM ({sql})"
|
f"SELECT * FROM ({sql})" for sql in restriction_parts
|
||||||
for sql in restriction_parts
|
|
||||||
)
|
)
|
||||||
ctes.append(f"a{i}_restriction AS (\n{restriction_intersect}\n)")
|
ctes.append(f"a{i}_restriction AS (\n{restriction_intersect}\n)")
|
||||||
verdict_sql = f"""({verdict_sql}) AND EXISTS (
|
verdict_sql = f"""({verdict_sql}) AND EXISTS (
|
||||||
|
|
@ -692,240 +673,3 @@ async def check_permission_for_resource(
|
||||||
child=child,
|
child=child,
|
||||||
)
|
)
|
||||||
return results[action]
|
return results[action]
|
||||||
|
|
||||||
|
|
||||||
async def explain_permission_for_resource(
|
|
||||||
*,
|
|
||||||
datasette: "Datasette",
|
|
||||||
actor: dict | None,
|
|
||||||
action: str,
|
|
||||||
parent: str | None,
|
|
||||||
child: str | None,
|
|
||||||
) -> dict:
|
|
||||||
"""Explain a permission decision for one action and resource.
|
|
||||||
|
|
||||||
This is intended for Datasette's permission debugging tools. It uses the
|
|
||||||
same ``permission_resources_sql`` hook results and the same resolution
|
|
||||||
rules as :func:`check_permissions_for_actions`, but also returns the
|
|
||||||
matching rules, actor restriction results and ``also_requires`` chain.
|
|
||||||
|
|
||||||
The returned dictionary is part of Datasette's unstable debugging API.
|
|
||||||
"""
|
|
||||||
|
|
||||||
action_obj = datasette.actions.get(action)
|
|
||||||
if action_obj is None:
|
|
||||||
raise ValueError(f"Unknown action: {action}")
|
|
||||||
|
|
||||||
explanation = await _explain_single_action(
|
|
||||||
datasette=datasette,
|
|
||||||
actor=actor,
|
|
||||||
action=action,
|
|
||||||
parent=parent,
|
|
||||||
child=child,
|
|
||||||
)
|
|
||||||
|
|
||||||
required_actions = []
|
|
||||||
if action_obj.also_requires:
|
|
||||||
required = await explain_permission_for_resource(
|
|
||||||
datasette=datasette,
|
|
||||||
actor=actor,
|
|
||||||
action=action_obj.also_requires,
|
|
||||||
parent=parent,
|
|
||||||
child=child,
|
|
||||||
)
|
|
||||||
required_actions.append(required)
|
|
||||||
|
|
||||||
explanation["required_actions"] = required_actions
|
|
||||||
explanation["allowed"] = bool(
|
|
||||||
explanation["rule_allowed"]
|
|
||||||
and explanation["restriction_allowed"]
|
|
||||||
and all(required["allowed"] for required in required_actions)
|
|
||||||
)
|
|
||||||
explanation["summary"] = _permission_explanation_summary(explanation)
|
|
||||||
return explanation
|
|
||||||
|
|
||||||
|
|
||||||
async def _explain_single_action(
|
|
||||||
*,
|
|
||||||
datasette: "Datasette",
|
|
||||||
actor: dict | None,
|
|
||||||
action: str,
|
|
||||||
parent: str | None,
|
|
||||||
child: str | None,
|
|
||||||
) -> dict:
|
|
||||||
"""Return matching rules and restrictions for a single action."""
|
|
||||||
from datasette.utils.permissions import SKIP_PERMISSION_CHECKS
|
|
||||||
|
|
||||||
permission_sqls = await gather_permission_sql_from_hooks(
|
|
||||||
datasette=datasette,
|
|
||||||
actor=actor,
|
|
||||||
action=action,
|
|
||||||
)
|
|
||||||
|
|
||||||
if permission_sqls is SKIP_PERMISSION_CHECKS:
|
|
||||||
return {
|
|
||||||
"action": action,
|
|
||||||
"rule_allowed": True,
|
|
||||||
"restriction_allowed": True,
|
|
||||||
"winning_scope": "global",
|
|
||||||
"matched_rules": [
|
|
||||||
{
|
|
||||||
"scope": "global",
|
|
||||||
"effect": "allow",
|
|
||||||
"source": "skip_permission_checks",
|
|
||||||
"reason": "Permission checks were explicitly skipped",
|
|
||||||
"decisive": True,
|
|
||||||
"ignored_because": None,
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"restrictions": [],
|
|
||||||
}
|
|
||||||
|
|
||||||
db = datasette.get_internal_database()
|
|
||||||
matched_rules = []
|
|
||||||
restrictions = []
|
|
||||||
child_collation = _child_collation(datasette.actions[action])
|
|
||||||
|
|
||||||
for permission_sql in permission_sqls:
|
|
||||||
params = dict(permission_sql.params or {})
|
|
||||||
parent_param = _unused_parameter_name(params, "_explain_parent")
|
|
||||||
params[parent_param] = parent
|
|
||||||
child_param = _unused_parameter_name(params, "_explain_child")
|
|
||||||
params[child_param] = child
|
|
||||||
|
|
||||||
if permission_sql.sql:
|
|
||||||
rows = await db.execute(
|
|
||||||
f"""
|
|
||||||
SELECT parent, child, allow, reason
|
|
||||||
FROM ({permission_sql.sql}) AS permission_rules
|
|
||||||
WHERE (parent IS NULL OR parent = :{parent_param})
|
|
||||||
AND (child IS NULL OR child COLLATE {child_collation} = :{child_param})
|
|
||||||
""",
|
|
||||||
params,
|
|
||||||
)
|
|
||||||
for row in rows:
|
|
||||||
specificity = (
|
|
||||||
2
|
|
||||||
if row["child"] is not None
|
|
||||||
else 1 if row["parent"] is not None else 0
|
|
||||||
)
|
|
||||||
matched_rules.append(
|
|
||||||
{
|
|
||||||
"scope": ("resource", "parent", "global")[2 - specificity],
|
|
||||||
"effect": "allow" if row["allow"] else "deny",
|
|
||||||
"source": permission_sql.source,
|
|
||||||
"reason": row["reason"],
|
|
||||||
"_specificity": specificity,
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
if permission_sql.restriction_sql:
|
|
||||||
restriction_row = (
|
|
||||||
await db.execute(
|
|
||||||
f"""
|
|
||||||
SELECT EXISTS(
|
|
||||||
SELECT 1 FROM ({permission_sql.restriction_sql}) AS restriction_rules
|
|
||||||
WHERE (parent IS NULL OR parent = :{parent_param})
|
|
||||||
AND (child IS NULL OR child COLLATE {child_collation} = :{child_param})
|
|
||||||
) AS resource_is_in_allowlist
|
|
||||||
""",
|
|
||||||
params,
|
|
||||||
)
|
|
||||||
).first()
|
|
||||||
restriction_allowed = bool(restriction_row[0])
|
|
||||||
restrictions.append(
|
|
||||||
{
|
|
||||||
"source": permission_sql.source,
|
|
||||||
"allowed": restriction_allowed,
|
|
||||||
"reason": params.get("deny")
|
|
||||||
or (
|
|
||||||
"Resource is included in this restriction allowlist"
|
|
||||||
if restriction_allowed
|
|
||||||
else "Resource is not included in this restriction allowlist"
|
|
||||||
),
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
matched_rules.sort(
|
|
||||||
key=lambda rule: (
|
|
||||||
-rule["_specificity"],
|
|
||||||
0 if rule["effect"] == "deny" else 1,
|
|
||||||
rule["source"] or "",
|
|
||||||
rule["reason"] or "",
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
if matched_rules:
|
|
||||||
winning_specificity = matched_rules[0]["_specificity"]
|
|
||||||
winning_rules = [
|
|
||||||
rule
|
|
||||||
for rule in matched_rules
|
|
||||||
if rule["_specificity"] == winning_specificity
|
|
||||||
]
|
|
||||||
rule_allowed = not any(rule["effect"] == "deny" for rule in winning_rules)
|
|
||||||
winning_scope = winning_rules[0]["scope"]
|
|
||||||
else:
|
|
||||||
winning_specificity = None
|
|
||||||
rule_allowed = False
|
|
||||||
winning_scope = None
|
|
||||||
|
|
||||||
for rule in matched_rules:
|
|
||||||
specificity = rule.pop("_specificity")
|
|
||||||
if specificity != winning_specificity:
|
|
||||||
rule["decisive"] = False
|
|
||||||
rule["ignored_because"] = "A more specific rule matched"
|
|
||||||
elif not rule_allowed and rule["effect"] == "allow":
|
|
||||||
rule["decisive"] = False
|
|
||||||
rule["ignored_because"] = "A deny rule matched at the same scope"
|
|
||||||
else:
|
|
||||||
rule["decisive"] = True
|
|
||||||
rule["ignored_because"] = None
|
|
||||||
|
|
||||||
return {
|
|
||||||
"action": action,
|
|
||||||
"rule_allowed": rule_allowed,
|
|
||||||
"restriction_allowed": all(
|
|
||||||
restriction["allowed"] for restriction in restrictions
|
|
||||||
),
|
|
||||||
"winning_scope": winning_scope,
|
|
||||||
"matched_rules": matched_rules,
|
|
||||||
"restrictions": restrictions,
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
def _unused_parameter_name(params: dict, preferred: str) -> str:
|
|
||||||
"""Return a SQL parameter name that is not already in ``params``."""
|
|
||||||
candidate = preferred
|
|
||||||
suffix = 2
|
|
||||||
while candidate in params:
|
|
||||||
candidate = f"{preferred}_{suffix}"
|
|
||||||
suffix += 1
|
|
||||||
return candidate
|
|
||||||
|
|
||||||
|
|
||||||
def _permission_explanation_summary(explanation: dict) -> str:
|
|
||||||
denied_requirement = next(
|
|
||||||
(
|
|
||||||
required
|
|
||||||
for required in explanation["required_actions"]
|
|
||||||
if not required["allowed"]
|
|
||||||
),
|
|
||||||
None,
|
|
||||||
)
|
|
||||||
if denied_requirement:
|
|
||||||
return (
|
|
||||||
f"Denied because {explanation['action']} also requires "
|
|
||||||
f"{denied_requirement['action']}, which was denied."
|
|
||||||
)
|
|
||||||
if not explanation["matched_rules"]:
|
|
||||||
return "Denied because no permission rule matched this actor and resource."
|
|
||||||
if not explanation["rule_allowed"]:
|
|
||||||
return (
|
|
||||||
f"Denied by a {explanation['winning_scope']}-level rule. "
|
|
||||||
"Deny rules take precedence over allow rules at the same scope."
|
|
||||||
)
|
|
||||||
if not explanation["restriction_allowed"]:
|
|
||||||
return (
|
|
||||||
"Denied because the resource is not included in the actor's restrictions."
|
|
||||||
)
|
|
||||||
return f"Allowed by the matching {explanation['winning_scope']}-level rule."
|
|
||||||
|
|
|
||||||
|
|
@ -1,30 +1,28 @@
|
||||||
import asyncio
|
|
||||||
import json
|
import json
|
||||||
import re
|
from typing import Optional
|
||||||
from http.cookies import Morsel, SimpleCookie
|
from datasette.utils import MultiParams, calculate_etag
|
||||||
from mimetypes import guess_type
|
|
||||||
from pathlib import Path
|
|
||||||
from urllib.parse import parse_qs, parse_qsl, urlunparse
|
|
||||||
|
|
||||||
import aiofiles
|
|
||||||
import aiofiles.os
|
|
||||||
|
|
||||||
from datasette.utils import MultiParams, calculate_etag, error_body, sha256_file
|
|
||||||
from datasette.utils.multipart import (
|
from datasette.utils.multipart import (
|
||||||
DEFAULT_MAX_FIELD_SIZE,
|
parse_form_data,
|
||||||
DEFAULT_MAX_FIELDS,
|
MultipartParseError,
|
||||||
|
FormData,
|
||||||
DEFAULT_MAX_FILE_SIZE,
|
DEFAULT_MAX_FILE_SIZE,
|
||||||
|
DEFAULT_MAX_REQUEST_SIZE,
|
||||||
|
DEFAULT_MAX_FIELDS,
|
||||||
DEFAULT_MAX_FILES,
|
DEFAULT_MAX_FILES,
|
||||||
|
DEFAULT_MAX_PARTS,
|
||||||
|
DEFAULT_MAX_FIELD_SIZE,
|
||||||
DEFAULT_MAX_MEMORY_FILE_SIZE,
|
DEFAULT_MAX_MEMORY_FILE_SIZE,
|
||||||
DEFAULT_MAX_PART_HEADER_BYTES,
|
DEFAULT_MAX_PART_HEADER_BYTES,
|
||||||
DEFAULT_MAX_PART_HEADER_LINES,
|
DEFAULT_MAX_PART_HEADER_LINES,
|
||||||
DEFAULT_MAX_PARTS,
|
|
||||||
DEFAULT_MAX_REQUEST_SIZE,
|
|
||||||
DEFAULT_MIN_FREE_DISK_BYTES,
|
DEFAULT_MIN_FREE_DISK_BYTES,
|
||||||
FormData,
|
|
||||||
MultipartParseError,
|
|
||||||
parse_form_data,
|
|
||||||
)
|
)
|
||||||
|
from mimetypes import guess_type
|
||||||
|
from urllib.parse import parse_qs, urlunparse, parse_qsl
|
||||||
|
from pathlib import Path
|
||||||
|
from http.cookies import SimpleCookie, Morsel
|
||||||
|
import aiofiles
|
||||||
|
import aiofiles.os
|
||||||
|
import re
|
||||||
|
|
||||||
# Workaround for adding samesite support to pre 3.8 python
|
# Workaround for adding samesite support to pre 3.8 python
|
||||||
Morsel._reserved["samesite"] = "SameSite"
|
Morsel._reserved["samesite"] = "SameSite"
|
||||||
|
|
@ -69,41 +67,16 @@ class BadRequest(Base400):
|
||||||
status = 400
|
status = 400
|
||||||
|
|
||||||
|
|
||||||
class PayloadTooLarge(Base400):
|
|
||||||
status = 413
|
|
||||||
|
|
||||||
|
|
||||||
SAMESITE_VALUES = ("strict", "lax", "none")
|
SAMESITE_VALUES = ("strict", "lax", "none")
|
||||||
|
|
||||||
# Bodies read fully into memory (post_body/post_vars/json) are capped at this
|
|
||||||
# size unless the max_post_body_bytes setting says otherwise. Kept deliberately
|
|
||||||
# far below multipart's DEFAULT_MAX_REQUEST_SIZE: that parser streams to disk,
|
|
||||||
# while these bodies are held in RAM and json.loads() can multiply their
|
|
||||||
# footprint several times over.
|
|
||||||
DEFAULT_MAX_POST_BODY_BYTES = 2 * 1024 * 1024 # 2MB
|
|
||||||
|
|
||||||
|
|
||||||
class _RequestHeaders(dict):
|
|
||||||
"""Incoming headers with lowercase keys and case-insensitive lookups."""
|
|
||||||
|
|
||||||
def __getitem__(self, key):
|
|
||||||
return super().__getitem__(key.lower())
|
|
||||||
|
|
||||||
def get(self, key, default=None):
|
|
||||||
return super().get(key.lower(), default)
|
|
||||||
|
|
||||||
def __contains__(self, key):
|
|
||||||
return super().__contains__(key.lower())
|
|
||||||
|
|
||||||
|
|
||||||
class Request:
|
class Request:
|
||||||
def __init__(self, scope, receive, max_post_body_bytes=DEFAULT_MAX_POST_BODY_BYTES):
|
def __init__(self, scope, receive):
|
||||||
self.scope = scope
|
self.scope = scope
|
||||||
self.receive = receive
|
self.receive = receive
|
||||||
self.max_post_body_bytes = max_post_body_bytes
|
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return f'<asgi.Request method="{self.method}" url="{self.url}">'
|
return '<asgi.Request method="{}" url="{}">'.format(self.method, self.url)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def method(self):
|
def method(self):
|
||||||
|
|
@ -125,10 +98,10 @@ class Request:
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def headers(self):
|
def headers(self):
|
||||||
return _RequestHeaders(
|
return {
|
||||||
(k.decode("latin-1").lower(), v.decode("latin-1"))
|
k.decode("latin-1").lower(): v.decode("latin-1")
|
||||||
for k, v in self.scope.get("headers") or []
|
for k, v in self.scope.get("headers") or []
|
||||||
)
|
}
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def host(self):
|
def host(self):
|
||||||
|
|
@ -168,43 +141,15 @@ class Request:
|
||||||
def actor(self):
|
def actor(self):
|
||||||
return self.scope.get("actor", None)
|
return self.scope.get("actor", None)
|
||||||
|
|
||||||
async def post_body(self, max_bytes=None):
|
async def post_body(self):
|
||||||
"""
|
body = b""
|
||||||
Read the request body fully into memory.
|
|
||||||
|
|
||||||
The body is capped at max_bytes - or self.max_post_body_bytes
|
|
||||||
(default 2MB, set from the max_post_body_bytes setting for requests
|
|
||||||
created by Datasette) if max_bytes is not provided. Pass max_bytes=0
|
|
||||||
to disable the limit. Raises PayloadTooLarge (HTTP 413) if exceeded -
|
|
||||||
oversized bodies are rejected as soon as the limit is passed, without
|
|
||||||
buffering the rest.
|
|
||||||
"""
|
|
||||||
if max_bytes is None:
|
|
||||||
max_bytes = self.max_post_body_bytes
|
|
||||||
too_large = PayloadTooLarge(
|
|
||||||
f"Request body exceeded maximum size of {max_bytes} bytes"
|
|
||||||
)
|
|
||||||
if max_bytes:
|
|
||||||
# Reject early if the client declares an oversized body
|
|
||||||
try:
|
|
||||||
if int(self.headers.get("content-length", "")) > max_bytes:
|
|
||||||
raise too_large
|
|
||||||
except ValueError:
|
|
||||||
# Missing or malformed - the streaming check below still applies
|
|
||||||
pass
|
|
||||||
chunks = []
|
|
||||||
received = 0
|
|
||||||
more_body = True
|
more_body = True
|
||||||
while more_body:
|
while more_body:
|
||||||
message = await self.receive()
|
message = await self.receive()
|
||||||
assert message["type"] == "http.request", message
|
assert message["type"] == "http.request", message
|
||||||
chunk = message.get("body", b"")
|
body += message.get("body", b"")
|
||||||
received += len(chunk)
|
|
||||||
if max_bytes and received > max_bytes:
|
|
||||||
raise too_large
|
|
||||||
chunks.append(chunk)
|
|
||||||
more_body = message.get("more_body", False)
|
more_body = message.get("more_body", False)
|
||||||
return b"".join(chunks)
|
return body
|
||||||
|
|
||||||
async def post_vars(self):
|
async def post_vars(self):
|
||||||
body = await self.post_body()
|
body = await self.post_body()
|
||||||
|
|
@ -221,7 +166,7 @@ class Request:
|
||||||
max_request_size: int = DEFAULT_MAX_REQUEST_SIZE,
|
max_request_size: int = DEFAULT_MAX_REQUEST_SIZE,
|
||||||
max_fields: int = DEFAULT_MAX_FIELDS,
|
max_fields: int = DEFAULT_MAX_FIELDS,
|
||||||
max_files: int = DEFAULT_MAX_FILES,
|
max_files: int = DEFAULT_MAX_FILES,
|
||||||
max_parts: int | None = DEFAULT_MAX_PARTS,
|
max_parts: Optional[int] = DEFAULT_MAX_PARTS,
|
||||||
max_field_size: int = DEFAULT_MAX_FIELD_SIZE,
|
max_field_size: int = DEFAULT_MAX_FIELD_SIZE,
|
||||||
max_memory_file_size: int = DEFAULT_MAX_MEMORY_FILE_SIZE,
|
max_memory_file_size: int = DEFAULT_MAX_MEMORY_FILE_SIZE,
|
||||||
max_part_header_bytes: int = DEFAULT_MAX_PART_HEADER_BYTES,
|
max_part_header_bytes: int = DEFAULT_MAX_PART_HEADER_BYTES,
|
||||||
|
|
@ -314,24 +259,12 @@ class AsgiLifespan:
|
||||||
while True:
|
while True:
|
||||||
message = await receive()
|
message = await receive()
|
||||||
if message["type"] == "lifespan.startup":
|
if message["type"] == "lifespan.startup":
|
||||||
try:
|
|
||||||
for fn in self.on_startup:
|
for fn in self.on_startup:
|
||||||
await fn()
|
await fn()
|
||||||
except Exception as e: # noqa: BLE001
|
|
||||||
await send(
|
|
||||||
{"type": "lifespan.startup.failed", "message": str(e)}
|
|
||||||
)
|
|
||||||
return
|
|
||||||
await send({"type": "lifespan.startup.complete"})
|
await send({"type": "lifespan.startup.complete"})
|
||||||
elif message["type"] == "lifespan.shutdown":
|
elif message["type"] == "lifespan.shutdown":
|
||||||
try:
|
|
||||||
for fn in self.on_shutdown:
|
for fn in self.on_shutdown:
|
||||||
await fn()
|
await fn()
|
||||||
except Exception as e: # noqa: BLE001
|
|
||||||
await send(
|
|
||||||
{"type": "lifespan.shutdown.failed", "message": str(e)}
|
|
||||||
)
|
|
||||||
return
|
|
||||||
await send({"type": "lifespan.shutdown.complete"})
|
await send({"type": "lifespan.shutdown.complete"})
|
||||||
return
|
return
|
||||||
else:
|
else:
|
||||||
|
|
@ -464,9 +397,6 @@ async def asgi_send_file(
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
HASHED_STATIC_CACHE_CONTROL = "max-age=31536000, immutable, public"
|
|
||||||
|
|
||||||
|
|
||||||
def asgi_static(root_path, chunk_size=4096, headers=None, content_type=None):
|
def asgi_static(root_path, chunk_size=4096, headers=None, content_type=None):
|
||||||
root_path = Path(root_path)
|
root_path = Path(root_path)
|
||||||
static_headers = {}
|
static_headers = {}
|
||||||
|
|
@ -493,17 +423,11 @@ def asgi_static(root_path, chunk_size=4096, headers=None, content_type=None):
|
||||||
return
|
return
|
||||||
try:
|
try:
|
||||||
# Calculate ETag for filepath
|
# Calculate ETag for filepath
|
||||||
hash_value = request.args.get("_hash")
|
|
||||||
if (
|
|
||||||
hash_value
|
|
||||||
and hash_value == sha256_file(full_path, chunk_size=chunk_size)[:12]
|
|
||||||
):
|
|
||||||
headers["Cache-Control"] = HASHED_STATIC_CACHE_CONTROL
|
|
||||||
etag = await calculate_etag(full_path, chunk_size=chunk_size)
|
etag = await calculate_etag(full_path, chunk_size=chunk_size)
|
||||||
headers["ETag"] = etag
|
headers["ETag"] = etag
|
||||||
if_none_match = request.headers.get("if-none-match")
|
if_none_match = request.headers.get("if-none-match")
|
||||||
if if_none_match and if_none_match == etag:
|
if if_none_match and if_none_match == etag:
|
||||||
return await asgi_send(send, "", 304, headers=headers)
|
return await asgi_send(send, "", 304)
|
||||||
await asgi_send_file(
|
await asgi_send_file(
|
||||||
send, full_path, chunk_size=chunk_size, headers=headers
|
send, full_path, chunk_size=chunk_size, headers=headers
|
||||||
)
|
)
|
||||||
|
|
@ -511,8 +435,6 @@ def asgi_static(root_path, chunk_size=4096, headers=None, content_type=None):
|
||||||
await asgi_send_html(send, "404: File not found", 404)
|
await asgi_send_html(send, "404: File not found", 404)
|
||||||
return
|
return
|
||||||
|
|
||||||
# Only the actual static-file handler can bypass dynamic response privacy.
|
|
||||||
inner_static._datasette_static = True
|
|
||||||
return inner_static
|
return inner_static
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -558,9 +480,9 @@ class Response:
|
||||||
httponly=False,
|
httponly=False,
|
||||||
samesite="lax",
|
samesite="lax",
|
||||||
):
|
):
|
||||||
assert (
|
assert samesite in SAMESITE_VALUES, "samesite should be one of {}".format(
|
||||||
samesite in SAMESITE_VALUES
|
SAMESITE_VALUES
|
||||||
), f"samesite should be one of {SAMESITE_VALUES}"
|
)
|
||||||
cookie = SimpleCookie()
|
cookie = SimpleCookie()
|
||||||
cookie[key] = value
|
cookie[key] = value
|
||||||
for prop_name, prop_value in (
|
for prop_name, prop_value in (
|
||||||
|
|
@ -604,18 +526,6 @@ class Response:
|
||||||
content_type="application/json; charset=utf-8",
|
content_type="application/json; charset=utf-8",
|
||||||
)
|
)
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def error(cls, messages, status=400, headers=None):
|
|
||||||
"""
|
|
||||||
A JSON error response using Datasette's standard error format.
|
|
||||||
|
|
||||||
messages can be a single string or a list of strings. For errors
|
|
||||||
that should content-negotiate between JSON and HTML, raise
|
|
||||||
Forbidden, NotFound, BadRequest or DatasetteError instead and let
|
|
||||||
Datasette's error handling hooks build the response.
|
|
||||||
"""
|
|
||||||
return cls.json(error_body(messages, status), status=status, headers=headers)
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def redirect(cls, path, status=302, headers=None):
|
def redirect(cls, path, status=302, headers=None):
|
||||||
headers = headers or {}
|
headers = headers or {}
|
||||||
|
|
@ -652,23 +562,10 @@ class AsgiRunOnFirstRequest:
|
||||||
self.asgi = asgi
|
self.asgi = asgi
|
||||||
self.on_startup = on_startup
|
self.on_startup = on_startup
|
||||||
self._started = False
|
self._started = False
|
||||||
# Guards against concurrent early requests interleaving with startup:
|
|
||||||
# without this, several requests could all observe `_started is
|
|
||||||
# False` and proceed before any of them finish running the hooks.
|
|
||||||
self._lock = asyncio.Lock()
|
|
||||||
|
|
||||||
async def __call__(self, scope, receive, send):
|
async def __call__(self, scope, receive, send):
|
||||||
# Leave "lifespan" scope events alone - this shim only exists as a
|
|
||||||
# fallback for hosts that never send them. It wraps AsgiLifespan, so
|
|
||||||
# if it ran on_startup here too, a startup exception would escape
|
|
||||||
# before AsgiLifespan's own try/except got a chance to turn it into
|
|
||||||
# a lifespan.startup.failed message.
|
|
||||||
if scope["type"] != "lifespan" and not self._started:
|
|
||||||
async with self._lock:
|
|
||||||
# Re-check: another request may have finished startup while
|
|
||||||
# we were waiting for the lock.
|
|
||||||
if not self._started:
|
if not self._started:
|
||||||
|
self._started = True
|
||||||
for hook in self.on_startup:
|
for hook in self.on_startup:
|
||||||
await hook()
|
await hook()
|
||||||
self._started = True
|
|
||||||
return await self.asgi(scope, receive, send)
|
return await self.asgi(scope, receive, send)
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,7 @@ Originally shared here: https://www.djangosnippets.org/snippets/1431/
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
class BaseConverter:
|
class BaseConverter(object):
|
||||||
decimal_digits = "0123456789"
|
decimal_digits = "0123456789"
|
||||||
|
|
||||||
def __init__(self, digits):
|
def __init__(self, digits):
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
import inspect
|
import inspect
|
||||||
import types
|
import types
|
||||||
from typing import Any, NamedTuple
|
from typing import NamedTuple, Any
|
||||||
|
|
||||||
|
|
||||||
class CallableStatus(NamedTuple):
|
class CallableStatus(NamedTuple):
|
||||||
|
|
@ -19,7 +19,7 @@ def check_callable(obj: Any) -> CallableStatus:
|
||||||
if isinstance(obj, types.FunctionType):
|
if isinstance(obj, types.FunctionType):
|
||||||
return CallableStatus(True, inspect.iscoroutinefunction(obj))
|
return CallableStatus(True, inspect.iscoroutinefunction(obj))
|
||||||
|
|
||||||
if callable(obj):
|
if hasattr(obj, "__call__"):
|
||||||
return CallableStatus(True, inspect.iscoroutinefunction(obj.__call__))
|
return CallableStatus(True, inspect.iscoroutinefunction(obj.__call__))
|
||||||
|
|
||||||
assert False, f"obj {obj!r} is somehow callable with no __call__ method"
|
assert False, "obj {} is somehow callable with no __call__ method".format(repr(obj))
|
||||||
|
|
|
||||||
|
|
@ -1,30 +1,9 @@
|
||||||
import textwrap
|
import textwrap
|
||||||
|
from datasette.utils import table_column_details
|
||||||
|
|
||||||
from sqlite_utils import Database as SQLiteUtilsDatabase
|
|
||||||
from sqlite_utils import Migrations
|
|
||||||
|
|
||||||
from datasette.utils import escape_sqlite, table_column_details
|
async def init_internal_db(db):
|
||||||
|
create_tables_sql = textwrap.dedent("""
|
||||||
INTERNAL_DB_SCHEMA_TABLES = {
|
|
||||||
"catalog_databases",
|
|
||||||
"catalog_tables",
|
|
||||||
"catalog_views",
|
|
||||||
"catalog_columns",
|
|
||||||
"catalog_indexes",
|
|
||||||
"catalog_foreign_keys",
|
|
||||||
"metadata_instance",
|
|
||||||
"metadata_databases",
|
|
||||||
"metadata_resources",
|
|
||||||
"metadata_columns",
|
|
||||||
"column_types",
|
|
||||||
"queries",
|
|
||||||
}
|
|
||||||
|
|
||||||
INTERNAL_DB_SCHEMA_INDEXES = {
|
|
||||||
"queries_owner_idx",
|
|
||||||
}
|
|
||||||
|
|
||||||
INTERNAL_DB_SCHEMA_SQL = textwrap.dedent("""
|
|
||||||
CREATE TABLE IF NOT EXISTS catalog_databases (
|
CREATE TABLE IF NOT EXISTS catalog_databases (
|
||||||
database_name TEXT PRIMARY KEY,
|
database_name TEXT PRIMARY KEY,
|
||||||
path TEXT,
|
path TEXT,
|
||||||
|
|
@ -88,7 +67,13 @@ INTERNAL_DB_SCHEMA_SQL = textwrap.dedent("""
|
||||||
FOREIGN KEY (database_name) REFERENCES catalog_databases(database_name),
|
FOREIGN KEY (database_name) REFERENCES catalog_databases(database_name),
|
||||||
FOREIGN KEY (database_name, table_name) REFERENCES catalog_tables(database_name, table_name)
|
FOREIGN KEY (database_name, table_name) REFERENCES catalog_tables(database_name, table_name)
|
||||||
);
|
);
|
||||||
|
""").strip()
|
||||||
|
await db.execute_write_script(create_tables_sql)
|
||||||
|
await initialize_metadata_tables(db)
|
||||||
|
|
||||||
|
|
||||||
|
async def initialize_metadata_tables(db):
|
||||||
|
await db.execute_write_script(textwrap.dedent("""
|
||||||
CREATE TABLE IF NOT EXISTS metadata_instance (
|
CREATE TABLE IF NOT EXISTS metadata_instance (
|
||||||
key text,
|
key text,
|
||||||
value text,
|
value text,
|
||||||
|
|
@ -149,40 +134,32 @@ INTERNAL_DB_SCHEMA_SQL = textwrap.dedent("""
|
||||||
|
|
||||||
CREATE INDEX IF NOT EXISTS queries_owner_idx
|
CREATE INDEX IF NOT EXISTS queries_owner_idx
|
||||||
ON queries(owner_id);
|
ON queries(owner_id);
|
||||||
""").strip()
|
"""))
|
||||||
|
|
||||||
|
|
||||||
internal_migrations = Migrations("datasette_internal")
|
async def populate_schema_tables(internal_db, db):
|
||||||
|
|
||||||
|
|
||||||
def _internal_schema_exists(db):
|
|
||||||
table_names = set(db.table_names())
|
|
||||||
if not INTERNAL_DB_SCHEMA_TABLES.issubset(table_names):
|
|
||||||
return False
|
|
||||||
index_names = {
|
|
||||||
row[0]
|
|
||||||
for row in db.execute("select name from sqlite_master where type = 'index'")
|
|
||||||
}
|
|
||||||
return INTERNAL_DB_SCHEMA_INDEXES.issubset(index_names)
|
|
||||||
|
|
||||||
|
|
||||||
@internal_migrations(name="0001_initial")
|
|
||||||
def initial_internal_schema(db):
|
|
||||||
if _internal_schema_exists(db):
|
|
||||||
return
|
|
||||||
db.executescript(INTERNAL_DB_SCHEMA_SQL)
|
|
||||||
|
|
||||||
|
|
||||||
async def init_internal_db(db):
|
|
||||||
def apply_migrations(conn):
|
|
||||||
internal_migrations.apply(SQLiteUtilsDatabase(conn, execute_plugins=False))
|
|
||||||
|
|
||||||
await db.execute_write_fn(apply_migrations, transaction=False)
|
|
||||||
|
|
||||||
|
|
||||||
async def populate_schema_tables(internal_db, db, schema_version):
|
|
||||||
database_name = db.name
|
database_name = db.name
|
||||||
|
|
||||||
|
def delete_everything(conn):
|
||||||
|
conn.execute(
|
||||||
|
"DELETE FROM catalog_tables WHERE database_name = ?", [database_name]
|
||||||
|
)
|
||||||
|
conn.execute(
|
||||||
|
"DELETE FROM catalog_views WHERE database_name = ?", [database_name]
|
||||||
|
)
|
||||||
|
conn.execute(
|
||||||
|
"DELETE FROM catalog_columns WHERE database_name = ?", [database_name]
|
||||||
|
)
|
||||||
|
conn.execute(
|
||||||
|
"DELETE FROM catalog_foreign_keys WHERE database_name = ?",
|
||||||
|
[database_name],
|
||||||
|
)
|
||||||
|
conn.execute(
|
||||||
|
"DELETE FROM catalog_indexes WHERE database_name = ?", [database_name]
|
||||||
|
)
|
||||||
|
|
||||||
|
await internal_db.execute_write_fn(delete_everything)
|
||||||
|
|
||||||
tables = (await db.execute("select * from sqlite_master WHERE type = 'table'")).rows
|
tables = (await db.execute("select * from sqlite_master WHERE type = 'table'")).rows
|
||||||
views = (await db.execute("select * from sqlite_master WHERE type = 'view'")).rows
|
views = (await db.execute("select * from sqlite_master WHERE type = 'view'")).rows
|
||||||
|
|
||||||
|
|
@ -207,30 +184,25 @@ async def populate_schema_tables(internal_db, db, schema_version):
|
||||||
columns = table_column_details(conn, table_name)
|
columns = table_column_details(conn, table_name)
|
||||||
columns_to_insert.extend(
|
columns_to_insert.extend(
|
||||||
{
|
{
|
||||||
"database_name": database_name,
|
**{"database_name": database_name, "table_name": table_name},
|
||||||
"table_name": table_name,
|
|
||||||
**column._asdict(),
|
**column._asdict(),
|
||||||
}
|
}
|
||||||
for column in columns
|
for column in columns
|
||||||
)
|
)
|
||||||
foreign_keys = conn.execute(
|
foreign_keys = conn.execute(
|
||||||
f"PRAGMA foreign_key_list({escape_sqlite(table_name)})"
|
f"PRAGMA foreign_key_list([{table_name}])"
|
||||||
).fetchall()
|
).fetchall()
|
||||||
foreign_keys_to_insert.extend(
|
foreign_keys_to_insert.extend(
|
||||||
{
|
{
|
||||||
"database_name": database_name,
|
**{"database_name": database_name, "table_name": table_name},
|
||||||
"table_name": table_name,
|
|
||||||
**dict(foreign_key),
|
**dict(foreign_key),
|
||||||
}
|
}
|
||||||
for foreign_key in foreign_keys
|
for foreign_key in foreign_keys
|
||||||
)
|
)
|
||||||
indexes = conn.execute(
|
indexes = conn.execute(f"PRAGMA index_list([{table_name}])").fetchall()
|
||||||
f"PRAGMA index_list({escape_sqlite(table_name)})"
|
|
||||||
).fetchall()
|
|
||||||
indexes_to_insert.extend(
|
indexes_to_insert.extend(
|
||||||
{
|
{
|
||||||
"database_name": database_name,
|
**{"database_name": database_name, "table_name": table_name},
|
||||||
"table_name": table_name,
|
|
||||||
**dict(index),
|
**dict(index),
|
||||||
}
|
}
|
||||||
for index in indexes
|
for index in indexes
|
||||||
|
|
@ -251,48 +223,21 @@ async def populate_schema_tables(internal_db, db, schema_version):
|
||||||
indexes_to_insert,
|
indexes_to_insert,
|
||||||
) = await db.execute_fn(collect_info)
|
) = await db.execute_fn(collect_info)
|
||||||
|
|
||||||
def replace_catalog(conn):
|
await internal_db.execute_write_many(
|
||||||
# Delete child rows before their catalog_tables parents so this also
|
|
||||||
# works if a prepare_connection plugin enables foreign key enforcement.
|
|
||||||
for table in (
|
|
||||||
"catalog_columns",
|
|
||||||
"catalog_foreign_keys",
|
|
||||||
"catalog_indexes",
|
|
||||||
"catalog_views",
|
|
||||||
"catalog_tables",
|
|
||||||
):
|
|
||||||
conn.execute(
|
|
||||||
f"DELETE FROM {table} WHERE database_name = ?",
|
|
||||||
[database_name],
|
|
||||||
)
|
|
||||||
conn.execute(
|
|
||||||
"""
|
|
||||||
INSERT OR REPLACE INTO catalog_databases (
|
|
||||||
database_name, path, is_memory, schema_version
|
|
||||||
) VALUES (?, ?, ?, ?)
|
|
||||||
""",
|
|
||||||
[
|
|
||||||
database_name,
|
|
||||||
str(db.path) if db.path is not None else None,
|
|
||||||
db.is_memory,
|
|
||||||
schema_version,
|
|
||||||
],
|
|
||||||
)
|
|
||||||
conn.executemany(
|
|
||||||
"""
|
"""
|
||||||
INSERT INTO catalog_tables (database_name, table_name, rootpage, sql)
|
INSERT INTO catalog_tables (database_name, table_name, rootpage, sql)
|
||||||
values (?, ?, ?, ?)
|
values (?, ?, ?, ?)
|
||||||
""",
|
""",
|
||||||
tables_to_insert,
|
tables_to_insert,
|
||||||
)
|
)
|
||||||
conn.executemany(
|
await internal_db.execute_write_many(
|
||||||
"""
|
"""
|
||||||
INSERT INTO catalog_views (database_name, view_name, rootpage, sql)
|
INSERT INTO catalog_views (database_name, view_name, rootpage, sql)
|
||||||
values (?, ?, ?, ?)
|
values (?, ?, ?, ?)
|
||||||
""",
|
""",
|
||||||
views_to_insert,
|
views_to_insert,
|
||||||
)
|
)
|
||||||
conn.executemany(
|
await internal_db.execute_write_many(
|
||||||
"""
|
"""
|
||||||
INSERT INTO catalog_columns (
|
INSERT INTO catalog_columns (
|
||||||
database_name, table_name, cid, name, type, "notnull", default_value, is_pk, hidden
|
database_name, table_name, cid, name, type, "notnull", default_value, is_pk, hidden
|
||||||
|
|
@ -302,7 +247,7 @@ async def populate_schema_tables(internal_db, db, schema_version):
|
||||||
""",
|
""",
|
||||||
columns_to_insert,
|
columns_to_insert,
|
||||||
)
|
)
|
||||||
conn.executemany(
|
await internal_db.execute_write_many(
|
||||||
"""
|
"""
|
||||||
INSERT INTO catalog_foreign_keys (
|
INSERT INTO catalog_foreign_keys (
|
||||||
database_name, table_name, "id", seq, "table", "from", "to", on_update, on_delete, match
|
database_name, table_name, "id", seq, "table", "from", "to", on_update, on_delete, match
|
||||||
|
|
@ -312,7 +257,7 @@ async def populate_schema_tables(internal_db, db, schema_version):
|
||||||
""",
|
""",
|
||||||
foreign_keys_to_insert,
|
foreign_keys_to_insert,
|
||||||
)
|
)
|
||||||
conn.executemany(
|
await internal_db.execute_write_many(
|
||||||
"""
|
"""
|
||||||
INSERT INTO catalog_indexes (
|
INSERT INTO catalog_indexes (
|
||||||
database_name, table_name, seq, name, "unique", origin, partial
|
database_name, table_name, seq, name, "unique", origin, partial
|
||||||
|
|
@ -322,5 +267,3 @@ async def populate_schema_tables(internal_db, db, schema_version):
|
||||||
""",
|
""",
|
||||||
indexes_to_insert,
|
indexes_to_insert,
|
||||||
)
|
)
|
||||||
|
|
||||||
await internal_db.execute_write_fn(replace_catalog)
|
|
||||||
|
|
|
||||||
|
|
@ -11,10 +11,15 @@ Supports:
|
||||||
import asyncio
|
import asyncio
|
||||||
import shutil
|
import shutil
|
||||||
import tempfile
|
import tempfile
|
||||||
from collections.abc import Callable
|
|
||||||
from dataclasses import dataclass, field
|
from dataclasses import dataclass, field
|
||||||
from typing import (
|
from typing import (
|
||||||
Any,
|
Any,
|
||||||
|
Callable,
|
||||||
|
Dict,
|
||||||
|
List,
|
||||||
|
Optional,
|
||||||
|
Tuple,
|
||||||
|
Union,
|
||||||
)
|
)
|
||||||
from urllib.parse import parse_qsl
|
from urllib.parse import parse_qsl
|
||||||
|
|
||||||
|
|
@ -24,7 +29,7 @@ DEFAULT_MAX_REQUEST_SIZE = 100 * 1024 * 1024 # 100MB
|
||||||
DEFAULT_MAX_FIELDS = 1000
|
DEFAULT_MAX_FIELDS = 1000
|
||||||
DEFAULT_MAX_FILES = 100
|
DEFAULT_MAX_FILES = 100
|
||||||
# If max_parts is not specified, it defaults to max_fields + max_files
|
# If max_parts is not specified, it defaults to max_fields + max_files
|
||||||
DEFAULT_MAX_PARTS: int | None = None
|
DEFAULT_MAX_PARTS: Optional[int] = None
|
||||||
DEFAULT_MAX_FIELD_SIZE = 100 * 1024 # 100KB
|
DEFAULT_MAX_FIELD_SIZE = 100 * 1024 # 100KB
|
||||||
DEFAULT_MAX_MEMORY_FILE_SIZE = 1024 * 1024 # 1MB
|
DEFAULT_MAX_MEMORY_FILE_SIZE = 1024 * 1024 # 1MB
|
||||||
DEFAULT_MAX_PART_HEADER_BYTES = 16 * 1024 # 16KB
|
DEFAULT_MAX_PART_HEADER_BYTES = 16 * 1024 # 16KB
|
||||||
|
|
@ -35,6 +40,8 @@ DEFAULT_MIN_FREE_DISK_BYTES = 50 * 1024 * 1024 # 50MB
|
||||||
class MultipartParseError(Exception):
|
class MultipartParseError(Exception):
|
||||||
"""Raised when multipart parsing fails."""
|
"""Raised when multipart parsing fails."""
|
||||||
|
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class UploadedFile:
|
class UploadedFile:
|
||||||
|
|
@ -50,7 +57,7 @@ class UploadedFile:
|
||||||
|
|
||||||
name: str
|
name: str
|
||||||
filename: str
|
filename: str
|
||||||
content_type: str | None
|
content_type: Optional[str]
|
||||||
size: int
|
size: int
|
||||||
_file: tempfile.SpooledTemporaryFile = field(repr=False)
|
_file: tempfile.SpooledTemporaryFile = field(repr=False)
|
||||||
|
|
||||||
|
|
@ -79,8 +86,7 @@ class UploadedFile:
|
||||||
def __del__(self):
|
def __del__(self):
|
||||||
try:
|
try:
|
||||||
self._file.close()
|
self._file.close()
|
||||||
except Exception: # noqa: BLE001, S110
|
except Exception:
|
||||||
# __del__ must never raise
|
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -92,27 +98,27 @@ class FormData:
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self._data: list[tuple[str, str | UploadedFile]] = []
|
self._data: List[Tuple[str, Union[str, UploadedFile]]] = []
|
||||||
|
|
||||||
def append(self, key: str, value: str | UploadedFile) -> None:
|
def append(self, key: str, value: Union[str, UploadedFile]) -> None:
|
||||||
"""Add a key-value pair."""
|
"""Add a key-value pair."""
|
||||||
self._data.append((key, value))
|
self._data.append((key, value))
|
||||||
|
|
||||||
def __getitem__(self, key: str) -> str | UploadedFile:
|
def __getitem__(self, key: str) -> Union[str, UploadedFile]:
|
||||||
"""Get the first value for a key."""
|
"""Get the first value for a key."""
|
||||||
for k, v in self._data:
|
for k, v in self._data:
|
||||||
if k == key:
|
if k == key:
|
||||||
return v
|
return v
|
||||||
raise KeyError(key)
|
raise KeyError(key)
|
||||||
|
|
||||||
def get(self, key: str, default: Any = None) -> str | UploadedFile | None:
|
def get(self, key: str, default: Any = None) -> Optional[Union[str, UploadedFile]]:
|
||||||
"""Get the first value for a key, or default if not found."""
|
"""Get the first value for a key, or default if not found."""
|
||||||
try:
|
try:
|
||||||
return self[key]
|
return self[key]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
return default
|
return default
|
||||||
|
|
||||||
def getlist(self, key: str) -> list[str | UploadedFile]:
|
def getlist(self, key: str) -> List[Union[str, UploadedFile]]:
|
||||||
"""Get all values for a key."""
|
"""Get all values for a key."""
|
||||||
return [v for k, v in self._data if k == key]
|
return [v for k, v in self._data if k == key]
|
||||||
|
|
||||||
|
|
@ -136,15 +142,15 @@ class FormData:
|
||||||
"""Return unique keys."""
|
"""Return unique keys."""
|
||||||
return list(self)
|
return list(self)
|
||||||
|
|
||||||
def items(self) -> list[tuple[str, str | UploadedFile]]:
|
def items(self) -> List[Tuple[str, Union[str, UploadedFile]]]:
|
||||||
"""Return all key-value pairs."""
|
"""Return all key-value pairs."""
|
||||||
return list(self._data)
|
return list(self._data)
|
||||||
|
|
||||||
def values(self) -> list[str | UploadedFile]:
|
def values(self) -> List[Union[str, UploadedFile]]:
|
||||||
"""Return all values."""
|
"""Return all values."""
|
||||||
return [v for _, v in self._data]
|
return [v for _, v in self._data]
|
||||||
|
|
||||||
def _uploaded_files(self) -> list[UploadedFile]:
|
def _uploaded_files(self) -> List[UploadedFile]:
|
||||||
"""Return UploadedFile instances contained in this form."""
|
"""Return UploadedFile instances contained in this form."""
|
||||||
return [v for _, v in self._data if isinstance(v, UploadedFile)]
|
return [v for _, v in self._data if isinstance(v, UploadedFile)]
|
||||||
|
|
||||||
|
|
@ -157,7 +163,7 @@ class FormData:
|
||||||
for uploaded in self._uploaded_files():
|
for uploaded in self._uploaded_files():
|
||||||
try:
|
try:
|
||||||
uploaded.close_sync()
|
uploaded.close_sync()
|
||||||
except Exception: # noqa: BLE001, S110
|
except Exception:
|
||||||
# Best-effort cleanup; ignore close errors
|
# Best-effort cleanup; ignore close errors
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
@ -166,7 +172,7 @@ class FormData:
|
||||||
for uploaded in self._uploaded_files():
|
for uploaded in self._uploaded_files():
|
||||||
try:
|
try:
|
||||||
await uploaded.close()
|
await uploaded.close()
|
||||||
except Exception: # noqa: BLE001, S110
|
except Exception:
|
||||||
# Best-effort cleanup; ignore close errors
|
# Best-effort cleanup; ignore close errors
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
@ -183,13 +189,13 @@ class FormData:
|
||||||
await self.aclose()
|
await self.aclose()
|
||||||
|
|
||||||
|
|
||||||
def parse_content_disposition(header: str) -> dict[str, str | None]:
|
def parse_content_disposition(header: str) -> Dict[str, Optional[str]]:
|
||||||
"""
|
"""
|
||||||
Parse Content-Disposition header value.
|
Parse Content-Disposition header value.
|
||||||
|
|
||||||
Returns dict with 'name', 'filename' keys (filename may be None).
|
Returns dict with 'name', 'filename' keys (filename may be None).
|
||||||
"""
|
"""
|
||||||
result: dict[str, str | None] = {"name": None, "filename": None}
|
result: Dict[str, Optional[str]] = {"name": None, "filename": None}
|
||||||
|
|
||||||
# Split on semicolons, handling quoted strings
|
# Split on semicolons, handling quoted strings
|
||||||
parts = []
|
parts = []
|
||||||
|
|
@ -232,8 +238,7 @@ def parse_content_disposition(header: str) -> dict[str, str | None]:
|
||||||
from urllib.parse import unquote
|
from urllib.parse import unquote
|
||||||
|
|
||||||
result["filename"] = unquote(encoded, encoding="utf-8")
|
result["filename"] = unquote(encoded, encoding="utf-8")
|
||||||
except Exception: # noqa: BLE001, S110
|
except Exception:
|
||||||
# Malformed RFC 5987 filename* - fall back to the plain filename
|
|
||||||
pass
|
pass
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
|
@ -245,8 +250,9 @@ def parse_content_disposition(header: str) -> dict[str, str | None]:
|
||||||
|
|
||||||
if key == "name":
|
if key == "name":
|
||||||
result["name"] = value
|
result["name"] = value
|
||||||
# Only set filename if filename* hasn't already set it
|
elif key == "filename":
|
||||||
elif key == "filename" and result["filename"] is None:
|
# Only set if filename* hasn't already set it
|
||||||
|
if result["filename"] is None:
|
||||||
# Strip path components (security)
|
# Strip path components (security)
|
||||||
# Handle both Unix and Windows paths
|
# Handle both Unix and Windows paths
|
||||||
value = value.replace("\\", "/")
|
value = value.replace("\\", "/")
|
||||||
|
|
@ -257,7 +263,7 @@ def parse_content_disposition(header: str) -> dict[str, str | None]:
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
def parse_content_type(header: str) -> tuple[str, dict[str, str]]:
|
def parse_content_type(header: str) -> Tuple[str, Dict[str, str]]:
|
||||||
"""
|
"""
|
||||||
Parse Content-Type header value.
|
Parse Content-Type header value.
|
||||||
|
|
||||||
|
|
@ -301,7 +307,7 @@ class MultipartParser:
|
||||||
max_request_size: int = DEFAULT_MAX_REQUEST_SIZE,
|
max_request_size: int = DEFAULT_MAX_REQUEST_SIZE,
|
||||||
max_fields: int = DEFAULT_MAX_FIELDS,
|
max_fields: int = DEFAULT_MAX_FIELDS,
|
||||||
max_files: int = DEFAULT_MAX_FILES,
|
max_files: int = DEFAULT_MAX_FILES,
|
||||||
max_parts: int | None = DEFAULT_MAX_PARTS,
|
max_parts: Optional[int] = DEFAULT_MAX_PARTS,
|
||||||
max_field_size: int = DEFAULT_MAX_FIELD_SIZE,
|
max_field_size: int = DEFAULT_MAX_FIELD_SIZE,
|
||||||
max_memory_file_size: int = DEFAULT_MAX_MEMORY_FILE_SIZE,
|
max_memory_file_size: int = DEFAULT_MAX_MEMORY_FILE_SIZE,
|
||||||
max_part_header_bytes: int = DEFAULT_MAX_PART_HEADER_BYTES,
|
max_part_header_bytes: int = DEFAULT_MAX_PART_HEADER_BYTES,
|
||||||
|
|
@ -342,12 +348,12 @@ class MultipartParser:
|
||||||
self._tempdir = tempfile.gettempdir()
|
self._tempdir = tempfile.gettempdir()
|
||||||
|
|
||||||
# Current part state
|
# Current part state
|
||||||
self.current_headers: dict[str, str] = {}
|
self.current_headers: Dict[str, str] = {}
|
||||||
self.current_file: tempfile.SpooledTemporaryFile | None = None
|
self.current_file: Optional[tempfile.SpooledTemporaryFile] = None
|
||||||
self.current_body = bytearray()
|
self.current_body = bytearray()
|
||||||
self.current_name: str | None = None
|
self.current_name: Optional[str] = None
|
||||||
self.current_filename: str | None = None
|
self.current_filename: Optional[str] = None
|
||||||
self.current_content_type: str | None = None
|
self.current_content_type: Optional[str] = None
|
||||||
|
|
||||||
def feed(self, chunk: bytes) -> None:
|
def feed(self, chunk: bytes) -> None:
|
||||||
"""Feed a chunk of data to the parser."""
|
"""Feed a chunk of data to the parser."""
|
||||||
|
|
@ -358,13 +364,6 @@ class MultipartParser:
|
||||||
self.buffer.extend(chunk)
|
self.buffer.extend(chunk)
|
||||||
self._process()
|
self._process()
|
||||||
|
|
||||||
def close(self) -> None:
|
|
||||||
"""Discard completed uploads and any file still being received."""
|
|
||||||
if self.current_file is not None:
|
|
||||||
self.current_file.close()
|
|
||||||
self.current_file = None
|
|
||||||
self.form_data.close()
|
|
||||||
|
|
||||||
def _process(self) -> None:
|
def _process(self) -> None:
|
||||||
"""Process buffered data."""
|
"""Process buffered data."""
|
||||||
while True:
|
while True:
|
||||||
|
|
@ -455,7 +454,7 @@ class MultipartParser:
|
||||||
# Parse header
|
# Parse header
|
||||||
try:
|
try:
|
||||||
line_str = line.decode("utf-8", errors="replace")
|
line_str = line.decode("utf-8", errors="replace")
|
||||||
except UnicodeDecodeError:
|
except Exception:
|
||||||
line_str = line.decode("latin-1")
|
line_str = line.decode("latin-1")
|
||||||
|
|
||||||
if ":" in line_str:
|
if ":" in line_str:
|
||||||
|
|
@ -482,9 +481,7 @@ class MultipartParser:
|
||||||
if self.file_count > self.max_files:
|
if self.file_count > self.max_files:
|
||||||
raise MultipartParseError("Too many files")
|
raise MultipartParseError("Too many files")
|
||||||
if self.handle_files:
|
if self.handle_files:
|
||||||
# Outlives this method - it is filled in across parser callbacks
|
self.current_file = tempfile.SpooledTemporaryFile(
|
||||||
# and then handed to the UploadedFile the caller consumes
|
|
||||||
self.current_file = tempfile.SpooledTemporaryFile( # noqa: SIM115
|
|
||||||
max_size=self.max_memory_file_size
|
max_size=self.max_memory_file_size
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
|
|
@ -584,9 +581,6 @@ class MultipartParser:
|
||||||
def _finish_part(self) -> None:
|
def _finish_part(self) -> None:
|
||||||
"""Finalize current part and add to form data."""
|
"""Finalize current part and add to form data."""
|
||||||
if self.current_name is None:
|
if self.current_name is None:
|
||||||
if self.current_file is not None:
|
|
||||||
self.current_file.close()
|
|
||||||
self.current_file = None
|
|
||||||
return
|
return
|
||||||
|
|
||||||
if self.current_filename is not None:
|
if self.current_filename is not None:
|
||||||
|
|
@ -650,7 +644,7 @@ async def parse_form_data(
|
||||||
max_request_size: int = DEFAULT_MAX_REQUEST_SIZE,
|
max_request_size: int = DEFAULT_MAX_REQUEST_SIZE,
|
||||||
max_fields: int = DEFAULT_MAX_FIELDS,
|
max_fields: int = DEFAULT_MAX_FIELDS,
|
||||||
max_files: int = DEFAULT_MAX_FILES,
|
max_files: int = DEFAULT_MAX_FILES,
|
||||||
max_parts: int | None = DEFAULT_MAX_PARTS,
|
max_parts: Optional[int] = DEFAULT_MAX_PARTS,
|
||||||
max_field_size: int = DEFAULT_MAX_FIELD_SIZE,
|
max_field_size: int = DEFAULT_MAX_FIELD_SIZE,
|
||||||
max_memory_file_size: int = DEFAULT_MAX_MEMORY_FILE_SIZE,
|
max_memory_file_size: int = DEFAULT_MAX_MEMORY_FILE_SIZE,
|
||||||
max_part_header_bytes: int = DEFAULT_MAX_PART_HEADER_BYTES,
|
max_part_header_bytes: int = DEFAULT_MAX_PART_HEADER_BYTES,
|
||||||
|
|
@ -732,29 +726,12 @@ async def parse_form_data(
|
||||||
batch_target = 64 * 1024
|
batch_target = 64 * 1024
|
||||||
batch = bytearray()
|
batch = bytearray()
|
||||||
|
|
||||||
async def run_parser(fn, *args):
|
|
||||||
# Cancellation must not close files while a worker is using them.
|
|
||||||
task = asyncio.create_task(asyncio.to_thread(fn, *args))
|
|
||||||
try:
|
|
||||||
return await asyncio.shield(task)
|
|
||||||
except asyncio.CancelledError as cancelled:
|
|
||||||
try:
|
|
||||||
while not task.done():
|
|
||||||
try:
|
|
||||||
await asyncio.shield(task)
|
|
||||||
except asyncio.CancelledError:
|
|
||||||
continue
|
|
||||||
task.result()
|
|
||||||
finally:
|
|
||||||
raise cancelled
|
|
||||||
|
|
||||||
async def flush_batch() -> None:
|
async def flush_batch() -> None:
|
||||||
if batch:
|
if batch:
|
||||||
data = bytes(batch)
|
data = bytes(batch)
|
||||||
batch.clear()
|
batch.clear()
|
||||||
await run_parser(parser.feed, data)
|
await asyncio.to_thread(parser.feed, data)
|
||||||
|
|
||||||
try:
|
|
||||||
while True:
|
while True:
|
||||||
message = await receive()
|
message = await receive()
|
||||||
message_type = message.get("type")
|
message_type = message.get("type")
|
||||||
|
|
@ -771,11 +748,7 @@ async def parse_form_data(
|
||||||
break
|
break
|
||||||
|
|
||||||
await flush_batch()
|
await flush_batch()
|
||||||
return await run_parser(parser.finalize)
|
return await asyncio.to_thread(parser.finalize)
|
||||||
except BaseException:
|
|
||||||
# No FormData is returned to the caller to take ownership on failure.
|
|
||||||
await asyncio.to_thread(parser.close)
|
|
||||||
raise
|
|
||||||
|
|
||||||
else:
|
else:
|
||||||
raise MultipartParseError(
|
raise MultipartParseError(
|
||||||
|
|
|
||||||
|
|
@ -2,9 +2,8 @@
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import json
|
import json
|
||||||
|
from typing import Any, Dict, Iterable, List, Sequence, Tuple
|
||||||
import sqlite3
|
import sqlite3
|
||||||
from collections.abc import Iterable, Sequence
|
|
||||||
from typing import Any
|
|
||||||
|
|
||||||
from datasette.permissions import PermissionSQL
|
from datasette.permissions import PermissionSQL
|
||||||
from datasette.plugins import pm
|
from datasette.plugins import pm
|
||||||
|
|
@ -16,7 +15,7 @@ SKIP_PERMISSION_CHECKS = object()
|
||||||
|
|
||||||
async def gather_permission_sql_from_hooks(
|
async def gather_permission_sql_from_hooks(
|
||||||
*, datasette, actor: dict | None, action: str
|
*, datasette, actor: dict | None, action: str
|
||||||
) -> list[PermissionSQL] | object:
|
) -> List[PermissionSQL] | object:
|
||||||
"""Collect PermissionSQL objects from the permission_resources_sql hook.
|
"""Collect PermissionSQL objects from the permission_resources_sql hook.
|
||||||
|
|
||||||
Ensures that each returned PermissionSQL has a populated ``source``.
|
Ensures that each returned PermissionSQL has a populated ``source``.
|
||||||
|
|
@ -35,7 +34,7 @@ async def gather_permission_sql_from_hooks(
|
||||||
hookimpls = hook_caller.get_hookimpls()
|
hookimpls = hook_caller.get_hookimpls()
|
||||||
hook_results = list(hook_caller(datasette=datasette, actor=actor, action=action))
|
hook_results = list(hook_caller(datasette=datasette, actor=actor, action=action))
|
||||||
|
|
||||||
collected: list[PermissionSQL] = []
|
collected: List[PermissionSQL] = []
|
||||||
actor_json = json.dumps(actor) if actor is not None else None
|
actor_json = json.dumps(actor) if actor is not None else None
|
||||||
actor_id = actor.get("id") if isinstance(actor, dict) else None
|
actor_id = actor.get("id") if isinstance(actor, dict) else None
|
||||||
|
|
||||||
|
|
@ -72,7 +71,7 @@ def _iter_permission_sql_from_result(
|
||||||
if isinstance(result, PermissionSQL):
|
if isinstance(result, PermissionSQL):
|
||||||
return [result]
|
return [result]
|
||||||
if isinstance(result, (list, tuple)):
|
if isinstance(result, (list, tuple)):
|
||||||
collected: list[PermissionSQL] = []
|
collected: List[PermissionSQL] = []
|
||||||
for item in result:
|
for item in result:
|
||||||
collected.extend(_iter_permission_sql_from_result(item, action=action))
|
collected.extend(_iter_permission_sql_from_result(item, action=action))
|
||||||
return collected
|
return collected
|
||||||
|
|
@ -91,7 +90,7 @@ def _iter_permission_sql_from_result(
|
||||||
|
|
||||||
def build_rules_union(
|
def build_rules_union(
|
||||||
actor: dict | None, plugins: Sequence[PermissionSQL]
|
actor: dict | None, plugins: Sequence[PermissionSQL]
|
||||||
) -> tuple[str, dict[str, Any]]:
|
) -> Tuple[str, Dict[str, Any]]:
|
||||||
"""
|
"""
|
||||||
Compose plugin SQL into a UNION ALL.
|
Compose plugin SQL into a UNION ALL.
|
||||||
|
|
||||||
|
|
@ -103,10 +102,10 @@ def build_rules_union(
|
||||||
The system reserves these parameter names: :actor, :actor_id, :action, :filter_parent
|
The system reserves these parameter names: :actor, :actor_id, :action, :filter_parent
|
||||||
Plugin parameters should be prefixed with a unique identifier (e.g., source name).
|
Plugin parameters should be prefixed with a unique identifier (e.g., source name).
|
||||||
"""
|
"""
|
||||||
parts: list[str] = []
|
parts: List[str] = []
|
||||||
actor_json = json.dumps(actor) if actor else None
|
actor_json = json.dumps(actor) if actor else None
|
||||||
actor_id = actor.get("id") if actor else None
|
actor_id = actor.get("id") if actor else None
|
||||||
params: dict[str, Any] = {"actor": actor_json, "actor_id": actor_id}
|
params: Dict[str, Any] = {"actor": actor_json, "actor_id": actor_id}
|
||||||
|
|
||||||
for p in plugins:
|
for p in plugins:
|
||||||
# No namespacing - just use plugin params as-is
|
# No namespacing - just use plugin params as-is
|
||||||
|
|
@ -142,10 +141,10 @@ async def resolve_permissions_from_catalog(
|
||||||
plugins: Sequence[Any],
|
plugins: Sequence[Any],
|
||||||
action: str,
|
action: str,
|
||||||
candidate_sql: str,
|
candidate_sql: str,
|
||||||
candidate_params: dict[str, Any] | None = None,
|
candidate_params: Dict[str, Any] | None = None,
|
||||||
*,
|
*,
|
||||||
implicit_deny: bool = True,
|
implicit_deny: bool = True,
|
||||||
) -> list[dict[str, Any]]:
|
) -> List[Dict[str, Any]]:
|
||||||
"""
|
"""
|
||||||
Resolve permissions by embedding the provided *candidate_sql* in a CTE.
|
Resolve permissions by embedding the provided *candidate_sql* in a CTE.
|
||||||
|
|
||||||
|
|
@ -169,8 +168,8 @@ async def resolve_permissions_from_catalog(
|
||||||
- parent, child, allow, reason, source_plugin, depth
|
- parent, child, allow, reason, source_plugin, depth
|
||||||
- resource (rendered "/parent/child" or "/parent" or "/")
|
- resource (rendered "/parent/child" or "/parent" or "/")
|
||||||
"""
|
"""
|
||||||
resolved_plugins: list[PermissionSQL] = []
|
resolved_plugins: List[PermissionSQL] = []
|
||||||
restriction_sqls: list[str] = []
|
restriction_sqls: List[str] = []
|
||||||
|
|
||||||
for plugin in plugins:
|
for plugin in plugins:
|
||||||
if callable(plugin) and not isinstance(plugin, PermissionSQL):
|
if callable(plugin) and not isinstance(plugin, PermissionSQL):
|
||||||
|
|
@ -399,11 +398,11 @@ async def resolve_permissions_with_candidates(
|
||||||
db,
|
db,
|
||||||
actor: dict | None,
|
actor: dict | None,
|
||||||
plugins: Sequence[Any],
|
plugins: Sequence[Any],
|
||||||
candidates: list[tuple[str, str | None]],
|
candidates: List[Tuple[str, str | None]],
|
||||||
action: str,
|
action: str,
|
||||||
*,
|
*,
|
||||||
implicit_deny: bool = True,
|
implicit_deny: bool = True,
|
||||||
) -> list[dict[str, Any]]:
|
) -> List[Dict[str, Any]]:
|
||||||
"""
|
"""
|
||||||
Resolve permissions without any external candidate table by embedding
|
Resolve permissions without any external candidate table by embedding
|
||||||
the candidates as a UNION of parameterized SELECTs in a CTE.
|
the candidates as a UNION of parameterized SELECTs in a CTE.
|
||||||
|
|
@ -412,8 +411,8 @@ async def resolve_permissions_with_candidates(
|
||||||
actor: actor dict (or None), made available as :actor (JSON), :actor_id, and :action
|
actor: actor dict (or None), made available as :actor (JSON), :actor_id, and :action
|
||||||
"""
|
"""
|
||||||
# Build a small CTE for candidates.
|
# Build a small CTE for candidates.
|
||||||
cand_rows_sql: list[str] = []
|
cand_rows_sql: List[str] = []
|
||||||
cand_params: dict[str, Any] = {}
|
cand_params: Dict[str, Any] = {}
|
||||||
for i, (parent, child) in enumerate(candidates):
|
for i, (parent, child) in enumerate(candidates):
|
||||||
pkey = f"cand_p_{i}"
|
pkey = f"cand_p_{i}"
|
||||||
ckey = f"cand_c_{i}"
|
ckey = f"cand_c_{i}"
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ https://github.com/python/cpython/blob/v3.8.3/LICENSE
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import os
|
import os
|
||||||
from shutil import Error, copy, copy2, copystat
|
from shutil import copy, copy2, copystat, Error
|
||||||
|
|
||||||
|
|
||||||
def _copytree(
|
def _copytree(
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,6 @@
|
||||||
import sys
|
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from typing import Literal
|
from typing import Literal
|
||||||
|
|
||||||
from datasette.utils import escape_sqlite
|
|
||||||
from datasette.utils.sqlite import SQLiteTableType, sqlite3, sqlite_table_type
|
from datasette.utils.sqlite import SQLiteTableType, sqlite3, sqlite_table_type
|
||||||
|
|
||||||
SQLOperation = Literal[
|
SQLOperation = Literal[
|
||||||
|
|
@ -152,6 +150,7 @@ _SQLITE_INTERNAL_SCHEMA_FUNCTIONS = {
|
||||||
"sqlite_rename_test",
|
"sqlite_rename_test",
|
||||||
"substr",
|
"substr",
|
||||||
}
|
}
|
||||||
|
|
||||||
_AUTHORIZER_ACTION_NAMES = {
|
_AUTHORIZER_ACTION_NAMES = {
|
||||||
getattr(sqlite3, name): name
|
getattr(sqlite3, name): name
|
||||||
for name in (
|
for name in (
|
||||||
|
|
@ -197,16 +196,6 @@ def _allow_authorizer_action(*args):
|
||||||
return sqlite3.SQLITE_OK
|
return sqlite3.SQLITE_OK
|
||||||
|
|
||||||
|
|
||||||
def _disable_authorizer(conn):
|
|
||||||
# Python 3.11 added support for unregistering an authorizer using None.
|
|
||||||
# On Python 3.10, None is installed as the callback instead, and the next
|
|
||||||
# statement fails with "not authorized" when sqlite3 tries to call it.
|
|
||||||
if sys.version_info >= (3, 11):
|
|
||||||
conn.set_authorizer(None)
|
|
||||||
else:
|
|
||||||
conn.set_authorizer(_allow_authorizer_action)
|
|
||||||
|
|
||||||
|
|
||||||
def analyze_sql_tables(
|
def analyze_sql_tables(
|
||||||
conn,
|
conn,
|
||||||
sql: str,
|
sql: str,
|
||||||
|
|
@ -220,9 +209,7 @@ def analyze_sql_tables(
|
||||||
|
|
||||||
This function is synchronous and connection-based. It temporarily installs a
|
This function is synchronous and connection-based. It temporarily installs a
|
||||||
SQLite authorizer, prepares ``EXPLAIN <sql>``, and returns the operation
|
SQLite authorizer, prepares ``EXPLAIN <sql>``, and returns the operation
|
||||||
callbacks observed while SQLite compiles the statement. ``CREATE VIEW`` is
|
callbacks observed while SQLite compiles the statement.
|
||||||
additionally executed inside a rolled-back savepoint so its source-table reads
|
|
||||||
can be discovered by analyzing a query against the temporary view.
|
|
||||||
"""
|
"""
|
||||||
operations: dict[OperationKey, set[str]] = {}
|
operations: dict[OperationKey, set[str]] = {}
|
||||||
|
|
||||||
|
|
@ -404,10 +391,6 @@ def analyze_sql_tables(
|
||||||
)
|
)
|
||||||
return sqlite3.SQLITE_OK
|
return sqlite3.SQLITE_OK
|
||||||
|
|
||||||
if action == sqlite3.SQLITE_RECURSIVE:
|
|
||||||
# Recursive CTE bookkeeping; table reads are reported separately.
|
|
||||||
return sqlite3.SQLITE_OK
|
|
||||||
|
|
||||||
if action == sqlite3.SQLITE_FUNCTION and arg2 is not None:
|
if action == sqlite3.SQLITE_FUNCTION and arg2 is not None:
|
||||||
record(
|
record(
|
||||||
"function",
|
"function",
|
||||||
|
|
@ -427,12 +410,12 @@ def analyze_sql_tables(
|
||||||
database=None,
|
database=None,
|
||||||
table=None,
|
table=None,
|
||||||
sqlite_schema=sqlite_schema,
|
sqlite_schema=sqlite_schema,
|
||||||
target=f"{arg1} {arg2}" if arg2 is not None else arg1,
|
target="{} {}".format(arg1, arg2) if arg2 is not None else arg1,
|
||||||
source=source,
|
source=source,
|
||||||
)
|
)
|
||||||
return sqlite3.SQLITE_OK
|
return sqlite3.SQLITE_OK
|
||||||
|
|
||||||
action_name = _AUTHORIZER_ACTION_NAMES.get(action, f"SQLITE_{action}")
|
action_name = _AUTHORIZER_ACTION_NAMES.get(action, "SQLITE_{}".format(action))
|
||||||
record(
|
record(
|
||||||
"unknown",
|
"unknown",
|
||||||
"unknown",
|
"unknown",
|
||||||
|
|
@ -495,24 +478,24 @@ def analyze_sql_tables(
|
||||||
conn, key.table, schema=key.sqlite_schema
|
conn, key.table, schema=key.sqlite_schema
|
||||||
)
|
)
|
||||||
finally:
|
finally:
|
||||||
_disable_authorizer(conn)
|
conn.set_authorizer(None)
|
||||||
|
|
||||||
has_schema_operation = any(
|
has_schema_operation = any(
|
||||||
key.target_type in {"table", "index", "view", "trigger", "virtual-table"}
|
key.target_type in {"table", "index", "view", "trigger", "virtual-table"}
|
||||||
and key.operation in {"create", "alter", "drop"}
|
and key.operation in {"create", "alter", "drop"}
|
||||||
for key in operations
|
for key in operations
|
||||||
)
|
)
|
||||||
dropped_tables_and_views = {
|
dropped_tables = {
|
||||||
(key.database, key.table)
|
(key.database, key.table)
|
||||||
for key in operations
|
for key in operations
|
||||||
if key.operation == "drop" and key.target_type in {"table", "view"}
|
if key.operation == "drop" and key.target_type == "table"
|
||||||
}
|
}
|
||||||
|
|
||||||
def key_is_drop_table_delete(key: OperationKey) -> bool:
|
def key_is_drop_table_delete(key: OperationKey) -> bool:
|
||||||
return (
|
return (
|
||||||
key.operation == "delete"
|
key.operation == "delete"
|
||||||
and key.target_type == "table"
|
and key.target_type == "table"
|
||||||
and (key.database, key.table) in dropped_tables_and_views
|
and (key.database, key.table) in dropped_tables
|
||||||
)
|
)
|
||||||
|
|
||||||
has_user_table_access_in_schema_operation = any(
|
has_user_table_access_in_schema_operation = any(
|
||||||
|
|
@ -535,7 +518,9 @@ def analyze_sql_tables(
|
||||||
and key.target in _SQLITE_INTERNAL_SCHEMA_FUNCTIONS
|
and key.target in _SQLITE_INTERNAL_SCHEMA_FUNCTIONS
|
||||||
):
|
):
|
||||||
return True
|
return True
|
||||||
return bool(key_is_drop_table_delete(key))
|
if key_is_drop_table_delete(key):
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
def table_kind_for(key: OperationKey) -> SQLiteTableType | None:
|
def table_kind_for(key: OperationKey) -> SQLiteTableType | None:
|
||||||
if (
|
if (
|
||||||
|
|
@ -546,7 +531,7 @@ def analyze_sql_tables(
|
||||||
return None
|
return None
|
||||||
return table_kind_cache[(key.sqlite_schema, key.table)]
|
return table_kind_cache[(key.sqlite_schema, key.table)]
|
||||||
|
|
||||||
analysis = SQLAnalysis(
|
return SQLAnalysis(
|
||||||
operations=tuple(
|
operations=tuple(
|
||||||
Operation(
|
Operation(
|
||||||
operation=key.operation,
|
operation=key.operation,
|
||||||
|
|
@ -563,58 +548,3 @@ def analyze_sql_tables(
|
||||||
for key, columns in operations.items()
|
for key, columns in operations.items()
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
# SQLite does not resolve the SELECT body of a view when preparing CREATE
|
|
||||||
# VIEW, so its authorizer does not report reads from the view's source
|
|
||||||
# tables. Temporarily create the view, analyze a query against it (which
|
|
||||||
# does resolve the body), then roll the schema change back. Database-level
|
|
||||||
# callers use an isolated writable connection for this analysis.
|
|
||||||
create_view_operations = tuple(
|
|
||||||
operation
|
|
||||||
for operation in analysis.operations
|
|
||||||
if operation.operation == "create" and operation.target_type == "view"
|
|
||||||
)
|
|
||||||
if not create_view_operations:
|
|
||||||
return analysis
|
|
||||||
|
|
||||||
savepoint = "datasette_analyze_create_view"
|
|
||||||
conn.execute(f"SAVEPOINT {savepoint}")
|
|
||||||
try:
|
|
||||||
conn.execute(sql, params if params is not None else {})
|
|
||||||
dependency_reads = []
|
|
||||||
for view_operation in create_view_operations:
|
|
||||||
if view_operation.sqlite_schema is None or view_operation.table is None:
|
|
||||||
raise sqlite3.OperationalError(
|
|
||||||
"Could not determine the created view name"
|
|
||||||
)
|
|
||||||
quoted_schema = escape_sqlite(view_operation.sqlite_schema)
|
|
||||||
quoted_view = escape_sqlite(view_operation.table)
|
|
||||||
qualified_view = f"{quoted_schema}.{quoted_view}"
|
|
||||||
view_analysis = analyze_sql_tables(
|
|
||||||
conn,
|
|
||||||
f"SELECT * FROM {qualified_view}",
|
|
||||||
database_name=database_name,
|
|
||||||
schema_to_database=schema_to_database,
|
|
||||||
)
|
|
||||||
dependency_reads.extend(
|
|
||||||
operation
|
|
||||||
for operation in view_analysis.operations
|
|
||||||
if operation.operation == "read"
|
|
||||||
and not (
|
|
||||||
operation.sqlite_schema == view_operation.sqlite_schema
|
|
||||||
and operation.table == view_operation.table
|
|
||||||
)
|
|
||||||
)
|
|
||||||
finally:
|
|
||||||
conn.execute(f"ROLLBACK TO {savepoint}")
|
|
||||||
conn.execute(f"RELEASE {savepoint}")
|
|
||||||
|
|
||||||
existing_operations = set(analysis.operations)
|
|
||||||
return SQLAnalysis(
|
|
||||||
operations=analysis.operations
|
|
||||||
+ tuple(
|
|
||||||
operation
|
|
||||||
for operation in dependency_reads
|
|
||||||
if operation not in existing_operations
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
|
||||||
|
|
@ -15,17 +15,8 @@ if hasattr(sqlite3, "enable_callback_tracebacks"):
|
||||||
_cached_sqlite_version = None
|
_cached_sqlite_version = None
|
||||||
_cached_supports_returning = None
|
_cached_supports_returning = None
|
||||||
SQLiteTableType = Literal["table", "view", "virtual", "shadow"]
|
SQLiteTableType = Literal["table", "view", "virtual", "shadow"]
|
||||||
_SQLITE_IDENTIFIER_RE = (
|
|
||||||
r"""(?:"(?:[^"]|"")*"|'(?:[^']|'')*'|`(?:[^`]|``)*`|\[[^\]]*\]|[^\s.()'"`\[\]]+)"""
|
|
||||||
)
|
|
||||||
_VIRTUAL_TABLE_MODULE_RE = re.compile(
|
_VIRTUAL_TABLE_MODULE_RE = re.compile(
|
||||||
r"^\s*CREATE\s+VIRTUAL\s+TABLE\b\s*(?:IF\s+NOT\s+EXISTS\s+)?"
|
r"\bCREATE\s+VIRTUAL\s+TABLE\b.*?\bUSING\s+([^\s(]+)",
|
||||||
+ _SQLITE_IDENTIFIER_RE
|
|
||||||
+ r"(?:\s*\.\s*"
|
|
||||||
+ _SQLITE_IDENTIFIER_RE
|
|
||||||
+ r")?\s*\bUSING\b\s*("
|
|
||||||
+ _SQLITE_IDENTIFIER_RE
|
|
||||||
+ r")",
|
|
||||||
re.IGNORECASE | re.DOTALL,
|
re.IGNORECASE | re.DOTALL,
|
||||||
)
|
)
|
||||||
_VIRTUAL_TABLE_SHADOW_SUFFIXES = {
|
_VIRTUAL_TABLE_SHADOW_SUFFIXES = {
|
||||||
|
|
@ -92,58 +83,24 @@ def sqlite_table_type(
|
||||||
) -> SQLiteTableType | None:
|
) -> SQLiteTableType | None:
|
||||||
if supports_table_list():
|
if supports_table_list():
|
||||||
try:
|
try:
|
||||||
# Use the "PRAGMA table_list" statement form rather than the
|
query = "select type from pragma_table_list where name = ?"
|
||||||
# pragma_table_list(...) table-valued function. The
|
params: tuple[str, ...] = (table,)
|
||||||
# table-valued function is resolved like an ordinary relation
|
|
||||||
# name, so an attacker-created table or view literally named
|
|
||||||
# "pragma_table_list" can shadow it and spoof the reported
|
|
||||||
# type (e.g. claiming a virtual table is an ordinary table).
|
|
||||||
# The PRAGMA statement form is a distinct piece of SQL syntax
|
|
||||||
# that always invokes SQLite's built-in pragma, so it cannot
|
|
||||||
# be shadowed by a user-created relation.
|
|
||||||
if schema is not None:
|
if schema is not None:
|
||||||
query = f"PRAGMA {_quote_identifier(schema)}.table_list"
|
query += " and schema = ?"
|
||||||
else:
|
params = (table, schema)
|
||||||
query = "PRAGMA table_list"
|
row = conn.execute(query, params).fetchone()
|
||||||
cursor = conn.execute(query)
|
if row is not None and row[0] in {"table", "view", "virtual", "shadow"}:
|
||||||
columns = [description[0] for description in cursor.description]
|
return row[0]
|
||||||
for row in cursor.fetchall():
|
|
||||||
record = dict(zip(columns, row))
|
|
||||||
if record.get("name") != table:
|
|
||||||
continue
|
|
||||||
if schema is not None and record.get("schema") != schema:
|
|
||||||
continue
|
|
||||||
row_type = record.get("type")
|
|
||||||
if row_type in {"table", "view", "virtual", "shadow"}:
|
|
||||||
return row_type
|
|
||||||
except sqlite3.DatabaseError:
|
except sqlite3.DatabaseError:
|
||||||
pass
|
pass
|
||||||
return _sqlite_table_type_from_schema(conn, table, schema=schema)
|
return _sqlite_table_type_from_schema(conn, table, schema=schema)
|
||||||
|
|
||||||
|
|
||||||
def check_structured_write_table(conn, table: str, *, allow_missing=False):
|
|
||||||
"""Validate a row-write target on the connection that will perform the write."""
|
|
||||||
# SQLite resolves identifiers case-insensitively. The create API must not
|
|
||||||
# treat a differently cased existing name as a missing table.
|
|
||||||
row = conn.execute(
|
|
||||||
"select name from main.sqlite_master where name = ? collate nocase "
|
|
||||||
"and type in ('table', 'view')",
|
|
||||||
(table,),
|
|
||||||
).fetchone()
|
|
||||||
if row is None and allow_missing:
|
|
||||||
return
|
|
||||||
if row is not None and sqlite_table_type(conn, row[0]) == "table":
|
|
||||||
return
|
|
||||||
# Virtual table modules can interpret row writes as administrative operations.
|
|
||||||
# Their shadow tables are internal storage, not independently writable data.
|
|
||||||
raise ValueError("Structured writes require an ordinary table")
|
|
||||||
|
|
||||||
|
|
||||||
def sqlite_hidden_table_names(conn, *, schema: str | None = "main") -> list[str]:
|
def sqlite_hidden_table_names(conn, *, schema: str | None = "main") -> list[str]:
|
||||||
schema_table = _sqlite_schema_table(schema)
|
schema_table = _sqlite_schema_table(schema)
|
||||||
try:
|
try:
|
||||||
rows = conn.execute(
|
rows = conn.execute(
|
||||||
f"select name, sql from {schema_table} where type = 'table'"
|
"select name, sql from {} where type = 'table'".format(schema_table)
|
||||||
).fetchall()
|
).fetchall()
|
||||||
except sqlite3.DatabaseError:
|
except sqlite3.DatabaseError:
|
||||||
return []
|
return []
|
||||||
|
|
@ -161,63 +118,6 @@ def sqlite_hidden_table_names(conn, *, schema: str | None = "main") -> list[str]
|
||||||
return sorted(hidden_tables) + content_fts_tables
|
return sorted(hidden_tables) + content_fts_tables
|
||||||
|
|
||||||
|
|
||||||
def sqlite_derived_table_dependencies(
|
|
||||||
conn, *, schema: str | None = "main"
|
|
||||||
) -> dict[str, str]:
|
|
||||||
"""Return implementation table -> logical/content table dependencies.
|
|
||||||
|
|
||||||
``PRAGMA table_list`` safely identifies virtual and shadow tables, but
|
|
||||||
does not report which virtual table owns a shadow table or which table is
|
|
||||||
named by an FTS ``content=`` option. Derive those relationships from
|
|
||||||
``sqlite_master`` DDL and the documented shadow-table suffixes.
|
|
||||||
|
|
||||||
Database errors propagate: failed discovery must not be mistaken for an
|
|
||||||
empty dependency map and cached as permission to skip inheritance.
|
|
||||||
"""
|
|
||||||
schema_table = _sqlite_schema_table(schema)
|
|
||||||
rows = conn.execute(
|
|
||||||
f"select name, sql from {schema_table} where type = 'table'"
|
|
||||||
).fetchall()
|
|
||||||
|
|
||||||
table_names = {row[0] for row in rows}
|
|
||||||
# SQLite identifiers fold ASCII letters only.
|
|
||||||
identifier_case = str.maketrans(
|
|
||||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz"
|
|
||||||
)
|
|
||||||
canonical_names = {name.translate(identifier_case): name for name in table_names}
|
|
||||||
dependencies = {}
|
|
||||||
for virtual_table, sql in rows:
|
|
||||||
module = _virtual_table_module(sql)
|
|
||||||
if module is None:
|
|
||||||
continue
|
|
||||||
|
|
||||||
# SQLite's documented shadow tables are implementation details of
|
|
||||||
# their logical virtual table.
|
|
||||||
for suffix in _VIRTUAL_TABLE_SHADOW_SUFFIXES.get(module, ()):
|
|
||||||
shadow_table = virtual_table + suffix
|
|
||||||
if shadow_table in table_names:
|
|
||||||
dependencies[shadow_table] = virtual_table
|
|
||||||
|
|
||||||
# An external-content FTS table can expose values fetched from its
|
|
||||||
# content table, so it must also depend on that table's permission.
|
|
||||||
if module in {"fts3", "fts4", "fts5"}:
|
|
||||||
content_table = _fts_external_content_table(sql)
|
|
||||||
if content_table:
|
|
||||||
dependencies[virtual_table] = content_table
|
|
||||||
|
|
||||||
if module in {"fts5vocab", "fts4aux"}:
|
|
||||||
source = _fts_vocabulary_source(sql, module, schema or "main")
|
|
||||||
source = (
|
|
||||||
canonical_names.get(source.translate(identifier_case))
|
|
||||||
if source
|
|
||||||
else None
|
|
||||||
)
|
|
||||||
# An unresolved source is itself derived, so the one-hop policy denies it.
|
|
||||||
dependencies[virtual_table] = source or virtual_table
|
|
||||||
|
|
||||||
return dependencies
|
|
||||||
|
|
||||||
|
|
||||||
def _sqlite_table_type_from_schema(
|
def _sqlite_table_type_from_schema(
|
||||||
conn,
|
conn,
|
||||||
table: str,
|
table: str,
|
||||||
|
|
@ -227,7 +127,7 @@ def _sqlite_table_type_from_schema(
|
||||||
schema_table = _sqlite_schema_table(schema)
|
schema_table = _sqlite_schema_table(schema)
|
||||||
try:
|
try:
|
||||||
row = conn.execute(
|
row = conn.execute(
|
||||||
f"select type, sql from {schema_table} where name = ?",
|
"select type, sql from {} where name = ?".format(schema_table),
|
||||||
(table,),
|
(table,),
|
||||||
).fetchone()
|
).fetchone()
|
||||||
except sqlite3.DatabaseError:
|
except sqlite3.DatabaseError:
|
||||||
|
|
@ -255,7 +155,7 @@ def _is_known_shadow_table(
|
||||||
schema_table = _sqlite_schema_table(schema)
|
schema_table = _sqlite_schema_table(schema)
|
||||||
try:
|
try:
|
||||||
rows = conn.execute(
|
rows = conn.execute(
|
||||||
f"select name, sql from {schema_table} where type = 'table'"
|
"select name, sql from {} where type = 'table'".format(schema_table)
|
||||||
).fetchall()
|
).fetchall()
|
||||||
except sqlite3.DatabaseError:
|
except sqlite3.DatabaseError:
|
||||||
return False
|
return False
|
||||||
|
|
@ -274,7 +174,7 @@ def _sqlite_schema_table(schema: str | None) -> str:
|
||||||
return "sqlite_master"
|
return "sqlite_master"
|
||||||
if schema == "temp":
|
if schema == "temp":
|
||||||
return "sqlite_temp_master"
|
return "sqlite_temp_master"
|
||||||
return f"{_quote_identifier(schema)}.sqlite_master"
|
return "{}.sqlite_master".format(_quote_identifier(schema))
|
||||||
|
|
||||||
|
|
||||||
def _quote_identifier(value: str) -> str:
|
def _quote_identifier(value: str) -> str:
|
||||||
|
|
@ -284,151 +184,10 @@ def _quote_identifier(value: str) -> str:
|
||||||
def _virtual_table_module(sql: str | None) -> str | None:
|
def _virtual_table_module(sql: str | None) -> str | None:
|
||||||
if not sql:
|
if not sql:
|
||||||
return None
|
return None
|
||||||
match = _VIRTUAL_TABLE_MODULE_RE.search(_strip_sql_comments(sql))
|
|
||||||
if match is None:
|
|
||||||
return None
|
|
||||||
return _unquote_sql_value(match.group(1)).lower()
|
|
||||||
|
|
||||||
|
|
||||||
def _fts_external_content_table(sql: str | None) -> str | None:
|
|
||||||
"""Extract the external ``content=`` table from an FTS declaration."""
|
|
||||||
if not sql:
|
|
||||||
return None
|
|
||||||
sql = _strip_sql_comments(sql)
|
|
||||||
match = _VIRTUAL_TABLE_MODULE_RE.search(sql)
|
match = _VIRTUAL_TABLE_MODULE_RE.search(sql)
|
||||||
if match is None:
|
if match is None:
|
||||||
return None
|
return None
|
||||||
open_paren = sql.find("(", match.end())
|
return match.group(1).strip("\"'[]`").lower()
|
||||||
if open_paren == -1:
|
|
||||||
return None
|
|
||||||
close_paren = sql.rfind(")")
|
|
||||||
if close_paren <= open_paren:
|
|
||||||
return None
|
|
||||||
|
|
||||||
for argument in _split_sql_arguments(sql[open_paren + 1 : close_paren]):
|
|
||||||
key, separator, value = argument.partition("=")
|
|
||||||
if not separator or key.strip().lower() != "content":
|
|
||||||
continue
|
|
||||||
return _unquote_sql_value(value.strip())
|
|
||||||
return None
|
|
||||||
|
|
||||||
|
|
||||||
def _fts_vocabulary_source(sql: str, module: str, schema: str) -> str | None:
|
|
||||||
"""Resolve a vocabulary source within the current SQLite schema.
|
|
||||||
|
|
||||||
Cross-schema sources cannot be represented by the dependency map and
|
|
||||||
are conservatively left unresolved.
|
|
||||||
"""
|
|
||||||
sql = _strip_sql_comments(sql)
|
|
||||||
match = _VIRTUAL_TABLE_MODULE_RE.search(sql)
|
|
||||||
if match is None:
|
|
||||||
return None
|
|
||||||
start = sql.find("(", match.end())
|
|
||||||
end = sql.rfind(")")
|
|
||||||
if start < 0 or end <= start:
|
|
||||||
return None
|
|
||||||
arguments = [
|
|
||||||
_unquote_sql_value(arg.strip())
|
|
||||||
for arg in _split_sql_arguments(sql[start + 1 : end])
|
|
||||||
]
|
|
||||||
expected = 2 if module == "fts5vocab" else 1
|
|
||||||
if len(arguments) == expected:
|
|
||||||
return arguments[0]
|
|
||||||
if len(arguments) == expected + 1 and arguments[0].lower() == schema.lower():
|
|
||||||
return arguments[1]
|
|
||||||
return None
|
|
||||||
|
|
||||||
|
|
||||||
def _split_sql_arguments(arguments: str) -> list[str]:
|
|
||||||
"""Split comma-separated SQLite arguments without splitting quoted text."""
|
|
||||||
parts = []
|
|
||||||
start = 0
|
|
||||||
quote = None
|
|
||||||
closing_quote = None
|
|
||||||
index = 0
|
|
||||||
while index < len(arguments):
|
|
||||||
char = arguments[index]
|
|
||||||
if quote is None:
|
|
||||||
if char in {"'", '"', "`", "["}:
|
|
||||||
quote = char
|
|
||||||
closing_quote = "]" if char == "[" else char
|
|
||||||
elif char == ",":
|
|
||||||
parts.append(arguments[start:index])
|
|
||||||
start = index + 1
|
|
||||||
elif char == closing_quote:
|
|
||||||
# Single/double/backtick quoting escapes the delimiter by
|
|
||||||
# doubling it. Square-bracket identifiers do not.
|
|
||||||
if (
|
|
||||||
quote != "["
|
|
||||||
and index + 1 < len(arguments)
|
|
||||||
and arguments[index + 1] == closing_quote
|
|
||||||
):
|
|
||||||
index += 1
|
|
||||||
else:
|
|
||||||
quote = None
|
|
||||||
closing_quote = None
|
|
||||||
index += 1
|
|
||||||
parts.append(arguments[start:])
|
|
||||||
return parts
|
|
||||||
|
|
||||||
|
|
||||||
def _strip_sql_comments(sql: str) -> str:
|
|
||||||
"""Remove SQLite comments while preserving quoted strings/identifiers."""
|
|
||||||
output = []
|
|
||||||
quote = None
|
|
||||||
closing_quote = None
|
|
||||||
index = 0
|
|
||||||
while index < len(sql):
|
|
||||||
char = sql[index]
|
|
||||||
next_char = sql[index + 1] if index + 1 < len(sql) else ""
|
|
||||||
if quote is None:
|
|
||||||
if char in {"'", '"', "`", "["}:
|
|
||||||
quote = char
|
|
||||||
closing_quote = "]" if char == "[" else char
|
|
||||||
output.append(char)
|
|
||||||
elif char == "-" and next_char == "-":
|
|
||||||
index += 2
|
|
||||||
while index < len(sql) and sql[index] not in "\r\n":
|
|
||||||
index += 1
|
|
||||||
output.append(" ")
|
|
||||||
continue
|
|
||||||
elif char == "/" and next_char == "*":
|
|
||||||
index += 2
|
|
||||||
while index + 1 < len(sql) and sql[index : index + 2] != "*/":
|
|
||||||
index += 1
|
|
||||||
index = min(index + 2, len(sql))
|
|
||||||
output.append(" ")
|
|
||||||
continue
|
|
||||||
else:
|
|
||||||
output.append(char)
|
|
||||||
else:
|
|
||||||
output.append(char)
|
|
||||||
if char == closing_quote:
|
|
||||||
if (
|
|
||||||
quote != "["
|
|
||||||
and index + 1 < len(sql)
|
|
||||||
and sql[index + 1] == closing_quote
|
|
||||||
):
|
|
||||||
output.append(sql[index + 1])
|
|
||||||
index += 1
|
|
||||||
else:
|
|
||||||
quote = None
|
|
||||||
closing_quote = None
|
|
||||||
index += 1
|
|
||||||
return "".join(output)
|
|
||||||
|
|
||||||
|
|
||||||
def _unquote_sql_value(value: str) -> str:
|
|
||||||
if len(value) < 2:
|
|
||||||
return value
|
|
||||||
pairs = {"'": "'", '"': '"', "`": "`", "[": "]"}
|
|
||||||
closing = pairs.get(value[0])
|
|
||||||
if closing is None or value[-1] != closing:
|
|
||||||
return value
|
|
||||||
unquoted = value[1:-1]
|
|
||||||
if value[0] != "[":
|
|
||||||
unquoted = unquoted.replace(closing * 2, closing)
|
|
||||||
return unquoted
|
|
||||||
|
|
||||||
|
|
||||||
def _is_fts_content_virtual_table(sql: str | None) -> bool:
|
def _is_fts_content_virtual_table(sql: str | None) -> bool:
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,9 @@
|
||||||
import json
|
|
||||||
from urllib.parse import urlencode
|
|
||||||
|
|
||||||
from asgiref.sync import async_to_sync
|
from asgiref.sync import async_to_sync
|
||||||
|
from urllib.parse import urlencode
|
||||||
|
import json
|
||||||
|
|
||||||
# These wrapper classes pre-date the introduction of
|
# These wrapper classes pre-date the introduction of
|
||||||
# datasette.client and httpx2 to Datasette. They could
|
# datasette.client and httpx to Datasette. They could
|
||||||
# be removed if the Datasette tests are modified to
|
# be removed if the Datasette tests are modified to
|
||||||
# call datasette.client directly.
|
# call datasette.client directly.
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,2 +1,2 @@
|
||||||
__version__ = "1.0a40"
|
__version__ = "1.0a34"
|
||||||
__version_info__ = tuple(__version__.split("."))
|
__version_info__ = tuple(__version__.split("."))
|
||||||
|
|
|
||||||
|
|
@ -1,87 +1,2 @@
|
||||||
import dataclasses
|
|
||||||
import types
|
|
||||||
import typing
|
|
||||||
from dataclasses import dataclass
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass(frozen=True)
|
|
||||||
class ContextField:
|
|
||||||
name: str
|
|
||||||
type_name: str
|
|
||||||
help: str
|
|
||||||
from_extra: bool = False
|
|
||||||
|
|
||||||
|
|
||||||
def _type_name(type_):
|
|
||||||
if type_ is type(None):
|
|
||||||
return "None"
|
|
||||||
origin = typing.get_origin(type_)
|
|
||||||
args = typing.get_args(type_)
|
|
||||||
if origin in (typing.Union, types.UnionType):
|
|
||||||
return " | ".join(_type_name(arg) for arg in args)
|
|
||||||
if origin is not None:
|
|
||||||
name = getattr(origin, "__name__", str(origin).removeprefix("typing."))
|
|
||||||
return "{}[{}]".format(name, ", ".join(_type_name(arg) for arg in args))
|
|
||||||
return getattr(type_, "__name__", str(type_).removeprefix("typing."))
|
|
||||||
|
|
||||||
|
|
||||||
def from_extra():
|
|
||||||
"""
|
|
||||||
Declare a Context dataclass field whose value comes from a registered
|
|
||||||
Extra of the same name - its documentation is the Extra description,
|
|
||||||
so the doc string lives next to the resolve() code rather than being
|
|
||||||
duplicated on the dataclass.
|
|
||||||
"""
|
|
||||||
return dataclasses.field(metadata={"from_extra": True})
|
|
||||||
|
|
||||||
|
|
||||||
class Context:
|
class Context:
|
||||||
"Base class for all documented contexts"
|
"Base class for all documented contexts"
|
||||||
|
|
||||||
# Set on subclasses whose from_extra() fields should be resolved
|
|
||||||
# against the extras registry for this scope
|
|
||||||
extras_scope = None
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def documented_fields(cls):
|
|
||||||
"List of ContextField describing the documented fields of this context"
|
|
||||||
documented = []
|
|
||||||
for f in dataclasses.fields(cls):
|
|
||||||
if f.name.startswith("_"):
|
|
||||||
continue
|
|
||||||
is_from_extra = bool(f.metadata.get("from_extra"))
|
|
||||||
if is_from_extra:
|
|
||||||
help_text = cls._extra_description(f.name)
|
|
||||||
else:
|
|
||||||
help_text = f.metadata.get("help", "")
|
|
||||||
documented.append(
|
|
||||||
ContextField(
|
|
||||||
name=f.name,
|
|
||||||
type_name=_type_name(f.type),
|
|
||||||
help=help_text,
|
|
||||||
from_extra=is_from_extra,
|
|
||||||
)
|
|
||||||
)
|
|
||||||
return documented
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def _extra_description(cls, name):
|
|
||||||
# Imported lazily - table_extras is not needed just to define
|
|
||||||
# Context subclasses
|
|
||||||
from datasette.views.table_extras import table_extra_registry
|
|
||||||
|
|
||||||
try:
|
|
||||||
extra_class = table_extra_registry.classes_by_name[name]
|
|
||||||
except KeyError:
|
|
||||||
raise KeyError(
|
|
||||||
f"{cls.__name__}.{name} is declared with from_extra() but there is no "
|
|
||||||
"registered extra of that name"
|
|
||||||
)
|
|
||||||
if cls.extras_scope is not None and not extra_class.available_for(
|
|
||||||
cls.extras_scope
|
|
||||||
):
|
|
||||||
raise ValueError(
|
|
||||||
f"{cls.__name__}.{name} is declared with from_extra() but the {name} extra is "
|
|
||||||
f"not available for scope {cls.extras_scope}"
|
|
||||||
)
|
|
||||||
return extra_class.description or ""
|
|
||||||
|
|
|
||||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue