Keep the part of an email address before an apostrophe when redacting

## What this changes

Email redaction no longer drops the part of an address before an apostrophe. The email scanner's `local` closure in `crates/redact/src/patterns.rs` did not accept an apostrophe in the local part. It treated `'` as a boundary and started the match after it, so for `o'brien@example.com` only `brien@example.com` matched. Search and Redact share that span, so the leading `o'` stayed visible.

The local part may now contain an apostrophe, but a match cannot start on one. `o'brien@example.com` and `'john'@example.com` are now matched as `o'brien@example.com` and `john'@example.com`. A quote around an address (`'john@example.com'`) stays outside the match.

No linked issue. Found while testing `crates/redact/src/patterns.rs:216` (email, `local` closure).

## How you verified it

- [ ] `cargo fmt --check`
- [ ] clippy with `-D warnings` on the crates I changed
- [x] tests pass on the crates I changed
- [ ] `cargo xtask layers` and `cargo xtask assets` (if you touched crate boundaries or any asset)

- Regression test `patterns::tests::email_local_part_may_contain_an_apostrophe` covers all three cases. It fails on `main` without the fix and passes with it (`cargo test -p pdfcraft-redact email_local_part_may_contain_an_apostrophe`).
- `cargo xtask ci` passes.

## Never-crash

- [ ] No new `unwrap`/`expect`/`panic!`/`unreachable!`/`todo!` outside tests, unless it is provably infallible with a comment saying why
- [ ] No indexing or slicing with input-derived positions (`get`, and strings only at char boundaries); checked or saturating arithmetic; allocations sized by input are capped; recursion is bounded
- [ ] No `unsafe`
- [ ] Any crash this fixes has a small synthetic regression test

## Provenance

- [ ] No asset from an Adobe product, and no Adobe icon recreated, traced or imitated
- [ ] No font files committed here (they live in [storytold/craft-fonts](https://github.com/storytold/craft-fonts))
- [ ] No GPL/AGPL code copied in (MuPDF, Ghostscript, Poppler, veraPDF and DSS may only be run as external oracle processes)
- [ ] Behaviour comes from the specification or black-box observation, not from reading another implementation's source
- [ ] Every new asset has an `ATTRIBUTION.toml` entry and `cargo xtask assets` passes

## Security

- [ ] `--root` confinement still holds on every path, including ones reached by nested tool dispatch
- [ ] MCP and the UI control channel stay opt-in, loopback-only and token-authenticated
- [ ] No new outbound network access

## Parity and reach

- [ ] `parity/acrobat-features.toml` updated in this PR, with `notes` saying what is still missing
- [ ] Reachable headlessly through an `automation` tool where that applies, so agents can drive it and not just the UI
- [ ] Docs updated if this changes a documented behaviour (`docs/`, crate README)

## Anything a reviewer should look at first

- A quote right before the local part, as in `'john'@example.com`, stays outside the match, so that leading quote stays visible after Redact. Matching it would also pick up the quote around quoted addresses. This is a deliberate restraint.
- The domain scan and the other patterns are unchanged.

🤖 Generated with AI by [ApolloOffice](https://github.com/Silvertree2010/ApolloOffice), see the Co-Authored-By trailers for the models
This commit is contained in:
NetterSilver
2026-11-18 16:32:21 +07:00
committed by GitHub
parent f435c182d2
commit 5aae9dbae2
+14 -2
View File
@@ -219,11 +219,15 @@ fn email(s: &[char], i: usize) -> Option<usize> {
let local = |c: &char| address_char(*c) || "._%+-".contains(*c);
// A match starts at the first character of a run of local characters, which may be
// punctuation ("_admin@host.com"), but the run needs a letter or digit.
if !local(&s[i]) || i.checked_sub(1).and_then(|p| s.get(p)).is_some_and(local) {
// An apostrophe ("o'brien@example.com") belongs to the local part but never starts it, so a
// quote around an address ("'john@example.com'") stays outside the match.
let inner = |c: &char| local(c) || *c == '\'';
let before = |n: usize| i.checked_sub(n).and_then(|p| s.get(p));
if !local(&s[i]) || before(1).is_some_and(local) || (before(1) == Some(&'\'') && before(2).is_some_and(inner)) {
return None;
}
let mut j = i;
while j < s.len() && local(&s[j]) {
while j < s.len() && inner(&s[j]) {
j += 1;
}
if s.get(j) != Some(&'@') || !s[i..j].iter().any(|c| address_char(*c)) {
@@ -627,6 +631,14 @@ mod tests {
find(p, &c).into_iter().map(|r| c[r].iter().collect()).collect()
}
#[test]
fn email_local_part_may_contain_an_apostrophe() {
assert_eq!(found(Pattern::Email, "Mail o'brien@example.com today"), ["o'brien@example.com"]);
assert_eq!(found(Pattern::Email, "Mail 'john@example.com' today"), ["john@example.com"]);
// Only the leading quote stays outside; the whole local part is matched.
assert_eq!(found(Pattern::Email, "Mail 'john'@example.com today"), ["john'@example.com"]);
}
#[test]
fn phone_numbers() {
assert_eq!(