@@ -7,21 +7,16 @@ import io.github.mojira.arisa.domain.CommentOptions
7
7
import io.github.mojira.arisa.domain.Issue
8
8
import java.time.Instant
9
9
10
+ private fun Iterable<Regex>.anyMatches (string : String ) = any { it.matches(string) }
11
+
10
12
class PrivacyModule (
11
13
private val message : String ,
12
14
private val commentNote : String ,
13
- private val allowedEmailsRegex : List <Regex >,
14
- private val sensitiveFileNames : List <String >
15
+ private val allowedEmailRegexes : List <Regex >,
16
+ private val sensitiveTextRegexes : List <Regex >,
17
+ private val sensitiveFileNameRegexes : List <Regex >
15
18
) : Module {
16
- private val patterns: List <Regex > = listOf (
17
- """ .*\(Session ID is token:.*""" .toRegex(),
18
- """ .*--accessToken ey.*""" .toRegex(),
19
- """ .*(?<![^\s])(?=[^\s]*[A-Z])(?=[^\s]*[0-9])[A-Z0-9]{17}(?![^\s]).*""" .toRegex(),
20
- // At the moment braintree transaction IDs seem to have 8 chars, but to be future-proof
21
- // match if there are more chars as well
22
- """ .*\bbraintree:[a-f0-9]{6,12}\b.*""" .toRegex()
23
- )
24
-
19
+ // Matches an email address, which is not part of a user mention ([~name])
25
20
private val emailRegex = " (?<!\\ [~)\\ b[a-zA-Z0-9.\\ -_]+@[a-zA-Z.\\ -_]+\\ .[a-zA-Z.\\ -]{2,15}\\ b" .toRegex()
26
21
27
22
override fun invoke (issue : Issue , lastRun : Instant ): Either <ModuleError , ModuleResponse > = with (issue) {
@@ -34,9 +29,9 @@ class PrivacyModule(
34
29
string + = " $summary $environment $description "
35
30
}
36
31
37
- attachments
32
+ val newAttachments = attachments.filter { it.created.isAfter(lastRun) }
33
+ newAttachments
38
34
.asSequence()
39
- .filter { it.created.isAfter(lastRun) }
40
35
.filter { it.mimeType.startsWith(" text/" ) }
41
36
.forEach { string + = " ${String (it.getContent())} " }
42
37
@@ -46,19 +41,19 @@ class PrivacyModule(
46
41
.filter { it.changedFromString == null }
47
42
.forEach { string + = " ${it.changedToString} " }
48
43
49
- val doesStringMatchPatterns = string.matches(patterns )
44
+ val doesStringMatchPatterns = string.containsMatch(sensitiveTextRegexes )
50
45
val doesEmailMatches = matchesEmail(string)
51
46
52
- val doesAttachmentNameMatch = attachments
47
+ val doesAttachmentNameMatch = newAttachments
53
48
.asSequence()
54
49
.map(Attachment ::name)
55
- .any(sensitiveFileNames::contains )
50
+ .any(sensitiveFileNameRegexes::anyMatches )
56
51
57
52
val restrictCommentFunctions = comments
58
53
.asSequence()
59
54
.filter { it.created.isAfter(lastRun) }
60
55
.filter { it.visibilityType == null }
61
- .filter { it.body?.matches(patterns ) ? : false || matchesEmail(it.body ? : " " ) }
56
+ .filter { it.body?.containsMatch(sensitiveTextRegexes ) ? : false || matchesEmail(it.body ? : " " ) }
62
57
.filterNot {
63
58
it.getAuthorGroups()?.any { group ->
64
59
listOf (" helper" , " global-moderators" , " staff" ).contains(group)
@@ -86,9 +81,10 @@ class PrivacyModule(
86
81
private fun matchesEmail (string : String ): Boolean {
87
82
return emailRegex
88
83
.findAll(string)
89
- .filterNot { email -> allowedEmailsRegex.any { regex -> regex.matches(email.value) } }
84
+ .map(MatchResult ::value)
85
+ .filterNot(allowedEmailRegexes::anyMatches)
90
86
.any()
91
87
}
92
88
93
- private fun String.matches (patterns : List <Regex >) = patterns.any { it.containsMatchIn(this ) }
89
+ private fun String.containsMatch (patterns : List <Regex >) = patterns.any { it.containsMatchIn(this ) }
94
90
}
0 commit comments