~cytrogen/masto-fe

1419f90ef2252e7ef87418b12749968a9eab3de9 — Claire 2 years ago 276c393
Fix some user-independent endpoints potentially reading session cookies (#24650)

M app/controllers/api/v1/instances/extended_descriptions_controller.rb => app/controllers/api/v1/instances/extended_descriptions_controller.rb +6 -0
@@ 2,11 2,17 @@

class Api::V1::Instances::ExtendedDescriptionsController < Api::BaseController
  skip_before_action :require_authenticated_user!, unless: :whitelist_mode?
  skip_around_action :set_locale

  before_action :set_extended_description

  vary_by ''

  # Override `current_user` to avoid reading session cookies unless in whitelist mode
  def current_user
    super if whitelist_mode?
  end

  def show
    cache_even_if_authenticated!
    render json: @extended_description, serializer: REST::ExtendedDescriptionSerializer

M app/controllers/api/v1/instances/peers_controller.rb => app/controllers/api/v1/instances/peers_controller.rb +6 -0
@@ 4,9 4,15 @@ class Api::V1::Instances::PeersController < Api::BaseController
  before_action :require_enabled_api!

  skip_before_action :require_authenticated_user!, unless: :whitelist_mode?
  skip_around_action :set_locale

  vary_by ''

  # Override `current_user` to avoid reading session cookies unless in whitelist mode
  def current_user
    super if whitelist_mode?
  end

  def index
    cache_even_if_authenticated!
    render_with_cache(expires_in: 1.day) { Instance.where.not(domain: DomainBlock.select(:domain)).pluck(:domain) }

M app/controllers/api/v1/instances/rules_controller.rb => app/controllers/api/v1/instances/rules_controller.rb +6 -0
@@ 2,11 2,17 @@

class Api::V1::Instances::RulesController < Api::BaseController
  skip_before_action :require_authenticated_user!, unless: :whitelist_mode?
  skip_around_action :set_locale

  before_action :set_rules

  vary_by ''

  # Override `current_user` to avoid reading session cookies unless in whitelist mode
  def current_user
    super if whitelist_mode?
  end

  def index
    cache_even_if_authenticated!
    render json: @rules, each_serializer: REST::RuleSerializer

M app/controllers/api/v1/instances_controller.rb => app/controllers/api/v1/instances_controller.rb +6 -0
@@ 2,9 2,15 @@

class Api::V1::InstancesController < Api::BaseController
  skip_before_action :require_authenticated_user!, unless: :whitelist_mode?
  skip_around_action :set_locale

  vary_by ''

  # Override `current_user` to avoid reading session cookies unless in whitelist mode
  def current_user
    super if whitelist_mode?
  end

  def show
    cache_even_if_authenticated!
    render_with_cache json: InstancePresenter.new, serializer: REST::V1::InstanceSerializer, root: 'instance'

M app/controllers/manifests_controller.rb => app/controllers/manifests_controller.rb +4 -0
@@ 1,6 1,10 @@
# frozen_string_literal: true

class ManifestsController < ActionController::Base # rubocop:disable Rails/ApplicationController
  # Prevent `active_model_serializer`'s `ActionController::Serialization` from calling `current_user`
  # and thus re-issuing session cookies
  serialization_scope nil

  def show
    expires_in 3.minutes, public: true
    render json: InstancePresenter.new, serializer: ManifestSerializer, root: 'instance'

M app/controllers/well_known/nodeinfo_controller.rb => app/controllers/well_known/nodeinfo_controller.rb +4 -0
@@ 4,6 4,10 @@ module WellKnown
  class NodeInfoController < ActionController::Base # rubocop:disable Rails/ApplicationController
    include CacheConcern

    # Prevent `active_model_serializer`'s `ActionController::Serialization` from calling `current_user`
    # and thus re-issuing session cookies
    serialization_scope nil

    def index
      expires_in 3.days, public: true
      render_with_cache json: {}, serializer: NodeInfo::DiscoverySerializer, adapter: NodeInfo::Adapter, expires_in: 3.days, root: 'nodeinfo'