Skip to content

fix: harden payload validation, PostgreSQL entry points and network handling - #96

Merged
andinux merged 20 commits into
mainfrom
fix/hardening
Nov 5, 2026
Merged

andinux merged 20 commits into
mainfrom
fix/hardening

Conversation

@andinux

@andinux andinux commented Nov 5, 2026

Copy link
Copy Markdown
Collaborator

Summary

Hardening across the SQLite and PostgreSQL extensions and the network layer, plus fixes found while reviewing it.

  • Payloads (both backends): stricter validation of payload fields, primary keys, block positions, clock values and sizes. Grow-only tables get the same clock checks as other tables.
  • PostgreSQL:
    • argument and type validation in PUBLIC-callable functions and in the cloudsync_changes trigger;
    • quoting of table and column names in generated SQL, and schema-qualified calls in generated triggers;
    • safer aggregate finalization and handling of NULL values in change payloads;
    • received text containing a NUL character is stored up to the NUL, with a warning naming the table and column, instead of failing the whole payload.
  • SQLite: stricter argument checks and quoting. cloudsync_set_filter() keeps the previous filter when a new one doesn't compile.
  • Metadata tables: only <table>_cloudsync tables of registered tables are counted, so lookalike tables are ignored. On SQLite the name match is case-insensitive. A dropped table never lowers cloudsync_db_version().
  • Network: stricter response handling, https-only addresses (except localhost), and credentials cleared when cloudsync_network_init_custom() changes address.

Behaviour changes

  • cloudsync_network_*, cloudsync_payload_save() and cloudsync_payload_load() can only be called directly, not from views, triggers or CHECK constraints. Direct queries, including recursive CTE loops, still work.
  • Network addresses must use https:// (http:// only for localhost).
  • cloudsync_network_init_custom() with a different address clears the previous token, API key and organization.
  • PostgreSQL: a role without SELECT on a metadata table gets a permission error, as on 1.2.0.

Notes

  • No extension upgrade script is needed. The extension SQL is unchanged since 1.2.0.
  • Existing PostgreSQL databases get the SECURITY DEFINER schema-version function on context load, when the session owns it. This step runs DDL inside the caller's transaction, so a rollback defers it to the next load. Sync roles never own the function, so it never runs on the apply path.
  • Docs: docs/postgresql/README.md lists the NUL limitation and recommends BLOB/BYTEA for binary or encrypted data.

Tests

  • New PostgreSQL regression tests 68–74, test/review_regressions.c, and an apply fuzzer in test/fuzz.
  • make unittest: all OK, 0 failed.
  • make network-unittest: all passed.
  • Standalone PostgreSQL suite: 620 PASS, 0 failures.

🤖 Generated with Claude Code

marcobambini and others added 20 commits November 2, 2026 12:00
…, SQL injection)

- cloudsync_changes_insert_trigger refuses a table that lacks the cloudsync_changes
  layout (attaching it to another table crashed the backend)
- a name value is no longer treated as a varlena (out-of-bounds read)
- cloudsync_update and database_value_dup copy every pass-by-reference datum
- payload and update aggregates refuse a window context
- database_column_blob only reads bytea columns
- single quotes in table/column names are escaped in generated SQL, trigger
  bodies use a dedicated dollar-quote tag, and the update trigger quotes the
  table literal correctly (tables with a ' in the name can now be initialized)
- add test 68_hardening; bump version to 1.2.1

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
- sqlite: validate argument counts/types in cloudsync_insert/delete/update
  instead of reading out of bounds
- core: reject pk blobs whose component count differs from the table's pks
  (merge_insert, merge_insert_col, merge_sentinel_only_insert)
- postgres: check NULL tbl in the cloudsync_changes insert trigger
- postgres: schema-qualify extension functions in generated triggers to
  defeat search_path hijacking
- network: make cloudsync_network_* DIRECTONLY (no longer INNOCUOUS)
- network: drop token/apikey/org when init_custom switches origin; require
  https (http only for loopback); validate database id and credentials

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
- network: cap responses at 1e9 bytes and always NUL-terminate the buffer
  (HTTP error bodies on binary downloads were read past their end)
- payload: reassemble v3 fragments only if staged bytes match total_size;
  cap total_size and LZ4 expanded_size at 1e9
- sqlite: cloudsync_payload_save/load are SQLITE_DIRECTONLY
- postgres: look up trigger names on the table being initialised
- postgres: use *_cloudsync tables only when they have the metadata shape,
  a same-owner base table and are readable by the caller
- postgres: bump_app_schema_version is SECURITY DEFINER with a pinned
  search_path; init is idempotent for roles that do not own the event trigger

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
…ew did not cover

- core: 16-byte site ids, block column names and position ids validated as
  well-formed order keys, clock values bounded, nine columns for every
  payload version, pk_decode no longer negates out-of-range magnitudes
- network: only 2xx is a success, ticket accepted from API endpoints only,
  balanced curl_global_cleanup, strict JSON, network.m refuses redirects
  and survives a missing endpoint
- sqlite: payload_chunks resume offset checked, zero-argument payload
  decode/apply, quoted blocks tables, escaped metadata suffix match with a
  shape filter, atomic set_filter/clear_filter, rowid left to SQLite
- postgres: text with NUL refused, payload_chunks NULL and resume checks,
  hashed trigger names beyond 63 bytes, quote-heavy column names,
  database_value_text rethrows non-data errors
- fractional-indexing allocator set once per process (data race)
- tests: apply fuzzer (ASan/UBSan, TSan, MSan), RLS isolation, low-finding
  regressions

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
The NULL col_value fix itself is already in the security fixes; keep the regression test.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…k case

pk_decode now refuses a magnitude past INT64_MAX, so the old 2^63+1 case no longer applies.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A hashed aggregate that is rescanned without being rebuilt finalizes the same
group state again. The final function encoded and released the state's buffer,
so a second call worked on freed memory. The state now caches the encoded result
and every later call returns a copy of it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…_changes view

The trigger function is PUBLIC-callable. Attached at statement level it has no
tuple to read; attached to another relation with the same columns it is not an
insert into the changes view. It now requires a row-level INSTEAD OF INSERT
trigger on the cloudsync_changes view of the extension's schema.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The encoder always writes zero as a positive integer, so a negative zero only
comes from a forged blob and would alias the key 0. The noncanonical pk test now
uses a wider-than-needed zero instead.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…alls to SECURITY DEFINER

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…at a NUL

Document that PostgreSQL text is stored up to the first NUL character and
that binary or encrypted data belongs in BLOB/BYTEA columns.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@andinux
andinux merged commit 11aa77f into main Nov 5, 2026
38 checks passed
@andinux
andinux deleted the fix/hardening branch November 5, 2026 15:48
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