test/html is cause of xss

This commit is contained in:
nazoking
2015-01-30 15:32:53 +09:00
parent da55bf6af3
commit 9ba564c864
5 changed files with 16 additions and 8 deletions

View File

@@ -135,8 +135,9 @@ trait AccountControllerBase extends AccountManagementControllerBase {
get("/:userName/_avatar"){ get("/:userName/_avatar"){
val userName = params("userName") val userName = params("userName")
getAccountByUserName(userName).flatMap(_.image).map { image => getAccountByUserName(userName).flatMap(_.image).map { image =>
contentType = FileUtil.getMimeType(image) outputUploadedRawData(
new java.io.File(getUserUploadDir(userName), image) FileUtil.getMimeType(image),
new java.io.File(getUserUploadDir(userName), image))
} getOrElse { } getOrElse {
contentType = "image/png" contentType = "image/png"
Thread.currentThread.getContextClassLoader.getResourceAsStream("noimage.png") Thread.currentThread.getContextClassLoader.getResourceAsStream("noimage.png")

View File

@@ -134,6 +134,16 @@ abstract class ControllerBase extends ScalatraFilter
if (path.startsWith("http")) path if (path.startsWith("http")) path
else baseUrl + super.url(path, params, false, false, false) else baseUrl + super.url(path, params, false, false, false)
/** against XSS */
def outputUploadedRawData[DATATYPE](contentType: String, rawData: DATATYPE): DATATYPE = {
if(contentType.split(";").head.trim.toLowerCase.startsWith("text/html")){
this.contentType = "text/plain"
} else {
this.contentType = contentType
}
response.addHeader("X-Content-Type-Options", "nosniff")
rawData
}
} }
/** /**

View File

@@ -292,8 +292,7 @@ trait IssuesControllerBase extends ControllerBase {
(Directory.getAttachedDir(repository.owner, repository.name) match { (Directory.getAttachedDir(repository.owner, repository.name) match {
case dir if(dir.exists && dir.isDirectory) => case dir if(dir.exists && dir.isDirectory) =>
dir.listFiles.find(_.getName.startsWith(params("file") + ".")).map { file => dir.listFiles.find(_.getName.startsWith(params("file") + ".")).map { file =>
contentType = FileUtil.getMimeType(file.getName) outputUploadedRawData(FileUtil.getMimeType(file.getName), file)
file
} }
case _ => None case _ => None
}) getOrElse NotFound }) getOrElse NotFound

View File

@@ -214,8 +214,7 @@ trait RepositoryViewerControllerBase extends ControllerBase {
if(raw){ if(raw){
// Download // Download
defining(JGitUtil.getContentFromId(git, objectId, false).get){ bytes => defining(JGitUtil.getContentFromId(git, objectId, false).get){ bytes =>
contentType = FileUtil.getContentType(path, bytes) outputUploadedRawData(FileUtil.getContentType(path, bytes), bytes)
bytes
} }
} else { } else {
repo.html.blob(id, repository, path.split("/").toList, JGitUtil.getContentInfo(git, path, objectId), repo.html.blob(id, repository, path.split("/").toList, JGitUtil.getContentInfo(git, path, objectId),

View File

@@ -164,8 +164,7 @@ trait WikiControllerBase extends ControllerBase {
val path = multiParams("splat").head val path = multiParams("splat").head
getFileContent(repository.owner, repository.name, path).map { bytes => getFileContent(repository.owner, repository.name, path).map { bytes =>
contentType = FileUtil.getContentType(path, bytes) outputUploadedRawData(FileUtil.getContentType(path, bytes), bytes)
bytes
} getOrElse NotFound } getOrElse NotFound
}) })