krabka-security 0.8.0

TLS, SASL, SCRAM, OAuth, and Kerberos security utilities for Krabka
Documentation
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
//! `java.util.regex` match and replacement semantics over the `regex` crate.
//!
//! Kafka's principal-mapping rules, `sasl.kerberos.principal.to.local.rules`
//! and `ssl.principal.mapping.rules`, are Java regular expressions applied
//! with `Matcher.matches()`, `Matcher.replaceAll` and `Matcher.replaceFirst`.
//! The `regex` crate differs from Java in the three places those rules touch:
//!
//! - **Whole-input match.** `Matcher.matches()` succeeds when *some* match of
//!   the pattern spans the whole input, found by backtracking. A leftmost-first
//!   `find` that happens to stop short, as `a|ab` does on `ab`, is not the
//!   same test. [`JavaPattern::matches`] runs a separately compiled
//!   `\A(?:pattern)\z`.
//! - **Group references in the replacement.** Java reads `$` then one digit,
//!   and extends the number digit by digit only while the result is still a
//!   group of the pattern, so `$1_$2` is group 1, `_`, group 2 and `$12` with
//!   one group is group 1 then `2`. The `regex` crate takes the longest
//!   `[_0-9A-Za-z]` run as the group name and expands an unknown group to
//!   nothing. A backslash escapes the next character, and malformed
//!   references are errors rather than empty text.
//! - **Empty matches during `replaceAll`.** Java retries one character past an
//!   empty match and allows an empty match right after a non-empty one, so
//!   `x*` over `xx` with `[$0]` is `[xx][]`.
//!
//! - **Predefined classes.** Without `UNICODE_CHARACTER_CLASS`, Java's `\d`,
//!   `\w`, `\s` and `\b` are ASCII-only; the `regex` crate's are Unicode.
//!   [`JavaPattern::new`] rewrites them to their ASCII forms. It also makes
//!   any escaped non-alphanumeric character a literal, as Java does, where
//!   the `regex` crate rejects an escaped `,` or `@`.
//!
//! Otherwise the pattern dialect is the `regex` crate's: constructs it does
//! not support, such as back-references and look-around, fail to compile.

use regex::{Captures, Regex};

/// Why a Java replacement string could not be expanded against a match.
///
/// Each variant is one of the `IllegalArgumentException` or
/// `IndexOutOfBoundsException` messages `Matcher.appendReplacement` throws.
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum JavaReplacementError {
    /// A trailing `\` with nothing after it.
    #[error("character to be escaped is missing")]
    MissingEscapedCharacter,
    /// A trailing `$` with nothing after it.
    #[error("Illegal group reference: group index is missing")]
    MissingGroupIndex,
    /// A `$` followed by something other than a digit or `{`.
    #[error("Illegal group reference")]
    IllegalGroupReference,
    /// `${}`.
    #[error("named capturing group has 0 length name")]
    EmptyGroupName,
    /// `${name` with no closing brace.
    #[error("named capturing group is missing trailing '}}'")]
    UnterminatedGroupName,
    /// `${1a}`: a group name has to start with a letter.
    #[error("capturing group name {{{0}}} starts with digit character")]
    GroupNameStartsWithDigit(String),
    /// `${name}` where the pattern has no group of that name.
    #[error("No group with name {{{0}}}")]
    NoGroupNamed(String),
    /// `$n` where the pattern has fewer than `n` groups.
    #[error("No group {0}")]
    NoGroup(usize),
    /// A match offset that does not fall on a character boundary of the
    /// input. The `regex` crate never reports one; Java has no equivalent.
    #[error("match offset {0} is not a character boundary of the input")]
    MatchOffset(usize),
}

/// A pattern compiled for Java's `matches`, `replaceAll` and `replaceFirst`.
#[derive(Debug, Clone)]
pub struct JavaPattern {
    source: String,
    unanchored: Regex,
    whole: Regex,
}

