Implement captcha check, don't reject entries yet.
continuous-integration/drone/push Build is passing Details

Report if captcha check failed but accept entry anyway.
This commit is contained in:
tastytea 2021-01-17 18:50:32 +01:00
parent 8b6a26d6b6
commit 62448b759a
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
3 changed files with 40 additions and 0 deletions

View File

@ -27,6 +27,7 @@
#include <unicode/unistr.h>
#include <algorithm>
#include <array>
#include <chrono>
#include <cstdint>
#include <fstream>
@ -68,6 +69,13 @@ entry_type parse_formdata()
throw SpamException{};
}
if (!captcha_valid(static_cast<std::uint8_t>(std::stoul(cgi("captcha_id"))),
cgi("captcha_answer")))
{
// throw CaptchaException{};
std::cout << "DEBUG: Captcha invalid. You can ignore this line.\r\n";
}
entry.instance = cgi("instance");
entry.tags = string_to_vector(cgi("tags"));
transform(entry.tags.begin(), entry.tags.end(), entry.tags.begin(),
@ -184,9 +192,24 @@ string text2html(string text)
return text;
}
bool captcha_valid(std::uint8_t id, const string_view answer)
{
std::array<string, 6> answers{"2", "6", "17", "12", "4", "1"};
if (answers.at(id) == answer)
{
return true;
}
return false;
}
} // namespace FediBlock::cgi
const char *SpamException::what() const noexcept
{
return "Spam detected.";
}
const char *CaptchaException::what() const noexcept
{
return "The solution to the captcha is not correct.";
}

View File

@ -19,6 +19,7 @@
#include "types.hpp"
#include <cstdint>
#include <exception>
#include <string>
#include <string_view>
@ -52,6 +53,9 @@ using std::vector;
// Replace certain characters with HTML tags or entities.
string text2html(string text);
// Check if the answer matches the solution we have for the id.
bool captcha_valid(std::uint8_t id, string_view answer);
} // namespace FediBlock::cgi
class SpamException : public std::exception
@ -61,4 +65,11 @@ public:
const char *what() const noexcept override;
};
class CaptchaException : public std::exception
{
public:
// NOLINTNEXTLINE(modernize-use-nodiscard)
const char *what() const noexcept override;
};
#endif // FEDIBLOCK_BACKEND_CGI_HPP

View File

@ -84,6 +84,12 @@ int main()
cerr << e.what() << '\n';
cout << e.what() << "\r\n";
}
catch (const CaptchaException &e)
{
cerr << e.what() << '\n';
cout << e.what() << "\r\n"
<< "Use the back button of your browser to try again.\r\n";
}
catch (const exception &e)
{
cerr << e.what() << '\n';