Merge pull request #335 from Coff0xc/fix/createdb-ident

fix(db): reject unsafe CREATE DATABASE identifiers
This commit is contained in:
BaaRai
2026-11-18 22:43:54 +01:00
committed by GitHub
3 changed files with 29 additions and 1 deletions
+1
View File
@@ -55,6 +55,7 @@ All notable changes to this project are documented in this file. The format is b
- **Change-password rate limit**: `POST /api/auth/change-password` uses the same limiter as login, keyed by client IP and by the session token, so a stolen session cannot guess the current password without backoff.
- **Ingested traffic headers**: `RecordExternal` rejects a method that is not an HTTP token and header names or values that contain CR, LF, or NUL, matching Replay, so ingested exchanges cannot inject extra header lines into stored evidence.
- **Browser MCP starts offline** (#329): the seeded `browser` MCP ran `npx @playwright/mcp`, which contacts the npm registry on every start even with the package installed globally, and Playwright MCP defaulted to a Chrome channel the image does not ship. It now runs the image's `playwright-mcp` binary with `--headless --browser chromium`. Existing databases that still have a stock `npx @playwright/mcp` row (including the common `-y` form) or a `playwright-mcp` row with no `--browser` are rewritten once; any other command or package is left alone. The image also installs the Chromium build `@playwright/mcp` and `@playwright/cli` expect, which is not the same build `playwright install` puts in the cache.
- **CREATE DATABASE identifiers**: `ensureDatabase` now builds the statement with `pgx.Identifier`, which quotes the DSN database name and doubles any embedded quote, so the name cannot close the identifier. No database name PostgreSQL accepts is refused, so existing installs with a hyphen, a dot or non-ASCII characters keep starting.
- **ask_operator image_path**: the screenshot path must resolve inside the task working directory (`<data>/tasks/<id>`). Absolute paths, `..` segments, and symlinks that leave that directory are refused, so the tool cannot copy an arbitrary server image into the operator transcript.
- **insert_assets fail-closed**: when intercept or task allow/block rules cannot be loaded, `insert_assets` refuses the whole call instead of evaluating an empty rule set (which allowed every asset). The tool result does not include the underlying database error.
- **BlobRange length no longer wraps int64**: `offset+length > size` overflowed when length was near MaxInt64, so the cap was skipped and `make([]byte, length)` could exhaust memory. The bound is now `length > size-offset`.
+10 -1
View File
@@ -25,6 +25,7 @@ import (
"time"
"github.com/Autumn-27/artex/config"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgconn"
_ "github.com/jackc/pgx/v5/stdlib" // pgx database/sql driver ("pgx")
)
@@ -156,13 +157,21 @@ func ensureDatabase(dsn string) error {
var exists bool
_ = admin.QueryRow(`SELECT true FROM pg_database WHERE datname=$1`, dbName).Scan(&exists)
if !exists {
if _, err := admin.Exec(`CREATE DATABASE "` + dbName + `"`); err != nil {
if _, err := admin.Exec(createDatabaseSQL(dbName)); err != nil {
return fmt.Errorf("create database %q: %w", dbName, err)
}
}
return nil
}
// createDatabaseSQL builds the CREATE DATABASE statement for a DSN-supplied
// database name. pgx.Identifier quotes it and doubles any embedded quote, so
// the name cannot close the identifier, and names PostgreSQL accepts but a
// restrictive allowlist would reject (hyphens, dots, non-ASCII) keep working.
func createDatabaseSQL(name string) string {
return "CREATE DATABASE " + pgx.Identifier{name}.Sanitize()
}
// Open connects, applies the schema (idempotent) and the embedded versioned
// migrations, and seeds builtin rows.
func Open(dsn string) (*DB, error) {
+18
View File
@@ -51,6 +51,24 @@ func TestApplySchemaRetriesOnlyDeadlocks(t *testing.T) {
}
}
func TestCreateDatabaseSQLQuotesName(t *testing.T) {
tests := []struct {
name string
want string
}{
{"artex", `CREATE DATABASE "artex"`},
{"artex-db", `CREATE DATABASE "artex-db"`},
{"café", `CREATE DATABASE "café"`},
{`artex"`, `CREATE DATABASE "artex"""`},
{`x"; DROP TABLE y; --`, `CREATE DATABASE "x""; DROP TABLE y; --"`},
}
for _, tt := range tests {
if got := createDatabaseSQL(tt.name); got != tt.want {
t.Errorf("createDatabaseSQL(%q)=%q, want %q", tt.name, got, tt.want)
}
}
}
// testDSN returns the configured DSN, skipping the test when neither the env var
// nor a config file supplies one (DSN no longer has a built-in default).
func testDSN(t *testing.T) string {