impl JavaPattern {
    /// Compiles `pattern`.
    ///
    /// # Errors
    ///
    /// [`regex::Error`] when `pattern` is not a valid `regex` crate pattern.
    pub fn new(pattern: &str) -> Result<Self, regex::Error> {
        let translated = ascii_predefined_classes(pattern);
        Ok(Self {
            source: pattern.to_owned(),
            unanchored: Regex::new(&translated)?,
            whole: Regex::new(&format!(r"\A(?:{translated})\z"))?,
        })
    }

    /// The pattern as written.
    #[must_use]
    pub fn as_str(&self) -> &str {
        &self.source
    }

    /// Java's `Matcher.groupCount()`: the capturing groups, not counting the
    /// whole match.
    #[must_use]
    pub fn group_count(&self) -> usize {
        self.unanchored.captures_len() - 1
    }

    /// Java's `Matcher.matches()`: whether some match spans all of `text`.
    #[must_use]
    pub fn matches(&self, text: &str) -> bool {
        self.whole.is_match(text)
    }

    /// Java's `Matcher.replaceAll(replacement)`.
    ///
    /// # Errors
    ///
    /// [`JavaReplacementError`] when `replacement` is malformed. As in Java,
    /// that is only found out when the pattern matches somewhere.
    pub fn replace_all(
        &self,
        text: &str,
        replacement: &str,
    ) -> Result<String, JavaReplacementError> {
        self.replace(text, replacement, usize::MAX)
    }

    /// Java's `Matcher.replaceFirst(replacement)`.
    ///
    /// # Errors
    ///
    /// As [`JavaPattern::replace_all`].
    pub fn replace_first(
        &self,
        text: &str,
        replacement: &str,
    ) -> Result<String, JavaReplacementError> {
        self.replace(text, replacement, 1)
    }

    fn replace(
        &self,
        text: &str,
        replacement: &str,
        limit: usize,
    ) -> Result<String, JavaReplacementError> {
        let mut out = String::with_capacity(text.len());
        let mut appended = 0;
        let mut search = 0;
        let mut replaced = 0;
        while replaced < limit && search <= text.len() {
            let Some(captures) = self.unanchored.captures_at(text, search) else {
                break;
            };
            let whole = captures.get_match();
            out.push_str(
                text.get(appended..whole.start())
                    .ok_or_else(|| JavaReplacementError::MatchOffset(whole.start()))?,
            );
            self.expand(&captures, replacement, &mut out)?;
            appended = whole.end();
            replaced += 1;
            // `Matcher.find` resumes at the previous end, one character
            // further when the previous match was empty.
            search = if whole.is_empty() {
                let rest = text
                    .get(whole.end()..)
                    .ok_or_else(|| JavaReplacementError::MatchOffset(whole.end()))?;
                match rest.chars().next() {
                    Some(next) => whole.end() + next.len_utf8(),
                    None => break,
                }
            } else {
                whole.end()
            };
        }
        out.push_str(
            text.get(appended..)
                .ok_or(JavaReplacementError::MatchOffset(appended))?,
        );
        Ok(out)
    }

    /// `Matcher.appendExpandedReplacement`.
    fn expand(
        &self,
        captures: &Captures<'_>,
        replacement: &str,
        out: &mut String,
    ) -> Result<(), JavaReplacementError> {
        let group_count = self.group_count();
        let mut chars = replacement.chars().peekable();
        while let Some(character) = chars.next() {
            match character {
                '\\' => out.push(
                    chars
                        .next()
                        .ok_or(JavaReplacementError::MissingEscapedCharacter)?,
                ),
                '$' => {
                    let group = match chars.next() {
                        None => return Err(JavaReplacementError::MissingGroupIndex),
                        Some('{') => {
                            let mut name = String::new();
                            while let Some(&next) = chars.peek()
                                && next.is_ascii_alphanumeric()
                            {
                                name.push(next);
                                chars.next();
                            }
                            if name.is_empty() {
                                return Err(JavaReplacementError::EmptyGroupName);
                            }
                            if chars.next() != Some('}') {
                                return Err(JavaReplacementError::UnterminatedGroupName);
                            }
                            if name.starts_with(|c: char| c.is_ascii_digit()) {
                                return Err(JavaReplacementError::GroupNameStartsWithDigit(name));
                            }
                            self.unanchored
                                .capture_names()
                                .position(|candidate| candidate == Some(name.as_str()))
                                .ok_or(JavaReplacementError::NoGroupNamed(name))?
                        }
                        Some(first) => {
                            let mut group = first
                                .to_digit(10)
                                .ok_or(JavaReplacementError::IllegalGroupReference)?
                                as usize;
                            // "Capture the largest legal group string."
                            while let Some(digit) = chars.peek().and_then(|c| c.to_digit(10)) {
                                let longer = group * 10 + digit as usize;
                                if longer > group_count {
                                    break;
                                }
                                group = longer;
                                chars.next();
                            }
                            if group > group_count {
                                return Err(JavaReplacementError::NoGroup(group));
                            }
                            group
                        }
                    };
                    if let Some(text) = captures.get(group) {
                        out.push_str(text.as_str());
                    }
                }
                literal => out.push(literal),
            }
        }
        Ok(())
    }
}

