Skip to content

fix(console): resolve numeric :id by primary key, not a colliding slug#659

Closed
jirkamotejl wants to merge 1 commit into
devel_39from
fix/console-file-finder-numeric-slug-collision
Closed

fix(console): resolve numeric :id by primary key, not a colliding slug#659
jirkamotejl wants to merge 1 commit into
devel_39from
fix/console-file-finder-numeric-slug-collision

Conversation

@jirkamotejl

Copy link
Copy Markdown
Contributor

Problem

The console loads records via FriendlyId (@klass.friendly.find(params[:id])), which matches a numeric :id against the slug column before the primary key. A record whose slug equals another record's id hijacks the lookup.

This bites files: a file uploaded as 349444.jpg gets slug 349444. Once the folio_files id sequence reaches 349444, friendly.find("349444") returns the slug owner instead of the file at id 349444 — so opening / selecting (file picker) / editing that id shows the wrong file. It grows over time as the id sequence advances into the numeric range historically used as file names, and is invisible in the data (each record is fine; only the lookup is ambiguous).

Fix

  • find_console_resource (Folio::Console::BaseController): for a numeric :id, prefer the primary key (find_by(id:)), falling back to FriendlyId only for genuine non-numeric slugs. Slug-addressed resources (pages, articles — non-numeric slugs) are unaffected; the numeric branch never triggers for them.
  • Folio::File#normalize_friendly_id: file slugs can no longer be purely numeric (neutral fallback), so the slug and id namespaces never overlap going forward. This also protects the public download/video lookups that resolve files via FriendlyId.

Tests

  • New: file_picker_file_hash with a numeric id returns the file at that id even when another file's slug equals it (reproduces the bug; red before, green after).
  • New: a generated Folio::File slug is never purely numeric.
  • Green regression across file controllers, images, pages (slug-addressed), by_query, the slug unique-index spec, and shared files.

The console loads records via FriendlyId, which matches a numeric :id param
against the slug column before the primary key. A record whose slug equals
another record's id (e.g. a file uploaded as "349444.jpg" gets slug "349444",
colliding with id 349444) hijacked the lookup, so opening/selecting/editing
that id returned the wrong record.

- Console finder prefers the primary key for numeric params, falling back to
  FriendlyId only for genuine (non-numeric) slugs.
- Folio::File slugs can no longer be purely numeric, so the slug and id
  namespaces never overlap (covers public download/video lookups too).

@mreq mreq left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

👍

@jirkamotejl To uzce souvisi s PoC #658 kdy prepiname celou console ze slugu na ID. Je to ale relativne dalekosahle rozhodnuti, ktere si musime i s @ornsteinfilip posvetit.

@jirkamotejl

Copy link
Copy Markdown
Contributor Author

@mreq merguju zatím aspoň tohle, ať tam nemáme regresi

@jirkamotejl

Copy link
Copy Markdown
Contributor Author

Merged into master manually via merge commit 0298dde ("Merge fix/console-file-finder-numeric-slug-collision into master"), so GitHub didn't auto-flip this PR to Merged. Commit ab89550 is fully contained in master. Closing to reflect the real state.

@jirkamotejl jirkamotejl closed this Jul 2, 2026
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