unicode

Branch: main

Switch branches or tags

Branch: main
Go to file t

413 files indexed

History
Code

idna/validation.mbt
1///|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