/// Rewrites Java's ASCII-only predefined classes and word boundaries into
/// `regex` crate syntax with the same meaning. Each class becomes a bracketed
/// class, which the `regex` crate also accepts nested inside another class.
fn ascii_predefined_classes(pattern: &str) -> String {
    let mut out = String::with_capacity(pattern.len());
    let mut class_depth = 0_usize;
    let mut chars = pattern.chars();
    while let Some(character) = chars.next() {
        match character {
            '\\' => {
                let Some(escaped) = chars.next() else {
                    out.push('\\');
                    break;
                };
                match escaped {
                    'd' => out.push_str("[0-9]"),
                    'D' => out.push_str("[^0-9]"),
                    'w' => out.push_str("[0-9A-Za-z_]"),
                    'W' => out.push_str("[^0-9A-Za-z_]"),
                    's' => out.push_str(r"[\t\n\x0B\f\r ]"),
                    'S' => out.push_str(r"[^\t\n\x0B\f\r ]"),
                    'b' if class_depth == 0 => out.push_str(r"(?-u:\b)"),
                    'B' if class_depth == 0 => out.push_str(r"(?-u:\B)"),
                    // Java takes any escaped non-alphanumeric character
                    // literally; the `regex` crate only accepts escaped
                    // metacharacters.
                    other if !other.is_ascii_alphanumeric() => {
                        out.push_str(&regex::escape(other.encode_utf8(&mut [0; 4])));
                    }
                    other => {
                        out.push('\\');
                        out.push(other);
                    }
                }
            }
            '[' => {
                class_depth += 1;
                out.push('[');
            }
            ']' => {
                class_depth = class_depth.saturating_sub(1);
                out.push(']');
            }
            other => out.push(other),
        }
    }
    out
}

#[cfg(test)]
mod tests {
    use assert2::check;

    use super::*;

