Kimi K3.1 Pro · 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 Haiku 5.5.

Gold bugs found
5 of 12
Comments published
15
Comments matching a bug
5
Comments not matching
10
Cost of this PR
$4.906

Published comments (15)

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

Invalid `end if` terminator prevents the embed template from compiling — Compiling this template fails because `end if` is not a valid standalone ERB/Ruby block terminator, preventing `embed/best` from rendering. Replace it with `end`.

mediumsecurityapp/models/post.rb:130-133

Feed-supplied scripts and event handlers are stored unchanged and later rendered by… — Sanitize imported HTML with an allowlist before saving it or before returning it from `Post#cook`.

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

embed.css is missing from the asset precompile list — Production disables runtime asset compilation, but `embed.css` is absent from `config.assets.precompile`, so this layout references an unavailable stylesheet and can raise an asset-not-precompiled error. Register `embed.css` for precompilation.

mediumbugapp/jobs/scheduled/poll_feed.rb:35

Description-only feed items crash polling when content is nil — Valid RSS items that provide only `description` and no `content:encoded` can make `i.content` nil, so `.scrub` raises and aborts the entire polling run. Fall back to `i.description` and handle blank content before scrubbing.

mediumbugapp/models/topic_embed.rb:23-25

Rolled-back posts pass the presence check and abort embed imports — `PostCreator#create` returns an unsaved Post after a host-spam rollback, so `post.present?` still succeeds and `TopicEmbed.create!` receives `post_id=nil`, raising and aborting the import. Require `post.persisted?` or no creator errors before creating the embed record.

mediumbugapp/models/topic_embed.rb:62-72

Relative links and image sources are not resolved against the article URL — References such as `href="article.html"` and `src="images/a.png"` remain relative and resolve against Discourse instead of the source article, producing broken links and images. Resolve every relative reference with `URI.join(url, value)` rather than handling only strings beginning with `/`.

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

Substring origin check permits unauthorized postMessage resizing — A different origin whose serialized origin is a substring of `discourseUrl`—for example `https://example.co` against `https://example.com/`—passes this check and can arbitrarily resize the host page's iframe. Compare `e.origin` with the parsed Discourse origin exactly and validate `e.source`.

mediumperformanceapp/controllers/embed_controller.rb:15

Unauthenticated embed requests can enqueue unbounded retrieval work — Every distinct missing `embed_url` queues work before any throttle runs, and varying query strings or fragments bypasses the worker's full-URL key, allowing unauthenticated requests with a forged allowed Referer to grow Sidekiq and outbound-fetch work without bound. Rate-limit this endpoint and atomically deduplicate pending retrieval before enqueueing.

mediumperformanceapp/models/topic_embed.rb:48

Unbounded remote response reads can exhaust worker memory — `open(url).read` buffers the entire remote response before Readability parses it, so a sufficiently large response from an allowed-host URL can exhaust a worker's memory. Read with a strict byte limit and reject oversized responses before parsing.

mediumsecurityapp/models/topic_embed.rb:48

Automatic redirects bypass SSRF host validation for embed imports — `TopicRetriever#invalid_host?` validates only the initial URL, while `open(url)` follows HTTP redirects without rechecking the destination, allowing an allowed-host redirect to make the worker fetch internal services. Disable automatic redirects or revalidate the scheme, host, and resolved IP at every hop.

mediumperformancedb/migrate/20131219203905_add_cook_method_to_posts.rb:3

On the PostgreSQL versions targeted here, adding a non-null column with a default rewrites… — Add the column nullable without a default, backfill in batches, then apply the default and NOT NULL constraint separately.

mediumsecurityapp/controllers/embed_controller.rb:19

Anonymous embed caching bypasses referer validation — AnonymousCache keys entries only by `HTTP_HOST` and `REQUEST_URI`, so after an allowed request is cached, a request for the same `embed_url` with a missing or hostile Referer receives the cached page without `ensure_embeddable` running. Disable `discourse_expires_in` for this action or vary the cache key by the validated referer.

mediumbugapp/models/topic_embed.rb:57-60

absolutize_urls drops port 80 or 443 regardless of scheme — Only omit the port when it matches `uri.default_port`.

mediumbuglib/tasks/disqus.thor:148

Disqus root topics lose their original creation timestamps — The new `TopicEmbed.import_remote` call no longer passes `created_at: Date.parse(t[:created_at])`, so Disqus root topics are dated at import time while replies keep their original dates, corrupting thread chronology. Thread the original timestamp through `import_remote` and `TopicEmbed.import` to `PostCreator`.

mediumbuglib/tasks/disqus.thor:148

Disqus import drops the selected category — Preserve the CLI option and pass the resolved category through `TopicEmbed.import` to `PostCreator`.

Gold bugs found (5)

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

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