From 918c296fe825fc93076dab5900207a3335684f6e Mon Sep 17 00:00:00 2001 From: Blake Erickson Date: Mon, 13 Jun 2022 11:47:44 -0600 Subject: [PATCH] SECURITY: banner-info (#17071) (#17073) --- app/controllers/application_controller.rb | 1 + spec/requests/application_controller_spec.rb | 49 ++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index dbe83dc2ef4..60644b22f68 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -642,6 +642,7 @@ class ApplicationController < ActionController::Base def banner_json json = ApplicationController.banner_json_cache["json"] + return "{}" if !current_user && SiteSetting.login_required? unless json topic = Topic.where(archetype: Archetype.banner).first diff --git a/spec/requests/application_controller_spec.rb b/spec/requests/application_controller_spec.rb index 4ffee8d2e32..bf3d26a9689 100644 --- a/spec/requests/application_controller_spec.rb +++ b/spec/requests/application_controller_spec.rb @@ -1007,4 +1007,53 @@ RSpec.describe ApplicationController do expect(response.status).to eq(200) end end + + describe "#banner_json" do + let(:admin) { Fabricate(:admin) } + let(:user) { Fabricate(:user) } + fab!(:banner_topic) { Fabricate(:banner_topic) } + fab!(:p1) { Fabricate(:post, topic: banner_topic, raw: "A banner topic") } + + before do + admin # to skip welcome wizard at home page `/` + end + + context "login_required" do + before do + SiteSetting.login_required = true + end + it "does not include banner info for anonymous users" do + get "/login" + + expect(response.body).to have_tag("div#data-preloaded") do |element| + json = JSON.parse(element.current_scope.attribute('data-preloaded').value) + expect(json['banner']).to eq("{}") + end + end + + it "includes banner info for logged-in users" do + sign_in(user) + get "/" + + expect(response.body).to have_tag("div#data-preloaded") do |element| + json = JSON.parse(element.current_scope.attribute('data-preloaded').value) + expect(JSON.parse(json['banner'])["html"]).to eq("

A banner topic

") + end + end + end + + context "login not required" do + before do + SiteSetting.login_required = false + end + it "does include banner info for anonymous users" do + get "/login" + + expect(response.body).to have_tag("div#data-preloaded") do |element| + json = JSON.parse(element.current_scope.attribute('data-preloaded').value) + expect(JSON.parse(json['banner'])["html"]).to eq("

A banner topic

") + end + end + end + end end