Skip to content

Commit ed319ec

Browse files
authored
Merge 3c67514 into f79c9c1
2 parents f79c9c1 + 3c67514 commit ed319ec

File tree

12 files changed

+503
-23
lines changed

12 files changed

+503
-23
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### Fixes
66

77
- Deprecate `enableTracing` option ([#3777](https://github.com/getsentry/sentry-java/pull/3777))
8+
- Vendor `java.util.Random` and replace `java.security.SecureRandom` usages ([#3783](https://github.com/getsentry/sentry-java/pull/3783))
89

910
## 7.15.0
1011

sentry-android-core/src/main/java/io/sentry/android/core/AnrV2EventProcessor.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@
5454
import io.sentry.protocol.SentryTransaction;
5555
import io.sentry.protocol.User;
5656
import io.sentry.util.HintUtils;
57+
import io.sentry.util.Random;
5758
import java.io.File;
58-
import java.security.SecureRandom;
5959
import java.util.ArrayList;
6060
import java.util.Arrays;
6161
import java.util.Collections;
@@ -83,7 +83,7 @@ public final class AnrV2EventProcessor implements BackfillingEventProcessor {
8383

8484
private final @NotNull SentryExceptionFactory sentryExceptionFactory;
8585

86-
private final @Nullable SecureRandom random;
86+
private final @Nullable Random random;
8787

8888
public AnrV2EventProcessor(
8989
final @NotNull Context context,
@@ -96,7 +96,7 @@ public AnrV2EventProcessor(
9696
final @NotNull Context context,
9797
final @NotNull SentryAndroidOptions options,
9898
final @NotNull BuildInfoProvider buildInfoProvider,
99-
final @Nullable SecureRandom random) {
99+
final @Nullable Random random) {
100100
this.context = ContextUtils.getApplicationContext(context);
101101
this.options = options;
102102
this.buildInfoProvider = buildInfoProvider;
@@ -180,7 +180,7 @@ private boolean sampleReplay(final @NotNull SentryEvent event) {
180180

181181
try {
182182
// we have to sample here with the old sample rate, because it may change between app launches
183-
final @NotNull SecureRandom random = this.random != null ? this.random : new SecureRandom();
183+
final @NotNull Random random = this.random != null ? this.random : new Random();
184184
final double replayErrorSampleRateDouble = Double.parseDouble(replayErrorSampleRate);
185185
if (replayErrorSampleRateDouble < random.nextDouble()) {
186186
options

sentry-android-replay/src/main/java/io/sentry/android/replay/ReplayIntegration.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ import io.sentry.transport.ICurrentDateProvider
3535
import io.sentry.util.FileUtils
3636
import io.sentry.util.HintUtils
3737
import io.sentry.util.IntegrationUtils.addIntegrationToSdkVersion
38+
import io.sentry.util.Random
3839
import java.io.Closeable
3940
import java.io.File
40-
import java.security.SecureRandom
4141
import java.util.LinkedList
4242
import java.util.concurrent.atomic.AtomicBoolean
4343
import kotlin.LazyThreadSafetyMode.NONE
@@ -78,7 +78,7 @@ public class ReplayIntegration(
7878
private var hub: IHub? = null
7979
private var recorder: Recorder? = null
8080
private var gestureRecorder: GestureRecorder? = null
81-
private val random by lazy { SecureRandom() }
81+
private val random by lazy { Random() }
8282
private val rootViewsSpy by lazy(NONE) { RootViewsSpy.install() }
8383

8484
// TODO: probably not everything has to be thread-safe here

sentry-android-replay/src/main/java/io/sentry/android/replay/capture/BufferCaptureStrategy.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@ import io.sentry.android.replay.util.submitSafely
1818
import io.sentry.protocol.SentryId
1919
import io.sentry.transport.ICurrentDateProvider
2020
import io.sentry.util.FileUtils
21+
import io.sentry.util.Random
2122
import java.io.File
22-
import java.security.SecureRandom
2323
import java.util.Date
2424
import java.util.concurrent.ScheduledExecutorService
2525

2626
internal class BufferCaptureStrategy(
2727
private val options: SentryOptions,
2828
private val hub: IHub?,
2929
private val dateProvider: ICurrentDateProvider,
30-
private val random: SecureRandom,
30+
private val random: Random,
3131
executor: ScheduledExecutorService? = null,
3232
replayCacheProvider: ((replayId: SentryId, recorderConfig: ScreenshotRecorderConfig) -> ReplayCache)? = null
3333
) : BaseCaptureStrategy(options, hub, dateProvider, executor = executor, replayCacheProvider = replayCacheProvider) {

sentry-android-replay/src/main/java/io/sentry/android/replay/util/Sampling.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package io.sentry.android.replay.util
22

3-
import java.security.SecureRandom
3+
import io.sentry.util.Random
44

5-
internal fun SecureRandom.sample(rate: Double?): Boolean {
5+
internal fun Random.sample(rate: Double?): Boolean {
66
if (rate != null) {
77
return !(rate < this.nextDouble()) // bad luck
88
}

sentry-android-replay/src/test/java/io/sentry/android/replay/capture/BufferCaptureStrategyTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import io.sentry.android.replay.capture.BufferCaptureStrategyTest.Fixture.Compan
2020
import io.sentry.protocol.SentryId
2121
import io.sentry.transport.CurrentDateProvider
2222
import io.sentry.transport.ICurrentDateProvider
23+
import io.sentry.util.Random
2324
import org.awaitility.kotlin.await
2425
import org.junit.Rule
2526
import org.junit.rules.TemporaryFolder
@@ -35,7 +36,6 @@ import org.mockito.kotlin.times
3536
import org.mockito.kotlin.verify
3637
import org.mockito.kotlin.whenever
3738
import java.io.File
38-
import java.security.SecureRandom
3939
import kotlin.test.Test
4040
import kotlin.test.assertEquals
4141
import kotlin.test.assertFalse
@@ -97,7 +97,7 @@ class BufferCaptureStrategyTest {
9797
options,
9898
hub,
9999
dateProvider,
100-
SecureRandom(),
100+
Random(),
101101
mock {
102102
doAnswer { invocation ->
103103
(invocation.arguments[0] as Runnable).run()

sentry/api/sentry.api

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5808,6 +5808,19 @@ public final class io/sentry/util/PropagationTargetsUtils {
58085808
public static fun contain (Ljava/util/List;Ljava/net/URI;)Z
58095809
}
58105810

5811+
public final class io/sentry/util/Random : java/io/Serializable {
5812+
public fun <init> ()V
5813+
public fun <init> (J)V
5814+
public fun nextBoolean ()Z
5815+
public fun nextBytes ([B)V
5816+
public fun nextDouble ()D
5817+
public fun nextFloat ()F
5818+
public fun nextInt ()I
5819+
public fun nextInt (I)I
5820+
public fun nextLong ()J
5821+
public fun setSeed (J)V
5822+
}
5823+
58115824
public final class io/sentry/util/SampleRateUtils {
58125825
public fun <init> ()V
58135826
public static fun isValidProfilesSampleRate (Ljava/lang/Double;)Z

sentry/src/main/java/io/sentry/SentryClient.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
import io.sentry.util.CheckInUtils;
1818
import io.sentry.util.HintUtils;
1919
import io.sentry.util.Objects;
20+
import io.sentry.util.Random;
2021
import io.sentry.util.TracingUtils;
2122
import java.io.Closeable;
2223
import java.io.IOException;
23-
import java.security.SecureRandom;
2424
import java.util.ArrayList;
2525
import java.util.Collection;
2626
import java.util.Collections;
@@ -40,7 +40,7 @@ public final class SentryClient implements ISentryClient, IMetricsClient {
4040

4141
private final @NotNull SentryOptions options;
4242
private final @NotNull ITransport transport;
43-
private final @Nullable SecureRandom random;
43+
private final @Nullable Random random;
4444
private final @NotNull SortBreadcrumbsByDate sortBreadcrumbsByDate = new SortBreadcrumbsByDate();
4545
private final @NotNull IMetricsAggregator metricsAggregator;
4646

@@ -67,7 +67,7 @@ public boolean isEnabled() {
6767
? new MetricsAggregator(options, this)
6868
: NoopMetricsAggregator.getInstance();
6969

70-
this.random = options.getSampleRate() == null ? null : new SecureRandom();
70+
this.random = options.getSampleRate() == null ? null : new Random();
7171
}
7272

7373
private boolean shouldApplyScopeData(

sentry/src/main/java/io/sentry/TracesSampler.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package io.sentry;
22

33
import io.sentry.util.Objects;
4-
import java.security.SecureRandom;
4+
import io.sentry.util.Random;
55
import org.jetbrains.annotations.NotNull;
66
import org.jetbrains.annotations.Nullable;
77
import org.jetbrains.annotations.TestOnly;
@@ -10,14 +10,14 @@ final class TracesSampler {
1010
private static final @NotNull Double DEFAULT_TRACES_SAMPLE_RATE = 1.0;
1111

1212
private final @NotNull SentryOptions options;
13-
private final @NotNull SecureRandom random;
13+
private final @NotNull Random random;
1414

1515
public TracesSampler(final @NotNull SentryOptions options) {
16-
this(Objects.requireNonNull(options, "options are required"), new SecureRandom());
16+
this(Objects.requireNonNull(options, "options are required"), new Random());
1717
}
1818

1919
@TestOnly
20-
TracesSampler(final @NotNull SentryOptions options, final @NotNull SecureRandom random) {
20+
TracesSampler(final @NotNull SentryOptions options, final @NotNull Random random) {
2121
this.options = options;
2222
this.random = random;
2323
}

sentry/src/main/java/io/sentry/metrics/MetricsHelper.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package io.sentry.metrics;
22

33
import io.sentry.MeasurementUnit;
4-
import java.security.SecureRandom;
4+
import io.sentry.util.Random;
55
import java.util.Collection;
66
import java.util.Collections;
77
import java.util.HashMap;
@@ -27,7 +27,7 @@ public final class MetricsHelper {
2727
private static final char TAGS_ESCAPE_CHAR = '\\';
2828

2929
private static long FLUSH_SHIFT_MS =
30-
(long) (new SecureRandom().nextFloat() * (ROLLUP_IN_SECONDS * 1000f));
30+
(long) (new Random().nextFloat() * (ROLLUP_IN_SECONDS * 1000f));
3131

3232
public static long getTimeBucketKey(final long timestampMs) {
3333
final long seconds = timestampMs / 1000;

0 commit comments

Comments
 (0)