Kimi K3.1 Pro · Heavy

enhance-embed-url-handling-and-validation-system-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
5 of 12
Comments published
19
Comments matching a bug
5
Comments not matching
14
Cost of this PR
$7.223

Published comments (19)

mediumbugapp/jobs/scheduled/poll_feed.rb:35

Nil RSS item content aborts the entire feed polling pass — RSS items that provide only `description` leave `i.content` nil, so `.scrub` raises and aborts the whole feed pass, with `retry: false` preventing Sidekiq retries. Use `i.content || i.description` and skip items with no usable content.

mediumbugapp/models/topic_embed.rb:64-70

The `start_with?('/')` guard leaves ordinary relative links and images such as `images/a.png`… — Resolve relative references with `URI.join(url, value)` for both links and images.

mediumbugapp/models/topic_embed.rb:23-25

Rolled-back spam posts still reach embed creation — `PostCreator#create` returns `@post` even after `rollback_if_host_spam_detected` rolls back its save, and that unsaved object is still `present?`; `TopicEmbed.create!` then receives nil `topic_id`/`post_id` and raises instead of skipping the blocked item. Require `post.persisted?` and no creator errors before creating the embed.

mediumsecurityapp/models/topic_embed.rb:48

Remote embed fetch permits SSRF via unchecked redirects — `import_remote` opens the user-supplied `embed_url` after `TopicRetriever` checks only the initial hostname, while open-uri follows redirects without reapplying that check. An allowed-host URL that redirects to loopback or private-network services can therefore trigger internal requests; validate scheme, port, resolved addresses, and every redirect target, or disable redirects.

mediumperformanceapp/models/topic_embed.rb:48

Unbounded embed downloads can exhaust Sidekiq worker memory — `open(url).read` buffers the entire response with no byte limit in a job reachable through attacker-selected embed URLs. A large response on the allowed host can exhaust a Sidekiq worker's memory; stream with a strict maximum size and explicit open/read timeouts.

mediumperformanceapp/controllers/embed_controller.rb:15

Cache-miss embeds can flood Sidekiq before validation and throttling — Unknown embeds enqueue a Sidekiq job before `TopicRetriever` performs host validation or its Redis throttle, so repeated cache-miss requests using unique query-string variants can grow the queue even when every job is later rejected or throttled. Deduplicate, rate-limit, and validate `embed_url` before enqueueing.

mediumperformancedb/migrate/20131219203905_add_cook_method_to_posts.rb:3

Non-null default column addition rewrites and locks the posts table — Add it nullable without a default, backfill in batches, then add the default and constraint separately.

mediumbugapp/views/layouts/embed.html.erb:4

Embed stylesheet is not precompiled for production — Production precompilation does not generate `embed.css` because it is absent from `config.assets.precompile` in `config/application.rb:61`, while production disables runtime compilation, so this layout cannot load its stylesheet. Add `embed.css` to the precompile list.

mediumbugapp/models/topic_embed.rb:57-60

Port filtering strips meaningful non-default ports from rewritten URLs — Omit the port only when it is the default for `uri.scheme`.

mediumbuglib/tasks/disqus.thor:148

Disqus imports lose the original thread publication date — The new import call drops the previous `created_at: Date.parse(t[:created_at])` option, and `TopicEmbed.import` never forwards a timestamp to `PostCreator`, so every imported Disqus thread is dated at import time rather than its original publication date. Thread `created_at` through `import_remote` and `import` into `PostCreator`.

mediumbugapp/views/embed/best.html.erb:14

Post dates ignore locale-specific date formatting — The user-visible post date is hard-coded with `strftime("%e %b %Y")`, giving every locale the same day-month-year ordering and English month abbreviations. Unlike the surrounding embed labels, this bypasses I18n; use `I18n.l` with a localized date format instead.

mediumbugapp/models/topic_embed.rb:15-28

Concurrent imports of the same previously unseen URL can both pass the `embed.blank?` check and… — The unique `embed_url` index then makes one transaction fail with `ActiveRecord::RecordNotUnique`. Because that race is not handled, a PollFeed run can abort before processing its remaining items, or a RetrieveTopic job can fail, instead of returning the embed created by the other caller.

mediumbuglib/topic_retriever.rb:26-29

Non-atomic throttle setup can leave a key without an expiry — The throttle key is created with SETNX and given its TTL by a separate EXPIRE command. If the process dies or the second command fails after SETNX succeeds, `retrieved:<url>` has no expiry; subsequent throttled calls will always consider it recently retrieved, permanently preventing retrieval of that URL.

mediumbugapp/models/topic_embed.rb:62-72