    /// Each row was checked against `java.util.regex` on `OpenJDK` 21:
    /// `Pattern.compile(pattern).matcher(text).replaceAll(replacement)`.
    #[test]
    fn replace_all_follows_java() {
        // (pattern, text, replacement, expected)
        let cases: &[(&str, &str, &str, Result<&str, JavaReplacementError>)] = &[
            // `$1_` is group 1 then `_`, not a group named `1_`.
            ("(a)(b)", "ab", "$1_$2", Ok("a_b")),
            ("(a)(b)", "ab", "$1suffix", Ok("asuffix")),
            // `$12` with two groups is group 1 then a literal `2`.
            ("(a)(b)", "ab", "$12", Ok("a2")),
            // With eleven groups `$11` is group 11 and `$111` is group 11 then `1`.
            (
                "(a)(b)(c)(d)(e)(f)(g)(h)(i)(j)(k)",
                "abcdefghijk",
                "$11,$111,$1a",
                Ok("k,k1,aa"),
            ),
            ("(a)(b)", "ab", "$3", Err(JavaReplacementError::NoGroup(3))),
            ("(a)(b)", "ab", r"\$1", Ok("$1")),
            ("(a)", "a", r"x\\", Ok(r"x\")),
            ("(a)", "a", r"x\/y", Ok("x/y")),
            (
                "(a)",
                "a",
                "x$",
                Err(JavaReplacementError::MissingGroupIndex),
            ),
            (
                "(a)",
                "a",
                r"x\",
                Err(JavaReplacementError::MissingEscapedCharacter),
            ),
            (
                "(a)",
                "a",
                "$x",
                Err(JavaReplacementError::IllegalGroupReference),
            ),
            ("(?<nm>a)(b)", "ab", "${nm}-$2", Ok("a-b")),
            ("(a)", "a", "${}", Err(JavaReplacementError::EmptyGroupName)),
            (
                "(a)",
                "a",
                "${nm",
                Err(JavaReplacementError::UnterminatedGroupName),
            ),
            (
                "(a)",
                "a",
                "${1a}",
                Err(JavaReplacementError::GroupNameStartsWithDigit(
                    "1a".to_owned(),
                )),
            ),
            (
                "(a)",
                "a",
                "${zz}",
                Err(JavaReplacementError::NoGroupNamed("zz".to_owned())),
            ),
            // A group that did not participate expands to nothing.
            ("(a)|(b)", "b", "[$1]", Ok("[]")),
            // Leftmost-first alternation, then the scan resumes after `a`.
            ("a|ab", "ab", "X", Ok("Xb")),
            // An empty match right after a non-empty one is still replaced.
            ("x*", "xx", "[$0]", Ok("[xx][]")),
            ("x*", "", "-", Ok("-")),
            ("", "ab", "-", Ok("-a-b-")),
            ("a*", "baaa", "X", Ok("XbXX")),
            // No match: a malformed replacement is never looked at.
            ("z", "ab", "$", Ok("ab")),
        ];
        for (pattern, text, replacement, expected) in cases {
            let compiled = JavaPattern::new(pattern).expect("pattern compiles");
            check!(
                compiled.replace_all(text, replacement).as_deref() == expected.as_deref(),
                "{pattern} over {text} with {replacement}"
            );
        }
    }

    #[test]
    fn replace_first_stops_after_one_match() {
        let compiled = JavaPattern::new("a").expect("pattern compiles");
        check!(compiled.replace_first("banana", "o").as_deref() == Ok("bonana"));
    }

    /// `Matcher.matches()` backtracks to any whole-input match, which a
    /// leftmost-first `find` can miss.
    #[test]
    fn matches_is_a_whole_input_match() {
        // (pattern, text, matches)
        let cases = [
            ("a|ab", "ab", true),
            ("CN=(.*?)", "CN=alice", true),
            ("CN=(.*?),", "CN=alice,OU=x", false),
            ("ali", "alice", false),
            ("^DC=(a)|(b)$", "DC=b", false),
            ("", "", true),
            // Java's predefined classes are ASCII-only.
            (r"CN=(\d+)", "CN=12", true),
            (r"CN=(\d+)", "CN=\u{663}", false),
            (r"\w+", "\u{e9}", false),
            (r"\W", "\u{e9}", true),
            (r"\s", "\u{a0}", false),
            (r"[\d\s]+", "1 2", true),
            (r"a\b.", "a\u{e9}", true),
            (r"\[\d\]", "[1]", true),
            // Escaped punctuation is literal, in and out of a class.
            (r"CN=a\,b\@c\/d", "CN=a,b@c/d", true),
            (r"[\,\]\-]+", ",]-", true),
            (r"\\,", r"\,", true),
        ];
        for (pattern, text, expected) in cases {
            let compiled = JavaPattern::new(pattern).expect("pattern compiles");
            check!(
                compiled.matches(text) == expected,
                "{pattern} against {text}"
            );
        }
    }

    #[test]
    fn group_count_excludes_the_whole_match() {
        check!(
            JavaPattern::new("(a)(?:b)(?<c>c)")
                .expect("compiles")
                .group_count()
                == 2
        );
    }
}