Skip to content

Begin modifying code that removes trailing slashes #2255

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions app/controllers/projects_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -848,10 +848,28 @@
end

# Clean up url; returns nil if given nil.
# In particular, remove trailing slashes
def clean_url(url)
return url if url.nil?
return url if url.blank?
return url if url == '/'
return url if url.last != '/'

# Remove trailing slashes.
# Attackers could make this version slow: url.gsub(%r{\/+\z}, '')
# Start from the last character and move backwards
i = url.length - 1

# Find the index of the last non-slash character
while i >= 0 && url[i] == '/'
i -= 1
end

url.gsub(%r{\/+\z}, '')
# If the string was all slashes or empty, return a single slash
if i == -1
return '/'

Check warning on line 869 in app/controllers/projects_controller.rb

View check run for this annotation

Codecov / codecov/patch

app/controllers/projects_controller.rb#L869

Added line #L869 was not covered by tests
else
return url[0..i]
end
end
end
# rubocop:enable Metrics/ClassLength
Loading