All files / src/redaction scanner-generic.ts

91.11% Statements 123/135
87.59% Branches 113/129
100% Functions 10/10
91.04% Lines 122/134

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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      9x 9x   9x 73x 1x     72x 72x 72x   72x 3158x   3158x 31x 31x 25x 21x   25x 25x 25x 25x       3133x 115x 115x 14x 13x   14x 14x 14x 14x       3119x     72x 38x     34x 9x     34x       31x 31x         31x 31x 174x   31x 31x   31x 1x       30x 2x     28x 126x 1x         27x 27x 27x 27x   27x 286x 286x 244x 244x 244x   42x 28x     28x 28x 28x 28x 28x   14x     27x 27x     27x 1x     26x 1x       25x     25x       25x                 115x 115x 115x 115x 115x 115x 115x   115x 15x 15x 15x         115x 878x 878x 628x 628x 250x 161x 161x 89x 4x 4x 4x 85x 13x 9x   4x 4x 72x           72x       106x 106x     106x 80x     26x 1x   25x 11x     14x           14x       4102x       542x       505x       37x       34x       198x       286x      
// Generic redaction for emails and phone numbers
// This performs a single linear scan to redact generic emails and phone numbers
 
const PHONE_MIN_DIGITS = 10;
const PHONE_MAX_DIGITS = 15;
 
export function redactGeneric(s: string): string {
  if (s.length === 0) {
    return s;
  }
 
  const result: string[] = [];
  let lastWrite = 0;
  let i = 0;
 
  while (i < s.length) {
    const c = s[i];
 
    if (c === '@') {
      const emailResult = scanEmailAt(s, i);
      if (emailResult.valid) {
        if (emailResult.start > lastWrite) {
          result.push(s.substring(lastWrite, emailResult.start));
        }
        result.push('[REDACTED]');
        lastWrite = emailResult.end;
        i = emailResult.end;
        continue;
      }
    }
 
    if (c === '+' || isDigit(c)) {
      const phoneResult = scanPhoneAt(s, i, PHONE_MIN_DIGITS, PHONE_MAX_DIGITS);
      if (phoneResult.valid) {
        if (phoneResult.start > lastWrite) {
          result.push(s.substring(lastWrite, phoneResult.start));
        }
        result.push('[REDACTED]');
        lastWrite = phoneResult.end;
        i = phoneResult.end;
        continue;
      }
    }
 
    i++;
  }
 
  if (lastWrite === 0) {
    return s;
  }
 
  if (lastWrite < s.length) {
    result.push(s.substring(lastWrite));
  }
 
  return result.join('');
}
 
function scanEmailAt(s: string, idx: number): { start: number; end: number; valid: boolean } {
  const n = s.length;
  Iif (idx <= 0 || idx >= n - 1) {
    return { start: 0, end: 0, valid: false };
  }
 
  // Expand left for local-part
  let l = idx - 1;
  while (l >= 0 && isEmailLocalChar(s[l])) {
    l--;
  }
  const localStart = l + 1;
  const localEnd = idx;
 
  if (localStart >= localEnd) {
    return { start: 0, end: 0, valid: false };
  }
 
  // Basic local-part validity: no leading/trailing dot, no consecutive dots
  if (s[localStart] === '.' || s[localEnd - 1] === '.') {
    return { start: 0, end: 0, valid: false };
  }
 
  for (let i = localStart + 1; i < localEnd; i++) {
    if (s[i] === '.' && s[i - 1] === '.') {
      return { start: 0, end: 0, valid: false };
    }
  }
 
  // Expand right for domain
  let r = idx + 1;
  let labelLen = 0;
  let hasDot = false;
  let lastDot = -1;
 
  while (r < n) {
    const ch = s[r];
    if (isDomainLabelChar(ch)) {
      labelLen++;
      r++;
      continue;
    }
    if (ch === '.') {
      Iif (labelLen === 0 || s[r - 1] === '-') {
        return { start: 0, end: 0, valid: false };
      }
      hasDot = true;
      lastDot = r;
      labelLen = 0;
      r++;
      continue;
    }
    break;
  }
 
  const domainEnd = r;
  Iif (labelLen === 0 || s[domainEnd - 1] === '-') {
    return { start: 0, end: 0, valid: false };
  }
  if (!hasDot) {
    return { start: 0, end: 0, valid: false };
  }
 
  if (lastDot < 0 || domainEnd - lastDot - 1 < 2 || domainEnd - lastDot - 1 > 24) {
    return { start: 0, end: 0, valid: false };
  }
 
  // Boundary checks
  Iif (localStart > 0 && isAlphaNumUnderscore(s[localStart - 1])) {
    return { start: 0, end: 0, valid: false };
  }
  Iif (domainEnd < n && isAlphaNumUnderscore(s[domainEnd])) {
    return { start: 0, end: 0, valid: false };
  }
 
  return { start: localStart, end: domainEnd, valid: true };
}
 
function scanPhoneAt(
  s: string,
  i: number,
  minDigits: number,
  maxDigits: number
): { start: number; end: number; valid: boolean } {
  const n = s.length;
  const start = i;
  let j = i;
  let digitCount = 0;
  let seenPlus = false;
  let seenSeparator = false;
  let parenDepth = 0;
 
  if (s[j] === '+') {
    seenPlus = true;
    j++;
    Iif (j >= n) {
      return { start: 0, end: 0, valid: false };
    }
  }
 
  while (j < n) {
    const ch = s[j];
    if (isDigit(ch)) {
      digitCount++;
      j++;
    } else if (ch === ' ' || ch === '-' || ch === '.') {
      seenSeparator = true;
      j++;
    } else if (ch === '(') {
      parenDepth++;
      seenSeparator = true;
      j++;
    } else if (ch === ')') {
      if (parenDepth === 0) {
        return { start: 0, end: 0, valid: false };
      }
      parenDepth--;
      j++;
    } else Iif (ch === 'x' || ch === 'X') {
      if (digitCount >= minDigits) {
        break;
      }
      break;
    } else {
      break;
    }
  }
 
  const end = j;
  Iif (parenDepth !== 0) {
    return { start: 0, end: 0, valid: false };
  }
  if (digitCount < minDigits || digitCount > maxDigits) {
    return { start: 0, end: 0, valid: false };
  }
 
  if (start > 0 && isAlphaUnderscore(s[start - 1])) {
    return { start: 0, end: 0, valid: false };
  }
  if (end < n && isAlphaUnderscore(s[end])) {
    return { start: 0, end: 0, valid: false };
  }
 
  Iif (!seenSeparator && !seenPlus && digitCount >= 12) {
    if (!(start + 1 < n && s[start] === '0' && s[start + 1] === '0')) {
      return { start: 0, end: 0, valid: false };
    }
  }
 
  return { start, end, valid: true };
}
 
function isDigit(b: string): boolean {
  return b >= '0' && b <= '9';
}
 
function isLetter(b: string): boolean {
  return (b >= 'a' && b <= 'z') || (b >= 'A' && b <= 'Z');
}
 
function isAlphaNum(b: string): boolean {
  return isLetter(b) || isDigit(b);
}
 
function isAlphaUnderscore(b: string): boolean {
  return isLetter(b) || b === '_';
}
 
function isAlphaNumUnderscore(b: string): boolean {
  return isAlphaNum(b) || b === '_';
}
 
function isEmailLocalChar(b: string): boolean {
  return b === '.' || b === '_' || b === '+' || b === '-' || isAlphaNum(b);
}
 
function isDomainLabelChar(b: string): boolean {
  return isAlphaNum(b) || b === '-';
}