Skip to content

3.x: Avoid using System.getProperties() due to security restrictions #6637

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

Merged
merged 1 commit into from
Aug 28, 2019
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
6 changes: 3 additions & 3 deletions src/main/java/io/reactivex/rxjava3/core/Flowable.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
* Reactive Streams implementations.
* <p>
* The Flowable hosts the default buffer size of 128 elements for operators, accessible via {@link #bufferSize()},
* that can be overridden globally via the system parameter {@code rx2.buffer-size}. Most operators, however, have
* that can be overridden globally via the system parameter {@code rx3.buffer-size}. Most operators, however, have
* overloads that allow setting their internal buffer size explicitly.
* <p>
* The documentation for this class makes use of marble diagrams. The following legend explains these diagrams:
Expand Down Expand Up @@ -153,7 +153,7 @@ public abstract class Flowable<T> implements Publisher<T> {
/** The default buffer size. */
static final int BUFFER_SIZE;
static {
BUFFER_SIZE = Math.max(1, Integer.getInteger("rx2.buffer-size", 128));
BUFFER_SIZE = Math.max(1, Integer.getInteger("rx3.buffer-size", 128));
}

/**
Expand Down Expand Up @@ -225,7 +225,7 @@ public static <T> Flowable<T> ambArray(Publisher<? extends T>... sources) {

/**
* Returns the default internal buffer size used by most async operators.
* <p>The value can be overridden via system parameter {@code rx2.buffer-size}
* <p>The value can be overridden via system parameter {@code rx3.buffer-size}
* <em>before</em> the Flowable class is loaded.
* @return the default internal buffer size.
*/
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/io/reactivex/rxjava3/core/Observable.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
* for such non-backpressured flows, which {@code Observable} itself implements as well.
* <p>
* The Observable's operators, by default, run with a buffer size of 128 elements (see {@link Flowable#bufferSize()}),
* that can be overridden globally via the system parameter {@code rx2.buffer-size}. Most operators, however, have
* that can be overridden globally via the system parameter {@code rx3.buffer-size}. Most operators, however, have
* overloads that allow setting their internal buffer size explicitly.
* <p>
* The documentation for this class makes use of marble diagrams. The following legend explains these diagrams:
Expand Down Expand Up @@ -160,7 +160,7 @@ public static <T> Observable<T> ambArray(ObservableSource<? extends T>... source
/**
* Returns the default 'island' size or capacity-increment hint for unbounded buffers.
* <p>Delegates to {@link Flowable#bufferSize} but is public for convenience.
* <p>The value can be overridden via system parameter {@code rx2.buffer-size}
* <p>The value can be overridden via system parameter {@code rx3.buffer-size}
* <em>before</em> the {@link Flowable} class is loaded.
* @return the default 'island' size or capacity-increment hint
*/
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/io/reactivex/rxjava3/core/Scheduler.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
* based on the relative time between it and {@link Worker#now(TimeUnit)}. However, drifts or changes in the
* system clock could affect this calculation either by scheduling subsequent runs too frequently or too far apart.
* Therefore, the default implementation uses the {@link #clockDriftTolerance()} value (set via
* {@code rx2.scheduler.drift-tolerance} in minutes) to detect a drift in {@link Worker#now(TimeUnit)} and
* {@code rx3.scheduler.drift-tolerance} in minutes) to detect a drift in {@link Worker#now(TimeUnit)} and
* re-adjust the absolute/relative time calculation accordingly.
* <p>
* The default implementations of {@link #start()} and {@link #shutdown()} do nothing and should be overridden if the
Expand All @@ -92,17 +92,17 @@ public abstract class Scheduler {
/**
* The tolerance for a clock drift in nanoseconds where the periodic scheduler will rebase.
* <p>
* The associated system parameter, {@code rx2.scheduler.drift-tolerance}, expects its value in minutes.
* The associated system parameter, {@code rx3.scheduler.drift-tolerance}, expects its value in minutes.
*/
static final long CLOCK_DRIFT_TOLERANCE_NANOSECONDS;
static {
CLOCK_DRIFT_TOLERANCE_NANOSECONDS = TimeUnit.MINUTES.toNanos(
Long.getLong("rx2.scheduler.drift-tolerance", 15));
Long.getLong("rx3.scheduler.drift-tolerance", 15));
}

/**
* Returns the clock drift tolerance in nanoseconds.
* <p>Related system property: {@code rx2.scheduler.drift-tolerance} in minutes.
* <p>Related system property: {@code rx3.scheduler.drift-tolerance} in minutes.
* @return the tolerance in nanoseconds
* @since 2.0
*/
Expand Down Expand Up @@ -345,7 +345,7 @@ public <S extends Scheduler & Disposable> S when(@NonNull Function<Flowable<Flow
* based on the relative time between it and {@link #now(TimeUnit)}. However, drifts or changes in the
* system clock would affect this calculation either by scheduling subsequent runs too frequently or too far apart.
* Therefore, the default implementation uses the {@link #clockDriftTolerance()} value (set via
* {@code rx2.scheduler.drift-tolerance} in minutes) to detect a drift in {@link #now(TimeUnit)} and
* {@code rx3.scheduler.drift-tolerance} in minutes) to detect a drift in {@link #now(TimeUnit)} and
* re-adjust the absolute/relative time calculation accordingly.
* <p>
* If the {@code Worker} is disposed, the {@code schedule} methods
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public final class ComputationScheduler extends Scheduler implements SchedulerMu
* Key to setting the maximum number of computation scheduler threads.
* Zero or less is interpreted as use available. Capped by available.
*/
static final String KEY_MAX_THREADS = "rx2.computation-threads";
static final String KEY_MAX_THREADS = "rx3.computation-threads";
/** The maximum number of computation scheduler threads. */
static final int MAX_THREADS;

Expand All @@ -47,7 +47,7 @@ public final class ComputationScheduler extends Scheduler implements SchedulerMu
final ThreadFactory threadFactory;
final AtomicReference<FixedSchedulerPool> pool;
/** The name of the system property for setting the thread priority for this Scheduler. */
private static final String KEY_COMPUTATION_PRIORITY = "rx2.computation-priority";
private static final String KEY_COMPUTATION_PRIORITY = "rx3.computation-priority";

static {
MAX_THREADS = cap(Runtime.getRuntime().availableProcessors(), Integer.getInteger(KEY_MAX_THREADS, 0));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public final class IoScheduler extends Scheduler {
static final RxThreadFactory EVICTOR_THREAD_FACTORY;

/** The name of the system property for setting the keep-alive time (in seconds) for this Scheduler workers. */
private static final String KEY_KEEP_ALIVE_TIME = "rx2.io-keep-alive-time";
private static final String KEY_KEEP_ALIVE_TIME = "rx3.io-keep-alive-time";
public static final long KEEP_ALIVE_TIME_DEFAULT = 60;

private static final long KEEP_ALIVE_TIME;
Expand All @@ -46,7 +46,7 @@ public final class IoScheduler extends Scheduler {
final AtomicReference<CachedWorkerPool> pool;

/** The name of the system property for setting the thread priority for this Scheduler. */
private static final String KEY_IO_PRIORITY = "rx2.io-priority";
private static final String KEY_IO_PRIORITY = "rx3.io-priority";

static final CachedWorkerPool NONE;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public final class NewThreadScheduler extends Scheduler {
private static final RxThreadFactory THREAD_FACTORY;

/** The name of the system property for setting the thread priority for this Scheduler. */
private static final String KEY_NEWTHREAD_PRIORITY = "rx2.newthread-priority";
private static final String KEY_NEWTHREAD_PRIORITY = "rx3.newthread-priority";

static {
int priority = Math.max(Thread.MIN_PRIORITY, Math.min(Thread.MAX_PRIORITY,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicReference;

import io.reactivex.rxjava3.functions.Function;

/**
* Manages the creating of ScheduledExecutorServices and sets up purging.
*/
Expand All @@ -29,14 +31,14 @@ private SchedulerPoolFactory() {
throw new IllegalStateException("No instances!");
}

static final String PURGE_ENABLED_KEY = "rx2.purge-enabled";
static final String PURGE_ENABLED_KEY = "rx3.purge-enabled";

/**
* Indicates the periodic purging of the ScheduledExecutorService is enabled.
*/
public static final boolean PURGE_ENABLED;

static final String PURGE_PERIOD_SECONDS_KEY = "rx2.purge-period-seconds";
static final String PURGE_PERIOD_SECONDS_KEY = "rx3.purge-period-seconds";

/**
* Indicates the purge period of the ScheduledExecutorServices created by create().
Expand Down Expand Up @@ -90,40 +92,48 @@ public static void shutdown() {
}

static {
Properties properties = System.getProperties();

PurgeProperties pp = new PurgeProperties();
pp.load(properties);

PURGE_ENABLED = pp.purgeEnable;
PURGE_PERIOD_SECONDS = pp.purgePeriod;
SystemPropertyAccessor propertyAccessor = new SystemPropertyAccessor();
PURGE_ENABLED = getBooleanProperty(true, PURGE_ENABLED_KEY, true, true, propertyAccessor);
PURGE_PERIOD_SECONDS = getIntProperty(PURGE_ENABLED, PURGE_PERIOD_SECONDS_KEY, 1, 1, propertyAccessor);

start();
}

static final class PurgeProperties {

boolean purgeEnable;

int purgePeriod;

void load(Properties properties) {
if (properties.containsKey(PURGE_ENABLED_KEY)) {
purgeEnable = Boolean.parseBoolean(properties.getProperty(PURGE_ENABLED_KEY));
} else {
purgeEnable = true;
static int getIntProperty(boolean enabled, String key, int defaultNotFound, int defaultNotEnabled, Function<String, String> propertyAccessor) {
if (enabled) {
try {
String value = propertyAccessor.apply(key);
if (value == null) {
return defaultNotFound;
}
return Integer.parseInt(value);
} catch (Throwable ex) {
return defaultNotFound;
}
}
return defaultNotEnabled;
}

if (purgeEnable && properties.containsKey(PURGE_PERIOD_SECONDS_KEY)) {
try {
purgePeriod = Integer.parseInt(properties.getProperty(PURGE_PERIOD_SECONDS_KEY));
} catch (NumberFormatException ex) {
purgePeriod = 1;
static boolean getBooleanProperty(boolean enabled, String key, boolean defaultNotFound, boolean defaultNotEnabled, Function<String, String> propertyAccessor) {
if (enabled) {
try {
String value = propertyAccessor.apply(key);
if (value == null) {
return defaultNotFound;
}
} else {
purgePeriod = 1;
return "true".equals(value);
} catch (Throwable ex) {
return defaultNotFound;
}
}
return defaultNotEnabled;
}

static final class SystemPropertyAccessor implements Function<String, String> {
@Override
public String apply(String t) throws Throwable {
return System.getProperty(t);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public final class SingleScheduler extends Scheduler {
final AtomicReference<ScheduledExecutorService> executor = new AtomicReference<ScheduledExecutorService>();

/** The name of the system property for setting the thread priority for this Scheduler. */
private static final String KEY_SINGLE_PRIORITY = "rx2.single-priority";
private static final String KEY_SINGLE_PRIORITY = "rx3.single-priority";

private static final String THREAD_NAME_PREFIX = "RxSingleScheduler";

Expand Down
28 changes: 14 additions & 14 deletions src/main/java/io/reactivex/rxjava3/schedulers/Schedulers.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@
* <p>
* <strong>Supported system properties ({@code System.getProperty()}):</strong>
* <ul>
* <li>{@code rx2.io-keep-alive-time} (long): sets the keep-alive time of the {@link #io()} Scheduler workers, default is {@link IoScheduler#KEEP_ALIVE_TIME_DEFAULT}</li>
* <li>{@code rx2.io-priority} (int): sets the thread priority of the {@link #io()} Scheduler, default is {@link Thread#NORM_PRIORITY}</li>
* <li>{@code rx2.computation-threads} (int): sets the number of threads in the {@link #computation()} Scheduler, default is the number of available CPUs</li>
* <li>{@code rx2.computation-priority} (int): sets the thread priority of the {@link #computation()} Scheduler, default is {@link Thread#NORM_PRIORITY}</li>
* <li>{@code rx2.newthread-priority} (int): sets the thread priority of the {@link #newThread()} Scheduler, default is {@link Thread#NORM_PRIORITY}</li>
* <li>{@code rx2.single-priority} (int): sets the thread priority of the {@link #single()} Scheduler, default is {@link Thread#NORM_PRIORITY}</li>
* <li>{@code rx2.purge-enabled} (boolean): enables periodic purging of all Scheduler's backing thread pools, default is false</li>
* <li>{@code rx2.purge-period-seconds} (int): specifies the periodic purge interval of all Scheduler's backing thread pools, default is 1 second</li>
* <li>{@code rx3.io-keep-alive-time} (long): sets the keep-alive time of the {@link #io()} Scheduler workers, default is {@link IoScheduler#KEEP_ALIVE_TIME_DEFAULT}</li>
* <li>{@code rx3.io-priority} (int): sets the thread priority of the {@link #io()} Scheduler, default is {@link Thread#NORM_PRIORITY}</li>
* <li>{@code rx3.computation-threads} (int): sets the number of threads in the {@link #computation()} Scheduler, default is the number of available CPUs</li>
* <li>{@code rx3.computation-priority} (int): sets the thread priority of the {@link #computation()} Scheduler, default is {@link Thread#NORM_PRIORITY}</li>
* <li>{@code rx3.newthread-priority} (int): sets the thread priority of the {@link #newThread()} Scheduler, default is {@link Thread#NORM_PRIORITY}</li>
* <li>{@code rx3.single-priority} (int): sets the thread priority of the {@link #single()} Scheduler, default is {@link Thread#NORM_PRIORITY}</li>
* <li>{@code rx3.purge-enabled} (boolean): enables periodic purging of all Scheduler's backing thread pools, default is false</li>
* <li>{@code rx3.purge-period-seconds} (int): specifies the periodic purge interval of all Scheduler's backing thread pools, default is 1 second</li>
* </ul>
*/
public final class Schedulers {
Expand Down Expand Up @@ -112,8 +112,8 @@ private Schedulers() {
* before the {@link Schedulers} class is referenced in your code.
* <p><strong>Supported system properties ({@code System.getProperty()}):</strong>
* <ul>
* <li>{@code rx2.computation-threads} (int): sets the number of threads in the {@link #computation()} Scheduler, default is the number of available CPUs</li>
* <li>{@code rx2.computation-priority} (int): sets the thread priority of the {@link #computation()} Scheduler, default is {@link Thread#NORM_PRIORITY}</li>
* <li>{@code rx3.computation-threads} (int): sets the number of threads in the {@link #computation()} Scheduler, default is the number of available CPUs</li>
* <li>{@code rx3.computation-priority} (int): sets the thread priority of the {@link #computation()} Scheduler, default is {@link Thread#NORM_PRIORITY}</li>
* </ul>
* <p>
* The default value of this scheduler can be overridden at initialization time via the
Expand Down Expand Up @@ -157,8 +157,8 @@ public static Scheduler computation() {
* before the {@link Schedulers} class is referenced in your code.
* <p><strong>Supported system properties ({@code System.getProperty()}):</strong>
* <ul>
* <li>{@code rx2.io-keep-alive-time} (long): sets the keep-alive time of the {@link #io()} Scheduler workers, default is {@link IoScheduler#KEEP_ALIVE_TIME_DEFAULT}</li>
* <li>{@code rx2.io-priority} (int): sets the thread priority of the {@link #io()} Scheduler, default is {@link Thread#NORM_PRIORITY}</li>
* <li>{@code rx3.io-keep-alive-time} (long): sets the keep-alive time of the {@link #io()} Scheduler workers, default is {@link IoScheduler#KEEP_ALIVE_TIME_DEFAULT}</li>
* <li>{@code rx3.io-priority} (int): sets the thread priority of the {@link #io()} Scheduler, default is {@link Thread#NORM_PRIORITY}</li>
* </ul>
* <p>
* The default value of this scheduler can be overridden at initialization time via the
Expand Down Expand Up @@ -216,7 +216,7 @@ public static Scheduler trampoline() {
* before the {@link Schedulers} class is referenced in your code.
* <p><strong>Supported system properties ({@code System.getProperty()}):</strong>
* <ul>
* <li>{@code rx2.newthread-priority} (int): sets the thread priority of the {@link #newThread()} Scheduler, default is {@link Thread#NORM_PRIORITY}</li>
* <li>{@code rx3.newthread-priority} (int): sets the thread priority of the {@link #newThread()} Scheduler, default is {@link Thread#NORM_PRIORITY}</li>
* </ul>
* <p>
* The default value of this scheduler can be overridden at initialization time via the
Expand Down Expand Up @@ -265,7 +265,7 @@ public static Scheduler newThread() {
* before the {@link Schedulers} class is referenced in your code.
* <p><strong>Supported system properties ({@code System.getProperty()}):</strong>
* <ul>
* <li>{@code rx2.single-priority} (int): sets the thread priority of the {@link #single()} Scheduler, default is {@link Thread#NORM_PRIORITY}</li>
* <li>{@code rx3.single-priority} (int): sets the thread priority of the {@link #single()} Scheduler, default is {@link Thread#NORM_PRIORITY}</li>
* </ul>
* <p>
* The default value of this scheduler can be overridden at initialization time via the
Expand Down
Loading