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)
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.
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.
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.
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.
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.
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.
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.
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`.
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 %>`.
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 `//`.
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`.
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.
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.
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.