Skip to content

Commit

Permalink
AddressBook: validate I2P hostname
Browse files Browse the repository at this point in the history
New helper to validate I2P hostnames, and refactor subscription
validation to use it.

Refs monero-project#835
  • Loading branch information
coneiric committed Mar 22, 2018
1 parent f87ebc4 commit 18afce2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
23 changes: 14 additions & 9 deletions src/client/address_book/impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -348,13 +348,6 @@ AddressBook::ValidateSubscription(std::istream& stream) {
std::map<std::string, kovri::core::IdentityEx> addresses;
// To ensure valid Hostname=Base64Address
std::string line;
// To ensure valid hostname
// Note: uncomment if this regexp fails on some locales (to not rely on [a-z])
//const std::string alpha = "abcdefghijklmnopqrstuvwxyz";
// TODO(unassigned): expand when we want to venture beyond the .i2p TLD
// TODO(unassigned): IDN ccTLDs support?
// TODO(unassigned): investigate alternatives to regex (maybe boost::spirit?)
std::regex regex("(?=^.{1,253}$)(^(((?!-)[a-zA-Z0-9-]{1,63})|((?!-)[a-zA-Z0-9-]{1,63}\\.)+[a-zA-Z]+[(i2p)]{2,63})$)");
try {
// Read through stream, add to address book
while (std::getline(stream, line)) {
Expand All @@ -372,8 +365,7 @@ AddressBook::ValidateSubscription(std::istream& stream) {
// Ensure only valid lines
try
{
if (host.empty() || !std::regex_search(host, regex))
throw std::runtime_error("AddressBook: invalid hostname");
ValidateHost(host);
ident.FromBase64(addr);
}
catch (...)
Expand All @@ -394,6 +386,19 @@ AddressBook::ValidateSubscription(std::istream& stream) {
return addresses;
}

void AddressBook::ValidateHost(const std::string& host) const
{
// To ensure valid hostname
// Note: uncomment if this regexp fails on some locales (to not rely on [a-z])
//const std::string alpha = "abcdefghijklmnopqrstuvwxyz";
// TODO(unassigned): expand when we want to venture beyond the .i2p TLD
// TODO(unassigned): IDN ccTLDs support?
// TODO(unassigned): investigate alternatives to regex (maybe boost::spirit?)
std::regex regex("(?=^.{1,253}$)(^(((?!-)[a-zA-Z0-9-]{1,63})|((?!-)[a-zA-Z0-9-]{1,63}\\.)+[a-zA-Z]+[(i2p)]{2,63})$)");
if (host.empty() || !std::regex_search(host, regex))
throw std::runtime_error("AddressBook: invalid hostname");
}

// For in-net download only
bool AddressBook::CheckAddressIdentHashFound(
const std::string& address,
Expand Down
5 changes: 5 additions & 0 deletions src/client/address_book/impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ class AddressBook : public AddressBookDefaults {
const std::map<std::string, kovri::core::IdentityEx>
ValidateSubscription(std::istream& stream);

/// @brief Validates I2P hostname
/// @param host Human-readable I2P hostname to validate
/// @notes Throws if hostname is invalid
void ValidateHost(const std::string& host) const;

/// @brief Sets the download state as complete and resets timer as needed
/// @details Resets update timer according to the state of completed download
/// @param success True if successful download, false if not
Expand Down

0 comments on commit 18afce2

Please sign in to comment.