Skip to content

FEATURE: Can edit category/host relationships for embedding#10

Draft
ShashankFC wants to merge 1 commit into
rest-serializer-enhancement-prefrom
rest-serializer-enhancement-post
Draft

FEATURE: Can edit category/host relationships for embedding#10
ShashankFC wants to merge 1 commit into
rest-serializer-enhancement-prefrom
rest-serializer-enhancement-post

Conversation

@ShashankFC

Copy link
Copy Markdown
Collaborator

@github-actions github-actions Bot added the i18n label Dec 31, 2025
@ShashankFC

Copy link
Copy Markdown
Collaborator Author

@claude

@claude

claude Bot commented Jan 7, 2026

Copy link
Copy Markdown

Claude finished @ShashankFC's task in 2m 22s —— View job


Code Review Complete

I've reviewed the PR "FEATURE: Can edit category/host relationships for embedding". This is a significant architectural change that migrates embedding host configuration from site settings to a proper database model with a full admin UI. Here's my detailed review:


Todo List

  • Check for repository guidelines (CLAUDE.md)
  • Review architectural changes (site_settings → database model)
  • Review frontend implementation (Ember.js components)
  • Review backend implementation (models, controllers, serializers)
  • Review database migration
  • Review test coverage

Overview

This PR moves embeddable host configuration from simple site settings (embeddable_hosts and embed_category) to a proper EmbeddableHost model with CRUD operations and a dedicated admin UI. This is a good architectural improvement that provides better data integrity and a more user-friendly interface.


Critical Issues

1. SQL Injection Vulnerability in Migration 🚨

Location: db/migrate/20150818190757_create_embeddable_hosts.rb:25

The migration concatenates user input directly into SQL:

execute "INSERT INTO embeddable_hosts (host, category_id, created_at, updated_at) VALUES ('#{h}', #{category_id}, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)"

This is a critical security vulnerability. Even though this is migration code running during deployment, it's processing data from site_settings which could theoretically contain malicious input.

Fix: Use parameterized queries or ActiveRecord methods:

records.each do |h|
  EmbeddableHost.create!(host: h, category_id: category_id)
end

2. Critical File Corruption - Category Fabricator Overwritten 🚨

Location: spec/fabricators/category_fabricator.rb

The entire category fabricator file was replaced with just the embeddable_host fabricator! This will break ALL tests that depend on category fabricators (:category, :diff_category, :happy_category, :private_category).

The embeddable_host fabricator should be in spec/fabricators/embeddable_host_fabricator.rb (which was added), NOT replacing the category fabricator.

Fix: Restore the category fabricator content and ensure only embeddable_host_fabricator.rb contains the new fabricator.


Major Issues

3. Incomplete Validation in EmbeddableHost Model

Location: app/models/embeddable_host.rb:2

The regex validation is complex but has issues:

  • Allows potentially invalid TLDs (.a through .zzzzz - should be 2-6+ chars for modern TLDs)
  • The port regex :[0-9]{1,5} allows invalid ports like :99999 (max is 65535)
  • The path component (\/.*)? should probably be removed since before_validation strips paths anyway

Recommendation: Simplify validation or add port range validation:

validates :host, presence: true, uniqueness: true
validate :valid_hostname

private
def valid_hostname
  # More robust hostname validation
end

4. Missing Nil Check Could Cause Error

Location: app/models/embeddable_host.rb:6-7

The before_validation block calls self.host.sub! without checking if host is nil:

self.host.sub!(/^https?:\/\//, '')
self.host.sub!(/\/.*$/, '')

This will raise NoMethodError if host is nil. Should add: return unless self.host.present?

5. Weak Error Handling in Controller

Location: app/controllers/admin/embeddable_hosts_controller.rb:14-17

The destroy action doesn't check if the host exists:

def destroy
  host = EmbeddableHost.where(id: params[:id]).first
  host.destroy  # Could be nil!
  render json: success_json
end

Should add: return render_json_error("Not found") unless host

6. Insufficient Test Coverage

Location: spec/controllers/admin/embeddable_hosts_controller_spec.rb

The controller spec only tests inheritance, not the actual CRUD operations. Should add tests for:

  • Creating valid/invalid hosts
  • Updating hosts
  • Deleting hosts
  • Authorization checks

Moderate Issues

7. Potential Race Condition in Store Hydration

Location: app/assets/javascripts/discourse/models/store.js.es6:222

The __rest_serializer flag check could benefit from documentation explaining when and why it's set, as this is a non-standard approach.

8. Missing Validation in Frontend Component

Location: app/assets/javascripts/admin/components/embeddable-host.js.es6:33-35

The component sends category_id but doesn't validate it exists. Should validate before save:

if (!this.get('categoryId')) {
  // Show error
  return;
}

9. Hardcoded OpenStruct in Controller

Location: app/controllers/admin/embedding_controller.rb:16-19

Using OpenStruct here is unusual and could be fragile. Consider creating a proper PORO or just returning the data directly in the serializer.

10. Migration Lacks Indexes

Location: db/migrate/20150818190757_create_embeddable_hosts.rb:3-7

The host column should have a unique index since it's validated as unique in the model:

add_index :embeddable_hosts, :host, unique: true
add_index :embeddable_hosts, :category_id

Minor Issues / Suggestions

11. Inconsistent Naming in REST Adapter

Location: app/assets/javascripts/discourse/adapters/rest.js.es6:1

The constant uses hyphens ('embeddable-host') while most of the codebase uses underscores. This is correct for Ember but worth noting for consistency.

12. Missing I18n Keys Validation

The PR adds new I18n keys in config/locales/client.en.yml but doesn't include validation that they're used correctly in templates.

13. Component Uses Deprecated sendAction

Location: app/assets/javascripts/admin/components/embeddable-host.js.es6:47,56

sendAction is deprecated in newer Ember versions. Should use closure actions instead:

this.deleteHost(this.get('host'));

14. Missing Down Migration

The migration doesn't define a down method or use reversible blocks, making it harder to rollback if needed.


Positive Aspects ✅

  1. Good architectural decision - Moving from site_settings to a proper model
  2. Comprehensive migration - Properly migrates existing data from site_settings
  3. Clean separation of concerns - Serializers, controllers, and models are well-structured
  4. Good test coverage for the model - embeddable_host_spec.rb covers key functionality
  5. Proper use of REST serializer flag - The rest_serializer: true flag enables proper nested object hydration
  6. Frontend validation - The component properly disables save when host is empty

Recommendations

Before Merging:

  1. [CRITICAL] Fix the SQL injection in the migration
  2. [CRITICAL] Restore the category_fabricator.rb file
  3. Fix the nil check in before_validation
  4. Add nil check in the destroy controller action
  5. Add comprehensive controller tests

Nice to Have:
6. Add database indexes
7. Improve model validation
8. Update to use closure actions instead of sendAction
9. Add down migration
10. Consider replacing OpenStruct with a proper object


Files Reviewed

  • 36 files changed (+449/-127)
  • Backend: Models, controllers, serializers, migrations
  • Frontend: Ember components, routes, templates, adapters
  • Tests: Model specs, controller specs, fabricators

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants