Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Github scanning webhook #15167

Merged
merged 6 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions app/controllers/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,11 @@ final class Main(
.map(url => JsonOk(Json.obj("imageUrl" -> url)))
case None => JsonBadRequest(jsonError("Image content only"))
}

def githubSecretScanning = AnonBodyOf(parse.json): body =>
env.oAuth.tokenApi
.secretScanning(body)
.flatMap:
_.traverse: (token, url) =>
env.msg.api.systemPost(token.userId, lila.msg.MsgPreset.apiTokenRevoked(url))
.as(NoContent)
1 change: 1 addition & 0 deletions conf/routes
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,7 @@ GET /verify-title controllers.Main.verifyTitle
GET /InstantChess.com controllers.Main.instantChess
GET /daily-puzzle-slack controllers.Main.dailyPuzzleSlackApp
POST /upload/image/user/:rel controllers.Main.uploadImage(rel)
POST /github/secret-scanning controllers.Main.githubSecretScanning

# Technical
GET /run/captcha/$id<\w{8}> controllers.Main.captchaCheck(id)
Expand Down
10 changes: 10 additions & 0 deletions modules/msg/src/main/MsgPreset.scala
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,13 @@ $forumPost
s"""@${by.name} has changed your leader permissions in the team "${team.name}".
Your new permissions are: ${perms.mkString(", ")}.
$baseUrl/team/${team.id}"""

def apiTokenRevoked(url: String) =
s"""Your Lichess API token has been found on GitHub

We detected one of your API tokens in a public code repository on GitHub at the following URL:

$url

We have automatically revoked the token to protect your account.
"""
24 changes: 24 additions & 0 deletions modules/oauth/src/main/AccessTokenApi.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package lila.oauth

import reactivemongo.api.bson.*
import play.api.libs.json.*

import lila.core.net.Bearer
import lila.db.dsl.{ *, given }
Expand Down Expand Up @@ -211,6 +212,29 @@ final class AccessTokenApi(
bearers.zip(openTokens).toMap
}

def secretScanning(body: JsValue): Fu[List[(AccessToken, String)]] =
body
.asOpt[List[JsObject]]
.map:
_.flatMap: obj =>
for
token <- (obj \ "token").asOpt[String]
url <- (obj \ "url").asOpt[String]
yield Bearer(token) -> url
.toMap
.so: tokensMap =>
test(tokensMap.keys.toList)
.flatMap:
_.toList.traverse: (bearer, token) =>
token match
case Some(token) =>
logger.branch("github").info(s"revoking token ${token.plain} for user ${token.userId}")
revoke(token.plain).inject(tokensMap.get(bearer).map(token -> _))
case None =>
logger.branch("github").info(s"ignoring token $bearer")
fuccess(none)
.map(_.flatten)

private val accessTokenCache =
cacheApi[AccessToken.Id, Option[AccessToken.ForAuth]](1024, "oauth.access_token"):
_.expireAfterWrite(5 minutes).buildAsyncFuture(fetchAccessToken)
Expand Down