Skip to content

feat(THU-448): App version management: track device versions and enforce minimum version (BACKEND)#985

Merged
arienemaiara merged 4 commits into
mainfrom
THU-448/backend
Jun 26, 2026
Merged

feat(THU-448): App version management: track device versions and enforce minimum version (BACKEND)#985
arienemaiara merged 4 commits into
mainfrom
THU-448/backend

Conversation

@arienemaiara

@arienemaiara arienemaiara commented Jun 15, 2026

Copy link
Copy Markdown
Collaborator

Note

Medium Risk
Adds a nullable DB column and changes PowerSync token/device upsert behavior; misconfigured MIN_APP_VERSION fails startup but enforcement impact depends on the frontend companion change.

Overview
Adds minimum app version enforcement plumbing on the backend: MIN_APP_VERSION is read from the environment, validated as semver at startup, and returned from public GET /config as minAppVersion (omitted when unset so clients treat it as no enforcement).

Devices gain an app_version column (migration 0020). On PowerSync token issuance, the server reads X-App-Version (trimmed, max 64 chars), stores it via upsertDevice, and ignores oversized values. Clients cannot set app_version through PowerSync upload PATCH—it is listed in upload deny columns alongside trust/crypto fields.

Tests cover config exposure, settings validation, token persistence, header omission, long header rejection, and upload stripping.

Reviewed by Cursor Bugbot for commit 240d0c7. Bugbot is set up for automated code reviews on this repo. Configure here.

- Add app_version column to devices and persist X-App-Version on token issue
- Cap header at 32 chars; leave column untouched when header is absent
- Surface minAppVersion via GET /config (omitted when unset) for client-side enforcement
@arienemaiara arienemaiara changed the title feat(THU-448): track device app version and expose minAppVersion (BACKEND) feat(THU-448): App version management: track device versions and enforce minimum version (BACKEND) Jun 15, 2026
@github-actions

Copy link
Copy Markdown

Semgrep Security Scan

No security issues found.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 19b1bd2. Configure here.

Comment thread backend/src/db/powersync-schema.ts
@github-actions

github-actions Bot commented Jun 15, 2026

Copy link
Copy Markdown

PR Metrics

Metric Value
Lines changed (prod code) +37 / -3
JS bundle size (gzipped) 🔴 682.5 KB → 766.2 KB (+83.7 KB, +12.3%)
Test coverage 🟡 78.65% → 78.46% (-0.2%)
Performance (preview) Preview not ready — Render deploy may have timed out
Accessibility
Best Practices
SEO

Updated Thu, 25 Jun 2026 21:13:46 GMT · run #2057

@github-actions

github-actions Bot commented Jun 15, 2026

Copy link
Copy Markdown

Preview environment destroyed 🧹

Stack preview-pr-985 and its Cloudflare subdomain have been cleaned up.

- Server manages devices.app_version via X-App-Version header on requests
- Add app_version to PowerSync upload deny list so clients can't override it
- Test confirms PATCH strips app_version while letting other fields through

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔭 thunder-deep-review (advisory)

Reviewed the diff — no issues to report. ✅ Never approves, never requests changes, never gates merge.
head: 990449c2f7e5 · mode: deep · deferred 2 item(s) already reported by other bots (best-effort dedup)

Comment thread backend/src/config/settings.ts Outdated

// Minimum app version clients must run. Empty string disables enforcement.
// Surfaced to the frontend via GET /config; clients below this hard-block until they update.
minAppVersion: z.string().default(''),

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Would it make sense to validate that MIN_APP_VERSION is actually semver before the app boots? Right now a typo like 0,2,0 or banana starts up fine and gets served to every client, where the frontend will then choke trying to compare it. Since this file already uses superRefine for the PowerSync secret check, a quick regex guard there would fail fast at startup and save operators some painful debugging down the line.

Comment thread backend/src/api/config.ts
builtInAgentEnabled: !settings.disableBuiltInAgent,
allowCustomAgents: settings.allowCustomAgents,
// Omit when unset so the frontend treats it as "no enforcement" without parsing an empty string as semver.
minAppVersion: settings.minAppVersion || undefined,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The || undefined correctly omits empty strings, but a whitespace only value like MIN_APP_VERSION=' ' would slip through as truthy and reach every client as a padded string. Trimming either here or in the settings parser would harden this nicely. No test covers this edge case today either.

Comment thread backend/src/api/powersync.ts Outdated
rawDeviceName && rawDeviceName.length > 0 && rawDeviceName.length <= 100 ? rawDeviceName : 'Unknown device'
const rawAppVersion = request.headers.get('x-app-version')?.trim()
// Cap to a sane length so a malformed/oversized header never bloats the row.
const appVersion = rawAppVersion && rawAppVersion.length > 0 && rawAppVersion.length <= 32 ? rawAppVersion : undefined

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

One thing worth a quick thought: a full semver with prerelease and build metadata can exceed 32 characters (something like 1.234.567-rc.1+build.2024.01.01 is 35). When that happens the value gets silently dropped and the prior version is kept, which could confuse operators looking at device rows. If rich semver is on the roadmap for the frontend, bumping the cap or documenting why 32 was chosen would help future readers. Totally fine to defer if build metadata is never expected.

- Raise X-App-Version length cap from 32 to 64 to fit full semver
  with prerelease + build metadata (e.g. 1.234.567-rc.1+build.x).
- Trim and semver-validate MIN_APP_VERSION at startup so typos like
  "banana" or "0,2,0" fail fast instead of reaching clients and
  breaking version comparison.

@ital0 ital0 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Re-reviewed after the fixes and this all looks great. A few small optional notes below, nothing blocking.

lastSeen: timestamp('last_seen').defaultNow(),
createdAt: timestamp('created_at').defaultNow(),
revokedAt: timestamp('revoked_at'),
appVersion: text('app_version'),

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Quick confirm: since devices syncs SELECT *, app_version reaches clients but isn't declared in the frontend schema (src/db/tables.ts), so PowerSync just keeps it internally. Reads as a deliberate server side field, all good. If you ever want it in the device list UI, the follow up is adding appVersion to the frontend devicesTable.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Confirmed — that's the intent. app_version is server-managed (written from X-App-Version on /v1/token, denied on PowerSync uploads via uploadDenyColumns), so it lives on the synced devices table for operator visibility without needing client-side reads. Surfacing it in the device list UI is a natural follow-up: declare appVersion on src/db/tables.ts's devicesTable and it'll start landing in the SQLite store. Happy to file a ticket if useful.

Comment thread backend/src/api/config.ts
builtInAgentEnabled: !settings.disableBuiltInAgent,
allowCustomAgents: settings.allowCustomAgents,
// Omit when unset so the frontend treats it as "no enforcement" without parsing an empty string as semver.
minAppVersion: settings.minAppVersion || undefined,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Confirming intent: enforcement is client side only (the upgrade screen), and the backend still issues sync tokens and accepts uploads from clients below the floor. Reads as a UX nudge by design, so fine. If old clients ever need a hard block, that check would live on /powersync/token.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Confirmed — client-side UX nudge by design. The original ticket scoped this as "hard-block the app with a non-dismissable modal/screen … no access to chat, settings, or sync until they upgrade," which the upgrade screen + provider tree short-circuit covers from the user's side. Backend-side hard block on /v1/token (and likely /v1/powersync/upload) is the right place if/when we want server-enforced refusal — happy to file a follow-up if that becomes a requirement.

@arienemaiara arienemaiara merged commit 59093cb into main Jun 26, 2026
29 of 30 checks passed
@arienemaiara arienemaiara deleted the THU-448/backend branch June 26, 2026 15:54
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