Skip to content

Commit

Permalink
Merge pull request #1188 from codidact/0valt/1169/fix-server-error-on…
Browse files Browse the repository at this point in the history
…-invalid-website-uri

Fix invalid profile website URI causing a 500 error page on invalid URIs
  • Loading branch information
Taeir authored Aug 23, 2023
2 parents 566683e + f9ce45c commit ed6a9d6
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -342,14 +342,25 @@ def edit_profile
render layout: 'without_sidebar'
end

def validate_profile_website(profile_params)
uri = profile_params[:website]

if URI.parse(uri).instance_of?(URI::Generic)
# URI::Generic indicates the user didn't include a protocol, so we'll add one now so that it can be
# parsed correctly in the view later on.
profile_params[:website] = "https://#{uri}"
end
rescue URI::InvalidURIError
profile_params.delete(:website)
flash[:danger] = 'Invalid profile website link.'
end

def update_profile
profile_params = params.require(:user).permit(:username, :profile_markdown, :website, :twitter, :discord)
profile_params[:twitter] = profile_params[:twitter].delete('@')

if profile_params[:website].present? && URI.parse(profile_params[:website]).instance_of?(URI::Generic)
# URI::Generic indicates the user didn't include a protocol, so we'll add one now so that it can be
# parsed correctly in the view later on.
profile_params[:website] = "https://#{profile_params[:website]}"
if profile_params[:website].present?
validate_profile_website(profile_params)
end

@user = current_user
Expand Down

0 comments on commit ed6a9d6

Please sign in to comment.