1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
|
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/dns/dns_names_util.h"
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <string>
#include <vector>
#include "base/big_endian.h"
#include "base/check.h"
#include "base/containers/span.h"
#include "base/strings/string_piece.h"
#include "net/base/ip_address.h"
#include "net/base/url_util.h"
#include "net/dns/public/dns_protocol.h"
#include "third_party/abseil-cpp/absl/types/optional.h"
#include "url/third_party/mozilla/url_parse.h"
#include "url/url_canon.h"
#include "url/url_canon_stdstring.h"
namespace net::dns_names_util {
bool IsValidDnsName(base::StringPiece dotted_form_name) {
return DottedNameToNetwork(dotted_form_name,
/*require_valid_internet_hostname=*/false)
.has_value();
}
bool IsValidDnsRecordName(base::StringPiece dotted_form_name) {
IPAddress ip_address;
return IsValidDnsName(dotted_form_name) &&
!HostStringIsLocalhost(dotted_form_name) &&
!ip_address.AssignFromIPLiteral(dotted_form_name) &&
!ParseURLHostnameToAddress(dotted_form_name, &ip_address);
}
// Based on DJB's public domain code.
absl::optional<std::vector<uint8_t>> DottedNameToNetwork(
base::StringPiece dotted_form_name,
bool require_valid_internet_hostname) {
// Use full IsCanonicalizedHostCompliant() validation if not
// `is_unrestricted`. All subsequent validity checks should not apply unless
// `is_unrestricted` because IsCanonicalizedHostCompliant() is expected to be
// more strict than any validation here.
if (require_valid_internet_hostname &&
!IsCanonicalizedHostCompliant(dotted_form_name))
return absl::nullopt;
const char* buf = dotted_form_name.data();
size_t n = dotted_form_name.size();
uint8_t label[dns_protocol::kMaxLabelLength];
size_t labellen = 0; /* <= sizeof label */
std::vector<uint8_t> name(dns_protocol::kMaxNameLength, 0);
size_t namelen = 0; /* <= sizeof name */
char ch;
for (;;) {
if (!n)
break;
ch = *buf++;
--n;
if (ch == '.') {
// Don't allow empty labels per http://crbug.com/456391.
if (!labellen) {
DCHECK(!require_valid_internet_hostname);
return absl::nullopt;
}
if (namelen + labellen + 1 > name.size()) {
DCHECK(!require_valid_internet_hostname);
return absl::nullopt;
}
name[namelen++] = static_cast<uint8_t>(labellen);
memcpy(name.data() + namelen, label, labellen);
namelen += labellen;
labellen = 0;
continue;
}
if (labellen >= sizeof(label)) {
DCHECK(!require_valid_internet_hostname);
return absl::nullopt;
}
label[labellen++] = ch;
}
// Allow empty label at end of name to disable suffix search.
if (labellen) {
if (namelen + labellen + 1 > name.size()) {
DCHECK(!require_valid_internet_hostname);
return absl::nullopt;
}
name[namelen++] = static_cast<uint8_t>(labellen);
memcpy(name.data() + namelen, label, labellen);
namelen += labellen;
labellen = 0;
}
if (namelen + 1 > name.size()) {
DCHECK(!require_valid_internet_hostname);
return absl::nullopt;
}
if (namelen == 0) { // Empty names e.g. "", "." are not valid.
DCHECK(!require_valid_internet_hostname);
return absl::nullopt;
}
name[namelen++] = 0; // This is the root label (of length 0).
name.resize(namelen);
return name;
}
absl::optional<std::string> NetworkToDottedName(
base::span<const uint8_t> dns_network_wire_name,
bool require_complete) {
base::BigEndianReader reader(dns_network_wire_name.data(),
dns_network_wire_name.size());
return NetworkToDottedName(reader, require_complete);
}
absl::optional<std::string> NetworkToDottedName(
base::StringPiece dns_network_wire_name,
bool require_complete) {
auto reader = base::BigEndianReader::FromStringPiece(dns_network_wire_name);
return NetworkToDottedName(reader, require_complete);
}
absl::optional<std::string> NetworkToDottedName(base::BigEndianReader& reader,
bool require_complete) {
std::string ret;
size_t octets_read = 0;
while (reader.remaining() > 0) {
// DNS name compression not allowed because it does not make sense without
// the context of a full DNS message.
if ((*reader.ptr() & dns_protocol::kLabelMask) ==
dns_protocol::kLabelPointer)
return absl::nullopt;
base::StringPiece label;
if (!reader.ReadU8LengthPrefixed(&label))
return absl::nullopt;
// Final zero-length label not included in size enforcement.
if (label.size() != 0)
octets_read += label.size() + 1;
if (label.size() > dns_protocol::kMaxLabelLength)
return absl::nullopt;
if (octets_read > dns_protocol::kMaxNameLength)
return absl::nullopt;
if (label.size() == 0)
return ret;
if (!ret.empty())
ret.append(".");
ret.append(label);
}
if (require_complete)
return absl::nullopt;
// If terminating zero-length label was not included in the input, no need to
// recheck against max name length because terminating zero-length label does
// not count against the limit.
return ret;
}
std::string UrlCanonicalizeNameIfAble(base::StringPiece name) {
std::string canonicalized;
url::StdStringCanonOutput output(&canonicalized);
url::CanonHostInfo host_info;
url::CanonicalizeHostVerbose(name.data(), url::Component(0, name.size()),
&output, &host_info);
if (host_info.family == url::CanonHostInfo::Family::BROKEN) {
return std::string(name);
}
output.Complete();
return canonicalized;
}
} // namespace net::dns_names_util
|