Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/main/scala/Plugin.scala
Original file line number Diff line number Diff line change
@@ -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._
Expand Down Expand Up @@ -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 = {
Expand All @@ -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
}
}
}

}
24 changes: 18 additions & 6 deletions src/main/scala/gitbucket/gist/controller/GistController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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")))
}
Expand All @@ -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()
}
}
Expand Down Expand Up @@ -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()
}
Expand Down Expand Up @@ -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))
})
Expand All @@ -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 {
Expand Down Expand Up @@ -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}")
Expand Down Expand Up @@ -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)
Expand Down