Skip to content

Commit 54d9279

Browse files
authored
3.x: Avoid using System.getProperties() due to security restrictions (#6637)
1 parent faf6392 commit 54d9279

File tree

10 files changed

+114
-92
lines changed

10 files changed

+114
-92
lines changed

src/main/java/io/reactivex/rxjava3/core/Flowable.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
* Reactive Streams implementations.
4545
* <p>
4646
* The Flowable hosts the default buffer size of 128 elements for operators, accessible via {@link #bufferSize()},
47-
* that can be overridden globally via the system parameter {@code rx2.buffer-size}. Most operators, however, have
47+
* that can be overridden globally via the system parameter {@code rx3.buffer-size}. Most operators, however, have
4848
* overloads that allow setting their internal buffer size explicitly.
4949
* <p>
5050
* The documentation for this class makes use of marble diagrams. The following legend explains these diagrams:
@@ -153,7 +153,7 @@ public abstract class Flowable<T> implements Publisher<T> {
153153
/** The default buffer size. */
154154
static final int BUFFER_SIZE;
155155
static {
156-
BUFFER_SIZE = Math.max(1, Integer.getInteger("rx2.buffer-size", 128));
156+
BUFFER_SIZE = Math.max(1, Integer.getInteger("rx3.buffer-size", 128));
157157
}
158158

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

226226
/**
227227
* Returns the default internal buffer size used by most async operators.
228-
* <p>The value can be overridden via system parameter {@code rx2.buffer-size}
228+
* <p>The value can be overridden via system parameter {@code rx3.buffer-size}
229229
* <em>before</em> the Flowable class is loaded.
230230
* @return the default internal buffer size.
231231
*/

src/main/java/io/reactivex/rxjava3/core/Observable.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
* for such non-backpressured flows, which {@code Observable} itself implements as well.
4444
* <p>
4545
* The Observable's operators, by default, run with a buffer size of 128 elements (see {@link Flowable#bufferSize()}),
46-
* that can be overridden globally via the system parameter {@code rx2.buffer-size}. Most operators, however, have
46+
* that can be overridden globally via the system parameter {@code rx3.buffer-size}. Most operators, however, have
4747
* overloads that allow setting their internal buffer size explicitly.
4848
* <p>
4949
* The documentation for this class makes use of marble diagrams. The following legend explains these diagrams:
@@ -160,7 +160,7 @@ public static <T> Observable<T> ambArray(ObservableSource<? extends T>... source
160160
/**
161161
* Returns the default 'island' size or capacity-increment hint for unbounded buffers.
162162
* <p>Delegates to {@link Flowable#bufferSize} but is public for convenience.
163-
* <p>The value can be overridden via system parameter {@code rx2.buffer-size}
163+
* <p>The value can be overridden via system parameter {@code rx3.buffer-size}
164164
* <em>before</em> the {@link Flowable} class is loaded.
165165
* @return the default 'island' size or capacity-increment hint
166166
*/

src/main/java/io/reactivex/rxjava3/core/Scheduler.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
* based on the relative time between it and {@link Worker#now(TimeUnit)}. However, drifts or changes in the
7575
* system clock could affect this calculation either by scheduling subsequent runs too frequently or too far apart.
7676
* Therefore, the default implementation uses the {@link #clockDriftTolerance()} value (set via
77-
* {@code rx2.scheduler.drift-tolerance} in minutes) to detect a drift in {@link Worker#now(TimeUnit)} and
77+
* {@code rx3.scheduler.drift-tolerance} in minutes) to detect a drift in {@link Worker#now(TimeUnit)} and
7878
* re-adjust the absolute/relative time calculation accordingly.
7979
* <p>
8080
* The default implementations of {@link #start()} and {@link #shutdown()} do nothing and should be overridden if the
@@ -92,17 +92,17 @@ public abstract class Scheduler {
9292
/**
9393
* The tolerance for a clock drift in nanoseconds where the periodic scheduler will rebase.
9494
* <p>
95-
* The associated system parameter, {@code rx2.scheduler.drift-tolerance}, expects its value in minutes.
95+
* The associated system parameter, {@code rx3.scheduler.drift-tolerance}, expects its value in minutes.
9696
*/
9797
static final long CLOCK_DRIFT_TOLERANCE_NANOSECONDS;
9898
static {
9999
CLOCK_DRIFT_TOLERANCE_NANOSECONDS = TimeUnit.MINUTES.toNanos(
100-
Long.getLong("rx2.scheduler.drift-tolerance", 15));
100+
Long.getLong("rx3.scheduler.drift-tolerance", 15));
101101
}
102102

103103
/**
104104
* Returns the clock drift tolerance in nanoseconds.
105-
* <p>Related system property: {@code rx2.scheduler.drift-tolerance} in minutes.
105+
* <p>Related system property: {@code rx3.scheduler.drift-tolerance} in minutes.
106106
* @return the tolerance in nanoseconds
107107
* @since 2.0
108108
*/
@@ -345,7 +345,7 @@ public <S extends Scheduler & Disposable> S when(@NonNull Function<Flowable<Flow
345345
* based on the relative time between it and {@link #now(TimeUnit)}. However, drifts or changes in the
346346
* system clock would affect this calculation either by scheduling subsequent runs too frequently or too far apart.
347347
* Therefore, the default implementation uses the {@link #clockDriftTolerance()} value (set via
348-
* {@code rx2.scheduler.drift-tolerance} in minutes) to detect a drift in {@link #now(TimeUnit)} and
348+
* {@code rx3.scheduler.drift-tolerance} in minutes) to detect a drift in {@link #now(TimeUnit)} and
349349
* re-adjust the absolute/relative time calculation accordingly.
350350
* <p>
351351
* If the {@code Worker} is disposed, the {@code schedule} methods

src/main/java/io/reactivex/rxjava3/internal/schedulers/ComputationScheduler.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public final class ComputationScheduler extends Scheduler implements SchedulerMu
3838
* Key to setting the maximum number of computation scheduler threads.
3939
* Zero or less is interpreted as use available. Capped by available.
4040
*/
41-
static final String KEY_MAX_THREADS = "rx2.computation-threads";
41+
static final String KEY_MAX_THREADS = "rx3.computation-threads";
4242
/** The maximum number of computation scheduler threads. */
4343
static final int MAX_THREADS;
4444

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

5252
static {
5353
MAX_THREADS = cap(Runtime.getRuntime().availableProcessors(), Integer.getInteger(KEY_MAX_THREADS, 0));

src/main/java/io/reactivex/rxjava3/internal/schedulers/IoScheduler.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public final class IoScheduler extends Scheduler {
3535
static final RxThreadFactory EVICTOR_THREAD_FACTORY;
3636

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

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

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

5151
static final CachedWorkerPool NONE;
5252

src/main/java/io/reactivex/rxjava3/internal/schedulers/NewThreadScheduler.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public final class NewThreadScheduler extends Scheduler {
3232
private static final RxThreadFactory THREAD_FACTORY;
3333

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

3737
static {
3838
int priority = Math.max(Thread.MIN_PRIORITY, Math.min(Thread.MAX_PRIORITY,

src/main/java/io/reactivex/rxjava3/internal/schedulers/SchedulerPoolFactory.java

+37-27
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import java.util.concurrent.*;
2121
import java.util.concurrent.atomic.AtomicReference;
2222

23+
import io.reactivex.rxjava3.functions.Function;
24+
2325
/**
2426
* Manages the creating of ScheduledExecutorServices and sets up purging.
2527
*/
@@ -29,14 +31,14 @@ private SchedulerPoolFactory() {
2931
throw new IllegalStateException("No instances!");
3032
}
3133

32-
static final String PURGE_ENABLED_KEY = "rx2.purge-enabled";
34+
static final String PURGE_ENABLED_KEY = "rx3.purge-enabled";
3335

3436
/**
3537
* Indicates the periodic purging of the ScheduledExecutorService is enabled.
3638
*/
3739
public static final boolean PURGE_ENABLED;
3840

39-
static final String PURGE_PERIOD_SECONDS_KEY = "rx2.purge-period-seconds";
41+
static final String PURGE_PERIOD_SECONDS_KEY = "rx3.purge-period-seconds";
4042

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

9294
static {
93-
Properties properties = System.getProperties();
94-
95-
PurgeProperties pp = new PurgeProperties();
96-
pp.load(properties);
97-
98-
PURGE_ENABLED = pp.purgeEnable;
99-
PURGE_PERIOD_SECONDS = pp.purgePeriod;
95+
SystemPropertyAccessor propertyAccessor = new SystemPropertyAccessor();
96+
PURGE_ENABLED = getBooleanProperty(true, PURGE_ENABLED_KEY, true, true, propertyAccessor);
97+
PURGE_PERIOD_SECONDS = getIntProperty(PURGE_ENABLED, PURGE_PERIOD_SECONDS_KEY, 1, 1, propertyAccessor);
10098

10199
start();
102100
}
103101

104-
static final class PurgeProperties {
105-
106-
boolean purgeEnable;
107-
108-
int purgePeriod;
109-
110-
void load(Properties properties) {
111-
if (properties.containsKey(PURGE_ENABLED_KEY)) {
112-
purgeEnable = Boolean.parseBoolean(properties.getProperty(PURGE_ENABLED_KEY));
113-
} else {
114-
purgeEnable = true;
102+
static int getIntProperty(boolean enabled, String key, int defaultNotFound, int defaultNotEnabled, Function<String, String> propertyAccessor) {
103+
if (enabled) {
104+
try {
105+
String value = propertyAccessor.apply(key);
106+
if (value == null) {
107+
return defaultNotFound;
108+
}
109+
return Integer.parseInt(value);
110+
} catch (Throwable ex) {
111+
return defaultNotFound;
115112
}
113+
}
114+
return defaultNotEnabled;
115+
}
116116

117-
if (purgeEnable && properties.containsKey(PURGE_PERIOD_SECONDS_KEY)) {
118-
try {
119-
purgePeriod = Integer.parseInt(properties.getProperty(PURGE_PERIOD_SECONDS_KEY));
120-
} catch (NumberFormatException ex) {
121-
purgePeriod = 1;
117+
static boolean getBooleanProperty(boolean enabled, String key, boolean defaultNotFound, boolean defaultNotEnabled, Function<String, String> propertyAccessor) {
118+
if (enabled) {
119+
try {
120+
String value = propertyAccessor.apply(key);
121+
if (value == null) {
122+
return defaultNotFound;
122123
}
123-
} else {
124-
purgePeriod = 1;
124+
return "true".equals(value);
125+
} catch (Throwable ex) {
126+
return defaultNotFound;
125127
}
126128
}
129+
return defaultNotEnabled;
130+
}
131+
132+
static final class SystemPropertyAccessor implements Function<String, String> {
133+
@Override
134+
public String apply(String t) throws Throwable {
135+
return System.getProperty(t);
136+
}
127137
}
128138

129139
/**

src/main/java/io/reactivex/rxjava3/internal/schedulers/SingleScheduler.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public final class SingleScheduler extends Scheduler {
3131
final AtomicReference<ScheduledExecutorService> executor = new AtomicReference<ScheduledExecutorService>();
3232

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

3636
private static final String THREAD_NAME_PREFIX = "RxSingleScheduler";
3737

src/main/java/io/reactivex/rxjava3/schedulers/Schedulers.java

+14-14
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@
3030
* <p>
3131
* <strong>Supported system properties ({@code System.getProperty()}):</strong>
3232
* <ul>
33-
* <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>
34-
* <li>{@code rx2.io-priority} (int): sets the thread priority of the {@link #io()} Scheduler, default is {@link Thread#NORM_PRIORITY}</li>
35-
* <li>{@code rx2.computation-threads} (int): sets the number of threads in the {@link #computation()} Scheduler, default is the number of available CPUs</li>
36-
* <li>{@code rx2.computation-priority} (int): sets the thread priority of the {@link #computation()} Scheduler, default is {@link Thread#NORM_PRIORITY}</li>
37-
* <li>{@code rx2.newthread-priority} (int): sets the thread priority of the {@link #newThread()} Scheduler, default is {@link Thread#NORM_PRIORITY}</li>
38-
* <li>{@code rx2.single-priority} (int): sets the thread priority of the {@link #single()} Scheduler, default is {@link Thread#NORM_PRIORITY}</li>
39-
* <li>{@code rx2.purge-enabled} (boolean): enables periodic purging of all Scheduler's backing thread pools, default is false</li>
40-
* <li>{@code rx2.purge-period-seconds} (int): specifies the periodic purge interval of all Scheduler's backing thread pools, default is 1 second</li>
33+
* <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>
34+
* <li>{@code rx3.io-priority} (int): sets the thread priority of the {@link #io()} Scheduler, default is {@link Thread#NORM_PRIORITY}</li>
35+
* <li>{@code rx3.computation-threads} (int): sets the number of threads in the {@link #computation()} Scheduler, default is the number of available CPUs</li>
36+
* <li>{@code rx3.computation-priority} (int): sets the thread priority of the {@link #computation()} Scheduler, default is {@link Thread#NORM_PRIORITY}</li>
37+
* <li>{@code rx3.newthread-priority} (int): sets the thread priority of the {@link #newThread()} Scheduler, default is {@link Thread#NORM_PRIORITY}</li>
38+
* <li>{@code rx3.single-priority} (int): sets the thread priority of the {@link #single()} Scheduler, default is {@link Thread#NORM_PRIORITY}</li>
39+
* <li>{@code rx3.purge-enabled} (boolean): enables periodic purging of all Scheduler's backing thread pools, default is false</li>
40+
* <li>{@code rx3.purge-period-seconds} (int): specifies the periodic purge interval of all Scheduler's backing thread pools, default is 1 second</li>
4141
* </ul>
4242
*/
4343
public final class Schedulers {
@@ -112,8 +112,8 @@ private Schedulers() {
112112
* before the {@link Schedulers} class is referenced in your code.
113113
* <p><strong>Supported system properties ({@code System.getProperty()}):</strong>
114114
* <ul>
115-
* <li>{@code rx2.computation-threads} (int): sets the number of threads in the {@link #computation()} Scheduler, default is the number of available CPUs</li>
116-
* <li>{@code rx2.computation-priority} (int): sets the thread priority of the {@link #computation()} Scheduler, default is {@link Thread#NORM_PRIORITY}</li>
115+
* <li>{@code rx3.computation-threads} (int): sets the number of threads in the {@link #computation()} Scheduler, default is the number of available CPUs</li>
116+
* <li>{@code rx3.computation-priority} (int): sets the thread priority of the {@link #computation()} Scheduler, default is {@link Thread#NORM_PRIORITY}</li>
117117
* </ul>
118118
* <p>
119119
* The default value of this scheduler can be overridden at initialization time via the
@@ -157,8 +157,8 @@ public static Scheduler computation() {
157157
* before the {@link Schedulers} class is referenced in your code.
158158
* <p><strong>Supported system properties ({@code System.getProperty()}):</strong>
159159
* <ul>
160-
* <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>
161-
* <li>{@code rx2.io-priority} (int): sets the thread priority of the {@link #io()} Scheduler, default is {@link Thread#NORM_PRIORITY}</li>
160+
* <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>
161+
* <li>{@code rx3.io-priority} (int): sets the thread priority of the {@link #io()} Scheduler, default is {@link Thread#NORM_PRIORITY}</li>
162162
* </ul>
163163
* <p>
164164
* The default value of this scheduler can be overridden at initialization time via the
@@ -216,7 +216,7 @@ public static Scheduler trampoline() {
216216
* before the {@link Schedulers} class is referenced in your code.
217217
* <p><strong>Supported system properties ({@code System.getProperty()}):</strong>
218218
* <ul>
219-
* <li>{@code rx2.newthread-priority} (int): sets the thread priority of the {@link #newThread()} Scheduler, default is {@link Thread#NORM_PRIORITY}</li>
219+
* <li>{@code rx3.newthread-priority} (int): sets the thread priority of the {@link #newThread()} Scheduler, default is {@link Thread#NORM_PRIORITY}</li>
220220
* </ul>
221221
* <p>
222222
* The default value of this scheduler can be overridden at initialization time via the
@@ -265,7 +265,7 @@ public static Scheduler newThread() {
265265
* before the {@link Schedulers} class is referenced in your code.
266266
* <p><strong>Supported system properties ({@code System.getProperty()}):</strong>
267267
* <ul>
268-
* <li>{@code rx2.single-priority} (int): sets the thread priority of the {@link #single()} Scheduler, default is {@link Thread#NORM_PRIORITY}</li>
268+
* <li>{@code rx3.single-priority} (int): sets the thread priority of the {@link #single()} Scheduler, default is {@link Thread#NORM_PRIORITY}</li>
269269
* </ul>
270270
* <p>
271271
* The default value of this scheduler can be overridden at initialization time via the

0 commit comments

Comments
 (0)