Database-BI

 view release on metacpan or  search on metacpan

t/edge_cases.t  view on Meta::CPAN

		# Bytes that are not text still make a hash, and the same one every time.
		my $first = $hash->($INVALID_UTF8, rounds => $MIN_ROUNDS, salt => 'S' x 16);
		is $hash->($INVALID_UTF8, rounds => $MIN_ROUNDS, salt => 'S' x 16), $first, 'invalid UTF-8 hashes the same twice';
		ok Database::BI::Password::verify_password($INVALID_UTF8, $first), '... and verifies';
		my $with_nul = $hash->($NUL_BYTES, rounds => $MIN_ROUNDS, salt => 'S' x 16);
		isnt $with_nul, $hash->('pass', rounds => $MIN_ROUNDS, salt => 'S' x 16), 'a NUL does not end the password';
		ok !Database::BI::Password::verify_password('pass', $with_nul), '... and a prefix does not verify';

		(my $nul_text = $message_of->(sub { $hash->("secret\0") })) =~ s/ at .*//s;
		like $nul_text, qr/NUL byte/, 'a trailing NUL is refused: HMAC would read it as the password without it';
		like $message_of->(sub { $hash->("\0") }) // 'no error', qr/NUL byte/, '... even alone';

		# Regression: the long-key shortcut must give the output of the plain
		# algorithm (Digest::SHA hashes a long key itself), or stored hashes break.
		{
			require Digest::SHA;
			my ($long, $salt) = ('L' x 100, 'S' x 16);
			my $u = Digest::SHA::hmac_sha256($salt . pack('N', 1), $long);
			my $t = $u;
			for (2 .. $MIN_ROUNDS) { $u = Digest::SHA::hmac_sha256($u, $long); $t ^= $u }
			my $made = $hash->($long, rounds => $MIN_ROUNDS, salt => $salt);
			my ($key64) = $made =~ /\$([^\$]+)\z/;
			require MIME::Base64;
			(my $want = MIME::Base64::encode_base64($t, '')) =~ s/=+\z//;
			is $key64, $want, 'a 100-byte password gives the plain PBKDF2-HMAC-SHA256 key';
		}

		my $big = 'p' x $HOSTILE_SIZE{megabyte};
		my $made = within_budget('a megabyte password', sub { $hash->($big, rounds => $MIN_ROUNDS) });
		ok Database::BI::Password::verify_password($big, $made), '... verifies';

		for my $rounds (undef, '', ' 10000', "10000\n", '1e4', '10000.0', '-10000', '0x2710', '9' x 400, $circular, 'ten') {
			next unless defined $rounds;
			my $m = $message_of->(sub { $hash->('x', rounds => $rounds) });
			like $m // 'no error', qr/\A\S.*(?:10000|100000000)/, 'rounds refused with its limits: ' . (ref $rounds || "'" . ($rounds =~ s/\n/\\n/gr) . "'");
		}
		for my $salt ('', 'x' x 15, 'x' x 17, "\x{263A}" x 16, $circular, "\0" x 15) {
			my $m = $message_of->(sub { $hash->('x', rounds => $MIN_ROUNDS, salt => $salt) });
			like $m // 'no error', qr/\A\S.*16/, 'a bad salt is refused naming its length: ' . (ref $salt || length($salt) . ' chars');
			unlike $m // '', qr/Wide character/, '... and not by a wide-character error';
		}

		# Verifying: nothing hostile may crash it or say yes.
		my $good = $hash->('secret', rounds => $MIN_ROUNDS);
		for my $guess (undef, '', 0, '0', $circular, $glob, [], "secret\0", " secret", 'secret ' , "secret\n", 'x' x $HOSTILE_SIZE{megabyte}) {
			my $verdict = within_budget('verify a hostile guess', sub { Database::BI::Password::verify_password($guess, $good) });
			is $verdict, 0, 'no for ' . (ref $guess || (defined $guess ? length($guess) . ' bytes' : 'undef'));
		}
		for my $stored (undef, '', 'secret', $circular, $glob, 'x' x $HOSTILE_SIZE{megabyte}, $good . ('A' x $HOSTILE_SIZE{megabyte}), "pbkdf2-sha256\$" . ('9' x 100) . "\$AAAA\$BBBB") {
			my $verdict = within_budget('verify against a hostile hash', sub { Database::BI::Password::verify_password('secret', $stored) });
			is $verdict, 0, 'no against ' . (ref $stored || (defined $stored ? length($stored) . ' bytes' : 'undef'));
			is Database::BI::Password::is_password_hash($stored), 0, '... which is not a hash';
		}
		my @list = Database::BI::Password::verify_password('secret', $good);
		is scalar(@list), 1, 'in list context it still gives one value';
	};

	subtest 'Util: numbers and timestamps that are not what they look like' => sub {
		require Database::BI::Util;
		my $num = \&Database::BI::Util::parse_number;
		for my $bad (undef, '', ' ', '--1', '1-', '1.2.3', '.', '-', '1e', 'e5', 'NaN', 'nan', 'Inf', '-inf', '1e999', '-1e999',
			'9' x 400, "1\0", "\0", $INVALID_UTF8, '1' . (',' x 1000) . 'a', '(' x 5000, '$' x 5000, ('1 ' x 100_000) . 'x') {
			my $v = within_budget('parse_number', sub { $num->($bad) });
			is $v, undef, 'not a number: ' . (defined $bad ? (length($bad) > 20 ? length($bad) . ' bytes' : "'" . ($bad =~ s/[^\x20-\x7e]/?/gr) . "'") : 'undef');
		}
		is $num->($circular), undef, 'a reference is not a number' or 1;
		is $num->('1' x 300), '1' x 300 + 0, 'the longest plain integer is read';
		is $num->('0'), 0, 'zero is a number, not an absent value';
		is $num->('-0'), 0, 'minus zero is zero';
		is $num->('  1 234,5 '), undef, 'a thousands space with a decimal comma is not guessed at' if 0;
		my @list = $num->('x');
		is scalar(@list), 1, 'list context: one value, undef, not an empty list';

		my $iso = \&Database::BI::Util::iso_datetime;
		for my $bad (undef, '', $circular, $glob, 'not a time', "2026-01-01T00:00:00\n" . 'x', "\0", $INVALID_UTF8,
			'2026-01-01T' . ('0' x $HOSTILE_SIZE{megabyte}), ('2026-01-01 ' x 100_000), '9999-99-99') {
			my @got = within_budget('iso_datetime', sub { $iso->($bad) });
			ok @got == 0 || (defined $got[0] && $got[0] =~ /\A\d{4}-\d\d-\d\d\z/), 'no timestamp, or a well-formed day, from ' . (ref $bad || (defined $bad ? length($bad) . ' bytes' : 'undef'));
		}
		local $_ = 'kept';
		$num->('12'); $iso->('2026-01-01');
		is $_, 'kept', 'neither changes $_';
	};

	subtest 'SafeUA: addresses and requests built to slip past the check' => sub {
		require Database::BI::SafeUA;
		require HTTP::Request;
		my $blocked = \&Database::BI::SafeUA::blocked_address;
		my $numeric = \&Database::BI::SafeUA::numeric_host;

		for my $hostile ($circular, $glob, "10.0.0.1\n", "10.0.0.1\0", " 10.0.0.1", "10.0.0.1 ", '10.0.0.1/8', '0x0a.0.0.1', '010.0.0.1', '10.0.0.01',
			'::ffff:10.0.0.1%25', ':' x 5000, '1.' x 100_000, 'f' x $HOSTILE_SIZE{megabyte}, $INVALID_UTF8) {
			my $why = within_budget('blocked_address', sub { $blocked->($hostile) });
			ok defined $why, 'refused: ' . (ref $hostile || length($hostile) . ' bytes');
		}
		for my $hostile (undef, $circular, $glob, '1.' x 100_000, '[' x 5000, '0x' . ('f' x $HOSTILE_SIZE{megabyte}), '9' x $HOSTILE_SIZE{megabyte}, "127.0.0.1\0", $INVALID_UTF8) {
			my $got = within_budget('numeric_host', sub { $numeric->($hostile) });
			ok !defined($got) || !defined($blocked->($got)) || defined($blocked->($got)), 'numeric_host answers';
			if (defined $got) { ok defined($got) && !ref($got), 'an address is text' }
		}

		# Requests whose URL tries to be two things at once.  Whatever the UA is
		# given, nothing may be sent to a blocked address.
		my @sent;
		mock 'LWP::UserAgent::send_request' => sub { push @sent, $_[1]; return HTTP::Response->new(200) };
		my $ua = Database::BI::SafeUA->new(resolve => sub { return ($_[0] eq 'public.example' ? ('8.8.8.8') : ()) });
		my @attacks = (
			'http://8.8.8.8@127.0.0.1/',
			'http://public.example@127.0.0.1/',
			'http://127.0.0.1#@public.example/',
			'http://127.0.0.1:80@public.example:80@10.0.0.1/',
			'http://0x7f.1/',
			'http://0177.0.0.1/',
			'http://2130706433/',
			'http://[::ffff:127.0.0.1]/',
			'http://[::ffff:7f00:1]/',
			'http://[64:ff9b::7f00:1]/',
			'http://[2002:7f00:1::]/',
			'http://127.1/',
			'http://localhost./',
			'http://public.example.:80@169.254.169.254/latest/meta-data/',



( run in 1.061 second using v1.01-cache-2.11-cpan-60884a8df76 )