Amazon Ergo 1.0 · Balanced

enhance-embed-url-handling-and-validation-system-discourse-cursor

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

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

Gold bugs found
4 of 30
Comments published
6
Comments matching a bug
4
Comments not matching
2
Cost of this PR
$2.523

Published comments (6)

criticalsecurityapp/models/topic_embed.rb:22

Imported embed HTML is stored unsanitized, enabling stored XSS — A feed or fetched page can execute arbitrary script on the forum origin because `Post#cook` returns `raw_html` unchanged and the embed views render it with `raw`. Sanitize the imported HTML before passing it to `PostCreator`.

criticalsecurityapp/models/topic_embed.rb:13

Unescaped URL in embed attribution link enables stored XSS — A crafted `url` can break out of the single-quoted `href` and inject an event handler, executing script even when the imported body is sanitized. Escape it with `ERB::Util.html_escape` before building the attribution link.

highbugapp/jobs/scheduled/poll_feed.rb:35

Polling aborts when an RSS item has no content:encoded — An item with only `description` makes `i.content.scrub` raise `NoMethodError`, aborting the poll and preventing all remaining items from importing. Fall back to `i.description` and convert the value with `to_s` before calling `scrub`.

highbugapp/models/topic_embed.rb:32-36

Soft-deleted embedded post crashes feed import — When `embed.post` is nil for a soft-deleted post, `PostRevisor.new(nil).revise!` raises `NoMethodError`; with `retry: false`, one changed item stops all later feed imports. Guard the revision with `post.present?` and skip or recreate the stale embed.

mediumperformanceapp/controllers/embed_controller.rb:15

Embed page views can flood Sidekiq with duplicate retrieve_topic jobs — Each embed request without a topic adds another job, and the in-job 60-second throttle cannot shrink the queue, so a traffic spike can exhaust Redis/Sidekiq capacity. Dedupe `embed_url` using `Rails.cache.write` with `unless_exist` and a short expiry before calling `Jobs.enqueue`.

mediumbugapp/models/topic_embed.rb:78-80

topic_id_for_embed returns topic IDs for deleted topics — A stale `TopicEmbed` makes `EmbedController#best` choose the `topic_id` branch, where `TopicView` raises `Discourse::NotFound`; the embed then returns 404 indefinitely instead of enqueueing `RetrieveTopic`. Verify the `Topic` still exists and destroy or ignore stale embeds.

Gold bugs found (4)

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.
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.
mediumtopic_id_for_embed returns the stored topic_id even after the topic is deleted, and topic_embeds rows are never removed. /embed/best errors permanently for that URL, and TopicRetriever never re-creates the topic.

Gold bugs missed (26)

mediumThe postMessage handler in embed.js checks the sender with discourseUrl.indexOf(e.origin), a substring test instead of exact origin equality, so a page on an origin that is a substring of the Discourse URL can send forged discourse-resize messages to the embedding page.
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.
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.
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.
lowThe embed view formats post dates with a hard-coded strftime pattern, so users in non-English locales see English month names and a fixed order while the rest of the view is translated.
highThe Disqus importer now creates topics through TopicEmbed.import_remote, dropping the thread's created_at and the category option and requiring a live HTTP fetch per thread, so imported topics get wrong dates/categories and a dead link aborts the import.
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 'is 404 without an embed_url' spec never sets an embeddable host, so the request is rejected by ensure_embeddable before the embed_url check runs, and the test would pass even without that check.
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.
mediumembed_url is a varchar(255) column, so importing/embedding a URL longer than 255 characters fails on insert and that page can never get its discussion topic.
mediumPostMover#copy recreates the first post via PostCreator without passing cook_method, so a moved raw_html (embedded/imported) first post is re-cooked through the markdown pipeline and its content changes.
mediumEmbedController#best renders the 'loading' placeholder and still calls discourse_expires_in 1.minute, so the anonymous cache stores the placeholder under the embed URL and visitors keep seeing 'Loading Discussion...' for up to a minute after the topic exists.
mediumTopicEmbed.import wraps PostCreator#create in Topic.transaction, so PostCreator's post-transaction job enqueueing (process_post, feature_topic_users, etc.) runs before the outer transaction commits, and a worker that picks up ProcessPost early finds no post and skips post-processing for the imported topic.
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).
mediumTopicEmbed.import_remote fetches the embed URL with open(url).read and no timeout or size limit, so a slow or huge remote page can block the background job and exhaust memory.
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.
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.
highThe embed layout writes the full request.referer into the page, and the page is anon-cached for a minute under a key without the referer. One embedding page's full URL, including query secrets, can be served to other visitors.
mediumTopicEmbed.import checks post.present? after PostCreator#create, but PostCreator returns an unsaved post when it rolls back on host spam. TopicEmbed.create! then runs with nil ids and raises, which aborts the import.
mediumabsolutize_urls calls URI(url) without a rescue, so a malformed URL that passes the http regex raises URI::InvalidURIError and aborts the PollFeed import.
highPollFeed always HTML-unescapes item content, so feeds that already carry literal HTML (CDATA) get their escaped entities decoded a second time. Escaped code samples become live markup in the raw_html post.
mediumEmbedController#best's anonymous cache key leaves out the referer. Within the cache window, a request from a non-embeddable or missing referer gets the cached page, including X-Frame-Options ALLOWALL, without ensure_embeddable running.
mediumTopicRetriever#perform_retrieve runs PollFeed inline without a rescue, so a failing or malformed feed raises and aborts retrieval before the fetch_http fallback runs.
highPollFeed fetches feed_polling_url with Kernel#open, so a setting value starting with '|' runs a shell command on the job server. Anyone who can edit site settings gets command execution.
mediumPost#cook returns self.raw for raw_html posts and ignores the text it is given. Callers that cook different text, such as add_topic_to_quotes.rake, store cooked output that does not match the new raw.