Update EnvHelper.java

This commit is contained in:
Zaxar163 2018-10-22 08:13:23 +03:00 committed by GitHub
parent 01fd6d2689
commit bde251486f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -10,33 +10,35 @@
public class EnvHelper { public class EnvHelper {
private static final boolean TST; private static final boolean TST;
private static final boolean HASXM; private static final boolean HASXW;
public static final String[] toTest; public static final String[] toTest;
public static final Pattern[] test; public static final Pattern[] test;
static { static {
toTest = new String[]{"_JAVA_OPTIONS", "_JAVA_OPTS", "JAVA_OPTS", "JAVA_OPTIONS"}; toTest = new String[]{"_JAVA_OPTIONS", "_JAVA_OPTS", "JAVA_OPTS", "JAVA_OPTIONS"};
test = new Pattern[]{Pattern.compile("-Xm.*\\d+[KMG]")}; test = new Pattern[]{Pattern.compile("-xm.*\\d+[KMG]")};
TST = check0(); TST = check0();
HASXM = check1(); HASXM = check1();
} }
public static void addEnv(ProcessBuilder builder) { public static void addEnv(ProcessBuilder builder) {
if (hasOptsVar()) { if (hasOptsEnv()) {
Map<String, String> repl = new HashMap<>(); Map<String, String> repl = new HashMap<>();
for (String str : toTest) { for (String str : toTest) {
repl.put(str, ""); repl.put(str, "");
repl.put(str.toLowerCase(Locale.ENGLISH), ""); repl.put(str.toLowerCase(Locale.US), "");
} }
JVMHelper.appendVars(builder, repl); JVMHelper.appendVars(builder, repl);
} }
} }
public static void checkDangerousParametrs() public static void checkDangerousParametrs()
{ {
if (hasOptsEnv())
for(String t : toTest) for(String t : toTest)
{ {
String env = System.getenv(t); String env = System.getenv(t).toLowerCase(Locale.US);
if(env.contains("-javaagent") || env.contains("-agentpath") || env.contains("-agentlib")) throw new SecurityException("JavaAgent in global optings not allow"); if (env != null)
if (env.contains("-cp") || env.contains("-classpath") || env.contains("-javaagent") || env.contains("-agentpath") || env.contains("-agentlib")) throw new SecurityException("JavaAgent in global optings not allow");
} }
} }
@ -49,10 +51,10 @@ private static boolean check0() {
* Вынужденное решение ибо тест на наличие -Xm* этакой нужен. * Вынужденное решение ибо тест на наличие -Xm* этакой нужен.
*/ */
private static boolean check1() { private static boolean check1() {
if (hasOptsVar()) if (hasOptsEnv())
for (String testStr : toTest) for (String testStr : toTest)
if (System.getProperty(testStr) != null) { if (System.getProperty(testStr) != null) {
String str = System.getenv(testStr); String str = System.getenv(testStr).toLowerCase(Locale.US);
StringTokenizer st = new StringTokenizer(str); StringTokenizer st = new StringTokenizer(str);
while (st.hasMoreTokens()) while (st.hasMoreTokens())
if (CommonHelper.multiMatches(test, st.nextToken())) return true; if (CommonHelper.multiMatches(test, st.nextToken())) return true;
@ -60,13 +62,11 @@ private static boolean check1() {
return false; return false;
} }
@LauncherAPI public static boolean hasWarnPreDef() {
public static boolean hasMemPreDef() { return HASXW;
return HASXM;
} }
@LauncherAPI public static boolean hasOptsEnv() {
public static boolean hasOptsVar() {
return TST; return TST;
} }
} }