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)
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.
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.
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.
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.
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.
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.
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.
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.
Port filtering strips meaningful non-default ports from rewritten URLs — Omit the port only when it is the default for `uri.scheme`.
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`.
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.
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.
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.
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`.
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.
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.
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.
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.
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`