Skip to content

Commit c599669

Browse files
fix: privacy module requests (#841)
* fix: don't stringify null * fix: remove identifier, which was mutually exclusive with value * feat: log error bodies for 4xx requests * feat: warn user when comment/s contained sensitive data * test: add test for marking sensitive comments as private * style: remove trailing comma
1 parent 2380025 commit c599669

File tree

8 files changed

+22
-5
lines changed

8 files changed

+22
-5
lines changed

config/config.yml

+1
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ arisa:
172172

173173
privacy:
174174
message: panel-mark-private
175+
commentMessage: panel-mark-private-comment
175176
commentNote: |-
176177
177178
----

src/main/kotlin/io/github/mojira/arisa/apiclient/JiraClient.kt

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import io.github.mojira.arisa.apiclient.models.IssueLink
1717
import io.github.mojira.arisa.apiclient.models.User
1818
import io.github.mojira.arisa.apiclient.models.Visibility
1919
import io.github.mojira.arisa.apiclient.requestModels.*
20+
import io.github.mojira.arisa.log
2021
import kotlinx.serialization.json.Json
2122
import okhttp3.MediaType.Companion.toMediaType
2223
import okhttp3.OkHttpClient
@@ -151,6 +152,7 @@ private inline fun <reified T> Call<T>.executeOrThrow(): T {
151152

152153
if (!response.isSuccessful) {
153154
if (response.code() in 400..499) {
155+
response.errorBody()?.let { log.error(it.string()) }
154156
throw ClientErrorException(
155157
response.code(),
156158
"Request failed - [${response.code()}] ${originalRequest.method} ${originalRequest.url}",

src/main/kotlin/io/github/mojira/arisa/infrastructure/config/ArisaConfig.kt

+4
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,10 @@ object Arisa : ConfigSpec() {
164164
val message by required<String>(
165165
description = "The key of the message that is posted when this module succeeds."
166166
)
167+
val commentMessage by required<String>(
168+
description = "The key of the message that is posted when this module detects sensitive data within" +
169+
"the comment."
170+
)
167171
val commentNote by optional(
168172
"",
169173
description = "The text which will be appended at the comments that are restricted by this module."

src/main/kotlin/io/github/mojira/arisa/infrastructure/jira/Functions.kt

+2-3
Original file line numberDiff line numberDiff line change
@@ -437,9 +437,8 @@ fun restrictCommentToGroup(
437437
val payload = UpdateCommentBody(
438438
body,
439439
visibility = Visibility(
440-
identifier = group,
441-
type = Visibility.Type.Group.value,
442-
value = group
440+
value = group,
441+
type = Visibility.Type.Group.value
443442
)
444443
)
445444
context.value.jiraClient.updateComment(

src/main/kotlin/io/github/mojira/arisa/infrastructure/jira/Mappers.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ fun MojiraComment.toDomain(
232232
getUpdateAuthorGroups = { if (updateAuthor == null) emptyList() else getGroups(jiraClient, updateAuthor.accountId).fold({ null }, { it }) },
233233
created = created!!.toInstant(),
234234
updated = updated!!.toInstant(),
235-
visibilityType = visibility?.type.toString(),
235+
visibilityType = visibility?.type,
236236
visibilityValue = visibility?.value,
237237
restrict = ::restrictCommentToGroup.partially1(context).partially1(this).partially1("staff"),
238238
update = ::updateCommentBody.partially1(context).partially1(this),

src/main/kotlin/io/github/mojira/arisa/modules/privacy/PrivacyModule.kt

+4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ private val log = LoggerFactory.getLogger("PrivacyModule")
2424

2525
class PrivacyModule(
2626
private val message: String,
27+
private val commentMessage: String,
2728
private val commentNote: String,
2829
private val allowedEmailRegexes: List<Regex>,
2930
private val sensitiveTextRegexes: List<Regex>,
@@ -58,6 +59,9 @@ class PrivacyModule(
5859
}
5960

6061
restrictCommentActions.forEach { it.invoke() }
62+
if (restrictCommentActions.isNotEmpty()) {
63+
addComment(CommentOptions(commentMessage))
64+
}
6165
}
6266
}
6367

src/main/kotlin/io/github/mojira/arisa/registry/InstantModuleRegistry.kt

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class InstantModuleRegistry(config: Config) : ModuleRegistry<CloudIssue>(config)
2828
Arisa.Modules.Privacy,
2929
PrivacyModule(
3030
config[Arisa.Modules.Privacy.message],
31+
config[Arisa.Modules.Privacy.commentMessage],
3132
config[Arisa.Modules.Privacy.commentNote],
3233
config[Arisa.Modules.Privacy.allowedEmailRegexes].map(String::toRegex),
3334
config[Arisa.Modules.Privacy.sensitiveTextRegexes].map(String::toRegex),

src/test/kotlin/io/github/mojira/arisa/modules/privacy/PrivacyModuleTest.kt

+7-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ private val TWO_SECONDS_AGO = RIGHT_NOW.minusSeconds(2)
2020
private val TEN_SECONDS_AGO = RIGHT_NOW.minusSeconds(10)
2121

2222
private const val MADE_PRIVATE_MESSAGE = "made-private"
23+
private const val MADE_PRIVATE_COMMENTS_MESSAGE = "made-private-comments"
2324
private const val COMMENT_NOTE = "\n----\nRestricted by PrivacyModule ??[~arisabot]??"
2425

2526
private val NOOP_REDACTOR = object : AttachmentRedactor {
@@ -30,13 +31,15 @@ private val NOOP_REDACTOR = object : AttachmentRedactor {
3031

3132
private fun createModule(
3233
message: String = MADE_PRIVATE_MESSAGE,
34+
commentMessage: String = MADE_PRIVATE_COMMENTS_MESSAGE,
3335
commentNote: String = COMMENT_NOTE,
3436
allowedEmailRegexes: List<Regex> = emptyList(),
3537
sensitiveTextRegexes: List<Regex> = emptyList(),
3638
attachmentRedactor: AttachmentRedactor = NOOP_REDACTOR,
3739
sensitiveFileNameRegexes: List<Regex> = emptyList()
3840
) = PrivacyModule(
3941
message,
42+
commentMessage,
4043
commentNote,
4144
allowedEmailRegexes,
4245
sensitiveTextRegexes,
@@ -320,6 +323,7 @@ class PrivacyModuleTest : FunSpec({
320323
test("should restrict to staff when the comment contains sensitive data") {
321324
var hasSetPrivate = false
322325
var hasRestrictedComment = false
326+
var addedComment: CommentOptions? = null
323327

324328
val issue = mockCloudIssue(
325329
comments = listOf(
@@ -331,14 +335,16 @@ class PrivacyModuleTest : FunSpec({
331335
}
332336
)
333337
),
334-
setPrivate = { hasSetPrivate = true }
338+
setPrivate = { hasSetPrivate = true },
339+
addComment = { addedComment = it }
335340
)
336341

337342
val result = testData.module(issue, TWO_SECONDS_AGO)
338343

339344
result.shouldBeRight(ModuleResponse)
340345
hasSetPrivate shouldBe false
341346
hasRestrictedComment shouldBe true
347+
addedComment shouldBe CommentOptions(MADE_PRIVATE_COMMENTS_MESSAGE)
342348
}
343349
}
344350
}

0 commit comments

Comments
 (0)