GLM-5.3 · Heavy
feature-can-edit-category-host-relationships-for-embedding-discourse-cursor
Discourse · Ruby · M · 100–299 lines · 6 files
The generalist plus 9 specialist lens agents in parallel, then the same verify and dedup steps. Judge: Claude Haiku 5.5.
- Gold bugs found
- 2 of 4
- Comments published
- 11
- Comments matching a bug
- 2
- Comments not matching
- 9
- Cost of this PR
- $3.285
Published comments (11)
Migration inserts legacy embeddable hosts without stripping scheme and path — The migration inserts legacy `embeddable_hosts` values verbatim via raw SQL, bypassing the model's `before_validation` normalization that strips `https?://` prefixes and paths. The new `EmbeddableHost.record_for_host` compares `lower(host) = uri.host`, so rows migrated as 'http://discourse.org' or 'https://example.com/1234' (formats the old `SiteSetting.allows_embeddable_host?` accepted by parsing `URI(h).host`) never match any referer, silently breaking embedding on upgraded sites until an admin re-saves each row.
Host comparison is case-sensitive on the referer side — `where("lower(host) = ?", host)` lowercases only the stored column while the referer's `uri.host` is left as-is, so a mixed-case referer (e.g. 'http://Eviltrout.com/page') or a mixed-case admin-entered host never matches even though the `lower()` shows case-insensitive matching was intended. DNS hostnames are case-insensitive, so validly configured hosts get falsely rejected and embedding fails for those sites.
Hosts saved with a port are never matched because record_for_host drops the port — A host saved with a port (explicitly permitted by the validation regex `(:[0-9]{1,5})?` and preserved by `before_validation`, which strips only protocol and path) can never be matched, because `record_for_host` compares `uri.host` — which excludes the port in Ruby — against the stored value via `where("lower(host) = ?", host)`. An admin who enters e.g. `example.com:8080` gets a permanently dead configuration: `EmbeddableHost.host_allowed?` returns false for every referer and embed_url from that host, so `EmbedController#ensure_embeddable` and `TopicRetriever#invalid_host?` reject all embedding from it.
Plural _hydrateEmbedded branch throws on null/non-array and keeps unhydrated keys with undefined… — The new plural branch of `_hydrateEmbedded` has no failure handling, unlike the guarded singular branch: `obj[k].map` throws a TypeError if a `_ids` key holds null or a non-array, and when `_lookupSubType` finds nothing for an id, the original key is still deleted and the model receives an array containing `undefined` entries — `hydrated || []` is dead code because `.map` always returns a truthy array. Since the regex change now routes every `_ids` key in every `__rest_serializer` payload through this branch (not just known associations), any such key that is null or whose ids aren't side-loaded in root either breaks hydration with an exception or silently corrupts the record's data.
Migration crashes with NoMethodError when embed_category setting row is absent — `execute(...)[0]['id']` raises NoMethodError when the `embed_category` site_settings row is missing or its value doesn't match any category name (PG::Result#[] returns nil for zero rows). This is the default state for nearly every install — Discourse only writes a site_settings row when a setting is explicitly changed, `embed_category` defaults to '' (which can never join on `c.name`), and fresh installs have an empty site_settings table — so `db:migrate` aborts with `undefined method '[]' for nil:NilClass` and blocks deploys.
Update and destroy crash with 500 when embeddable host id is missing or stale — `EmbeddableHost.where(id: params[:id]).first` returns nil when the id doesn't exist (e.g., the record was deleted in another admin tab or the request uses a stale/invalid id), and the result is used without a guard: `save_host(nil)` does `nil.host = ...` in update, and `destroy` calls `nil.destroy`. Both actions crash with NoMethodError and return a 500 instead of a 404.
update/destroy raise NoMethodError (500) when embeddable host id does not exist — `EmbeddableHost.where(id: params[:id]).first` returns nil when no host with that id exists (e.g. a double-delete from a stale admin UI or a PUT to a deleted id). Both `update` (save_host(nil) → `nil.host = ...`) and `destroy` (`nil.destroy`) then raise NoMethodError and answer with an unhandled 500 instead of a 404 for a missing resource.
update action ignores params and persists nothing, contradicting its name — The `update` action is named and invoked as an update — the route is `put "customize/embedding" => "embedding#update"` and the frontend controller's `saveChanges` action calls `this.get('embedding').update({})` — but the action ignores all request parameters and persists nothing; it only re-renders the serializer. A caller reading the name (or issuing the PUT) expects changes to be saved.
update/destroy raise NoMethodError on nil when host id doesn't exist — `update` and `destroy` do `EmbeddableHost.where(id: params[:id]).first` and then immediately call `save_host(host)` / `host.destroy` with no nil check. When the id doesn't match a record (stale id after a concurrent deletion by another admin, or a direct API request), this raises NoMethodError on nil and returns a 500 instead of a 404 — and in the JS delete flow the destroyRecord promise rejects unhandled, leaving the row stuck in the UI.
before_validation calls sub! on nil host, causing 500 instead of validation error — The `before_validation` block calls `self.host.sub!` without a nil guard. A create/update request with `host: null` (params[:embeddable_host][:host] is nil) raises NoMethodError on nil before `validates_format_of` can run, turning invalid input into an unhandled 500 instead of a rendered validation error.
"is false if embeddable_host is blank" test no longer tests anything — The test "it is false if embeddable_host is blank" verifies nothing after this PR's restructuring. It now sits outside the 'with an emeddable host' describe block, so besides no host being fabricated, `SiteSetting.embed_truncate` is left false and `has_topic_embed?` is never stubbed true (the topic is Fabricate.build with no topic embed).