From 5bcef6323a02da48a9c9d1590c2967749b17c51e Mon Sep 17 00:00:00 2001 From: aadrian Date: Thu, 19 Nov 2026 15:59:49 +0100 Subject: [PATCH] Apply the visibility of a private gist to all its pages and to Git access, and let only the author or the gist's owner edit or delete a comment. --- src/main/scala/Plugin.scala | 15 ++++++++++-- .../gist/controller/GistController.scala | 24 ++++++++++++++----- 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/src/main/scala/Plugin.scala b/src/main/scala/Plugin.scala index 0313426..f081cd8 100755 --- a/src/main/scala/Plugin.scala +++ b/src/main/scala/Plugin.scala @@ -1,6 +1,7 @@ import gitbucket.core.controller.Context import gitbucket.core.model._ import gitbucket.core.service.AccountService +import gitbucket.gist.service.GistService import gitbucket.core.service.SystemSettingsService.SystemSettings import gitbucket.gist.controller.GistController import gitbucket.core.plugin._ @@ -95,7 +96,7 @@ class Plugin extends gitbucket.core.plugin.Plugin { // } } -class GistRepositoryFilter extends GitRepositoryFilter with AccountService { +class GistRepositoryFilter extends GitRepositoryFilter with AccountService with GistService { override def filter(path: String, userName: Option[String], settings: SystemSettings, isUpdating: Boolean) (implicit session: Session): Boolean = { @@ -106,7 +107,17 @@ class GistRepositoryFilter extends GitRepositoryFilter with AccountService { } yield path.startsWith("/" + userName + "/") || account.isAdmin ).getOrElse(false) - } else true + } else { + // A private gist only for its owner (or the owner group's members) and admins + "^/gist/([^/]+)/([^/]+?)\\.git(/|$)".r.findFirstMatchIn(path).flatMap(m => getGist(m.group(1), m.group(2))) match { + case Some(gist) if gist.mode == "PRIVATE" => + userName.flatMap(getAccountByUserName(_)).exists { account => + account.isAdmin || account.userName == gist.userName || + getGroupsByUserName(account.userName).contains(gist.userName) + } + case _ => true + } + } } } diff --git a/src/main/scala/gitbucket/gist/controller/GistController.scala b/src/main/scala/gitbucket/gist/controller/GistController.scala index 6dde722..8f71c07 100644 --- a/src/main/scala/gitbucket/gist/controller/GistController.scala +++ b/src/main/scala/gitbucket/gist/controller/GistController.scala @@ -64,6 +64,11 @@ trait GistControllerBase extends ControllerBase { html.list(None, gists, page, page * Limit < count) } + // A private gist is visible only to those who can edit it; this covers all pages below a gist + before("/gist/:userName/:repoName/*") { + getGist(params("userName"), params("repoName")).filterNot(isReadable).foreach(_ => halt(NotFound())) + } + get("/gist/:userName/:repoName"){ _gist(params("userName"), Some(params("repoName"))) } @@ -72,9 +77,9 @@ trait GistControllerBase extends ControllerBase { val userName = params("userName") val repoName = params("repoName") getGist(userName, repoName) match { - case Some(gist) => + case Some(gist) if isReadable(gist) => _embedJs(gist, userName, repoName, "master") - case None => + case _ => NotFound() } } @@ -226,7 +231,7 @@ trait GistControllerBase extends ControllerBase { if(gist.mode == "PUBLIC" || context.loginAccount.exists(x => x.isAdmin || x.userName == userName)){ JGitUtil.getFileList(git, revision, ".").find(_.name == fileName).map { file => val bytes = JGitUtil.getContentFromId(git, file.id, false).get - RawData(FileUtil.getMimeType(file.name, bytes), bytes) + RawData(if (FileUtil.isText(bytes)) "text/plain; charset=UTF-8" else FileUtil.getSafeMimeType(file.name), bytes) } getOrElse NotFound() } else Unauthorized() } @@ -399,7 +404,7 @@ trait GistControllerBase extends ControllerBase { val repoName = params("repoName") val commentId = params("commentId").toInt - // TODO Access check + if (!isCommentEditable(userName, repoName, commentId)) halt(Unauthorized()) Ok(deleteGistComment(userName, repoName, commentId)) }) @@ -409,7 +414,6 @@ trait GistControllerBase extends ControllerBase { val repoName = params("repoName") val commentId = params("commentId").toInt - // TODO Access check getGist(userName, repoName).flatMap { gist => getGistComment(userName, repoName, commentId).map { comment => params.get("dataType") collect { @@ -437,7 +441,7 @@ trait GistControllerBase extends ControllerBase { val repoName = params("repoName") val commentId = params("commentId").toInt - // TODO Access check + if (!isCommentEditable(userName, repoName, commentId)) halt(Unauthorized()) updateGistComment(userName, repoName, commentId, form.content) redirect(s"/gist/${userName}/${repoName}/_comments/${commentId}") @@ -537,6 +541,14 @@ trait GistControllerBase extends ControllerBase { } } + private def isReadable(gist: Gist): Boolean = + gist.mode != "PRIVATE" || isEditable(gist.userName, loginUserGroups) + + private def isCommentEditable(userName: String, repoName: String, commentId: Int): Boolean = + isEditable(userName, loginUserGroups) || getGistComment(userName, repoName, commentId).exists { comment => + context.loginAccount.exists(_.userName == comment.commentedUserName) + } + private def loginUserGroups: Seq[String] = { context.loginAccount.map { account => getGroupsByUserName(account.userName)