Scheme-relative URLs are incorrectly rewritten to the article host — Scheme-relative (`//host/path`) links and image sources are rewritten to the article's host. `start_with?('/')` is true for these values and `sub(/^\/+/, '')` removes both slashes, so `//cdn.example/a.png` becomes `https://blog.example/cdn.example/a.png` rather than `https://cdn.example/a.png`.

mediumbugapp/controllers/embed_controller.rb:15

Embed controller spec still expects direct TopicRetriever calls — Retrieval now happens through `Jobs.enqueue(:retrieve_topic, ...)`, but `spec/controllers/embed_controller_spec.rb` still expects the controller to call `TopicRetriever.new` and `retrieve` directly. Those calls never occur in this action, leaving the example's mock expectations unsatisfied.

mediumbugapp/views/layouts/embed.html.erb:11

Full referer URL is an invalid postMessage target origin — `request.referer` is normally a full URL such as `http://eviltrout.com/2013/...`, but `postMessage`'s second argument must be an origin (`scheme://host[:port]`), not a URL with a path. Sending this resize message raises a `SyntaxError`, so the `discourse-resize` listener in `embed.js` never receives the height and the iframe is not resized.

mediumsecurityapp/models/post.rb:133

Raw HTML posts bypass sanitization and enable stored XSS — Returning `raw` for `raw_html` bypasses `PrettyText`'s sanitizer; `PollFeed` HTML-unescapes remote feed content and `embed/best` renders `post.cooked` with `raw`, so a malicious feed or article can persist scripts or event handlers that execute for Discourse readers. Sanitize imported HTML and its URL attributes with an allowlist before using `raw_html`, or keep it in the normal cooking pipeline.

mediumsecurityapp/assets/javascripts/embed.js:17

Origin substring check lets untrusted sites resize the iframe — This substring test does not authenticate the sender's origin. For example, with `discourseUrl = "https://community.example.com/"`, a message from the distinct origin `https://community.example.co` passes because that origin is a prefix of the configured URL, allowing that sender to issue `discourse-resize` messages and manipulate the iframe height.

mediumbugspec/jobs/poll_feed_spec.rb:12-16

feed_polling_enabled? spec leaves other prerequisites unset — The enabled-setting test leaves `feed_polling_url` and `embed_by_username` blank, so `poll_feed` remains skipped even if the `feed_polling_enabled?` check is removed. Stub the other two prerequisites to valid values so the disabled flag is the only condition preventing polling. **Also found in:** - `spec/jobs/poll_feed_spec.rb:18-22` - `spec/jobs/poll_feed_spec.rb:24-28`

Gold bugs found (5)

highPollFeed reads i.content.scrub for every feed item, so items without a content element (common in RSS feeds that only have description) raise NoMethodError and abort the import of the remaining items.
mediumabsolutize_urls only handles root-relative paths: protocol-relative URLs like //cdn.example.com/x.png are rewritten to http://host/cdn.example.com/x.png and path-relative links are left unresolved, breaking links and images in imported posts.
high`import_remote` fetches the embed URL with open-uri, which follows redirects, so the embeddable-host check on the initial URL can be bypassed to reach internal services (SSRF).
mediumThe "requires feed_polling_enabled?" spec leaves feed_polling_url and embed_by_username at their blank defaults, so poll_feed is skipped regardless and the test would pass even without the enabled check.
highThe embed layout references the 'embed' stylesheet and embed.js, but neither is in config.assets.precompile, so with precompiled assets in production the embed page fails to load them.

Gold bugs missed (7)

highThe referer and embed_url host checks compare against SiteSetting.embeddable_host case-sensitively, so a host differing only in case is rejected even though hostnames are case-insensitive.
criticalTopicEmbed.import appends a footer that interpolates the URL unescaped into an <a href='...'> tag in raw HTML that is later rendered unsanitized, so a crafted URL can inject markup; it also appends to contents with << without handling nil.
highWhen the embedded post has been deleted, TopicEmbed.import's update path passes a nil embed.post to PostRevisor and crashes on every later import of that URL.
mediumClaims import_remote crashes on unparseable pages (nil content) and dead URLs because it does no nil check or network error handling around open(url).read.
mediumEvery retrieval of an article not yet embedded synchronously polls and imports the entire RSS feed, and since throttling is per URL, many different article requests repeat the full feed download and processing.
mediumThe 'requires feed_polling_url' and 'requires embed_by_username' specs leave feed_polling_enabled at its false default, so they never reach the condition they name and would pass with those checks deleted.
mediumThe retrieve throttle is keyed by the full embed URL, so an anonymous client can vary the query string on the embeddable host to bypass it and make the server create an unbounded number of topics.