Skip to content
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

Make secure_compare fail on undef or empty string #1292

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions lib/Mojo/Util.pm
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,8 @@ sub quote {
}

sub secure_compare {
my ($one, $two) = @_;
return undef if length $one != length $two;
my ($one, $two) = (shift // '', shift // '');
return undef if !length $one or length $one != length $two;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid failing faster for ('', '1') than ('1', '') due to the shortcut conditional on $one only, you could multiply both lengths ensuring an equal operation on both $one and $two.

Suggested change
return undef if !length $one or length $one != length $two;
return undef if !(length $one * length $two) or length $one != length $two;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather see something like this

Suggested change
return undef if !length $one or length $one != length $two;
return undef if !length $one != length $two;
return undef unless length $one;

Copy link
Contributor

@karenetheridge karenetheridge Dec 7, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new line 268 is confusing to read without more parentheses. I'm not sure what part of the expression the ! in front of length is intending to surround.

my $r = 0;
$r |= ord(substr $one, $_) ^ ord(substr $two, $_) for 0 .. length($one) - 1;
return $r == 0;
Expand Down
3 changes: 3 additions & 0 deletions t/mojo/util.t
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,9 @@ ok !secure_compare('0♥', '♥'), 'values are not equal';
ok !secure_compare('0♥1', '1♥0'), 'values are not equal';
ok !secure_compare('', '♥'), 'values are not equal';
ok !secure_compare('♥', ''), 'values are not equal';
ok !secure_compare(undef, undef), 'values are not equal';
ok !secure_compare(undef, ''), 'values are not equal';
ok !secure_compare('', ''), 'values are not equal';

# xor_encode
is xor_encode('hello', 'foo'), "\x0e\x0a\x03\x0a\x00", 'right result';
Expand Down