-
Notifications
You must be signed in to change notification settings - Fork 536
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2945 from armanbilge/topic/CVE-2022-28355
Securely implement `UUIDGen` for Scala.js
- Loading branch information
Showing
5 changed files
with
206 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
std/js/src/main/scala/cats/effect/std/UUIDGenCompanionPlatform.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Copyright 2020-2022 Typelevel | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
/* | ||
* Scala.js (https://www.scala-js.org/) | ||
* | ||
* Copyright EPFL. | ||
* | ||
* Licensed under Apache License 2.0 | ||
* (https://www.apache.org/licenses/LICENSE-2.0). | ||
* | ||
* See the NOTICE file distributed with this work for | ||
* additional information regarding copyright ownership. | ||
*/ | ||
|
||
package cats.effect.std | ||
|
||
import cats.effect.kernel.Sync | ||
|
||
import java.util.UUID | ||
|
||
private[std] trait UUIDGenCompanionPlatform { | ||
implicit def fromSync[F[_]](implicit ev: Sync[F]): UUIDGen[F] = new UUIDGen[F] { | ||
private val csprng = new JavaSecureRandom() | ||
private val randomUUIDBuffer = new Array[Byte](16) | ||
override final val randomUUID: F[UUID] = | ||
ev.delay { | ||
val buffer = randomUUIDBuffer // local copy | ||
|
||
/* We use nextBytes() because that is the primitive of most secure RNGs, | ||
* and therefore it allows to perform a unique call to the underlying | ||
* secure RNG. | ||
*/ | ||
csprng.nextBytes(randomUUIDBuffer) | ||
|
||
@inline def intFromBuffer(i: Int): Int = | ||
(buffer(i) << 24) | ((buffer(i + 1) & 0xff) << 16) | ((buffer( | ||
i + 2) & 0xff) << 8) | (buffer(i + 3) & 0xff) | ||
|
||
val i1 = intFromBuffer(0) | ||
val i2 = (intFromBuffer(4) & ~0x0000f000) | 0x00004000 | ||
val i3 = (intFromBuffer(8) & ~0xc0000000) | 0x80000000 | ||
val i4 = intFromBuffer(12) | ||
val msb = (i1.toLong << 32) | (i2.toLong & 0xffffffffL) | ||
val lsb = (i3.toLong << 32) | (i4.toLong & 0xffffffffL) | ||
new UUID(msb, lsb) | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
std/jvm/src/main/scala/cats/effect/std/UUIDGenCompanionPlatform.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Copyright 2020-2022 Typelevel | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package cats.effect.std | ||
|
||
import cats.effect.kernel.Sync | ||
|
||
import java.util.UUID | ||
|
||
private[std] trait UUIDGenCompanionPlatform { | ||
implicit def fromSync[F[_]](implicit ev: Sync[F]): UUIDGen[F] = new UUIDGen[F] { | ||
override final val randomUUID: F[UUID] = | ||
ev.blocking(UUID.randomUUID()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
tests/shared/src/test/scala/cats/effect/std/UUIDGenSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright 2020-2022 Typelevel | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package cats.effect | ||
package std | ||
|
||
class UUIDGenSpec extends BaseSpec { | ||
|
||
"UUIDGen" should { | ||
"securely generate UUIDs" in real { | ||
for { | ||
left <- UUIDGen.randomUUID[IO] | ||
right <- UUIDGen.randomUUID[IO] | ||
} yield left != right | ||
} | ||
"use the correct variant and version" in real { | ||
for { | ||
uuid <- UUIDGen.randomUUID[IO] | ||
} yield (uuid.variant should be_==(2)) and (uuid.version should be_==(4)) | ||
} | ||
} | ||
|
||
} |