-
Notifications
You must be signed in to change notification settings - Fork 60
/
SandboxTransformerTest.java
1255 lines (1181 loc) · 51.2 KB
/
SandboxTransformerTest.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* The MIT License
*
* Copyright 2019 CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.kohsuke.groovy.sandbox;
import groovy.lang.Binding;
import groovy.lang.EmptyRange;
import groovy.lang.GroovyShell;
import groovy.lang.IntRange;
import groovy.lang.ObjectRange;
import java.io.File;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;
import org.codehaus.groovy.control.CompilerConfiguration;
import org.codehaus.groovy.control.customizers.ImportCustomizer;
import org.codehaus.groovy.runtime.ProxyGeneratorAdapter;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ErrorCollector;
import org.jvnet.hudson.test.Issue;
import org.kohsuke.groovy.sandbox.impl.GroovyCallSiteSelector;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.fail;
public class SandboxTransformerTest {
public @Rule ErrorCollector ec = new ErrorCollector();
public Binding binding = new Binding();
public GroovyShell sandboxedSh;
public GroovyShell unsandboxedSh;
public ClassRecorder cr = new ClassRecorder();
@Before
public void setUp() {
CompilerConfiguration cc = new CompilerConfiguration();
cc.addCompilationCustomizers(new ImportCustomizer().addImports(SandboxTransformerTest.class.getName()).addStarImports("org.kohsuke.groovy.sandbox"));
cc.addCompilationCustomizers(new SandboxTransformer());
sandboxedSh = new GroovyShell(binding,cc);
cc = new CompilerConfiguration();
cc.addCompilationCustomizers(new ImportCustomizer().addImports(SandboxTransformerTest.class.getName()).addStarImports("org.kohsuke.groovy.sandbox"));
unsandboxedSh = new GroovyShell(binding,cc);
}
/**
* Use {@code ShouldFail.class} as the expected result for {@link #sandboxedEval} and {@link #unsandboxedEval}
* when the expression is expected to throw an exception.
*/
private static final class ShouldFail { }
@FunctionalInterface
public interface ExceptionHandler {
public void handleException(Throwable e) throws Exception;
}
/**
* Executes a Groovy expression inside of the sandbox.
* @param expression The Groovy expression to execute.
* @return the result of executing the expression.
*/
private void sandboxedEval(String expression, Object expectedResult, ExceptionHandler handler) {
cr.reset();
cr.register();
try {
Object actual = sandboxedSh.evaluate(expression);
String actualType = GroovyCallSiteSelector.getName(actual);
String expectedType = GroovyCallSiteSelector.getName(expectedResult);
ec.checkThat("Sandboxed result (" + actualType + ") does not match expected result (" + expectedType + ")", actual, equalTo(expectedResult));
} catch (Throwable e) {
ec.checkSucceeds(() -> {
try {
handler.handleException(e);
} catch (Throwable t) {
t.addSuppressed(e); // Keep the original error around in case an assertion fails in the handler.
throw t;
}
return null;
});
} finally {
cr.unregister();
}
}
/**
* Executes a Groovy expression outside of the sandbox.
* @param expression The Groovy expression to execute.
* @return the result of executing the expression.
*/
private void unsandboxedEval(String expression, Object expectedResult, ExceptionHandler handler) {
try {
Object actual = unsandboxedSh.evaluate(expression);
String actualType = GroovyCallSiteSelector.getName(actual);
String expectedType = GroovyCallSiteSelector.getName(expectedResult);
ec.checkThat("Unsandboxed result (" + actualType + ") does not match expected result (" + expectedType + ")", actual, equalTo(expectedResult));
} catch (Exception e) {
ec.checkSucceeds(() -> {
handler.handleException(e);
return null;
});
}
}
/**
* Execute a Groovy expression both in and out of the sandbox and check that the return value matches the
* expected value and that the given list of method calls are intercepted by the sandbox.
* @param expression The Groovy expression to execute.
* @param expectedReturnValue The expected return value for running the script.
* @param expectedCalls The method calls that are expected to be intercepted by the sandbox.
*/
private void assertIntercept(String expression, Object expectedReturnValue, String... expectedCalls) {
assertEvaluate(expression, expectedReturnValue);
assertIntercepted(expectedCalls);
}
/**
* Check that the most recently executed expression intercepted the expected calls.
* @param expectedCalls The method calls that were expected to be intercepted by the sandbox.
*/
private void assertIntercepted(String... expectedCalls) {
String[] interceptedCalls = cr.toString().split("\n");
if (interceptedCalls.length == 1 && interceptedCalls[0].equals("")) {
interceptedCalls = new String[0];
}
ec.checkThat(interceptedCalls, equalTo(expectedCalls));
}
/**
* Execute a Groovy expression both in and out of the sandbox and check that the return value matches the
* expected value.
* @param expression The Groovy expression to execute.
* @param expectedReturnValue The expected return value for running the script.
*/
private void assertEvaluate(String expression, Object expectedReturnValue) {
sandboxedEval(expression, expectedReturnValue, e -> {
throw new RuntimeException("Failed to evaluate sandboxed expression: " + expression, e);
});
unsandboxedEval(expression, expectedReturnValue, e -> {
throw new RuntimeException("Failed to evaluate unsandboxed expression: " + expression, e);
});
}
/**
* Execute a Groovy expression both in and out of the sandbox and check that the script throws an exception with
* the same class and message in both cases.
* @param expression The Groovy expression to execute.
*/
private void assertFailsWithSameException(String expression) {
AtomicReference<Throwable> sandboxedException = new AtomicReference<>();
sandboxedEval(expression, ShouldFail.class, sandboxedException::set);
AtomicReference<Throwable> unsandboxedException = new AtomicReference<>();
unsandboxedEval(expression, ShouldFail.class, unsandboxedException::set);
if (sandboxedException.get() == null || unsandboxedException.get() == null) {
return; // Either sandboxedEval or unsandboxedEval will have already recorded an error because the result was not ShouldFail.
}
ec.checkThat("Sandboxed and unsandboxed exception should have the same type",
unsandboxedException.get().getClass(), equalTo(sandboxedException.get().getClass()));
ec.checkThat("Sandboxed and unsandboxed exception should have the same message",
unsandboxedException.get().getMessage(), equalTo(sandboxedException.get().getMessage()));
}
@Issue("SECURITY-1465")
@Test public void sandboxTransformsMethodPointerLhs() throws Exception {
assertIntercept(
"({" +
" System.getProperties()\n" +
" 1" +
"}().&toString)()",
"1",
"Script1$_run_closure1.call()",
"System:getProperties()",
"SandboxedMethodClosure.call()",
"Integer.toString()");
}
@Issue("SECURITY-1465")
@Test public void sandboxTransformsMethodPointerRhs() throws Exception {
try {
System.setProperty("sandboxTransformsMethodPointerRhs", "toString");
assertIntercept(
"1.&(System.getProperty('sandboxTransformsMethodPointerRhs'))()",
"1",
"System:getProperty(String)",
"SandboxedMethodClosure.call()",
"Integer.toString()");
} finally {
System.clearProperty("sandboxTransformsMethodPointerRhs");
}
}
@Issue("SECURITY-1465")
@Test public void sandboxWillNotCastNonStandardCollections() throws Exception {
// Note: If you run this test in a debugger and inspect the proxied closure in Checker#preCheckedCast, the test
// will probably fail because the debugger will invoke the closure while trying to display the value, which will
// update the value of i, changing the behavior.
/* The cast to Collection proxies the Closure to a Collection.
* The cast to File invokes `new File(... proxied collection's elements as constructor arguments)`.
* The cast to Object[] reads lines from the File.
* The trick here is that the closure returns null from the first call to `toArray` in `Checker.preCheckedCast`,
* so it bypassed the interceptor but still worked correctly in the actual cast before the fix.
*/
sandboxedEval(
"def i = 0\n" +
"(({-> if(i) {\n" +
" return ['secret.txt'] as Object[]\n" + // Cast here is just so `toArray` returns an array instead of a List.
" } else {\n" +
" i = 1\n" +
" return null\n" +
" }\n" +
"} as Collection) as File) as Object[]",
ShouldFail.class,
e -> {
assertThat(e, instanceOf(UnsupportedOperationException.class));
assertThat(e.getMessage(),
containsString("Casting non-standard Collections to a type via constructor is not supported."));
});
}
@Issue("SECURITY-1465")
@Test public void sandboxWillNotCastNonStandardCollectionsEvenIfHarmless() throws Exception {
// Not problematic even before the fix because it is consistent, but there is no good way to differentiate
// between this and the expression in sandboxWillNotCastNonStandardCollections, so they both get blocked.
sandboxedEval("(({-> return ['secret.txt'] as Object[] } as Collection) as File) as Object[]", ShouldFail.class, e -> {
assertThat(e, instanceOf(UnsupportedOperationException.class));
assertThat(e.getMessage(),
containsString("Casting non-standard Collections to a type via constructor is not supported."));
});
}
@Issue("SECURITY-1465")
@Test public void sandboxWillCastStandardCollections() throws Exception {
Path secret = Paths.get("secret.txt");
try {
Files.write(secret, Arrays.asList("secretValue"));
assertIntercept(
"(Arrays.asList('secret.txt') as File) as Object[]",
new String[]{"secretValue"},
"Arrays:asList(String)",
"new File(String)",
"ResourceGroovyMethods:readLines(File)");
assertIntercept(
"(Collections.singleton('secret.txt') as File) as Object[]",
new String[]{"secretValue"},
"Collections:singleton(String)",
"new File(String)",
"ResourceGroovyMethods:readLines(File)");
assertIntercept(
"(new ArrayList<>(Arrays.asList('secret.txt')) as File) as Object[]",
new String[]{"secretValue"},
"Arrays:asList(String)",
"new ArrayList(Arrays$ArrayList)",
"new File(String)",
"ResourceGroovyMethods:readLines(File)");
assertIntercept(
"(new HashSet<>(Arrays.asList('secret.txt')) as File) as Object[]",
new String[]{"secretValue"},
"Arrays:asList(String)",
"new HashSet(Arrays$ArrayList)",
"new File(String)",
"ResourceGroovyMethods:readLines(File)");
} finally {
Files.deleteIfExists(secret);
}
}
@Issue("SECURITY-1465")
@Test public void sandboxInterceptsEnumClassToArrayCasts() throws Exception {
assertIntercept(
"(java.util.concurrent.TimeUnit.class as Object[])",
TimeUnit.values(),
"Class.NANOSECONDS",
"Class.MICROSECONDS",
"Class.MILLISECONDS",
"Class.SECONDS",
"Class.MINUTES",
"Class.HOURS",
"Class.DAYS");
}
@Issue("SECURITY-1538")
@Test public void sandboxTransformsMethodNameInMethodCalls() throws Exception {
assertIntercept(
"1.({ System.getProperties(); 'toString' }())()",
"1",
"Script1$_run_closure1.call()",
"System:getProperties()",
"Integer.toString()");
}
@Issue("SECURITY-1538")
@Test public void sandboxTransformsPropertyNameInLhsOfAssignmentOps() throws Exception {
assertIntercept(
"class Test {\n" +
" def x\n" +
"}\n" +
"def t = new Test()\n" +
"t.({\n" +
" System.getProperties()\n" +
" 'x'\n" +
"}()) = 1\n" +
"t.x",
1,
"new Test()",
"Script1$_run_closure1.call()",
"System:getProperties()",
"Test.x=Integer",
"Test.x");
}
@Issue("SECURITY-1538")
@Test public void sandboxTransformsPropertyNameInPrefixPostfixOps() throws Exception {
assertIntercept(
"class Test {\n" +
" def x = 0\n" +
"}\n" +
"def t = new Test()\n" +
"(t.({\n" +
" System.getProperties()\n" +
" 'x'\n" +
"}()))++\n" +
"t.x",
1,
"new Test()",
"Script1$_run_closure1.call()",
"System:getProperties()",
"Test.x",
"Integer.next()",
"Test.x=Integer",
"Test.x");
}
@Issue("SECURITY-1538")
@Test public void sandboxTransformsComplexExpressionsInPrefixOps() throws Exception {
assertIntercept(
"++({ System.getProperties(); 1 }())",
2,
"Script1$_run_closure1.call()",
"System:getProperties()",
"Integer.next()");
}
@Issue("SECURITY-1538")
@Test public void sandboxTransformsComplexExpressionsInPostfixOps() throws Exception {
assertIntercept(
"({ System.getProperties(); 1 }())++",
1,
"Script1$_run_closure2.call()",
"System:getProperties()",
"Script1$_run_closure1.call(Integer)",
"Integer.next()");
}
@Test public void sandboxTransformsInitialExpressionsForConstructorParameters() throws Exception {
assertIntercept(
"class B { }\n" +
"class A extends B {\n" +
" A(x = System.getProperties()) {\n" +
" super()\n" +
" }\n" +
"}\n" +
"new A()\n" +
"true\n",
true,
"new A()",
"System:getProperties()",
"new A(Properties)",
"new B()");
}
@Issue("SECURITY-1658")
@Test public void sandboxTransformsInitialExpressionsForClosureParameters() throws Exception {
assertIntercept(
"({ p = System.getProperties() -> true })()",
true,
"Script1$_run_closure1.call()",
"System:getProperties()");
}
@Issue("SECURITY-1754")
@Test public void interceptThisConstructorCalls() throws Exception {
assertIntercept(
"class Superclass { }\n" +
"class Subclass extends Superclass {\n" +
" Subclass() { this(1) }\n" +
" Subclass(int x) { }\n" +
"}\n" +
"new Subclass()\n" +
"null",
null,
"new Subclass()",
"new Subclass(Integer)",
"new Superclass()");
}
@Issue({ "SECURITY-1754", "SECURITY-2824" })
@Test public void blocksDirectCallsToSyntheticConstructors() throws Exception {
sandboxedEval(
"class Superclass { }\n" +
"class Subclass extends Superclass {\n" +
" Subclass() { }\n" +
"}\n" +
"new Subclass(null)\n",
ShouldFail.class,
e -> assertThat(e.getMessage(), equalTo(
"Rejecting illegal call to synthetic constructor: private Subclass(org.kohsuke.groovy.sandbox.impl.Checker$SuperConstructorWrapper). " +
"Perhaps you meant to use one of these constructors instead: public Subclass()")));
// Calls are blocked even if you manage to obtain a valid wrapper.
sandboxedEval(
"class Superclass { Superclass(String x) { } }\n" +
"class Subclass extends Superclass {\n" +
" def wrapper\n" +
" Subclass() { super('secret.key'); def $cw = $cw; wrapper = $cw }\n" +
"}\n" +
"def wrapper = new Subclass().wrapper\n" +
"class MyFile extends File {\n" +
" MyFile(String path) {\n" +
" super(path)\n" +
" }\n" +
"}\n" +
"new MyFile(wrapper, 'unused')",
ShouldFail.class,
e -> assertThat(e.getMessage(), equalTo("Rejecting illegal call to synthetic constructor: private MyFile(org.kohsuke.groovy.sandbox.impl.Checker$SuperConstructorWrapper,java.lang.String). " +
"Perhaps you meant to use one of these constructors instead: public MyFile(java.lang.String)")));
}
@Issue("SECURITY-1754")
@Test public void blocksCallsToSyntheticConstructorsViaOtherConstructors() throws Exception {
sandboxedEval(
"class Superclass { }\n" +
"class Subclass extends Superclass {\n" +
" Subclass() { }\n" +
" Subclass(int x, int y) { this(null) }\n" + // Directly calls synthetic constructor generated the handle the other constructor.
"}\n" +
"new Subclass(1, 2)\n",
ShouldFail.class,
e -> assertThat(e.getMessage(), equalTo(
"Rejecting illegal call to synthetic constructor: private Subclass(org.kohsuke.groovy.sandbox.impl.Checker$SuperConstructorWrapper). " +
"Perhaps you meant to use one of these constructors instead: public Subclass(), public Subclass(int,int)")));
}
@Issue("SECURITY-1754")
@Test public void blocksUnintendedCallsToNonSyntheticConstructors() throws Exception {
sandboxedEval(
"class B { }\n" +
"class F extends B { }\n" +
"class S extends B {\n" +
" Object scw\n" +
" S(Object o) { }\n" +
" S(Object o, F f) { scw = o }\n" +
"}\n" +
"new S(new F()).scw",
ShouldFail.class,
e -> assertThat(e.getMessage(), equalTo(
"Rejecting unexpected invocation of constructor: public S(java.lang.Object,F). " +
"Expected to invoke synthetic constructor: private S(org.kohsuke.groovy.sandbox.impl.Checker$SuperConstructorWrapper,java.lang.Object)")));
}
@Issue("SECURITY-1754")
@Test public void localVarsInIfStatementsAreNotInScopeInElseStatements() throws Exception {
sandboxedEval(
"class Super { }\n" +
"class Sub extends Super {\n" +
" def var\n" +
" Sub() {\n" +
" if (false)\n" + // Intentionally not using braces for the body.
" def $cw\n" + // The name of the parameter for constructor wrappers added by `SandboxTransformer.processConstructors()`.
" else {\n" +
" this.var = $cw\n" +
" }\n" +
" }\n" +
"}\n" +
"new Sub().var\n",
ShouldFail.class, // Previously, would have been an instance of Checker.SuperConstructorWrapper.
e -> assertThat(e.getMessage(), containsString("No such property: $cw for class: Sub")));
}
@Issue("SECURITY-1754")
@Test public void statementsInSyntheticConstructorsAreScopedCorrectly() throws Exception {
assertIntercept(
"class Super { }\n" +
"class Sub extends Super {\n" +
" Sub() {\n" +
" def x = 1\n" +
" x += 1\n" +
" }\n" +
"}\n" +
"new Sub()\n" +
"null\n",
null,
"new Sub()",
"new Super()");
}
@Issue("SECURITY-2020")
@Test public void sandboxedCodeRejectedWhenExecutedOutsideOfSandbox() throws Exception {
cr.reset();
cr.register();
Object returnValue;
try {
returnValue = sandboxedSh.evaluate(
"class Test {\n" +
" @Override public String toString() {\n" +
" System.getProperties()\n" +
" 'test'\n" +
" }\n" +
"}\n" +
"new Test()");
} finally {
cr.unregister();
}
try {
// Test.equals and Test.getClass are inherited and not sandbox-transformed, so they can be called outside of the sandbox.
assertFalse(returnValue.equals(new Object()));
assertThat(returnValue.getClass().getSimpleName(), equalTo("Test"));
// Test.toString is defined in the sandbox, so it cannot be called outside of the sandbox.
returnValue.toString();
fail("Test.toString should have thrown a SecurityException");
} catch (SecurityException e) {
assertThat(e.getMessage(), equalTo("Rejecting unsandboxed static method call: java.lang.System.getProperties()"));
}
}
@Test public void equalsAndHashCode() throws Exception {
assertIntercept(
"@groovy.transform.EqualsAndHashCode\n" +
"class C {\n" +
" def prop\n" +
" def getProp() {\n" +
" System.setProperty('x', 'y')\n" +
" 'foo'\n" +
" }\n" +
"}\n" +
"[new C().equals(new C()), new C().hashCode()]\n",
Arrays.asList(true, 105511),
"new C()",
"new C()",
"C.equals(C)",
"C.equals(null)",
"C.is(C)",
"C.canEqual(C)",
"C.getProp()",
"System:setProperty(String,String)",
"C.getProp()",
"System:setProperty(String,String)",
"String.compareTo(String)",
"new C()",
"C.hashCode()",
"HashCodeHelper:initHash()",
// `getProp` is called twice by the generated `hashCode` method. The first call is used to prevent cycles in case it returns `this`.
"C.getProp()",
"System:setProperty(String,String)",
"String.is(C)",
"C.getProp()",
"System:setProperty(String,String)",
"HashCodeHelper:updateHash(Integer,String)");
}
@Test public void sandboxInterceptsUnaryOperatorExpressions() {
assertIntercept(
"def auditLog = []\n" +
"def o = new SandboxTransformerTest.OperatorOverloader(auditLog, 2)\n" +
"[-o, +o, ~o, *auditLog]",
Arrays.asList(-2, 2, ~2, "negative", "positive", "bitwiseNegate"),
"new SandboxTransformerTest$OperatorOverloader(ArrayList,Integer)",
"SandboxTransformerTest$OperatorOverloader.negative()",
"SandboxTransformerTest$OperatorOverloader.positive()",
"SandboxTransformerTest$OperatorOverloader.bitwiseNegate()");
}
@Test public void sandboxInterceptsRangeExpressions() {
assertIntercept(
"def auditLog = []\n" +
"def range = new SandboxTransformerTest.OperatorOverloader(auditLog, 1)..<(new SandboxTransformerTest.OperatorOverloader(auditLog, 4))\n" +
"def result = []\n" +
"for (o in range) { result.add(o.value) }\n" +
"result.addAll(auditLog)\n" +
"result\n",
// These are the calls that actually happened at runtime.
Arrays.asList(1, 2, 3, "compareTo", "compareTo", "previous", "compareTo", "compareTo", "next", "compareTo", "compareTo", "next", "compareTo", "compareTo", "next", "compareTo", "compareTo", "next", "next"),
"new SandboxTransformerTest$OperatorOverloader(ArrayList,Integer)",
"new SandboxTransformerTest$OperatorOverloader(ArrayList,Integer)",
// These next 10 interceptions are from Checker.checkedRange and Checker.checkedComparison.
"SandboxTransformerTest$OperatorOverloader.compareTo(SandboxTransformerTest$OperatorOverloader)",
"SandboxTransformerTest$OperatorOverloader.compareTo(SandboxTransformerTest$OperatorOverloader)",
"SandboxTransformerTest$OperatorOverloader.previous()",
"SandboxTransformerTest$OperatorOverloader.compareTo(null)",
"SandboxTransformerTest$OperatorOverloader.next()",
"SandboxTransformerTest$OperatorOverloader.previous()",
"SandboxTransformerTest$OperatorOverloader.compareTo(null)",
"SandboxTransformerTest$OperatorOverloader.next()",
"SandboxTransformerTest$OperatorOverloader.previous()",
"SandboxTransformerTest$OperatorOverloader.value",
"ArrayList.add(Integer)",
"SandboxTransformerTest$OperatorOverloader.value",
"ArrayList.add(Integer)",
"SandboxTransformerTest$OperatorOverloader.value",
"ArrayList.add(Integer)",
"ArrayList.addAll(ArrayList)");
}
@Test public void unaryExpressionsSmoke() {
// Bitwise negate
assertEvaluate("~1", ~1);
assertEvaluate("~2L", ~2L);
assertEvaluate("~BigInteger.valueOf(3L)", BigInteger.valueOf(3L).not());
assertEvaluate("(~'test').matcher('test').matches()", true); // Pattern does not override equals or hashcode.
assertEvaluate("(~\"tes${'t'}\").matcher('test').matches()", true); // Pattern does not override equals or hashcode.
assertEvaluate("~[1, 2L]", Arrays.asList(~1, ~2L));
// Unary minus
assertEvaluate("-1", -1);
assertEvaluate("-2L", -2L);
assertEvaluate("-BigInteger.valueOf(3L)", BigInteger.valueOf(3L).negate());
assertEvaluate("-4.1", BigDecimal.valueOf(4.1).negate());
assertEvaluate("-5.2d", -5.2);
assertEvaluate("-6.3f", -6.3f);
assertEvaluate("-(short)7", (short)(-7));
assertEvaluate("-(byte)8", (byte)(-8));
assertEvaluate("-[1, 2L, 6.3f]", Arrays.asList(-1, -2L, -6.3f));
// Unary plus
assertEvaluate("+1", 1);
assertEvaluate("+2L", 2L);
assertEvaluate("+BigInteger.valueOf(3L)", BigInteger.valueOf(3L));
assertEvaluate("+4.1", BigDecimal.valueOf(4.1));
assertEvaluate("+5.2d", 5.2);
assertEvaluate("+6.3f", 6.3f);
assertEvaluate("+(short)7", (short)7);
assertEvaluate("+(byte)8", (byte)8);
assertEvaluate("+[1, 2L, 6.3f]", Arrays.asList(1, 2L, 6.3f));
}
@Test
public void rangeExpressionsSmoke() {
assertEvaluate("1..3", new IntRange(true, 1, 3));
assertEvaluate("1..<3", new IntRange(false, 1, 3));
assertEvaluate("'a'..'c'", new ObjectRange('a', 'c'));
assertEvaluate("'a'..<'c'", new ObjectRange('a', 'b'));
assertEvaluate("'a'..<'a'", new EmptyRange('a'));
assertEvaluate("1..<1", new EmptyRange(1));
assertEvaluate("'A'..67", new IntRange(true, 65, 67));
assertEvaluate("'a'..'ab'", new ObjectRange("a", "ab"));
assertEvaluate("'ab'..'a'", new ObjectRange("ab", "a"));
// Checking consistency in error messages.
assertFailsWithSameException("'a'..67");
assertFailsWithSameException("null..1");
assertFailsWithSameException("1..null");
assertFailsWithSameException("null..null");
assertFailsWithSameException("1..'abc'");
assertFailsWithSameException("'abc'..1");
assertFailsWithSameException("(new Object())..1");
assertFailsWithSameException("1..(new Object())");
}
private static class OperatorOverloader implements Comparable<OperatorOverloader> {
private final List<String> auditLog;
private final int value;
private OperatorOverloader(List<String> auditLog, int value) {
this.auditLog = auditLog;
this.value = value;
}
public int negative() {
auditLog.add("negative");
return -value;
}
public int positive() {
auditLog.add("positive");
return value;
}
public int bitwiseNegate() {
auditLog.add("bitwiseNegate");
return ~value;
}
@Override
public int compareTo(OperatorOverloader other) {
auditLog.add("compareTo");
return Integer.compare(value, other.value);
}
public OperatorOverloader next() {
auditLog.add("next");
return new OperatorOverloader(auditLog, value + 1);
}
public OperatorOverloader previous() {
auditLog.add("previous");
return new OperatorOverloader(auditLog, value - 1);
}
}
@Issue("SECURITY-2824")
@Test
public void sandboxInterceptsImplicitCastsMethodReturnValues() {
assertIntercept(
"File createFile(String path) {\n" +
" [path]\n" +
"}\n" +
"createFile('secret.key')\n",
new File("secret.key"),
"Script1.createFile(String)",
"new File(String)");
assertIntercept(
"File createFile(String path) {\n" +
" return [path]\n" +
"}\n" +
"createFile('secret.key')\n",
new File("secret.key"),
"Script2.createFile(String)",
"new File(String)");
}
@Issue("SECURITY-2824")
@Test
public void sandboxInterceptsImplicitCastsVariableAssignment() {
assertIntercept(
"File file\n" +
"file = ['secret.key']\n " +
"file",
new File("secret.key"),
"new File(String)");
}
// https://github.com/jenkinsci/groovy-sandbox/issues/7 would allow these casts to be intercepted here, but for now,
// we handle them in script-security's SandboxInterceptor
@Ignore("These casts cannot be intercepted by groovy-sandbox itself without extensive modifications")
@Issue("SECURITY-2824")
@Test
public void sandboxInterceptsImplicitCastsPropertyAndAttributeAssignment() {
assertIntercept(
"class Test {\n" +
" File file\n" +
"}\n" +
"def t = new Test()\n" +
"t.file = ['secret1.key']\n " +
"def temp = t.file\n" +
"t.@file = ['secret2.key']\n " +
"[temp, t.@file]\n",
Arrays.asList(new File("secret1.key"), new File("secret2.key")),
"new Test()",
"new File(String)",
"Test.file=File",
"Test.file",
"new File(String)",
"Test.@file=File",
"Test.@file");
}
@Issue("SECURITY-2824")
@Test
public void sandboxInterceptsImplicitCastsPropertyAssignmentThisField() {
assertIntercept(
"class Test {\n" +
" File file\n" +
" def setFile(String path) {\n" +
" this.file = [path]\n" + // This form of property access is handled as a special case in SandboxTransformer
" }\n" +
"}\n" +
"def t = new Test()\n" +
"t.setFile('secret.key')\n " +
"t.file",
new File("secret.key"),
"new Test()",
"Test.setFile(String)",
"new File(String)",
"Test.file");
}
@Issue("SECURITY-2824")
@Test
public void sandboxInterceptsImplicitCastsArrayAssignment() {
// Regular Groovy casts the rhs of array assignments to match the component type of the array, but the
// sandbox does not do this. Ideally the sandbox would have the same behavior as regular Groovy, but the
// current behavior is safe, which is good enough.
sandboxedEval(
"File[] files = [null]\n" +
"files[0] = ['secret.key']\n" +
"files[0]",
ShouldFail.class,
e -> ec.checkThat(e.toString(), equalTo("java.lang.ArrayStoreException: java.util.ArrayList")));
}
@Issue("SECURITY-2824")
@Test
public void sandboxInterceptsImplicitCastsInitialParameterExpressions() {
assertIntercept(
"def method(File file = ['secret.key']) { file }; method()",
new File("secret.key"),
"Script1.method()",
"new File(String)",
"Script1.method(File)");
assertIntercept(
"({ File file = ['secret.key'] -> file })()",
new File("secret.key"),
"Script2$_run_closure1.call()",
"new File(String)");
assertIntercept(
"class Test {\n" +
" def x\n" +
" Test(File file = ['secret.key']) {\n" +
" x = file\n" +
" }\n" +
"}\n" +
"new Test().x",
new File("secret.key"),
"new Test()",
"new File(String)",
"Test.x");
}
@Issue("SECURITY-2824")
@Test
public void sandboxInterceptsImplicitCastsFields() {
assertIntercept(
"class Test {\n" +
" File file = ['secret.key']\n" +
"}\n" +
"new Test().file",
new File("secret.key"),
"new Test()",
"new File(String)",
"Test.file");
assertIntercept(
"@groovy.transform.Field File file = ['secret.key']\n" +
"file",
new File("secret.key"),
"new File(String)",
"Script2.file");
}
@Issue("SECURITY-2824")
@Test
public void sandboxInterceptsElementCastsInArrayCasts() {
assertIntercept(
"([['secret.key']] as File[])[0]",
new File("secret.key"),
"new File(String)",
"File[][Integer]");
assertIntercept(
"(([['secret.key']] as Object[]) as File[])[0]",
new File("secret.key"),
"new File(String)",
"File[][Integer]");
assertIntercept(
"((File[])[['secret.key']])[0]",
new File("secret.key"),
"new File(String)",
"File[][Integer]");
assertIntercept(
"((File[])((Object[])[['secret.key']]))[0]",
new File("secret.key"),
"new File(String)",
"File[][Integer]");
assertIntercept(
"([[['secret.key']]] as File[][])[0][0]",
new File("secret.key"),
"new File(String)",
"File[][][Integer]",
"File[][Integer]");
}
@Issue("SECURITY-2824")
@Test
public void sandboxUsesCastToTypeForImplicitCasts() {
assertIntercept(
"class Test {\n" +
" def auditLog = []\n" +
" def asType(Class c) {\n" +
" auditLog.add('asType')\n" +
" 'Test.asType'\n" +
" }\n" +
" String toString() {\n" +
" auditLog.add('toString')\n" +
" 'Test.toString'\n" +
" }\n" +
"}\n" +
"def t = new Test()\n" +
"String methodReturnValue(def o) { o }\n" +
"methodReturnValue(t)\n" +
"String variable = t\n" +
"String[] array = [t]\n" +
"(String)t\n" +
"t as String\n" + // This is the only cast that should call asType.
"t.auditLog\n",
Arrays.asList("toString", "toString", "toString", "toString", "asType"),
"new Test()",
"Script1.methodReturnValue(Test)",
"Test.auditLog",
"ArrayList.add(String)",
"Test.auditLog",
"ArrayList.add(String)",
"Test.auditLog",
"ArrayList.add(String)",
"Test.auditLog",
"ArrayList.add(String)",
"Test.auditLog",
"ArrayList.add(String)",
"Test.auditLog");
}
@Test
public void sandboxInterceptsAttributeExpressionsInPrefixPostfixOps() {
assertIntercept(
"class Test { int x }\n" +
"def t = new Test()\n" +
"t.@x++\n" +
"t.@x\n",
1,
"new Test()", "Test.@x", "Integer.next()", "Test.@x=Integer", "Test.@x");
}
@Test
public void sandboxInterceptsEnums() {
assertIntercept(
"enum Test { FIRST, SECOND }\n" +
"Test.FIRST.toString()\n",
"FIRST",
// Enum classes are generated before SandboxTransformer runs, so various synthetic constructs are
// (unnecessarily?) intercepted if you do not define an explicit constructor.
"Class.FIRST",
"Test:$INIT(String,Integer)",
"new LinkedHashMap()",
"new Test(String,Integer,LinkedHashMap)",
"new Enum(String,Integer)",
"LinkedHashMap.equals(null)",
"ImmutableASTTransformation:checkPropNames(Test,LinkedHashMap)",
"Test:$INIT(String,Integer)",
"new LinkedHashMap()",
"new Test(String,Integer,LinkedHashMap)",
"new Enum(String,Integer)",
"LinkedHashMap.equals(null)",
"ImmutableASTTransformation:checkPropNames(Test,LinkedHashMap)",
"Class.@FIRST",
"Class.@SECOND",
"Class.@FIRST",
"Class.@SECOND",
"Test.toString()");
assertIntercept(
"enum Test { FIRST(), SECOND(); Test() {} }\n" +
"Test.FIRST.toString()\n",
"FIRST",
// You can define an explicit constructor to simplify the generated code.
"Class.FIRST",
"Test:$INIT(String,Integer)",
"new Enum(String,Integer)",
"Test:$INIT(String,Integer)",
"new Enum(String,Integer)",
"Class.@FIRST",
"Class.@SECOND",
"Class.@FIRST",
"Class.@SECOND",
"Test.toString()");
}
@Test
public void sandboxInterceptsBooleanCasts() {
assertIntercept("null as Boolean", null);
assertIntercept("true as Boolean", true);
assertIntercept("[:] as Boolean", false,
"LinkedHashMap.asBoolean()");
assertIntercept("[] as Boolean", false,