Resolves false-positive UnsafeRedirectError (#19297)

This commit is contained in:
Mac Siri 2023-04-07 09:17:56 -04:00 committed by GitHub
parent 6a80f51e02
commit 0813a0a322
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 29 deletions

View file

@ -165,8 +165,12 @@ class ApplicationController < ActionController::Base
end
end
def redirect_permanently_to(location)
redirect_to location + internal_nav_param, status: :moved_permanently
def redirect_permanently_to(url = nil, **args)
if url
redirect_to(url + internal_nav_param, status: :moved_permanently)
else
redirect_to(args.merge({ i: params[:i] }), status: :moved_permanently)
end
end
def customize_params

View file

@ -324,7 +324,7 @@ class StoriesController < ApplicationController
def redirect_to_lowercase_username
return unless params[:username]&.match?(/[[:upper:]]/)
redirect_permanently_to("/#{params[:username].downcase}")
redirect_permanently_to(action: :index, username: params[:username].downcase)
end
def set_user_json_ld

View file

@ -1,16 +1,16 @@
require "rails_helper"
RSpec.shared_examples "redirects to the lowercase route" do
context "when a path contains uppercase characters" do
it "redirects to the lowercase route" do
get path
expect(response).to have_http_status(:moved_permanently)
expect(response).to redirect_to(path.downcase)
end
end
end
RSpec.describe "StoriesIndex" do
it "redirects to the lowercase route", :aggregate_failures do
get "/Bad_name"
expect(response).to have_http_status(:moved_permanently)
expect(response).to redirect_to("/bad_name")
get "/Bad_name?i=i"
expect(response).to have_http_status(:moved_permanently)
expect(response).to redirect_to("/bad_name?i=i")
end
describe "GET stories index" do
let(:user) { create(:user) }
@ -346,10 +346,6 @@ RSpec.describe "StoriesIndex" do
end
describe "GET podcast index" do
include_examples "redirects to the lowercase route" do
let(:path) { "/#{build(:podcast).slug.upcase}" }
end
it "renders page with proper header" do
podcast = create(:podcast)
create(:podcast_episode, podcast: podcast)
@ -357,16 +353,4 @@ RSpec.describe "StoriesIndex" do
expect(response.body).to include(podcast.title)
end
end
describe "GET user_path" do
include_examples "redirects to the lowercase route" do
let(:path) { "/#{build(:user).username.upcase}" }
end
end
describe "GET organization_path" do
include_examples "redirects to the lowercase route" do
let(:path) { "/#{build(:organization).slug.upcase}" }
end
end
end