Check if hash is valid

This commit is contained in:
tastytea 2018-11-30 06:48:45 +01:00
parent 513391f44b
commit 1aca78df88
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
4 changed files with 37 additions and 1 deletions

View File

@ -1,6 +1,6 @@
cmake_minimum_required (VERSION 3.2)
project(libravatarserv
VERSION 0.6.4
VERSION 0.6.5
LANGUAGES CXX
)

View File

@ -69,3 +69,31 @@ bool hash::fill_table()
}
return true;
}
bool hash::is_valid(const string &digest)
{
if (digest.length() != 64 && digest.length() != 32)
{
return false;
}
if (std::any_of(digest.begin(), digest.end(), not_hex))
{
return false;
}
return true;
}
bool hash::not_hex(const char &c)
{
if (c >= 0x61 && c <= 0x66)
{ // a-f
return false;
}
if (c >= 0x30 && c <= 0x39)
{ // 0-9
return false;
}
return true;
}

View File

@ -45,6 +45,12 @@ int main()
return 1;
}
http::Request avatar = http::parse_request(request);
if (!hash::is_valid(avatar.digest))
{
cout << "Status: 400 Bad Request\n\n";
cerr << "Error: Hash is invalid\n";
return 1;
}
if (!find_avatar_dir())
{

View File

@ -71,6 +71,8 @@ namespace hash // hash.cpp
const string md5(const string &text);
const string sha256(const string &text);
bool fill_table();
bool is_valid(const string &digest);
bool not_hex(const char &c);
}
namespace image // image.cpp