Skip to content

Commit

Permalink
Limit first name length to mitigate potential DoS
Browse files Browse the repository at this point in the history
  • Loading branch information
akadusei committed Jul 28, 2024
1 parent fd2212c commit 4f73d70
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
1 change: 1 addition & 0 deletions lang/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ en:
email_exists: Email is already taken
first_name_invalid: First name is not a valid name
first_name_required: First name is required
first_name_too_long: First name cannot be longer than %{max} characters
inactive_at_earlier: Inactive time cannot be earlier than active time
ip_address_required: IP address could not be determined
last_name_invalid: Last name is not a valid name
Expand Down
11 changes: 11 additions & 0 deletions spec/app/operations/mixins/validate_user_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,15 @@ describe Mixins::ValidateUser do
operation.last_name.should have_error("not a valid")
end
end

it "rejects long first name" do
SaveUser.create(params(
first_name: "f" * 300,
last_name: "Atta",
level: :author
)) do |operation, _|
operation.saved?.should be_false
operation.first_name.should have_error("longer than")
end
end
end
10 changes: 10 additions & 0 deletions src/operations/mixins/validate_user.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ module Mixins::ValidateUser

validate_first_name_valid
validate_last_name_valid

validate_first_name_length
end

private def validate_first_name_required
Expand All @@ -32,5 +34,13 @@ module Mixins::ValidateUser
validate_name last_name,
message: Rex.t(:"operation.error.last_name_invalid")
end

private def validate_first_name_length
max = 255

validate_size_of first_name,
max: max,
message: Rex.t(:"operation.error.first_name_too_long", max: max)
end
end
end

0 comments on commit 4f73d70

Please sign in to comment.