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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

change java.util.Random to java.security.SecureRandom #1831

Merged
merged 4 commits into from Dec 9, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -3,6 +3,7 @@
## Unreleased

* Ref: Rename Fragment span operation from `ui.fragment.load` to `ui.load` (#1824)
* Ref: change `java.util.Random` to `java.security.SecureRandom` for possible security reasons (#1831)

## 5.4.3

Expand Down
6 changes: 3 additions & 3 deletions sentry/src/main/java/io/sentry/SentryClient.java
Expand Up @@ -9,14 +9,14 @@
import io.sentry.util.Objects;
import java.io.Closeable;
import java.io.IOException;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand All @@ -29,7 +29,7 @@ public final class SentryClient implements ISentryClient {

private final @NotNull SentryOptions options;
private final @NotNull ITransport transport;
private final @Nullable Random random;
private final @Nullable SecureRandom random;

private final @NotNull SortBreadcrumbsByDate sortBreadcrumbsByDate = new SortBreadcrumbsByDate();

Expand All @@ -51,7 +51,7 @@ public boolean isEnabled() {
final RequestDetailsResolver requestDetailsResolver = new RequestDetailsResolver(options);
transport = transportFactory.create(options, requestDetailsResolver.resolve());

this.random = options.getSampleRate() == null ? null : new Random();
this.random = options.getSampleRate() == null ? null : new SecureRandom();
}

private boolean shouldApplyScopeData(
Expand Down
8 changes: 4 additions & 4 deletions sentry/src/main/java/io/sentry/TracesSampler.java
@@ -1,20 +1,20 @@
package io.sentry;

import io.sentry.util.Objects;
import java.util.Random;
import java.security.SecureRandom;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.TestOnly;

final class TracesSampler {
private final @NotNull SentryOptions options;
private final @NotNull Random random;
private final @NotNull SecureRandom random;

public TracesSampler(final @NotNull SentryOptions options) {
this(Objects.requireNonNull(options, "options are required"), new Random());
this(Objects.requireNonNull(options, "options are required"), new SecureRandom());
}

@TestOnly
TracesSampler(final @NotNull SentryOptions options, final @NotNull Random random) {
TracesSampler(final @NotNull SentryOptions options, final @NotNull SecureRandom random) {
this.options = options;
this.random = random;
}
Expand Down
4 changes: 2 additions & 2 deletions sentry/src/test/java/io/sentry/TracesSamplerTest.kt
Expand Up @@ -2,15 +2,15 @@ package io.sentry

import com.nhaarman.mockitokotlin2.mock
import com.nhaarman.mockitokotlin2.whenever
import java.util.Random
import java.security.SecureRandom
import kotlin.test.Test
import kotlin.test.assertFalse
import kotlin.test.assertTrue

class TracesSamplerTest {
class Fixture {
internal fun getSut(randomResult: Double? = null, tracesSampleRate: Double? = null, tracesSamplerResult: Double? = Double.MIN_VALUE): TracesSampler {
val random = mock<Random>()
val random = mock<SecureRandom>()
if (randomResult != null) {
whenever(random.nextDouble()).thenReturn(randomResult)
}
Expand Down