-
Notifications
You must be signed in to change notification settings - Fork 214
/
JavaScriptMessageMapperRhinoSandboxingTest.java
175 lines (151 loc) · 6.68 KB
/
JavaScriptMessageMapperRhinoSandboxingTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
* Copyright (c) 2017 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.connectivity.service.mapping.javascript;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import org.assertj.core.api.Assertions;
import org.eclipse.ditto.connectivity.api.ExternalMessage;
import org.eclipse.ditto.connectivity.api.ExternalMessageFactory;
import org.eclipse.ditto.connectivity.model.Connection;
import org.eclipse.ditto.connectivity.model.MessageMapperConfigurationFailedException;
import org.eclipse.ditto.connectivity.model.MessageMappingFailedException;
import org.eclipse.ditto.connectivity.service.config.ConnectivityConfig;
import org.eclipse.ditto.connectivity.service.config.DittoConnectivityConfig;
import org.eclipse.ditto.connectivity.service.mapping.MessageMapper;
import org.eclipse.ditto.connectivity.service.messaging.TestConstants;
import org.eclipse.ditto.internal.utils.config.DefaultScopedConfig;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import org.apache.pekko.actor.ActorSystem;
/**
* Tests the {@link JavaScriptMessageMapperRhino} sandboxing capabilities by trying to exploit CPU time, exiting, etc.
*/
public final class JavaScriptMessageMapperRhinoSandboxingTest {
private static ActorSystem actorSystem;
@BeforeClass
public static void setup() {
actorSystem = ActorSystem.create("Test", TestConstants.CONFIG);
}
@AfterClass
public static void tearDown() {
if (actorSystem != null) {
actorSystem.terminate();
}
}
@Test
public void ensureExitForbidden() {
final MessageMapper mapper = createMapper("exit(1);");
Assertions.assertThatExceptionOfType(MessageMappingFailedException.class)
.isThrownBy(() -> mapper.map(createMessage()));
}
@Test
public void ensureQuitForbidden() {
final MessageMapper mapper = createMapper("quit(1);");
Assertions.assertThatExceptionOfType(MessageMappingFailedException.class)
.isThrownBy(() -> mapper.map(createMessage()));
}
@Test
public void ensureEndlessLoopGetsAborted() {
final MessageMapper mapper = createMapper("while (true);");
final long startTs = System.nanoTime();
Assertions.assertThatExceptionOfType(MessageMappingFailedException.class)
.isThrownBy(() -> mapper.map(createMessage()));
System.out.println(
"ensureEndlessLoopGetsAborted aborted after: " + (System.nanoTime() - startTs) / 1000000.0 + "ms");
}
@Test
public void ensureRecursionGetsAborted() {
final MessageMapper mapper = createMapper("function recurse() {\n" +
" recurse();\n" +
"};\n" +
"recurse();");
final long startTs = System.nanoTime();
Assertions.assertThatExceptionOfType(MessageMappingFailedException.class)
.isThrownBy(() -> mapper.map(createMessage()));
System.out.println(
"ensureRecursionGetsAborted aborted after: " + (System.nanoTime() - startTs) / 1000000.0 + "ms");
}
@Test
public void ensureTooBigMappingScriptIsNotLoaded() {
final StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < 100_000; i++) {
stringBuilder
.append("function foo").append(i)
.append("() { return ").append(i).append("; };")
.append('\n');
}
Assertions.assertThatExceptionOfType(MessageMapperConfigurationFailedException.class).isThrownBy(() ->
createMapper(stringBuilder.toString())
);
}
private static MessageMapper createMapper(final String maliciousStuff) {
final MessageMapper mapper = JavaScriptMessageMapperFactory.createJavaScriptMessageMapperRhino();
final Config config =
ConfigFactory.parseString("ditto.connectivity.mapping.javascript {\n" +
" maxScriptSizeBytes = 50000 # 50kB\n" +
" maxScriptExecutionTime = 500ms\n" +
" maxScriptStackDepth = 10\n" +
" }").withFallback(ConfigFactory.load("test"));
final ConnectivityConfig connectivityConfig =
DittoConnectivityConfig.of(DefaultScopedConfig.dittoScoped(config));
final Connection connection = TestConstants.createConnection();
mapper.configure(connection, connectivityConfig,
JavaScriptMessageMapperFactory
.createJavaScriptMessageMapperConfigurationBuilder("malicious", Collections.emptyMap())
.incomingScript(getMappingWrapperScript(maliciousStuff))
.build(),
actorSystem
);
return mapper;
}
private static ExternalMessage createMessage() {
final String correlationId = UUID.randomUUID().toString();
final Map<String, String> headers = new HashMap<>();
headers.put("correlation-id", correlationId);
headers.put(ExternalMessage.CONTENT_TYPE_HEADER, "text/plain");
return ExternalMessageFactory.newExternalMessageBuilder(headers)
.withText("what's up?")
.build();
}
private static String getMappingWrapperScript(final String maliciousStuff) {
return "function mapToDittoProtocolMsg(\n" +
" headers,\n" +
" textPayload,\n" +
" bytePayload,\n" +
" contentType\n" +
") {\n" +
"\n" +
" // ###\n" +
" // Insert your mapping logic here\n" +
" " + maliciousStuff + "\n" +
" // ###\n" +
"\n" +
" return Ditto.buildDittoProtocolMsg(\n" +
" 'org.eclipse.ditto',\n" +
" 'should-not-come-trough',\n" +
" 'things',\n" +
" 'twin',\n" +
" 'commands',\n" +
" 'delete',\n" +
" '/',\n" +
" {},\n" +
" 'oh oh!'\n" +
" );\n" +
"}";
}
}