Skip to content

Harden database-explorer extension against write bypass and CSRF - #181

Open
RamyaR1010 wants to merge 1 commit into
github-samples:mainfrom
RamyaR1010:ramyar1010-harden-database-explorer-queries
Open

RamyaR1010 wants to merge 1 commit into
github-samples:mainfrom
RamyaR1010:ramyar1010-harden-database-explorer-queries

Conversation

@RamyaR1010

Copy link
Copy Markdown

Description

Hardens the database-explorer Copilot extension. Its read-only guard only checked that a query started with SELECT/WITH, so WITH x AS (SELECT 1) DELETE FROM games passed and ran on a read-write connection. Its local HTTP server also had no Host/Origin/CSRF protection, so a web page could reach it.

Related Issue

N/A (found in a security audit)

Type of Change

  • Bug fix (non-breaking change that fixes an issue)

Changes Made

  • Run each query after PRAGMA query_only = ON
  • Reject DML/DDL/PRAGMA keywords (outside string literals) in validated queries
  • Server: validate Host and Origin, require Content-Type: application/json on /api/query, and require a per-instance random token (X-Explorer-Token) on API routes

Testing

  • node --check on the extension passes.
  • Not run: npm run lint, npm run test:unit, npm run test:e2e (dependencies not installed in this worktree; the change only touches .github/extensions, outside the app and its test suites).
  • Not manually exercised in the canvas UI.

Checklist

  • My changes are focused on a single concern
  • I have written clear commit messages explaining what and why

- Run queries with PRAGMA query_only and reject DML/DDL keywords in WITH queries
- Require Host/Origin checks, JSON content type, and a per-instance token on the local server

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings November 3, 2026 06:14

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The SQL blacklist incorrectly blocks SQLite’s read-only replace() function, causing legitimate SELECT queries to fail through both /api/query and the canvas action.

1 open issue
What changed in this PR

Hardens the database-explorer Copilot extension against SQL write bypasses and cross-site requests to its local HTTP server.

Changes:

  • Enables SQLite query-only mode and adds SQL keyword validation.
  • Validates Host and Origin headers, requires an API token, and checks query request content types.
  • Updates the embedded UI to send the per-instance token.
File Description
.github/​extensions/​database-explorer/​extension.mjs Adds database read-only safeguards and local HTTP request protections.

Review effort: Balanced


// Blocks data-modifying statements hidden behind WITH; the read-only connection is the primary defense.
const withoutLiterals = statement.replace(/'(?:[^']|'')*'|"(?:[^"]|"")*"|`(?:[^`]|``)*`/g, " ");
if (/\b(INSERT|UPDATE|DELETE|REPLACE|DROP|ALTER|CREATE|ATTACH|DETACH|PRAGMA|VACUUM|REINDEX)\b/i.test(withoutLiterals)) {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants