Skip to content

Commit 08052a9

Browse files
committed
✨ Add trace stringprep profile for ANONYMOUS SASL
1 parent 41d855b commit 08052a9

File tree

3 files changed

+112
-0
lines changed

3 files changed

+112
-0
lines changed

lib/net/imap/stringprep.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class IMAP < Protocol
1111
module StringPrep
1212
autoload :SASLprep, File.expand_path("stringprep/saslprep", __dir__)
1313
autoload :Tables, File.expand_path("stringprep/tables", __dir__)
14+
autoload :Trace, File.expand_path("stringprep/trace", __dir__)
1415

1516
# ArgumentError raised when +string+ is invalid for the stringprep
1617
# +profile+.

lib/net/imap/stringprep/trace.rb

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# frozen_string_literal: true
2+
3+
module Net
4+
class IMAP
5+
module StringPrep
6+
7+
# Defined in RFC-4505[https://tools.ietf.org/html/rfc4505] §3, The +trace+
8+
# profile of \StringPrep is used by the +ANONYMOUS+ \SASL mechanism.
9+
module Trace
10+
11+
# Defined in RFC-4505[https://tools.ietf.org/html/rfc4505] §3.
12+
STRINGPREP_PROFILE = "trace"
13+
14+
# >>>
15+
# The character repertoire of this profile is Unicode 3.2 [Unicode].
16+
UNASSIGNED_TABLE = "A.1"
17+
18+
# >>>
19+
# No mapping is required by this profile.
20+
MAPPING_TABLES = nil
21+
22+
# >>>
23+
# No Unicode normalization is required by this profile.
24+
NORMALIZATION = nil
25+
26+
# From RFC-4505[https://tools.ietf.org/html/rfc4505] §3, The "trace"
27+
# Profile of "Stringprep":
28+
# >>>
29+
# Characters from the following tables of [StringPrep] are prohibited:
30+
#
31+
# - C.2.1 (ASCII control characters)
32+
# - C.2.2 (Non-ASCII control characters)
33+
# - C.3 (Private use characters)
34+
# - C.4 (Non-character code points)
35+
# - C.5 (Surrogate codes)
36+
# - C.6 (Inappropriate for plain text)
37+
# - C.8 (Change display properties are deprecated)
38+
# - C.9 (Tagging characters)
39+
#
40+
# No additional characters are prohibited.
41+
PROHIBITED_TABLES = %w[C.2.1 C.2.2 C.3 C.4 C.5 C.6 C.8 C.9].freeze
42+
43+
# >>>
44+
# This profile requires bidirectional character checking per Section 6
45+
# of [StringPrep].
46+
CHECK_BIDI = true
47+
48+
module_function
49+
50+
# From RFC-4505[https://tools.ietf.org/html/rfc4505] §3, The "trace"
51+
# Profile of "Stringprep":
52+
# >>>
53+
# The character repertoire of this profile is Unicode 3.2 [Unicode].
54+
#
55+
# No mapping is required by this profile.
56+
#
57+
# No Unicode normalization is required by this profile.
58+
#
59+
# The list of unassigned code points for this profile is that provided
60+
# in Appendix A of [StringPrep]. Unassigned code points are not
61+
# prohibited.
62+
#
63+
# Characters from the following tables of [StringPrep] are prohibited:
64+
# (documented on PROHIBITED_TABLES)
65+
#
66+
# This profile requires bidirectional character checking per Section 6
67+
# of [StringPrep].
68+
def stringprep_trace(string, **opts)
69+
StringPrep.stringprep(
70+
string,
71+
unassigned: UNASSIGNED_TABLE,
72+
maps: MAPPING_TABLES,
73+
prohibited: PROHIBITED_TABLES,
74+
normalization: NORMALIZATION,
75+
bidi: CHECK_BIDI,
76+
profile: STRINGPREP_PROFILE,
77+
**opts,
78+
)
79+
end
80+
81+
end
82+
83+
end
84+
end
85+
end
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# frozen_string_literal: true
2+
3+
require "net/imap"
4+
require "net/imap/sasl/stringprep"
5+
require "test/unit"
6+
7+
class StringPrepProfilesTest < Test::Unit::TestCase
8+
include Net::IMAP::StringPrep
9+
include Net::IMAP::StringPrep::Trace
10+
11+
def test_trace_profile_prohibit_ctrl_chars
12+
assert_raise(ProhibitedCodepoint) {
13+
stringprep_trace("no\ncontrol\rchars")
14+
}
15+
end
16+
17+
def test_trace_profile_prohibit_tagging_chars
18+
assert_raise(ProhibitedCodepoint) {
19+
stringprep_trace("regional flags use tagging chars: e.g." \
20+
"🏴󠁧󠁢󠁥󠁮󠁧󠁿 England, " \
21+
"🏴󠁧󠁢󠁳󠁣󠁴󠁿 Scotland, " \
22+
"🏴󠁧󠁢󠁷󠁬󠁳󠁿 Wales.")
23+
}
24+
end
25+
26+
end

0 commit comments

Comments
 (0)