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

Properly sanitize Windows reserved names in evaluator paths #2964

Merged
merged 2 commits into from
Jan 22, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion main/eval/src/mill/eval/EvaluatorPaths.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ object EvaluatorPaths {
): EvaluatorPaths = {
val refinedSegments = foreignSegments.map(_ ++ segments).getOrElse(segments)
val segmentStrings = makeSegmentStrings(refinedSegments)
val targetPath = workspacePath / segmentStrings
val targetPath = workspacePath / segmentStrings.map(sanitizePathSegment)
EvaluatorPaths(
targetPath / os.up / s"${targetPath.last}.dest",
targetPath / os.up / s"${targetPath.last}.json",
Expand All @@ -39,4 +39,14 @@ object EvaluatorPaths {
workspacePath: os.Path,
task: NamedTask[_]
): EvaluatorPaths = resolveDestPaths(workspacePath, task.ctx.segments, task.ctx.foreign)

// case-insensitive match on reserved names
private val ReservedWinNames =
raw"^([cC][oO][nN]|[pP][rR][nN]|[aA][uU][xX]|[nN][uU][lL]|[cC][oO][mM][0-9¹²³]|[lL][pP][tT][0-9¹²³])($$|[.].*$$)".r
def sanitizePathSegment(segment: String): os.PathChunk = {
segment match {
case ReservedWinNames(keyword, rest) => s"${keyword}~${rest}"
case s => s
}
}
}
26 changes: 26 additions & 0 deletions main/eval/test/src/mill/eval/EvaluatorPathsTests.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package mill.eval

import utest._

object EvaluatorPathsTests extends TestSuite {

override def tests: Tests = Tests {
"sanitizedPathSegment" - {
"WindowsReservedNames" - {
val replace = Seq(
"com1.json" -> "com1~.json",
"LPT¹" -> "LPT¹~"
)
val noReplace = Seq(
"con10.json"
)
for {
(segment, result) <- replace ++ noReplace.map(s => (s, s))
} yield {
EvaluatorPaths.sanitizePathSegment(segment).toString ==> result
(segment, result)
}
}
}
}
}