module Net::IMAP::SASL::StringPrep

Regexps and utility methods for implementing stringprep profiles. The StringPrep algorithm is defined by RFC-3454. Each codepoint table defined in the RFC-3454 appendices is matched by a Regexp defined in this module.

Constants

BIDI_DESC_REQ2
BIDI_DESC_REQ3
BIDI_FAILS_REQ2
Bidirectional Characters [StringPrep, §6], Requirement 2

If a string contains any RandALCat character, the string MUST NOT contain any LCat character.

BIDI_FAILS_REQ3
Bidirectional Characters [StringPrep, §6], Requirement 3

If a string contains any RandALCat character, a RandALCat character MUST be the first character of the string, and a RandALCat character MUST be the last character of the string.

BIDI_FAILURE

Bidirectional Characters [StringPrep, §6]

IN_A_1

Unassigned code points in Unicode 3.2 StringPrep["A.1"]

IN_B_1

Commonly mapped to nothing StringPrep["B.1"]

IN_B_2

Mapping for case-folding used with NFKC StringPrep["B.2"]

IN_B_3

Mapping for case-folding used with no normalization StringPrep["B.3"]

IN_C_1_1

ASCII space characters StringPrep["C.1.1"]

IN_C_1_2

Non-ASCII space characters StringPrep["C.1.2"]

IN_C_2_1

ASCII control characters StringPrep["C.2.1"]

IN_C_2_2

Non-ASCII control characters StringPrep["C.2.2"]

IN_C_3

Private use StringPrep["C.3"]

IN_C_4

Non-character code points StringPrep["C.4"]

IN_C_5

Surrogate codes StringPrep["C.5"]

IN_C_6

Inappropriate for plain text StringPrep["C.6"]

IN_C_7

Inappropriate for canonical representation StringPrep["C.7"]

IN_C_8

Change display properties or are deprecated StringPrep["C.8"]

IN_C_9

Tagging characters StringPrep["C.9"]

IN_D_1

Characters with bidirectional property “R” or “AL” StringPrep["D.1"]

IN_D_1_NEGATED

Used to check req3 of bidirectional checks Matches the negation of the D.1 table

IN_D_2

Characters with bidirectional property “L” StringPrep["D.2"]

TABLE_REGEXPS

Regexps matching each codepoint table in the RFC-3454 appendices

TABLE_TITLES

Names of each codepoint table in the RFC-3454 appendices

Public Class Methods

[](table) click to toggle source

Returns a Regexp matching the given table name.

# File net-imap-0.3.4.1/lib/net/imap/sasl/stringprep.rb, line 18
def self.[](table)
  TABLE_REGEXPS.fetch(table)
end

Public Instance Methods

check_bidi!(string, c_8: false, profile: nil) click to toggle source

Checks that string obeys all of the “Bidirectional Characters” requirements in RFC-3454, §6:

  • The characters in StringPrep["C.8"] MUST be prohibited

  • If a string contains any RandALCat character, the string MUST NOT contain any LCat character.

  • If a string contains any RandALCat character, a RandALCat character MUST be the first character of the string, and a RandALCat character MUST be the last character of the string.

This is usually combined with check_prohibited!, so table “C.8” is only checked when c_8: true.

Raises either ProhibitedCodepoint or BidiStringError unless all requirements are met. profile is an optional string which will be added to any exception that is raised (it does not affect behavior).

# File net-imap-0.3.4.1/lib/net/imap/sasl/stringprep.rb, line 58
def check_bidi!(string, c_8: false, profile: nil)
  check_prohibited!(string, "C.8", profile: profile) if c_8
  if BIDI_FAILS_REQ2.match?(string)
    raise BidiStringError.new(
      BIDI_DESC_REQ2, string: string, profile: profile,
    )
  elsif BIDI_FAILS_REQ3.match?(string)
    raise BidiStringError.new(
      BIDI_DESC_REQ3, string: string, profile: profile,
    )
  end
end
check_prohibited!(string, *tables, bidi: false, profile: nil) click to toggle source

Checks string for any codepoint in tables. Raises a ProhibitedCodepoint describing the first matching table.

Also checks bidirectional characters, when bidi: true, which may raise a BidiStringError.

profile is an optional string which will be added to any exception that is raised (it does not affect behavior).

# File net-imap-0.3.4.1/lib/net/imap/sasl/stringprep.rb, line 32
def check_prohibited!(string, *tables, bidi: false, profile: nil)
  tables = TABLE_TITLES.keys.grep(/^C/) if tables.empty?
  tables |= %w[C.8] if bidi
  table = tables.find {|t| TABLE_REGEXPS[t].match?(string) }
  raise ProhibitedCodepoint.new(
    table, string: string, profile: nil
  ) if table
  check_bidi!(string, profile: profile) if bidi
end