GLM-5.3 · 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
2 of 12
Comments published
5
Comments matching a bug
2
Comments not matching
3
Cost of this PR
$1.139

Published comments (5)

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

Invalid `<%- end if %>` tag in embed template causes SyntaxError on every render — The `<%- end if %>` ERB tag compiles to a trailing `if` modifier with no condition after `end` — stdlib ERB joins compiled segments with `;` and the Rails Erubis handler likewise appends `;` after the tag body, producing invalid Ruby (`end if;`) and a SyntaxError when ActionView compiles the template. Every `/embed/best` request for a URL that already has a topic returns a 500, so the embed widget can never render comments (the controller specs never render views, so this is uncaught).

highbugapp/jobs/scheduled/poll_feed.rb:35

poll_feed crashes on Ruby 2.0 and nil content: String#scrub is undefined — `i.content.scrub` raises NoMethodError: `String#scrub` does not exist on Ruby 2.0.0 (the version this project runs and tests on per `.travis.yml`; the method was added in Ruby 2.1), and `i.content` is nil for feed items that lack a content element. The exception aborts `poll_feed` at the first item — with `sidekiq_options retry: false` the hourly job fails silently forever, so no feed items are ever imported and the RSS branch of `TopicRetriever#perform_retrieve` can never succeed (the specs stub `poll_feed`, so this path is untested).

highperformancedb/migrate/20131219203905_add_cook_method_to_posts.rb:3

NOT NULL default column add rewrites posts table under ACCESS EXCLUSIVE lock — `add_column :posts, :cook_method, :integer, default: 1, null: false` forces a full table rewrite under an ACCESS EXCLUSIVE lock on PostgreSQL 9.x (the no-rewrite optimization for constant defaults only landed in PG 11), blocking all reads and writes on the forum's largest, hottest table for the duration of the copy during deploy. Add the column as nullable with the default, backfill existing rows in batches, then attach the NOT NULL constraint.

mediumbugapp/models/topic_embed.rb:62-73

Protocol-relative URLs (//host/...) are broken by absolutize_urls — Protocol-relative URLs such as `//cdn.example.com/img.jpg` satisfy `href.start_with?('/')`, and `href.sub(/^\/+/, '')` strips both slashes, so the URL is rewritten to `http://<embed-host>/cdn.example.com/img.jpg`. Imported articles that use protocol-relative links or images (common in RSS/WordPress feeds and CDN assets) get every such link broken.

mediumbugapp/jobs/scheduled/poll_feed.rb:35

Double HTML-unescaping corrupts escaped HTML in imported feed items — `CGI.unescapeHTML` decodes the feed item's HTML a second time: SimpleRSS already returns XML-unescaped HTML, so content-level escapes such as `&lt;div&gt;` in an article's code sample become literal `<div>` markup, which is then stored via the `raw_html` cook_method and rendered with `raw post.cooked`. Every imported article containing escaped HTML examples or entity-encoded text is silently corrupted (and escaped tags become live tags) — drop the `CGI.unescapeHTML` call and pass `i.content` through as-is.

Gold bugs found (2)

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.

Gold bugs missed (10)

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