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

Forbid / and \ in org and module name #80

Merged
merged 1 commit into from
Nov 12, 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
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ final case class DependencyLike[+A <: NameAttributes, +E <: NameAttributes](
exclude: CovariantSet[ModuleLike[E]],
userParams: Seq[(String, Option[String])]
) {

ModuleLike.validateValue(version, "version")

def applyParams(params: ScalaParameters): Dependency =
DependencyLike(
module.applyParams(params),
Expand Down
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[dependency] 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 @@ -38,6 +38,7 @@ object DependencyParser {
}

for {
_ <- ModuleParser.validateValue(version, "version")
exclusions <- maybeExclusions
} yield {
val userParams = remainingParams.iterator.map(parseParam).toSeq ++ configOpt.toSeq.map("$inlineConfiguration" -> Some(_))
Expand Down
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[dependency] 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(())
}
73 changes: 73 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,77 @@ 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)
}
test("refuse to instantiate a dependency with slash in version") {
val createdModule =
try {
Dependency("org", "name", "1./2")
true
}
catch {
case _: IllegalArgumentException =>
false
}
assert(!createdModule)
}
test("refuse to instantiate a dependency with backslash in version") {
val createdModule =
try {
Dependency("org", "name", "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,29 @@ 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("org::\\name:1.2")
expect(res.isLeft)
}
test("reject slash in version") {
val res = DependencyParser.parse("org::name:1./2")
expect(res.isLeft)
}
test("reject backslash in version") {
val res = DependencyParser.parse("org::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)
}

}