GLM-5.3 · 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
7 of 12
Comments published
14
Comments matching a bug
7
Comments not matching
7
Cost of this PR
$4.337

Published comments (14)

criticalsecurityapp/models/topic_embed.rb:11-13

Unescaped embed URL interpolated into raw HTML footer causes stored XSS — Because the footer post is stored with `cook_method: raw_html` and rendered raw, any quote or `<` in `url` becomes stored XSS; `embed_url` only passes a host check and RSS `<link>` values get no URI parsing. HTML-escape the URL before interpolating it into the I18n link argument.

criticalsecurityapp/models/post.rb:130-137

raw_html cook method bypasses the whitelist sanitizer, enabling stored XSS — `return raw if cook_method == Post.cook_methods[:raw_html]` persists unsanitized external HTML as `cooked`, so any `<script>` in the feed — including entity-encoded or MITM-injected over http — executes on the Discourse origin for every viewer. Run imported content through the whitelist sanitizer before storing it.

highbugapp/jobs/scheduled/poll_feed.rb:31-37

PollFeed crashes on nil content and String#scrub missing on Ruby 2.0 — `i.content.scrub` crashes the poll job in two ways: `i.content` is nil for feeds without `content:encoded`, and `String#scrub` doesn't exist on the project's pinned Ruby 2.0.0 (.ruby-version.sample), so with `retry: false` the hourly import dies silently. Guard nil content and use a 2.0-compatible scrub or drop it.

highbugapp/models/topic_embed.rb:31-38

Nil post in TopicEmbed.import crashes feed imports on stale embeds — If the topic or post backing an embed is deleted — nothing cleans up `topic_embeds` — `embed.post` is nil, so the next poll crashes in `revise!` via `PostRevisor.new(nil)`, aborting the `rss.items.each` loop and permanently stopping all imports. Guard `embed.post` and destroy or skip stale rows.

highbugapp/views/embed/best.html.erb:16-17

Best-of embed view crashes on posts from deleted users (post.user can be nil) — `UserDestroyer` nulls `posts.user_id` while keeping the posts (lib/user_destroyer.rb), so a best-of post from a deleted user raises NoMethodError here — a permanent 500 for that article's embedded comments. Fall back to a placeholder avatar and username when `post.user` is nil.

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

Embed assets not precompiled, breaking /embed/best in production — With `config.assets.compile = false` in production (config/environments/production.rb), `stylesheet_link_tag 'embed'` raises "embed.css isn't precompiled" and every `/embed/best` request returns a 500; `embed.js` can't be served at all. Add both assets to the precompile list in config/application.rb.

highsecuritylib/topic_retriever.rb:15

SSRF: open-uri follows redirects, defeating the embeddable host check — `invalid_host?` checks only the initial `embed_url` host, but `open(url)` follows redirects, so a redirecting URL on the embeddable host lets unauthenticated callers make the server fetch arbitrary internal targets and import their content into a publicly readable topic. Disable redirect following or re-validate each hop's host.

highbugapp/jobs/scheduled/poll_feed.rb:29-37

Poll feed has no error handling; nil content aborts every import — With `retry: false`, any transient failure in `open`, `SimpleRSS.parse`, or `i.content.scrub` aborts the run mid-iteration, and feeds whose items lack `<content>` (plain RSS 2.0 uses `description`) crash on nil every hour. Wrap the fetch/parse and each item in error handling, falling back to `i.description`.

highbugapp/views/embed/best.html.erb:6

Invalid ERB closer `<%- end if %>` breaks template compilation — `<%- end if %>` compiles to the incomplete Ruby statement `end if`, so the template fails to compile and every `embed/best` render — the feature's main success path — raises an ActionView error. Close the `if`/`else` block with plain `<%- end %>`.

mediumbugapp/models/topic_embed.rb:62-73

absolutize_urls mangles protocol-relative URLs by treating them as root-relative paths — `href.start_with?('/')` also matches protocol-relative URLs, and `href.sub(/^\/+/, '')` strips their leading slashes, turning `//cdn.example.com/img.jpg` into a broken path on the embed host; the same applies to `img` `src`. Only rewrite values that don't start with `//`.

mediumsecurityapp/assets/javascripts/embed.js:15-23

postMessage origin check uses substring match, allowing spoofed origins — `discourseUrl.indexOf(e.origin) === -1` accepts any origin that is a substring of the configured URL — e.g. an attacker-controlled `http://forums.com` matches `forums.company.com` — so a third-party window can spoof the channel and set arbitrary iframe heights. Compare `e.origin` for exact equality against the normalized origin of `discourseUrl`.

mediumbugspec/jobs/poll_feed_spec.rb:12-28

Guard-condition specs are vacuous: sibling settings not stubbed, so tests always pass — Each test stubs only one of the three guards in `execute`, leaving siblings like `feed_polling_url` and `embed_by_username` at unsatisfied defaults, so `poll_feed` is never called regardless — deleting any requirement wouldn't fail these specs. Stub the sibling settings to satisfying values in each test.

mediumbugspec/controllers/embed_controller_spec.rb:8-11

Spec never reaches action: embeddable_host not stubbed, so 404 path untested — Without `embeddable_host` stubbed, the `ensure_embeddable` filter raises `Discourse::InvalidAccess` before the action runs, so `params.require(:embed_url)` is never reached and the test passes even if that line is deleted. Stub `embeddable_host` (and a matching referer) and assert the actual 404 status.

mediumbuglib/topic_retriever.rb:26-28

Non-atomic setnx/expire can leave throttle key with no TTL, permanently blocking retrieval — If the process dies between `$redis.setnx` and `$redis.expire` in `retrieved_recently?`, the `retrieved:` key is left without a TTL and nothing ever clears it, permanently blocking that URL's retrieval and leaving its embed stuck loading. Set the key and TTL atomically in one command.

Gold bugs found (7)

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.
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.
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.
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).
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 (5)

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.
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 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.