Spaces:
Paused
Paused
File size: 8,590 Bytes
3401f26 | 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 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 | "use strict";
const punycode = require("punycode/");
const regexes = require("./lib/regexes.js");
const mappingTable = require("./lib/mappingTable.json");
const { STATUS_MAPPING } = require("./lib/statusMapping.js");
function containsNonASCII(str) {
return /[^\x00-\x7F]/u.test(str);
}
function findStatus(val) {
let start = 0;
let end = mappingTable.length - 1;
while (start <= end) {
const mid = Math.floor((start + end) / 2);
const target = mappingTable[mid];
const min = Array.isArray(target[0]) ? target[0][0] : target[0];
const max = Array.isArray(target[0]) ? target[0][1] : target[0];
if (min <= val && max >= val) {
return target.slice(1);
} else if (min > val) {
end = mid - 1;
} else {
start = mid + 1;
}
}
return null;
}
function mapChars(domainName, { transitionalProcessing }) {
let processed = "";
for (const ch of domainName) {
const [status, mapping] = findStatus(ch.codePointAt(0));
switch (status) {
case STATUS_MAPPING.disallowed:
processed += ch;
break;
case STATUS_MAPPING.ignored:
break;
case STATUS_MAPPING.mapped:
if (transitionalProcessing && ch === "ẞ") {
processed += "ss";
} else {
processed += mapping;
}
break;
case STATUS_MAPPING.deviation:
if (transitionalProcessing) {
processed += mapping;
} else {
processed += ch;
}
break;
case STATUS_MAPPING.valid:
processed += ch;
break;
}
}
return processed;
}
function validateLabel(label, {
checkHyphens,
checkBidi,
checkJoiners,
transitionalProcessing,
useSTD3ASCIIRules,
isBidi
}) {
// "must be satisfied for a non-empty label"
if (label.length === 0) {
return true;
}
// "1. The label must be in Unicode Normalization Form NFC."
if (label.normalize("NFC") !== label) {
return false;
}
const codePoints = Array.from(label);
// "2. If CheckHyphens, the label must not contain a U+002D HYPHEN-MINUS character in both the
// third and fourth positions."
//
// "3. If CheckHyphens, the label must neither begin nor end with a U+002D HYPHEN-MINUS character."
if (checkHyphens) {
if ((codePoints[2] === "-" && codePoints[3] === "-") ||
(label.startsWith("-") || label.endsWith("-"))) {
return false;
}
}
// "4. If not CheckHyphens, the label must not begin with “xn--”."
if (!checkHyphens) {
if (label.startsWith("xn--")) {
return false;
}
}
// "5. The label must not contain a U+002E ( . ) FULL STOP."
if (label.includes(".")) {
return false;
}
// "6. The label must not begin with a combining mark, that is: General_Category=Mark."
if (regexes.combiningMarks.test(codePoints[0])) {
return false;
}
// "7. Each code point in the label must only have certain Status values according to Section 5"
for (const ch of codePoints) {
const codePoint = ch.codePointAt(0);
const [status] = findStatus(codePoint);
if (transitionalProcessing) {
// "For Transitional Processing (deprecated), each value must be valid."
if (status !== STATUS_MAPPING.valid) {
return false;
}
} else if (status !== STATUS_MAPPING.valid && status !== STATUS_MAPPING.deviation) {
// "For Nontransitional Processing, each value must be either valid or deviation."
return false;
}
// "In addition, if UseSTD3ASCIIRules=true and the code point is an ASCII code point (U+0000..U+007F), then it must
// be a lowercase letter (a-z), a digit (0-9), or a hyphen-minus (U+002D). (Note: This excludes uppercase ASCII
// A-Z which are mapped in UTS #46 and disallowed in IDNA2008.)"
if (useSTD3ASCIIRules && codePoint <= 0x7F) {
if (!/^(?:[a-z]|[0-9]|-)$/u.test(ch)) {
return false;
}
}
}
// "8. If CheckJoiners, the label must satisify the ContextJ rules"
// https://tools.ietf.org/html/rfc5892#appendix-A
if (checkJoiners) {
let last = 0;
for (const [i, ch] of codePoints.entries()) {
if (ch === "\u200C" || ch === "\u200D") {
if (i > 0) {
if (regexes.combiningClassVirama.test(codePoints[i - 1])) {
continue;
}
if (ch === "\u200C") {
// TODO: make this more efficient
const next = codePoints.indexOf("\u200C", i + 1);
const test = next < 0 ? codePoints.slice(last) : codePoints.slice(last, next);
if (regexes.validZWNJ.test(test.join(""))) {
last = i + 1;
continue;
}
}
}
return false;
}
}
}
// "9. If CheckBidi, and if the domain name is a Bidi domain name, then the label must satisfy..."
// https://tools.ietf.org/html/rfc5893#section-2
if (checkBidi && isBidi) {
let rtl;
// 1
if (regexes.bidiS1LTR.test(codePoints[0])) {
rtl = false;
} else if (regexes.bidiS1RTL.test(codePoints[0])) {
rtl = true;
} else {
return false;
}
if (rtl) {
// 2-4
if (!regexes.bidiS2.test(label) ||
!regexes.bidiS3.test(label) ||
(regexes.bidiS4EN.test(label) && regexes.bidiS4AN.test(label))) {
return false;
}
} else if (!regexes.bidiS5.test(label) ||
!regexes.bidiS6.test(label)) { // 5-6
return false;
}
}
return true;
}
function isBidiDomain(labels) {
const domain = labels.map(label => {
if (label.startsWith("xn--")) {
try {
return punycode.decode(label.substring(4));
} catch {
return "";
}
}
return label;
}).join(".");
return regexes.bidiDomain.test(domain);
}
function processing(domainName, options) {
// 1. Map.
let string = mapChars(domainName, options);
// 2. Normalize.
string = string.normalize("NFC");
// 3. Break.
const labels = string.split(".");
const isBidi = isBidiDomain(labels);
// 4. Convert/Validate.
let error = false;
for (const [i, origLabel] of labels.entries()) {
let label = origLabel;
let transitionalProcessingForThisLabel = options.transitionalProcessing;
if (label.startsWith("xn--")) {
if (containsNonASCII(label)) {
error = true;
continue;
}
try {
label = punycode.decode(label.substring(4));
} catch {
if (!options.ignoreInvalidPunycode) {
error = true;
continue;
}
}
labels[i] = label;
if (label === "" || !containsNonASCII(label)) {
error = true;
}
transitionalProcessingForThisLabel = false;
}
// No need to validate if we already know there is an error.
if (error) {
continue;
}
const validation = validateLabel(label, {
...options,
transitionalProcessing: transitionalProcessingForThisLabel,
isBidi
});
if (!validation) {
error = true;
}
}
return {
string: labels.join("."),
error
};
}
function toASCII(domainName, {
checkHyphens = false,
checkBidi = false,
checkJoiners = false,
useSTD3ASCIIRules = false,
verifyDNSLength = false,
transitionalProcessing = false,
ignoreInvalidPunycode = false
} = {}) {
const result = processing(domainName, {
checkHyphens,
checkBidi,
checkJoiners,
useSTD3ASCIIRules,
transitionalProcessing,
ignoreInvalidPunycode
});
let labels = result.string.split(".");
labels = labels.map(l => {
if (containsNonASCII(l)) {
try {
return `xn--${punycode.encode(l)}`;
} catch {
result.error = true;
}
}
return l;
});
if (verifyDNSLength) {
const total = labels.join(".").length;
if (total > 253 || total === 0) {
result.error = true;
}
for (let i = 0; i < labels.length; ++i) {
if (labels[i].length > 63 || labels[i].length === 0) {
result.error = true;
break;
}
}
}
if (result.error) {
return null;
}
return labels.join(".");
}
function toUnicode(domainName, {
checkHyphens = false,
checkBidi = false,
checkJoiners = false,
useSTD3ASCIIRules = false,
transitionalProcessing = false,
ignoreInvalidPunycode = false
} = {}) {
const result = processing(domainName, {
checkHyphens,
checkBidi,
checkJoiners,
useSTD3ASCIIRules,
transitionalProcessing,
ignoreInvalidPunycode
});
return {
domain: result.string,
error: result.error
};
}
module.exports = {
toASCII,
toUnicode
};
|