Improve logging on some backup validation errors.

This commit is contained in:
Greyson Parrelli
2025-02-14 11:40:28 -05:00
parent c1ce4ba80d
commit 16309d87cd
6 changed files with 107 additions and 18 deletions

View File

@@ -22,8 +22,8 @@ object Scrubber {
* Supposedly, the shortest international phone numbers in use contain seven digits.
* Handles URL encoded +, %2B
*/
private val E164_PATTERN = Pattern.compile("(\\+|%2B)(\\d{7,15})")
private val E164_ZERO_PATTERN = Pattern.compile("\\b0(\\d{10})\\b")
private val E164_PATTERN = Pattern.compile("(KEEP_E164::)?(\\+|%2B)(\\d{7,15})")
private val E164_ZERO_PATTERN = Pattern.compile("\\b(KEEP_E164::)?0(\\d{10})\\b")
/** The second group will be censored.*/
private val CRUDE_EMAIL_PATTERN = Pattern.compile("\\b([^\\s/])([^\\s/]*@[^\\s]+)")
@@ -104,17 +104,29 @@ object Scrubber {
private fun CharSequence.scrubE164(): CharSequence {
return scrub(this, E164_PATTERN) { matcher, output ->
output
.append("E164:")
.append(hash(matcher.group(2)))
if (matcher.group(1) != null && matcher.group(1)!!.isNotEmpty()) {
output
.append("KEEP_E164::")
.append((matcher.group(2) + matcher.group(3)).censorMiddle(2, 2))
} else {
output
.append("E164:")
.append(hash(matcher.group(3)))
}
}
}
private fun CharSequence.scrubE164Zero(): CharSequence {
return scrub(this, E164_ZERO_PATTERN) { matcher, output ->
output
.append("E164:")
.append(hash(matcher.group(1)))
if (matcher.group(1) != null && matcher.group(1)!!.isNotEmpty()) {
output
.append("KEEP_E164::")
.append(("0" + matcher.group(2)).censorMiddle(2, 2))
} else {
output
.append("E164:")
.append(hash(matcher.group(2)))
}
}
}
@@ -204,6 +216,15 @@ object Scrubber {
}
}
private fun String.censorMiddle(leading: Int, trailing: Int): String {
val totalKept = leading + trailing
if (this.length < totalKept) {
return "*".repeat(this.length)
}
val middle = "*".repeat(this.length - totalKept)
return this.take(leading) + middle + this.takeLast(trailing)
}
private fun scrub(input: CharSequence, pattern: Pattern, processMatch: MatchProcessor): CharSequence {
val output = StringBuilder(input.length)
val matcher: Matcher = pattern.matcher(input)

View File

@@ -74,6 +74,22 @@ class ScrubberTest(private val input: String, private val expected: String) {
"Longest number +155556789012345",
"Longest number E164:<90596>"
),
arrayOf(
"An E164 number KEEP_E164::+15551234567",
"An E164 number KEEP_E164::+1********67"
),
arrayOf(
"A UK number KEEP_E164::+447700900000",
"A UK number KEEP_E164::+4*********00"
),
arrayOf(
"A Japanese number KEEP_E164::08011112222",
"A Japanese number KEEP_E164::08*******22"
),
arrayOf(
"A Japanese number (KEEP_E164::08011112222)",
"A Japanese number (KEEP_E164::08*******22)"
),
arrayOf(
"One more than longest number +1234567890123456",
"One more than longest number E164:<78d5b>6"