Skip to content

Commit

Permalink
Use os-lib to handle incoming paths
Browse files Browse the repository at this point in the history
This is also more secure, ensuring these do not contain '..' parts
  • Loading branch information
alexarchambault committed May 28, 2024
1 parent 16119ca commit 331b267
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 19 deletions.
3 changes: 2 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ lazy val server = project
"org.slf4j" % "slf4j-api" % "2.0.13",
"org.jboss.xnio" % "xnio-nio" % "3.8.0.Final",
"org.scalameta" % "semanticdb-scalac-core" % Version.scalameta cross CrossVersion.full,
("org.scalameta" %% "mtags" % "1.1.0").cross(CrossVersion.full)
("org.scalameta" %% "mtags" % "1.1.0").cross(CrossVersion.full),
"com.lihaoyi" %% "os-lib" % "0.10.1"
),
(Compile / packageBin) := {
import java.io.FileOutputStream
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,30 +232,30 @@ class MetabrowseServer(
.build()

private def getBytes(exchange: HttpServerExchange): Array[Byte] = {
val path = exchange.getRequestPath.stripSuffix(".gz")
if (path.endsWith("index.workspace")) {
val path = os.SubPath("." + exchange.getRequestPath.stripSuffix(".gz"))
if (path.lastOpt.exists(_.endsWith("index.workspace"))) {
getWorkspace.toByteArray
} else if (path.endsWith(".symbolindexes")) {
} else if (path.lastOpt.exists(_.endsWith(".symbolindexes"))) {
val header = exchange.getRequestHeaders.get("Metabrowse-Symbol")
if (header.isEmpty) {
logger.error(s"no Metabrowse-Symbol header: $exchange")
Array.emptyByteArray
} else {
getSymbol(header.getFirst).toByteArray
}
} else if (path.endsWith(".semanticdb")) {
} else if (path.lastOpt.exists(_.endsWith(".semanticdb"))) {
getSemanticdb(path).toByteArray
} else if (path.endsWith(".map")) {
} else if (path.lastOpt.exists(_.endsWith(".map"))) {
// Ignore requests for sourcemaps.
Array.emptyByteArray
} else {
val actualPath = if (path == "/") "/index.html" else path
val actualPath = if (path == os.sub) os.sub / "index.html" else path
withInputStream(
Thread
.currentThread()
.getContextClassLoader
.getResourceAsStream(
s"metabrowse/server/assets/${actualPath.stripPrefix("/")}"
(os.sub / "metabrowse" / "server" / "assets" / actualPath).toString
)
) { is =>
if (is == null) {
Expand Down Expand Up @@ -301,27 +301,34 @@ class MetabrowseServer(
Workspace(filenames.result().toSeq)
}

private def getSemanticdb(filename: String): TextDocuments = {
val path = filename
.stripPrefix("/semanticdb/")
.stripPrefix("/") // optional '/'
.stripSuffix(".semanticdb")
logger.info(path)
private def getSemanticdb(subPath: os.SubPath): TextDocuments = {
val path = {
val subPath0 =
if (subPath.startsWith(os.sub / "semanticdb"))
subPath.relativeTo(os.sub / "semanticdb").asSubPath
else
subPath
subPath0.lastOpt match {
case Some(name) if name.endsWith(".semanticdb") => subPath0 / os.up / name.stripSuffix(".semanticdb")
case _ => subPath0
}
}
logger.info(path.toString)
for {
text <- state.get().source(path).orElse {
text <- state.get().source(path.toString).orElse {
logger.warn(s"no source file: $path")
None
}
doc <- try {
val timeout = TimeUnit.SECONDS.toMillis(10)
val textDocument = if (path.endsWith(".java")) {
val input = Input.VirtualFile(path, text)
val textDocument = if (path.lastOpt.exists(_.endsWith(".java"))) {
val input = Input.VirtualFile(path.toString, text)
Mtags.index(input, dialect)
} else {
InteractiveSemanticdb.toTextDocument(
global,
text,
filename,
subPath.toString,
timeout,
List(
"-P:semanticdb:synthetics:on",
Expand All @@ -332,7 +339,7 @@ class MetabrowseServer(
Some(textDocument)
} catch {
case NonFatal(e) =>
logger.error(s"compile error: $filename", e)
logger.error(s"compile error: $subPath", e)
None
}
} yield TextDocuments(List(doc.withText(text)))
Expand Down

0 comments on commit 331b267

Please sign in to comment.