-
Notifications
You must be signed in to change notification settings - Fork 0
Enhance embed URL handling and validation system #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: embed-url-handling-pre
Are you sure you want to change the base?
Enhance embed URL handling and validation system #7
Conversation
|
@claude review this PR |
1 similar comment
|
@claude review this PR |
|
Claude finished @akshayutture-augment's task —— View job Code Review Complete ✓I've reviewed the PR that adds embeddable Discourse comments functionality. Here's my comprehensive analysis: 🔴 Critical Security Issues1. XSS Vulnerability in embed.js (app/assets/javascripts/embed.js:17)if (discourseUrl.indexOf(e.origin) === -1) { return; }Issue: This validation is flawed. Example: If Fix: Use proper origin validation: if (e.origin !== discourseUrl.replace(/\/$/, '')) { return; }2. XSS Vulnerability in embed layout (app/views/layouts/embed.html.erb:11)parent.postMessage({...}, '<%= request.referer %>');Issue: Fix: Use JSON encoding or validate/sanitize the referer: parent.postMessage({...}, <%= request.referer.to_json %>);3. Potential XSS in embed view (app/views/embed/best.html.erb:19)<div class='cooked'><%= raw post.cooked %></div>Issue: Using Recommendation: Ensure RSS content is properly sanitized before storage, or add sanitization at render time for embed views. 4. Open Redirect Vulnerability (lib/topic_retriever.rb:52 & app/models/topic_embed.rb:48)TopicEmbed.import_remote(user, @embed_url)
# which calls:
open(url).readIssue: Fix: Validate URLs and use a safer HTTP library: return unless url =~ /^https?:\/\//
require 'open-uri'
open(url, redirect: false, open_timeout: 10, read_timeout: 30) do |f|
# process
end🟡 High Priority Issues5. Missing CSRF Protection (app/controllers/embed_controller.rb:2)skip_before_filter :check_xhrIssue: Skipping XHR checks for embed controller removes CSRF protection. While necessary for embedding, this should be documented and carefully validated. Recommendation: Add comprehensive origin validation to compensate. 6. Race Condition in TopicEmbed.import (app/models/topic_embed.rb:20-30)Issue: The transaction doesn't prevent race conditions. Multiple simultaneous requests with the same URL could create duplicate topics. Fix: Add database-level locking: embed = TopicEmbed.where(embed_url: url).lock.first7. SQL Injection Risk (app/jobs/scheduled/poll_feed.rb:35)content = CGI.unescapeHTML(i.content.scrub)
TopicEmbed.import(user, url, i.title, content)Issue: While not direct SQL injection, the RSS content is imported with Recommendation: Add content validation/sanitization even for raw HTML imports. 8. Referer Validation is Too Strict (app/controllers/embed_controller.rb:26)raise Discourse::InvalidAccess.new('invalid referer host') if URI(request.referer || '').host != SiteSetting.embeddable_hostIssue: Comparing only the host is insufficient - should also validate the scheme. Also, this breaks when referer has subdomains. Better approach: referer_uri = URI.parse(request.referer || '')
allowed_host = SiteSetting.embeddable_host
raise Discourse::InvalidAccess.new('invalid referer host') unless referer_uri.host == allowed_host || referer_uri.host&.end_with?(".#{allowed_host}")🟢 Code Quality & Best Practices9. Inconsistent Error Handling (lib/topic_retriever.rb:15-18)The 10. Missing Index on content_sha1 (db/migrate/20131217174004_create_topic_embeds.rb:11)Issue: The Recommendation: Add index if this field is queried frequently. 11. Magic Number (app/controllers/embed_controller.rb:13)@topic_view = TopicView.new(topic_id, current_user, {best: 5})Issue: The hardcoded 12. Missing Validation in Controller (app/controllers/embed_controller.rb:9)embed_url = params.require(:embed_url)Issue: While 13. Resource Exhaustion Risk (lib/topic_retriever.rb:27-28)$redis.expire(retrieved_key, 60)Issue: 60-second throttle might be too permissive for public endpoints. Consider rate limiting by IP or session as well. 14. Job Enqueueing Without User Context (app/controllers/embed_controller.rb:15)Jobs.enqueue(:retrieve_topic, user_id: current_user.try(:id), embed_url: embed_url)Issue: Anonymous users can trigger background jobs. While throttled, this could still be abused. Recommendation: Consider additional rate limiting or requiring authentication for first-time embeds. 📋 Test Coverage AnalysisGood coverage includes:
Missing test coverage:
🔧 Additional Recommendations
SummaryThis PR implements a solid foundation for embeddable comments, but has critical security vulnerabilities that must be addressed before merging:
The test coverage is reasonable but should be expanded to include security test cases. Recommendation: |
Test 4
Replicated from ai-code-review-evaluation/discourse-greptile#4