idna/validation.mbt1///|2/// IDNA label validation (UTS #46 Section 4.1)34///|5/// Validate a single label according to UTS #46 validity criteria, and6/// return its Bidi flags (`RtlLabel`, `BreaksBidiRule`) when `check_bidi`7/// is set, or 0 otherwise.8///9/// Every caller passes a label it has just mapped and normalized to NFC, so10/// the label is not checked for NFC again. `statuses_known` tells that the11/// mapping and normalization left the label as it was, so that every12/// character already has a valid UTS #46 status. IDNA2008 repertoire checks13/// are independent and cannot be skipped on that basis.14fn validate_label(15 label : StringView,16 use_std3_ascii_rules~ : Bool,17 check_hyphens~ : Bool,18 check_joiners~ : Bool,19 check_idna2008~ : Bool,20 check_contexto~ : Bool,21 check_bidi~ : Bool,22 statuses_known~ : Bool,23) -> Int32 raise IdnaError {24 // Empty labels should raise an error - they indicate either intentional empty25 // labels (like trailing dots) or labels that became empty after IDNA mapping26 // (like soft hyphen U+00AD). The caller can catch EmptyLabel and decide27 // whether to allow it based on context.28 if label.length() == 0 {29 raise EmptyLabel30 }3132 // Check 2 & 3: Hyphen restrictions (when check_hyphens)33 if check_hyphens {34 validate_hyphens(label)35 }3637 // Check 4: No U+002E (full stop) within label - handled by label splitting3839 // Check 5: No leading combining marks. An unpaired surrogate is not a mark.40 if label.get_char(0) is Some(first) && is_combining_mark(first) {41 raise LeadingCombiningMark42 }4344 // Check 6 (every character has a valid status) in the same pass that45 // looks for joiners and gathers what the Bidi rule needs.46 let mut has_joiner = false47 let mut has_contexto = false48 let mut has_katakana_middle_dot = false49 let bidi = LabelBidi::new()50 for c in label {51 if !statuses_known {52 validate_char_status(c, use_std3_ascii_rules)53 } else if use_std3_ascii_rules && c.to_int() < 0x80 && !is_ldh_ascii(c) {54 // The table has these valid; STD3 rules allow only LDH in ASCII.55 raise DisallowedCodePoint(cp=c.to_int())56 }57 if check_idna2008 &&58 !@idna_data.lookup_idna_properties(c).is_idna2008_allowed() {59 raise DisallowedCodePoint(cp=c.to_int())60 }61 if c is ('\u{200C}' | '\u{200D}') {62 has_joiner = true63 }64 if check_contexto &&65 c66 is ('\u{00B7}'67 | '\u{0375}'68 | '\u{05F3}'69 | '\u{05F4}'70 | '\u{30FB}'71 | '\u{0660}'..<='\u{0669}'72 | '\u{06F0}'..<='\u{06F9}') {73 has_contexto = true74 if c == '\u{30FB}' {75 has_katakana_middle_dot = true76 }77 }78 if check_bidi {79 bidi.add(@bidi.bidi_class(c))80 }81 }8283 // Check 7: ContextJ rules (when check_joiners)84 if check_joiners && has_joiner {85 validate_contextj(label)86 }87 if has_contexto {88 validate_contexto(label, has_katakana_middle_dot~)89 }90 if check_bidi {91 bidi.flags()92 } else {93 094 }95}9697///|98/// Validate hyphen restrictions on a non-empty label99fn validate_hyphens(label : StringView) -> Unit raise IdnaError {100 // Cannot start or end with hyphen. A hyphen is one UTF-16 code unit.101 if label[0] == '-' || label[label.length() - 1] == '-' {102 raise InvalidHyphen103 }104105 // Cannot have hyphens at positions 3 and 4 (third and fourth characters)106 // This catches things like "xn--" in non-ACE context107 let mut position = 0108 for c in label {109 if position >= 2 && c != '-' {110 break111 }112 if position == 3 {113 raise InvalidHyphen114 }115 position += 1116 }117}118119///|120/// Check if a character is a combining mark (General_Category = Mn, Mc, or Me)121/// This is used for the IDNA "no leading combining mark" validation (V5/V6)122fn is_combining_mark(c : Char) -> Bool {123 @general_category.is_mark(c)124}125126///|127/// Check if an ASCII character is an LDH character (Letter, Digit, Hyphen)128/// This implements STD3 ASCII rules from RFC 952/1123129fn is_ldh_ascii(c : Char) -> Bool {130 // ASCII lowercase letters (a-z)131 (c >= 'a' && c <= 'z') ||132 // ASCII uppercase letters (A-Z) - should be mapped to lowercase already133 (c >= 'A' && c <= 'Z') ||134 // ASCII digits (0-9)135 (c >= '0' && c <= '9') ||136 // Hyphen-minus137 c == '-'138}139140///|141/// Validate that a character has an acceptable IDNA status142fn validate_char_status(143 c : Char,144 use_std3_ascii_rules : Bool,145) -> Unit raise IdnaError {146 // STD3 ASCII rules: only LDH characters allowed in ASCII range147 // This check is separate from IdnaMappingTable.txt which marks148 // these characters as "valid" but they should be rejected under STD3149 if c.to_int() < 0x80 {150 // IdnaMappingTable has every ASCII character valid but A-Z, which it151 // maps to a-z.152 if (use_std3_ascii_rules && !is_ldh_ascii(c)) || c is ('A'..<='Z') {153 raise DisallowedCodePoint(cp=c.to_int())154 }155 return156 }157 match @idna_data.lookup_idna_entry(c).status() {158 Valid | Deviation =>159 // OK160 ()161 Mapped | Ignored =>162 // These should have been processed in the mapping step163 // If we see them here, something went wrong164 raise DisallowedCodePoint(cp=c.to_int())165 Disallowed => raise DisallowedCodePoint(cp=c.to_int())166 DisallowedSTD3Valid =>167 if use_std3_ascii_rules {168 raise DisallowedCodePoint(cp=c.to_int())169 }170 // Otherwise OK171 DisallowedSTD3Mapped =>172 if use_std3_ascii_rules {173 raise DisallowedCodePoint(cp=c.to_int())174 }175 // Otherwise should have been mapped176 }177}178179///|180/// Validate ContextJ rules for ZWNJ (U+200C) and ZWJ (U+200D)181fn validate_contextj(label : StringView) -> Unit raise IdnaError {182 // Joiners are rare; only a label containing one needs random access.183 let chars = label.to_array()184 for i, c in chars {185 if c == '\u{200C}' {186 // ZWNJ: Zero Width Non-Joiner187 if !is_valid_zwnj_context(chars, i) {188 raise InvalidJoiner189 }190 } else if c == '\u{200D}' {191 // ZWJ: Zero Width Joiner192 if !is_valid_zwj_context(chars, i) {193 raise InvalidJoiner194 }195 }196 }197}198199///|200/// Check if ZWNJ is in a valid context201/// Valid if:202/// - There is a combining Virama (U+094D, etc.) before, OR203/// - Surrounded by appropriate joining characters204fn is_valid_zwnj_context(chars : Array[Char], pos : Int32) -> Bool {205 // Rule 1: After a Virama (e.g., for Indic scripts)206 if pos > 0 && is_virama(chars[pos - 1]) {207 return true208 }209210 // Rule 2: Between joining characters211 // Look for L or D type before, and R or D type after212 if has_left_joining_before(chars, pos) && has_right_joining_after(chars, pos) {213 return true214 }215 false216}217218///|219/// Check if ZWJ is in a valid context220/// Valid if preceded by a Virama221fn is_valid_zwj_context(chars : Array[Char], pos : Int32) -> Bool {222 // ZWJ is valid if preceded by a combining Virama223 pos > 0 && is_virama(chars[pos - 1])224}225226///|227/// Check if a character is a Virama (combining class 9)228fn is_virama(c : Char) -> Bool {229 @ccc.lookup_ccc(c) == 9230}231232///|233/// Check if there's a left-joining or dual-joining character before position234fn has_left_joining_before(chars : Array[Char], pos : Int32) -> Bool {235 for i in pos>..0 {236 let jt = @ucd.joining_type(chars[i])237 match jt {238 @ucd.LeftJoining | @ucd.DualJoining => return true239 @ucd.Transparent => continue240 _ => return false241 }242 }243 false244}245246///|247/// Check if there's a right-joining or dual-joining character after position248fn has_right_joining_after(chars : Array[Char], pos : Int32) -> Bool {249 for i in (pos + 1)..<chars.length() {250 let jt = @ucd.joining_type(chars[i])251 match jt {252 @ucd.RightJoining | @ucd.DualJoining => return true253 @ucd.Transparent => continue254 _ => return false255 }256 }257 false258}259260///|261/// A label in a right-to-left (Bidi) domain name: one with an R, AL or AN.262const RtlLabel : Int32 = 1263264///|265/// A label that breaks the RFC 5893 Bidi rule that applies to it in a Bidi266/// domain name: the rule for right-to-left labels if it is one, and the rule267/// for left-to-right labels otherwise.268const BreaksBidiRule : Int32 = 2269270///|271/// What the RFC 5893 Bidi rule needs to know about a label, gathered one272/// Bidi class at a time. Whether the label is right-to-left is known only at273/// the end, so both the right-to-left and the left-to-right rule are tracked.274priv struct LabelBidi {275 mut first : @bidi.BidiClass?276 /// The last class that is not NSM and that either rule allows (B3, B6).277 mut last : @bidi.BidiClass278 mut has_l : Bool279 mut has_rtl : Bool280 mut has_en : Bool281 mut has_an : Bool282}283284///|285fn LabelBidi::new() -> LabelBidi {286 {287 first: None,288 last: @bidi.ON,289 has_l: false,290 has_rtl: false,291 has_en: false,292 has_an: false,293 }294}295296///|297fn LabelBidi::add(self : LabelBidi, bc : @bidi.BidiClass) -> Unit {298 if self.first is None {299 self.first = Some(bc)300 self.last = bc301 }302 match bc {303 @bidi.L => self.has_l = true304 @bidi.R | @bidi.AL => self.has_rtl = true305 @bidi.AN => {306 self.has_rtl = true307 self.has_an = true308 }309 @bidi.EN => self.has_en = true310 _ => ()311 }312 // Each rule rejects a label with a class outside its list (L for the313 // right-to-left rule; R, AL and AN for the left-to-right one), so the314 // union of both lists can be tracked for whichever rule applies.315 if bc316 is (@bidi.L317 | @bidi.R318 | @bidi.AL319 | @bidi.AN320 | @bidi.EN321 | @bidi.ES322 | @bidi.CS323 | @bidi.ET324 | @bidi.ON325 | @bidi.BN) {326 self.last = bc327 }328}329330///|331/// `RtlLabel` and `BreaksBidiRule` for the label.332fn LabelBidi::flags(self : LabelBidi) -> Int32 {333 if self.has_rtl {334 // B1: starts with R or AL. B2: no L. B3: ends with R, AL, EN or AN335 // (ignoring NSM). B4: not both EN and AN.336 let valid = self.first is Some(@bidi.R | @bidi.AL) &&337 !self.has_l &&338 self.last is (@bidi.R | @bidi.AL | @bidi.EN | @bidi.AN) &&339 !(self.has_en && self.has_an)340 if valid {341 RtlLabel342 } else {343 RtlLabel | BreaksBidiRule344 }345 } else {346 // B5 is implied, with no R, AL or AN. Starting with L is stricter than347 // B1 requires, as left-to-right labels in a Bidi domain name must.348 // B6: ends with L or EN (ignoring NSM).349 let valid = self.first is Some(@bidi.L) && self.last is (@bidi.L | @bidi.EN)350 if valid {351 0352 } else {353 BreaksBidiRule354 }355 }356}357
///|
/// IDNA label validation (UTS #46 Section 4.1)
///|
/// Validate a single label according to UTS #46 validity criteria, and
/// return its Bidi flags (`RtlLabel`, `BreaksBidiRule`) when `check_bidi`
/// is set, or 0 otherwise.
///
/// Every caller passes a label it has just mapped and normalized to NFC, so
/// the label is not checked for NFC again. `statuses_known` tells that the
/// mapping and normalization left the label as it was, so that every
/// character already has a valid UTS #46 status. IDNA2008 repertoire checks
/// are independent and cannot be skipped on that basis.
fn validate_label(
label : StringView,
use_std3_ascii_rules~ : Bool,
check_hyphens~ : Bool,
check_joiners~ : Bool,
check_idna2008~ : Bool,
check_contexto~ : Bool,
check_bidi~ : Bool,
statuses_known~ : Bool,
) -> Int32 raise IdnaError {
// Empty labels should raise an error - they indicate either intentional empty
// labels (like trailing dots) or labels that became empty after IDNA mapping
// (like soft hyphen U+00AD). The caller can catch EmptyLabel and decide
// whether to allow it based on context.
if label.length() == 0 {
raise EmptyLabel
}
// Check 2 & 3: Hyphen restrictions (when check_hyphens)
if check_hyphens {
validate_hyphens(label)
}
// Check 4: No U+002E (full stop) within label - handled by label splitting
// Check 5: No leading combining marks. An unpaired surrogate is not a mark.
if label.get_char(0) is Some(first) && is_combining_mark(first) {
raise LeadingCombiningMark
}
// Check 6 (every character has a valid status) in the same pass that
// looks for joiners and gathers what the Bidi rule needs.
let mut has_joiner = false
let mut has_contexto = false
let mut has_katakana_middle_dot = false
let bidi = LabelBidi::new()
for c in label {
if !statuses_known {
validate_char_status(c, use_std3_ascii_rules)
} else if use_std3_ascii_rules && c.to_int() < 0x80 && !is_ldh_ascii(c) {
// The table has these valid; STD3 rules allow only LDH in ASCII.
raise DisallowedCodePoint(cp=c.to_int())
}
if check_idna2008 &&
!@idna_data.lookup_idna_properties(c).is_idna2008_allowed() {
raise DisallowedCodePoint(cp=c.to_int())
}
if c is ('\u{200C}' | '\u{200D}') {
has_joiner = true
}
if check_contexto &&
c
is ('\u{00B7}'
| '\u{0375}'
| '\u{05F3}'
| '\u{05F4}'
| '\u{30FB}'
| '\u{0660}'..<='\u{0669}'
| '\u{06F0}'..<='\u{06F9}') {
has_contexto = true
if c == '\u{30FB}' {
has_katakana_middle_dot = true
}
}
if check_bidi {
bidi.add(@bidi.bidi_class(c))
}
}
// Check 7: ContextJ rules (when check_joiners)
if check_joiners && has_joiner {
validate_contextj(label)
}
if has_contexto {
validate_contexto(label, has_katakana_middle_dot~)
}
if check_bidi {
bidi.flags()
} else {
0
}
}
///|
/// Validate hyphen restrictions on a non-empty label
fn validate_hyphens(label : StringView) -> Unit raise IdnaError {
// Cannot start or end with hyphen. A hyphen is one UTF-16 code unit.
if label[0] == '-' || label[label.length() - 1] == '-' {
raise InvalidHyphen
}
// Cannot have hyphens at positions 3 and 4 (third and fourth characters)
// This catches things like "xn--" in non-ACE context
let mut position = 0
for c in label {
if position >= 2 && c != '-' {
break
}
if position == 3 {
raise InvalidHyphen
}
position += 1
}
}
///|
/// Check if a character is a combining mark (General_Category = Mn, Mc, or Me)
/// This is used for the IDNA "no leading combining mark" validation (V5/V6)
fn is_combining_mark(c : Char) -> Bool {
@general_category.is_mark(c)
}
///|
/// Check if an ASCII character is an LDH character (Letter, Digit, Hyphen)
/// This implements STD3 ASCII rules from RFC 952/1123
fn is_ldh_ascii(c : Char) -> Bool {
// ASCII lowercase letters (a-z)
(c >= 'a' && c <= 'z') ||
// ASCII uppercase letters (A-Z) - should be mapped to lowercase already
(c >= 'A' && c <= 'Z') ||
// ASCII digits (0-9)
(c >= '0' && c <= '9') ||
// Hyphen-minus
c == '-'
}
///|
/// Validate that a character has an acceptable IDNA status
fn validate_char_status(
c : Char,
use_std3_ascii_rules : Bool,
) -> Unit raise IdnaError {
// STD3 ASCII rules: only LDH characters allowed in ASCII range
// This check is separate from IdnaMappingTable.txt which marks
// these characters as "valid" but they should be rejected under STD3
if c.to_int() < 0x80 {
// IdnaMappingTable has every ASCII character valid but A-Z, which it
// maps to a-z.
if (use_std3_ascii_rules && !is_ldh_ascii(c)) || c is ('A'..<='Z') {
raise DisallowedCodePoint(cp=c.to_int())
}
return
}
match @idna_data.lookup_idna_entry(c).status() {
Valid | Deviation =>
// OK
()
Mapped | Ignored =>
// These should have been processed in the mapping step
// If we see them here, something went wrong
raise DisallowedCodePoint(cp=c.to_int())
Disallowed => raise DisallowedCodePoint(cp=c.to_int())
DisallowedSTD3Valid =>
if use_std3_ascii_rules {
raise DisallowedCodePoint(cp=c.to_int())
}
// Otherwise OK
DisallowedSTD3Mapped =>
if use_std3_ascii_rules {
raise DisallowedCodePoint(cp=c.to_int())
}
// Otherwise should have been mapped
}
}
///|
/// Validate ContextJ rules for ZWNJ (U+200C) and ZWJ (U+200D)
fn validate_contextj(label : StringView) -> Unit raise IdnaError {
// Joiners are rare; only a label containing one needs random access.
let chars = label.to_array()
for i, c in chars {
if c == '\u{200C}' {
// ZWNJ: Zero Width Non-Joiner
if !is_valid_zwnj_context(chars, i) {
raise InvalidJoiner
}
} else if c == '\u{200D}' {
// ZWJ: Zero Width Joiner
if !is_valid_zwj_context(chars, i) {
raise InvalidJoiner
}
}
}
}
///|
/// Check if ZWNJ is in a valid context
/// Valid if:
/// - There is a combining Virama (U+094D, etc.) before, OR
/// - Surrounded by appropriate joining characters
fn is_valid_zwnj_context(chars : Array[Char], pos : Int32) -> Bool {
// Rule 1: After a Virama (e.g., for Indic scripts)
if pos > 0 && is_virama(chars[pos - 1]) {
return true
}
// Rule 2: Between joining characters
// Look for L or D type before, and R or D type after
if has_left_joining_before(chars, pos) && has_right_joining_after(chars, pos) {
return true
}
false
}
///|
/// Check if ZWJ is in a valid context
/// Valid if preceded by a Virama
fn is_valid_zwj_context(chars : Array[Char], pos : Int32) -> Bool {
// ZWJ is valid if preceded by a combining Virama
pos > 0 && is_virama(chars[pos - 1])
}
///|
/// Check if a character is a Virama (combining class 9)
fn is_virama(c : Char) -> Bool {
@ccc.lookup_ccc(c) == 9
}
///|
/// Check if there's a left-joining or dual-joining character before position
fn has_left_joining_before(chars : Array[Char], pos : Int32) -> Bool {
for i in pos>..0 {
let jt = @ucd.joining_type(chars[i])
match jt {
@ucd.LeftJoining | @ucd.DualJoining => return true
@ucd.Transparent => continue
_ => return false
}
}
false
}
///|
/// Check if there's a right-joining or dual-joining character after position
fn has_right_joining_after(chars : Array[Char], pos : Int32) -> Bool {
for i in (pos + 1)..<chars.length() {
let jt = @ucd.joining_type(chars[i])
match jt {
@ucd.RightJoining | @ucd.DualJoining => return true
@ucd.Transparent => continue
_ => return false
}
}
false
}
///|
/// A label in a right-to-left (Bidi) domain name: one with an R, AL or AN.
const RtlLabel : Int32 = 1
///|
/// A label that breaks the RFC 5893 Bidi rule that applies to it in a Bidi
/// domain name: the rule for right-to-left labels if it is one, and the rule
/// for left-to-right labels otherwise.
const BreaksBidiRule : Int32 = 2
///|
/// What the RFC 5893 Bidi rule needs to know about a label, gathered one
/// Bidi class at a time. Whether the label is right-to-left is known only at
/// the end, so both the right-to-left and the left-to-right rule are tracked.
priv struct LabelBidi {
mut first : @bidi.BidiClass?
/// The last class that is not NSM and that either rule allows (B3, B6).
mut last : @bidi.BidiClass
mut has_l : Bool
mut has_rtl : Bool
mut has_en : Bool
mut has_an : Bool
}
///|
fn LabelBidi::new() -> LabelBidi {
{
first: None,
last: @bidi.ON,
has_l: false,
has_rtl: false,
has_en: false,
has_an: false,
}
}
///|
fn LabelBidi::add(self : LabelBidi, bc : @bidi.BidiClass) -> Unit {
if self.first is None {
self.first = Some(bc)
self.last = bc
}
match bc {
@bidi.L => self.has_l = true
@bidi.R | @bidi.AL => self.has_rtl = true
@bidi.AN => {
self.has_rtl = true
self.has_an = true
}
@bidi.EN => self.has_en = true
_ => ()
}
// Each rule rejects a label with a class outside its list (L for the
// right-to-left rule; R, AL and AN for the left-to-right one), so the
// union of both lists can be tracked for whichever rule applies.
if bc
is (@bidi.L
| @bidi.R
| @bidi.AL
| @bidi.AN
| @bidi.EN
| @bidi.ES
| @bidi.CS
| @bidi.ET
| @bidi.ON
| @bidi.BN) {
self.last = bc
}
}
///|
/// `RtlLabel` and `BreaksBidiRule` for the label.
fn LabelBidi::flags(self : LabelBidi) -> Int32 {
if self.has_rtl {
// B1: starts with R or AL. B2: no L. B3: ends with R, AL, EN or AN
// (ignoring NSM). B4: not both EN and AN.
let valid = self.first is Some(@bidi.R | @bidi.AL) &&
!self.has_l &&
self.last is (@bidi.R | @bidi.AL | @bidi.EN | @bidi.AN) &&
!(self.has_en && self.has_an)
if valid {
RtlLabel
} else {
RtlLabel | BreaksBidiRule
}
} else {
// B5 is implied, with no R, AL or AN. Starting with L is stricter than
// B1 requires, as left-to-right labels in a Bidi domain name must.
// B6: ends with L or EN (ignoring NSM).
let valid = self.first is Some(@bidi.L) && self.last is (@bidi.L | @bidi.EN)
if valid {
0
} else {
BreaksBidiRule
}
}
}