Amazon Ergo 1.0 · 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 Opus 5.5.
- Gold bugs found
- 13 of 30
- Comments published
- 18
- Comments matching a bug
- 13
- Comments not matching
- 5
- Cost of this PR
- $5.323
Published comments (18)
Imported-from URL is not HTML-escaped, allowing stored XSS — A `'` in the URL can break out of the `href` attribute and inject event handlers; because imported posts are rendered with `raw`/`html_safe`, this becomes stored XSS on the forum origin. Escape the URL in both the attribute and link text, or build the link with a tag helper.
Imported embed HTML is stored without sanitization — Feed or remote HTML can contain `<script>` or event-handler attributes that execute for every embed viewer because `cook_method: raw_html` and `raw post.cooked` bypass Discourse's normal sanitization; `absolutize_urls` does not remove active content. Sanitize imported HTML with a whitelist before storing it.
SSRF via redirect following in TopicEmbed.import_remote — An allowlisted URL can redirect `open(url)` to an internal or link-local address, bypassing `TopicRetriever#invalid_host?` and fetching the response server-side. Disable redirects or re-validate every redirect target.
embed_url column rejects valid URLs longer than 255 characters — Valid URLs over 255 characters fail on insert with a truncation error, or are silently truncated under permissive MySQL settings, so they cannot be imported reliably. Change `embed_url` from `varchar(255)` to a `text` column.
Malformed `<%- end if %>` breaks the embed view — The successful embed path cannot render because Ruby parses the trailing `if` in `<%- end if %>` as a modifier conditional without a condition, making the ERB template a syntax error. Replace it with `<%- end %>`.
One bad Disqus link aborts the entire import — A dead or malformed `t[:link]` now raises `OpenURI::HTTPError`, `SocketError`, or `URI::InvalidURIError` during the `open` in `TopicEmbed.import_remote`, aborting the whole Disqus import before the `post.present?` fallback. Rescue these fetch errors and skip the link, or make `import_remote` return `nil`.
Nil RSS item content aborts the entire feed poll — RSS items that only provide `description` leave `i.content` nil, so `i.content.scrub` raises `NoMethodError` and aborts the whole poll, including any `TopicRetriever` call that invoked it. Use `i.content.to_s.scrub` or fall back to `i.description`.
Trashed embedded post causes NoMethodError during import — The `Trashable` default scope makes `embed.post` `nil` for trashed posts, so `PostRevisor.new(nil).revise!` calls `@post.raw` in `should_revise?` and raises `NoMethodError`, breaking later updates and polls. Check `post.present?` before revising.
Every new embed URL triggers a synchronous full-feed poll — A burst of distinct embed pages can multiply full-feed downloads, parsing, and re-imports into substantial network and database load because throttling is per exact URL. Remove the inline `Jobs::PollFeed` call and rely on the scheduled poll, or use conditional/cached fetching.
Non-atomic throttle key setup can block an embed URL forever — If the process stops between `$redis.setnx` and `$redis.expire`, or the expiration fails, the key never expires and `retrieved_recently?` returns true forever, permanently blocking that URL. Use `$redis.set` with `nx: true, ex: 60` to set the value and TTL atomically.
Concurrent imports can abort on the unique embed_url index — Rescue the conflict and run the update path, or serialize imports per URL.
Substring-based postMessage origin check allows spoofed resize messages — An unrelated iframe, popup, or ad whose origin string occurs inside `discourseUrl` can pass `discourseUrl.indexOf(e.origin)` and send a `discourse-resize` message with an attacker-chosen height. Require `e.origin === new URL(discourseUrl).origin` and verify `e.source === iframe.contentWindow`.
Missing embed_url test passes without reaching the action — `ensure_embeddable` rejects the request before `params.require(:embed_url)` because `embeddable_host` is blank and there is no referer, so this test stays green even if the required-parameter handling is removed. Set a valid `embeddable_host` and matching referer, then assert the 400 response.
Test passes even if the feed_polling_enabled? guard is removed — Blank feed settings let the example pass without exercising the enabled guard, so removing it would go unnoticed. Stub `feed_polling_url` and `embed_by_username` to present values while `feed_polling_enabled?` is false so `poll_feed` is blocked only by that guard.
feed_polling_url test does not exercise the URL guard — With `feed_polling_enabled?` defaulting to false, the `&&` short-circuits before `feed_polling_url.present?`, so removing that guard would leave this test green. Enable polling and stub `embed_by_username` so the URL check is the only condition preventing `poll_feed`.
Test passes without exercising the embed_by_username guard — The default-disabled polling setting and blank feed URL prevent `poll_feed` before the `embed_by_username.present?` check, so this test would survive removing that guard. Enable polling and stub `feed_polling_url` to a present value to isolate the username condition.
Malformed feed URLs abort the entire import — Feed links can pass the `http(s)://` prefix check yet make `URI(url)` in `absolutize_urls` raise `URI::InvalidURIError`, aborting the entire poll or import. Rescue `URI::Error` around `URI.parse` and skip invalid items or return the content unchanged. **Also found in:** - `app/models/topic_embed.rb:11-11`
absolutize_urls mishandles relative and protocol-relative URLs — Relative references such as `images/photo.jpg` resolve against the Discourse embed URL, while `//cdn.example.com/x.jpg` is rewritten as a feed-host path, breaking imported links and images. Use `URI.join(url, href)` or `URI.join(url, src)` for non-protocol-relative references and leave `//` URLs intact.