Skip to content

Commit

Permalink
Forbid / and \ in org and module name
Browse files Browse the repository at this point in the history
  • Loading branch information
alexarchambault committed Nov 12, 2024
1 parent c9ff1f5 commit c147e27
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 4 deletions.
9 changes: 9 additions & 0 deletions dependency/shared/src/main/scala/dependency/ModuleLike.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ final case class ModuleLike[+A <: NameAttributes](
attributes: Map[String, String]
) {

ModuleLike.validateValue(organization, "organization")
ModuleLike.validateValue(name, "module name")

def applyParams(params: ScalaParameters): Module =
copy(
name = name + nameAttributes.suffix(params),
Expand All @@ -22,3 +25,9 @@ final case class ModuleLike[+A <: NameAttributes](
override def toString: String =
render
}

object ModuleLike {
private def validateValue(value: String, name: String): Unit =
if (value.contains("/")) throw new IllegalArgumentException(s"$name $value contains invalid '/'")
else if (value.contains("\\")) throw new IllegalArgumentException(s"$name $value contains invalid '\\'")
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ object ModuleParser {
}

for {
(org, name, nameAttributes) <- values
(name, attributes) <- parseNamePart(name)
(org, name0, nameAttributes) <- values
_ <- validateValue(org, "organization")
(name, attributes) <- parseNamePart(name0)
_ <- validateValue(name, "module name")
} yield ModuleLike(org, name, nameAttributes, attributes)
}

Expand All @@ -50,8 +52,10 @@ object ModuleParser {
}

for {
(org, name, nameAttributes, rest) <- values
(name, attributes) <- parseNamePart(name)
(org, name0, nameAttributes, rest) <- values
_ <- validateValue(org, "organization")
(name, attributes) <- parseNamePart(name0)
_ <- validateValue(name, "module name")
} yield (ModuleLike(org, name, nameAttributes, attributes), rest.map(_.getOrElse("")).mkString(":"))
}

Expand All @@ -71,4 +75,9 @@ object ModuleParser {
Right((name, attributes.toMap))
}
}

private def validateValue(value: String, name: String): Either[String, Unit] =
if (value.contains("/")) Left(s"$name $value contains invalid '/'")
else if (value.contains("\\")) Left(s"$name $value contains invalid '\\'")
else Right(())
}
49 changes: 49 additions & 0 deletions dependency/shared/src/test/scala/dependency/DependencyTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,53 @@ class DependencyTests extends munit.FunSuite {
)
expect(res == expected)
}

test("refuse to instantiate a dependency with slash in org") {
val createdModule =
try {
Dependency("or/g", "name", "1.2")
true
}
catch {
case _: IllegalArgumentException =>
false
}
assert(!createdModule)
}
test("refuse to instantiate a dependency with backslash in org") {
val createdModule =
try {
Dependency("org\\", "name", "1.2")
true
}
catch {
case _: IllegalArgumentException =>
false
}
assert(!createdModule)
}
test("refuse to instantiate a dependency with slash in name") {
val createdModule =
try {
Dependency("org", "n/ame", "1.2")
true
}
catch {
case _: IllegalArgumentException =>
false
}
assert(!createdModule)
}
test("refuse to instantiate a dependency with backslash in name") {
val createdModule =
try {
Dependency("org", "nam\\e", "1.2")
true
}
catch {
case _: IllegalArgumentException =>
false
}
assert(!createdModule)
}
}
49 changes: 49 additions & 0 deletions dependency/shared/src/test/scala/dependency/ModuleTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,53 @@ class ModuleTests extends munit.FunSuite {
expect(mod == expected)
}

test("refuse to instantiate a module with slash in org") {
val createdModule =
try {
Module("or/g", "name")
true
}
catch {
case _: IllegalArgumentException =>
false
}
assert(!createdModule)
}
test("refuse to instantiate a module with backslash in org") {
val createdModule =
try {
Module("org\\", "name")
true
}
catch {
case _: IllegalArgumentException =>
false
}
assert(!createdModule)
}
test("refuse to instantiate a module with slash in name") {
val createdModule =
try {
Module("org", "n/ame")
true
}
catch {
case _: IllegalArgumentException =>
false
}
assert(!createdModule)
}
test("refuse to instantiate a module with backslash in name") {
val createdModule =
try {
Module("org", "nam\\e")
true
}
catch {
case _: IllegalArgumentException =>
false
}
assert(!createdModule)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,21 @@ class DependencyParserTests extends munit.FunSuite {
)
expect(res == expected)
}

test("reject slash in org") {
val res = DependencyParser.parse("o/rg::name:1.2")
expect(res.isLeft)
}
test("reject backslash in org") {
val res = DependencyParser.parse("o\\rg::name:1.2")
expect(res.isLeft)
}
test("reject slash in name") {
val res = DependencyParser.parse("org::/name:1.2")
expect(res.isLeft)
}
test("reject backslash in name") {
val res = DependencyParser.parse("o\\rg::\\name:1.2")
expect(res.isLeft)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,21 @@ class ModuleParserTests extends munit.FunSuite {
expect(res.isLeft)
}

test("reject slash in org") {
val res = ModuleParser.parse("o/rg::name")
expect(res.isLeft)
}
test("reject backslash in org") {
val res = ModuleParser.parse("o\\rg::name")
expect(res.isLeft)
}
test("reject slash in name") {
val res = ModuleParser.parse("org::/name")
expect(res.isLeft)
}
test("reject backslash in name") {
val res = ModuleParser.parse("o\\rg::\\name")
expect(res.isLeft)
}

}

0 comments on commit c147e27

Please sign in to comment.