Kimi K3.1 Pro · Balanced

feature-can-edit-category-host-relationships-for-embedding-discourse-cursor

Discourse · Ruby · M · 100–299 lines · 6 files

The normal review, with one generalist agent. Judge: Claude Haiku 5.5.

Gold bugs found
4 of 4
Comments published
11
Comments matching a bug
4
Comments not matching
7
Cost of this PR
$1.912

Published comments (11)

mediumbugdb/migrate/20150818190757_create_embeddable_hosts.rb:9-11

Missing embed category result is dereferenced before fallback — When `embed_category` was never overridden, is blank, or no longer matches a category name, this query returns no rows and `[0]['id']` raises before the uncategorized fallback can run. Guard the missing result and fall back when it is absent instead of dereferencing it unconditionally.

mediumbugdb/migrate/20150818190757_create_embeddable_hosts.rb:24-25

Legacy embeddable hosts are migrated without host normalization — Legacy values are copied verbatim even though entries such as `http://discourse.org` and `https://example.com/1234` were supported, while `record_for_host` compares only `uri.host` with the stored value. Those previously working hosts stop authorizing embeds after upgrade; normalize schemes and paths with the same rules as `EmbeddableHost` before inserting them.

mediumsecuritydb/migrate/20150818190757_create_embeddable_hosts.rb:25

Stored embeddable host values allow SQL injection during migration — A stored `embeddable_hosts` entry is interpolated directly into executable SQL, so a quote can abort the migration and a crafted value can execute arbitrary SQL with database privileges during the upgrade. Parameterize the insert or escape `h` with the connection's quoting API instead of interpolating it.

mediumbugapp/models/embeddable_host.rb:5-7

Nil host causes NoMethodError in validation callback — Guard nil or assign a normalized `host.to_s` result before applying the substitutions.

mediumbugapp/models/embeddable_host.rb:14-17

Validation accepts values such as `example.com:3000`, but `URI(...).host` returns only… — Compare a consistently normalized host and optional port, or reject/strip ports when writing the record.

mediumbugapp/models/embeddable_host.rb:2

Five-character TLD cap rejects valid hostnames — The two-to-five-character TLD restriction rejects valid domains such as `example.museum` and `example.technology`, preventing those sites from being configured for embedding. Remove the five-character cap or validate the hostname without imposing this TLD-length limit.

mediumbugdb/migrate/20150818190757_create_embeddable_hosts.rb:3-7

Embeddable hosts lack case-insensitive uniqueness — The schema permits duplicate case-insensitive hostnames, but `record_for_host` uses only the first matching row, so duplicate entries with different categories make later mappings ineffective and confuse subsequent edits. Enforce case-insensitive host uniqueness in the model and back it with a unique functional database index.

mediumbugapp/controllers/admin/embeddable_hosts_controller.rb:9-16

Missing embeddable hosts cause 500s in update and destroy — Both `update` and `destroy` dereference the lookup result without checking it, so a stale or nonexistent ID causes a 500 through `save_host(nil)` or `host.destroy`. Return a 404 when no host is found before calling either operation.

mediumbugapp/assets/javascripts/admin/components/embeddable-host.js.es6:38

Stale categoryId clears the category badge after saving a new host — Resolve the category from the saved record's `category_id` and synchronize `categoryId` instead of reusing the stale input value.

mediumperformanceapp/models/embeddable_host.rb:17

Unindexed lowercase host lookup scans the host table on embed requests — Every embedded comments/count request now executes `lower(host) = ?` on a table with no host index, replacing the cached site-setting check and requiring a scan of the host table for denied requests. Add an index on `lower(host)` and normalize the query value consistently so this hot-path lookup can use it.

mediumbugapp/models/topic.rb:868-870

First-post expansion remains enabled without embeddable hosts — With `embed_truncate` enabled, an embedded topic still exposes the first-post expansion UI after the last embeddable host is deleted, whereas the previous behavior and the retained no-host spec require `false`. Restore an `EmbeddableHost.exists?` guard, and make that spec enable truncation and stub `has_topic_embed?` so it exercises the no-host branch.

Gold bugs found (4)

highThe migration reads execute(...)[0]['id'] for embed_category without checking for an empty result, so installs without that setting stored crash the migration instead of falling back to the uncategorized category.
mediumAfter saving a host, the component overwrites its category with findById(categoryId); when no category was chosen this is null even though the server saved the uncategorized category, so the row shows no category until reload.
highHosts saved with a port (allowed by the validation regex) are stored as host:port, but lookup compares only URI.host, so those configured hosts never match and embedding fails for them.
highEmbeddableHost's host format validation only accepts TLDs of 2-5 letters, so admins cannot add embeddable hosts on longer TLDs like example.technology.

Gold bugs missed (0)

None.