Skip to content

Commit

Permalink
core: fix ascii auth bug with missing newlines
Browse files Browse the repository at this point in the history
If a user supplies an authfile to -Y with a single user:pass and no
newline, we were cutting the final character of the password and failing
to authenticate.
  • Loading branch information
dormando committed Oct 20, 2024
1 parent 30f302f commit 8203c93
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
7 changes: 5 additions & 2 deletions authfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,13 @@ enum authfile_ret authfile_load(const char *file) {
return AUTHFILE_STATFAIL;
}

auth_data = calloc(1, sb.st_size + 1);
auth_data = calloc(1, sb.st_size + 2);

char *auth_cur = auth_data;
char *auth_end = auth_data + sb.st_size;
// fgets will stop at EOF or a newline, reading at most one bytes less
// than the size limit. If a user supplies a file without an ending
// newline we will end up chopping the last character of the password.
char *auth_end = auth_data + sb.st_size + 1;
auth_t *entry_cur = auth_entries;
int used = 0;

Expand Down
25 changes: 25 additions & 0 deletions t/ascii-auth2.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env perl
# Testing for single-line authfiles with no newline at the end.

use strict;
use Test::More tests => 4;
use FindBin qw($Bin);
use lib "$Bin/lib";
use MemcachedTest;

my $server = new_memcached("-Y $Bin/authfile2 -U 0");
my $sock = $server->sock;

# Fail to authenticate.
print $sock "set foo 0 0 7\r\nfoo bab\r\n";
like(scalar <$sock>, qr/CLIENT_ERROR/, "failed to authenticate");

# Try for real.
print $sock "set foo 0 0 7\r\nfoo bar\r\n";
like(scalar <$sock>, qr/STORED/, "authenticated?");

print $sock "set toast 0 0 2\r\nhi\r\n";
like(scalar <$sock>, qr/STORED/, "stored an item that didn't look like user/pass");

mem_get_is($sock, "toast", "hi");

1 change: 1 addition & 0 deletions t/authfile2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
foo:bar
1 change: 1 addition & 0 deletions t/whitespace.t
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ BEGIN {
push(@exempted, glob("*.orig"));
push(@exempted, glob(".*.swp"));
push(@exempted, glob("queue.h"));
push(@exempted, glob("t/authfile2"));
my %exempted_hash = map { $_ => 1 } @exempted;

my @stuff = split /\0/, `git ls-files -z -c -m -o --exclude-standard`;
Expand Down

0 comments on commit 8203c93

Please sign in to comment.