Skip to content

fix(api): return clean 404 for malformed tenant_id in namespace resolve#6405

Merged
gustavosbarreto merged 1 commit into
masterfrom
fix/6404-malformed-tenant-id-404
Jun 5, 2026
Merged

fix(api): return clean 404 for malformed tenant_id in namespace resolve#6405
gustavosbarreto merged 1 commit into
masterfrom
fix/6404-malformed-tenant-id-404

Conversation

@otavio
Copy link
Copy Markdown
Member

@otavio otavio commented Jun 3, 2026

Summary

When a device authenticates with a tenant_id that is not a valid UUID, the Postgres store passed the raw string straight into the uuid-typed namespaces.id column. The query failed with invalid input syntax for type uuid (SQLSTATE 22P02) and logged a misleading SQL error — even though the request still returned 404.

With the previous Mongo store, tenant_id was a plain string field, so a malformed value (e.g. a misconfigured agent sending 83176492-e6cl-..., a letter l instead of digit 1) simply matched nothing and returned a silent 404. This restores that behavior.

Change

NamespaceResolve now validates the value as a UUID (via the existing pkg/uuid.Parse) before issuing the query when resolving by tenant ID, returning store.ErrNoDocuments when invalid. This:

  • Restores Mongo parity — a malformed tenant_id is a clean not-found, no error logged.
  • Avoids a doomed database round-trip for malformed input.
  • Flows up exactly as a normal not-found: the service wraps it via NewErrNamespaceNotFound, which maps to HTTP 404. No service/handler changes needed.

The targeted per-resolver validation was chosen over a blanket SQLSTATE 22P02 → not-found mapping in fromSQLError, to avoid masking genuine type-mismatch bugs elsewhere.

Tests

Added a returns ErrNoDocuments for malformed tenant ID subtest to the shared TestNamespaceResolve suite, which runs against both Mongo and Postgres backends to verify parity. The Postgres suite passes locally (the Mongo run requires testcontainers networking unavailable in my local dev setup, but the Mongo path needs no code change — a non-matching string already returns ErrNoDocuments).

Notes

A related latent issue exists at sibling call sites that filter on the same uuid-typed namespace_id via the shared InNamespace query option (DeviceResolve, APIKeyResolve, PublicKeyResolve, SessionResolve). That's out of scope for this issue and could be a follow-up.

Fixes: #6404

Resolving a namespace by tenant_id passed the raw string straight into the
uuid-typed namespaces.id column on Postgres. A malformed value (e.g. a
misconfigured agent sending '83176492-e6cl-...') failed with SQLSTATE 22P02
and logged a misleading SQL error, even though the request still returned 404.

With the previous Mongo store, tenant_id was a plain string field, so a
malformed value simply matched nothing and returned a silent 404. Validate the
value as a UUID before it reaches the query and return store.ErrNoDocuments when
invalid, restoring that behavior and keeping the logs clean for misconfigured
agents.

Fixes: #6404
@otavio otavio requested a review from a team as a code owner June 3, 2026 19:33
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Jun 3, 2026

Claude finished @otavio's task in 5m 36s —— View job


Code Review Complete

Reviewed 2 files across code quality, security, testing, Go patterns, and architecture — no issues found. The code looks good as-is.

Review highlights:

  • UUID validation guard is correctly scoped to NamespaceTenantIDResolver only, using the project's existing pkg/uuid.Parse wrapper
  • store.ErrNoDocuments is the correct sentinel — consistent with every other not-found path in the store layer
  • The query was already parameterized (bun placeholder binding), so no injection risk existed; the fix prevents a misleading SQLSTATE 22P02 error
  • Test is placed in the shared storetest suite, validating both Mongo and Postgres backends
  • Cloud store inherits the fix automatically via its s.Store.NamespaceResolve() delegation — no cross-repo impact

To request another review round, comment /review.

@gustavosbarreto gustavosbarreto merged commit 2ebd286 into master Jun 5, 2026
12 checks passed
@gustavosbarreto gustavosbarreto deleted the fix/6404-malformed-tenant-id-404 branch June 5, 2026 14:00
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.

Malformed tenant_id in device auth logs a Postgres uuid error (22P02) instead of a clean 404

2 participants