-
-
Notifications
You must be signed in to change notification settings - Fork 11.5k
Expand file tree
/
Copy pathSecureASTCustomizer.java
More file actions
1566 lines (1387 loc) · 62 KB
/
SecureASTCustomizer.java
File metadata and controls
1566 lines (1387 loc) · 62 KB
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
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.codehaus.groovy.control.customizers;
import org.codehaus.groovy.ast.ClassNode;
import org.codehaus.groovy.ast.CodeVisitorSupport;
import org.codehaus.groovy.ast.GroovyCodeVisitor;
import org.codehaus.groovy.ast.ImportNode;
import org.codehaus.groovy.ast.MethodNode;
import org.codehaus.groovy.ast.ModuleNode;
import org.codehaus.groovy.ast.expr.ArgumentListExpression;
import org.codehaus.groovy.ast.expr.ArrayExpression;
import org.codehaus.groovy.ast.expr.AttributeExpression;
import org.codehaus.groovy.ast.expr.BinaryExpression;
import org.codehaus.groovy.ast.expr.BitwiseNegationExpression;
import org.codehaus.groovy.ast.expr.BooleanExpression;
import org.codehaus.groovy.ast.expr.CastExpression;
import org.codehaus.groovy.ast.expr.ClassExpression;
import org.codehaus.groovy.ast.expr.ClosureExpression;
import org.codehaus.groovy.ast.expr.ClosureListExpression;
import org.codehaus.groovy.ast.expr.ConstantExpression;
import org.codehaus.groovy.ast.expr.ConstructorCallExpression;
import org.codehaus.groovy.ast.expr.DeclarationExpression;
import org.codehaus.groovy.ast.expr.ElvisOperatorExpression;
import org.codehaus.groovy.ast.expr.Expression;
import org.codehaus.groovy.ast.expr.FieldExpression;
import org.codehaus.groovy.ast.expr.GStringExpression;
import org.codehaus.groovy.ast.expr.LambdaExpression;
import org.codehaus.groovy.ast.expr.ListExpression;
import org.codehaus.groovy.ast.expr.MapEntryExpression;
import org.codehaus.groovy.ast.expr.MapExpression;
import org.codehaus.groovy.ast.expr.MethodCallExpression;
import org.codehaus.groovy.ast.expr.MethodPointerExpression;
import org.codehaus.groovy.ast.expr.MethodReferenceExpression;
import org.codehaus.groovy.ast.expr.NotExpression;
import org.codehaus.groovy.ast.expr.PostfixExpression;
import org.codehaus.groovy.ast.expr.PrefixExpression;
import org.codehaus.groovy.ast.expr.PropertyExpression;
import org.codehaus.groovy.ast.expr.RangeExpression;
import org.codehaus.groovy.ast.expr.SpreadExpression;
import org.codehaus.groovy.ast.expr.SpreadMapExpression;
import org.codehaus.groovy.ast.expr.StaticMethodCallExpression;
import org.codehaus.groovy.ast.expr.TernaryExpression;
import org.codehaus.groovy.ast.expr.TupleExpression;
import org.codehaus.groovy.ast.expr.UnaryMinusExpression;
import org.codehaus.groovy.ast.expr.UnaryPlusExpression;
import org.codehaus.groovy.ast.expr.VariableExpression;
import org.codehaus.groovy.ast.stmt.AssertStatement;
import org.codehaus.groovy.ast.stmt.BlockStatement;
import org.codehaus.groovy.ast.stmt.BreakStatement;
import org.codehaus.groovy.ast.stmt.CaseStatement;
import org.codehaus.groovy.ast.stmt.CatchStatement;
import org.codehaus.groovy.ast.stmt.ContinueStatement;
import org.codehaus.groovy.ast.stmt.DoWhileStatement;
import org.codehaus.groovy.ast.stmt.EmptyStatement;
import org.codehaus.groovy.ast.stmt.ExpressionStatement;
import org.codehaus.groovy.ast.stmt.ForStatement;
import org.codehaus.groovy.ast.stmt.IfStatement;
import org.codehaus.groovy.ast.stmt.ReturnStatement;
import org.codehaus.groovy.ast.stmt.Statement;
import org.codehaus.groovy.ast.stmt.SwitchStatement;
import org.codehaus.groovy.ast.stmt.SynchronizedStatement;
import org.codehaus.groovy.ast.stmt.ThrowStatement;
import org.codehaus.groovy.ast.stmt.TryCatchStatement;
import org.codehaus.groovy.ast.stmt.WhileStatement;
import org.codehaus.groovy.classgen.BytecodeExpression;
import org.codehaus.groovy.classgen.GeneratorContext;
import org.codehaus.groovy.control.CompilationFailedException;
import org.codehaus.groovy.control.CompilePhase;
import org.codehaus.groovy.control.SourceUnit;
import org.codehaus.groovy.syntax.Token;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
/**
* This customizer allows securing source code by controlling what code constructs are permitted.
* This is typically done when using Groovy for its scripting or domain specific language (DSL) features.
* For example, if you only want to allow arithmetic operations in a groovy shell,
* you can configure this customizer to restrict package imports, method calls and so on.
* <p>
* Most of the security customization options found in this class work with either <i>allowed</i> or <i>disallowed</i> lists.
* This means that, for a single option, you can set an allowed list OR a disallowed list, but not both.
* You can mix allowed/disallowed strategies for different options.
* For example, you can have an allowed import list and a disallowed tokens list.
* <p>
* The recommended way of securing shells is to use allowed lists because it is guaranteed that future features of the
* Groovy language won't be accidentally allowed unless explicitly added to the allowed list.
* Using disallowed lists, you can limit the features of the language constructs supported by your shell by opting
* out, but new language features are then implicitly also available and this may not be desirable.
* The implication is that you might need to update your configuration with each new release.
* <p>
* If neither an allowed list nor a disallowed list is set, then everything is permitted.
* <p>
* Combinations of import and star import constraints are authorized as long as you use the same type of list for both.
* For example, you may use an import allowed list and a star import allowed list together, but you cannot use an import
* allowed list with a star import disallowed list. Static imports are handled separately, meaning that disallowing an
* import <b>does not</b> prevent from allowing a static import.
* <p>
* Eventually, if the features provided here are not sufficient, you may implement custom AST filtering handlers, either
* implementing the {@link StatementChecker} interface or {@link ExpressionChecker} interface then register your
* handlers thanks to the {@link #addExpressionCheckers(ExpressionChecker...)}
* and {@link #addStatementCheckers(StatementChecker...)}
* methods.
* <p>
* Here is an example of usage. We will create a groovy classloader which only supports arithmetic operations and imports
* the {@code java.lang.Math} classes by default.
*
* <pre>
* final ImportCustomizer imports = new ImportCustomizer().addStaticStars('java.lang.Math') // add static import of java.lang.Math
* final SecureASTCustomizer secure = new SecureASTCustomizer()
* secure.with {
* closuresAllowed = false
* methodDefinitionAllowed = false
*
* allowedImports = []
* allowedStaticImports = []
* allowedStaticStarImports = ['java.lang.Math'] // only java.lang.Math is allowed
*
* allowedTokens = [
* PLUS,
* MINUS,
* MULTIPLY,
* DIVIDE,
* REMAINDER,
* POWER,
* PLUS_PLUS,
* MINUS_MINUS,
* COMPARE_EQUAL,
* COMPARE_NOT_EQUAL,
* COMPARE_LESS_THAN,
* COMPARE_LESS_THAN_EQUAL,
* COMPARE_GREATER_THAN,
* COMPARE_GREATER_THAN_EQUAL,
* ].asImmutable()
*
* allowedConstantTypesClasses = [
* Integer,
* Float,
* Long,
* Double,
* BigDecimal,
* Integer.TYPE,
* Long.TYPE,
* Float.TYPE,
* Double.TYPE
* ].asImmutable()
*
* allowedReceiversClasses = [
* Math,
* Integer,
* Float,
* Double,
* Long,
* BigDecimal
* ].asImmutable()
* }
* CompilerConfiguration config = new CompilerConfiguration()
* config.addCompilationCustomizers(imports, secure)
* GroovyClassLoader loader = new GroovyClassLoader(this.class.classLoader, config)
* </pre>
* <p>
* Note: {@code SecureASTCustomizer} allows you to lock down the grammar of scripts but by itself isn't intended
* to be the complete solution of all security issues when running scripts on the JVM. You might also want to
* consider setting the {@code groovy.grape.enable} System property to false, augmenting use of the customizer
* with additional techniques, and following standard security principles for JVM applications.
* <p>
* For more information, please read:
* <li><a href="https://melix.github.io/blog/2015/03/sandboxing.html">Improved sandboxing of Groovy scripts</a></li>
* <li><a href="https://www.oracle.com/java/technologies/javase/seccodeguide.html">Oracle's Secure Coding Guidelines</a></li>
* <li><a href="https://snyk.io/blog/10-java-security-best-practices/">10 Java security best practices</a></li>
* <li><a href="https://www.infoworld.com/article/2076837/twelve-rules-for-developing-more-secure-java-code.html">Thirteen rules for developing secure Java applications</a></li>
* <li><a href="https://www.guardrails.io/blog/12-java-security-best-practices/">12 Java Security Best Practices</a></li>
*
* @since 1.8.0
*/
public class SecureASTCustomizer extends CompilationCustomizer {
private boolean isPackageAllowed = true;
private boolean isClosuresAllowed = true;
private boolean isMethodDefinitionAllowed = true;
// imports
private List<String> allowedImports;
private List<String> disallowedImports;
// static imports
private List<String> allowedStaticImports;
private List<String> disallowedStaticImports;
// star imports
private List<String> allowedStarImports;
private List<String> disallowedStarImports;
// static star imports
private List<String> allowedStaticStarImports;
private List<String> disallowedStaticStarImports;
// indirect import checks
// if set to true, then security rules on imports will also be applied on classnodes.
// Direct instantiation of classes without imports will therefore also fail if this option is enabled
private boolean isIndirectImportCheckEnabled;
// statements
private List<Class<? extends Statement>> allowedStatements;
private List<Class<? extends Statement>> disallowedStatements;
private final List<StatementChecker> statementCheckers = new LinkedList<>();
// expressions
private List<Class<? extends Expression>> allowedExpressions;
private List<Class<? extends Expression>> disallowedExpressions;
private final List<ExpressionChecker> expressionCheckers = new LinkedList<>();
// tokens from Types
private List<Integer> allowedTokens;
private List<Integer> disallowedTokens;
// constant types
private List<String> allowedConstantTypes;
private List<String> disallowedConstantTypes;
// receivers
private List<String> allowedReceivers;
private List<String> disallowedReceivers;
public SecureASTCustomizer() {
super(CompilePhase.CANONICALIZATION);
}
public boolean isMethodDefinitionAllowed() {
return isMethodDefinitionAllowed;
}
public void setMethodDefinitionAllowed(final boolean methodDefinitionAllowed) {
isMethodDefinitionAllowed = methodDefinitionAllowed;
}
public boolean isPackageAllowed() {
return isPackageAllowed;
}
public boolean isClosuresAllowed() {
return isClosuresAllowed;
}
public void setClosuresAllowed(final boolean closuresAllowed) {
isClosuresAllowed = closuresAllowed;
}
public void setPackageAllowed(final boolean packageAllowed) {
isPackageAllowed = packageAllowed;
}
public List<String> getDisallowedImports() {
return disallowedImports;
}
/**
* Legacy alias for {@link #getDisallowedImports()}
*/
@Deprecated
public List<String> getImportsBlacklist() {
return getDisallowedImports();
}
public void setDisallowedImports(final List<String> disallowedImports) {
if (allowedImports != null || allowedStarImports != null) {
throw new IllegalArgumentException("You are not allowed to set both an allowed list and a disallowed list");
}
this.disallowedImports = disallowedImports;
}
/**
* Legacy alias for {@link #setDisallowedImports(List)}
*/
@Deprecated
public void setImportsBlacklist(final List<String> disallowedImports) {
setDisallowedImports(disallowedImports);
}
public List<String> getAllowedImports() {
return allowedImports;
}
/**
* Legacy alias for {@link #getAllowedImports()}
*/
@Deprecated
public List<String> getImportsWhitelist() {
return getAllowedImports();
}
public void setAllowedImports(final List<String> allowedImports) {
if (disallowedImports != null || disallowedStarImports != null) {
throw new IllegalArgumentException("You are not allowed to set both an allowed list and a disallowed list");
}
this.allowedImports = allowedImports;
}
/**
* Legacy alias for {@link #setAllowedImports(List)}
*/
@Deprecated
public void setImportsWhitelist(final List<String> allowedImports) {
setAllowedImports(allowedImports);
}
public List<String> getDisallowedStarImports() {
return disallowedStarImports;
}
/**
* Legacy alias for {@link #getDisallowedStarImports()}
*/
@Deprecated
public List<String> getStarImportsBlacklist() {
return getDisallowedStarImports();
}
public void setDisallowedStarImports(final List<String> disallowedStarImports) {
if (allowedImports != null || allowedStarImports != null) {
throw new IllegalArgumentException("You are not allowed to set both an allowed list and a disallowed list");
}
this.disallowedStarImports = normalizeStarImports(disallowedStarImports);
if (this.disallowedImports == null) disallowedImports = Collections.emptyList();
}
/**
* Legacy alias for {@link #setDisallowedStarImports(List)}
*/
@Deprecated
public void setStarImportsBlacklist(final List<String> disallowedStarImports) {
setDisallowedStarImports(disallowedStarImports);
}
public List<String> getAllowedStarImports() {
return allowedStarImports;
}
/**
* Legacy alias for {@link #getAllowedStarImports()}
*/
@Deprecated
public List<String> getStarImportsWhitelist() {
return getAllowedStarImports();
}
public void setAllowedStarImports(final List<String> allowedStarImports) {
if (disallowedImports != null || disallowedStarImports != null) {
throw new IllegalArgumentException("You are not allowed to set both an allowed list and a disallowed list");
}
this.allowedStarImports = normalizeStarImports(allowedStarImports);
if (this.allowedImports == null) allowedImports = Collections.emptyList();
}
/**
* Legacy alias for {@link #setAllowedStarImports(List)}
*/
@Deprecated
public void setStarImportsWhitelist(final List<String> allowedStarImports) {
setAllowedStarImports(allowedStarImports);
}
private static List<String> normalizeStarImports(List<String> starImports) {
List<String> result = new ArrayList<>(starImports.size());
for (String starImport : starImports) {
if (starImport.endsWith(".*")) {
result.add(starImport);
} else if (starImport.endsWith("**")) {
result.add(starImport.replaceFirst("\\*+$", ""));
} else if (starImport.endsWith(".")) {
result.add(starImport + "*");
} else {
result.add(starImport + ".*");
}
}
return Collections.unmodifiableList(result);
}
public List<String> getDisallowedStaticImports() {
return disallowedStaticImports;
}
/**
* Legacy alias for {@link #getDisallowedStaticImports()}
*/
@Deprecated
public List<String> getStaticImportsBlacklist() {
return getDisallowedStaticImports();
}
public void setDisallowedStaticImports(final List<String> disallowedStaticImports) {
if (allowedStaticImports != null || allowedStaticStarImports != null) {
throw new IllegalArgumentException("You are not allowed to set both an allowed list and a disallowed list");
}
this.disallowedStaticImports = disallowedStaticImports;
}
/**
* Legacy alias for {@link #setDisallowedStaticImports(List)}
*/
@Deprecated
public void setStaticImportsBlacklist(final List<String> disallowedStaticImports) {
setDisallowedStaticImports(disallowedStaticImports);
}
public List<String> getAllowedStaticImports() {
return allowedStaticImports;
}
/**
* Legacy alias for {@link #getAllowedStaticImports()}
*/
@Deprecated
public List<String> getStaticImportsWhitelist() {
return getAllowedStaticImports();
}
public void setAllowedStaticImports(final List<String> allowedStaticImports) {
if (disallowedStaticImports != null || disallowedStaticStarImports != null) {
throw new IllegalArgumentException("You are not allowed to set both an allowed list and a disallowed list");
}
this.allowedStaticImports = allowedStaticImports;
}
/**
* Legacy alias for {@link #setAllowedStaticImports(List)}
*/
@Deprecated
public void setStaticImportsWhitelist(final List<String> allowedStaticImports) {
setAllowedStaticImports(allowedStaticImports);
}
public List<String> getDisallowedStaticStarImports() {
return disallowedStaticStarImports;
}
/**
* Legacy alias for {@link #getDisallowedStaticStarImports()}
*/
@Deprecated
public List<String> getStaticStarImportsBlacklist() {
return getDisallowedStaticStarImports();
}
public void setDisallowedStaticStarImports(final List<String> disallowedStaticStarImports) {
if (allowedStaticImports != null || allowedStaticStarImports != null) {
throw new IllegalArgumentException("You are not allowed to set both an allowed list and a disallowed list");
}
this.disallowedStaticStarImports = normalizeStarImports(disallowedStaticStarImports);
if (this.disallowedStaticImports == null) this.disallowedStaticImports = Collections.emptyList();
}
/**
* Legacy alias for {@link #setDisallowedStaticStarImports(List)}
*/
@Deprecated
public void setStaticStarImportsBlacklist(final List<String> disallowedStaticStarImports) {
setDisallowedStaticStarImports(disallowedStaticStarImports);
}
public List<String> getAllowedStaticStarImports() {
return allowedStaticStarImports;
}
/**
* Legacy alias for {@link #getAllowedStaticStarImports()}
*/
@Deprecated
public List<String> getStaticStarImportsWhitelist() {
return getAllowedStaticStarImports();
}
public void setAllowedStaticStarImports(final List<String> allowedStaticStarImports) {
if (disallowedStaticImports != null || disallowedStaticStarImports != null) {
throw new IllegalArgumentException("You are not allowed to set both an allowed list and a disallowed list");
}
this.allowedStaticStarImports = normalizeStarImports(allowedStaticStarImports);
if (this.allowedStaticImports == null) this.allowedStaticImports = Collections.emptyList();
}
/**
* Legacy alias for {@link #setAllowedStaticStarImports(List)}
*/
@Deprecated
public void setStaticStarImportsWhitelist(final List<String> allowedStaticStarImports) {
setAllowedStaticStarImports(allowedStaticStarImports);
}
public List<Class<? extends Expression>> getDisallowedExpressions() {
return disallowedExpressions;
}
/**
* Legacy alias for {@link #getDisallowedExpressions()}
*/
@Deprecated
public List<Class<? extends Expression>> getExpressionsBlacklist() {
return getDisallowedExpressions();
}
public void setDisallowedExpressions(final List<Class<? extends Expression>> disallowedExpressions) {
if (allowedExpressions != null) {
throw new IllegalArgumentException("You are not allowed to set both an allowed list and a disallowed list");
}
this.disallowedExpressions = disallowedExpressions;
}
/**
* Legacy alias for {@link #setDisallowedExpressions(List)}
*/
@Deprecated
public void setExpressionsBlacklist(final List<Class<? extends Expression>> disallowedExpressions) {
setDisallowedExpressions(disallowedExpressions);
}
public List<Class<? extends Expression>> getAllowedExpressions() {
return allowedExpressions;
}
/**
* Legacy alias for {@link #getAllowedExpressions()}
*/
@Deprecated
public List<Class<? extends Expression>> getExpressionsWhitelist() {
return getAllowedExpressions();
}
public void setAllowedExpressions(final List<Class<? extends Expression>> allowedExpressions) {
if (disallowedExpressions != null) {
throw new IllegalArgumentException("You are not allowed to set both an allowed list and a disallowed list");
}
this.allowedExpressions = allowedExpressions;
}
/**
* Legacy alias for {@link #setAllowedExpressions(List)}
*/
@Deprecated
public void setExpressionsWhitelist(final List<Class<? extends Expression>> allowedExpressions) {
setAllowedExpressions(allowedExpressions);
}
public List<Class<? extends Statement>> getDisallowedStatements() {
return disallowedStatements;
}
/**
* Legacy alias for {@link #getDisallowedStatements()}
*/
@Deprecated
public List<Class<? extends Statement>> getStatementsBlacklist() {
return getDisallowedStatements();
}
public void setDisallowedStatements(final List<Class<? extends Statement>> disallowedStatements) {
if (allowedStatements != null) {
throw new IllegalArgumentException("You are not allowed to set both an allowed list and a disallowed list");
}
this.disallowedStatements = disallowedStatements;
}
/**
* Legacy alias for {@link #setDisallowedStatements(List)}
*/
@Deprecated
public void setStatementsBlacklist(final List<Class<? extends Statement>> disallowedStatements) {
setDisallowedStatements(disallowedStatements);
}
public List<Class<? extends Statement>> getAllowedStatements() {
return allowedStatements;
}
/**
* Legacy alias for {@link #getAllowedStatements()}
*/
@Deprecated
public List<Class<? extends Statement>> getStatementsWhitelist() {
return getAllowedStatements();
}
public void setAllowedStatements(final List<Class<? extends Statement>> allowedStatements) {
if (disallowedStatements != null) {
throw new IllegalArgumentException("You are not allowed to set both an allowed list and a disallowed list");
}
this.allowedStatements = allowedStatements;
}
/**
* Legacy alias for {@link #setAllowedStatements(List)}
*/
@Deprecated
public void setStatementsWhitelist(final List<Class<? extends Statement>> allowedStatements) {
setAllowedStatements(allowedStatements);
}
public boolean isIndirectImportCheckEnabled() {
return isIndirectImportCheckEnabled;
}
/**
* Set this option to true if you want your import rules to be checked against every class node. This means that if
* someone uses a fully qualified class name, then it will also be checked against the import rules, preventing, for
* example, instantiation of classes without imports thanks to FQCN.
*
* @param indirectImportCheckEnabled set to true to enable indirect checks
*/
public void setIndirectImportCheckEnabled(final boolean indirectImportCheckEnabled) {
isIndirectImportCheckEnabled = indirectImportCheckEnabled;
}
public List<Integer> getDisallowedTokens() {
return disallowedTokens;
}
/**
* Legacy alias for {@link #getDisallowedTokens()}
*/
@Deprecated
public List<Integer> getTokensBlacklist() {
return getDisallowedTokens();
}
/**
* Sets the list of tokens which are not permitted.
*
* @param disallowedTokens the tokens. The values of the tokens must be those of {@link org.codehaus.groovy.syntax.Types}
*/
public void setDisallowedTokens(final List<Integer> disallowedTokens) {
if (allowedTokens != null) {
throw new IllegalArgumentException("You are not allowed to set both an allowed list and a disallowed list");
}
this.disallowedTokens = disallowedTokens;
}
/**
* Legacy alias for {@link #setDisallowedTokens(List)}.
*/
@Deprecated
public void setTokensBlacklist(final List<Integer> disallowedTokens) {
setDisallowedTokens(disallowedTokens);
}
public List<Integer> getAllowedTokens() {
return allowedTokens;
}
/**
* Legacy alias for {@link #getAllowedTokens()}
*/
@Deprecated
public List<Integer> getTokensWhitelist() {
return getAllowedTokens();
}
/**
* Sets the list of tokens which are permitted.
*
* @param allowedTokens the tokens. The values of the tokens must be those of {@link org.codehaus.groovy.syntax.Types}
*/
public void setAllowedTokens(final List<Integer> allowedTokens) {
if (disallowedTokens != null) {
throw new IllegalArgumentException("You are not allowed to set both an allowed list and a disallowed list");
}
this.allowedTokens = allowedTokens;
}
/**
* Legacy alias for {@link #setAllowedTokens(List)}
*/
@Deprecated
public void setTokensWhitelist(final List<Integer> allowedTokens) {
setAllowedTokens(allowedTokens);
}
public void addStatementCheckers(StatementChecker... checkers) {
statementCheckers.addAll(Arrays.asList(checkers));
}
public void addExpressionCheckers(ExpressionChecker... checkers) {
expressionCheckers.addAll(Arrays.asList(checkers));
}
public List<String> getDisallowedConstantTypes() {
return disallowedConstantTypes;
}
/**
* Legacy alias for {@link #getDisallowedConstantTypes()}
*/
@Deprecated
public List<String> getConstantTypesBlackList() {
return getDisallowedConstantTypes();
}
public void setConstantTypesBlackList(final List<String> constantTypesBlackList) {
if (allowedConstantTypes != null) {
throw new IllegalArgumentException("You are not allowed to set both an allowed list and a disallowed list");
}
this.disallowedConstantTypes = constantTypesBlackList;
}
public List<String> getAllowedConstantTypes() {
return allowedConstantTypes;
}
/**
* Legacy alias for {@link #getAllowedStatements()}
*/
@Deprecated
public List<String> getConstantTypesWhiteList() {
return getAllowedConstantTypes();
}
public void setAllowedConstantTypes(final List<String> allowedConstantTypes) {
if (disallowedConstantTypes != null) {
throw new IllegalArgumentException("You are not allowed to set both an allowed list and a disallowed list");
}
this.allowedConstantTypes = allowedConstantTypes;
}
/**
* Legacy alias for {@link #setAllowedConstantTypes(List)}
*/
@Deprecated
public void setConstantTypesWhiteList(final List<String> allowedConstantTypes) {
setAllowedConstantTypes(allowedConstantTypes);
}
/**
* An alternative way of setting constant types.
*
* @param allowedConstantTypes a list of classes.
*/
public void setAllowedConstantTypesClasses(final List<Class> allowedConstantTypes) {
List<String> values = new LinkedList<>();
for (Class aClass : allowedConstantTypes) {
values.add(aClass.getName());
}
setConstantTypesWhiteList(values);
}
/**
* Legacy alias for {@link #setAllowedConstantTypesClasses(List)}
*/
@Deprecated
public void setConstantTypesClassesWhiteList(final List<Class> allowedConstantTypes) {
setAllowedConstantTypesClasses(allowedConstantTypes);
}
/**
* An alternative way of setting constant types.
*
* @param disallowedConstantTypes a list of classes.
*/
public void setDisallowedConstantTypesClasses(final List<Class> disallowedConstantTypes) {
List<String> values = new LinkedList<>();
for (Class aClass : disallowedConstantTypes) {
values.add(aClass.getName());
}
setConstantTypesBlackList(values);
}
/**
* Legacy alias for {@link #setDisallowedConstantTypesClasses(List)}
*/
@Deprecated
public void setConstantTypesClassesBlackList(final List<Class> disallowedConstantTypes) {
setDisallowedConstantTypesClasses(disallowedConstantTypes);
}
public List<String> getDisallowedReceivers() {
return disallowedReceivers;
}
/**
* Legacy alias for {@link #getDisallowedReceivers()}
*/
@Deprecated
public List<String> getReceiversBlackList() {
return getDisallowedReceivers();
}
/**
* Sets the list of classes which deny method calls.
*
* Please note that since Groovy is a dynamic language, and
* this class performs a static type check, it will be relatively
* simple to bypass any disallowed list unless the disallowed receivers list contains, at
* a minimum, Object, Script, GroovyShell, and Eval. Additionally,
* it is necessary to also have MethodPointerExpression in the
* disallowed expressions list for the disallowed receivers list to function
* as a security check.
*
* @param disallowedReceivers the list of refused classes, as fully qualified names
*/
public void setDisallowedReceivers(final List<String> disallowedReceivers) {
if (allowedReceivers != null) {
throw new IllegalArgumentException("You are not allowed to set both an allowed list and a disallowed list");
}
this.disallowedReceivers = disallowedReceivers;
}
/**
* Legacy alias for {@link #setDisallowedReceivers(List)}
*/
@Deprecated
public void setReceiversBlackList(final List<String> disallowedReceivers) {
setDisallowedReceivers(disallowedReceivers);
}
/**
* An alternative way of setting {@link #setDisallowedReceivers(java.util.List) receiver classes}.
*
* @param disallowedReceivers a list of classes.
*/
public void setDisallowedReceiversClasses(final List<Class> disallowedReceivers) {
List<String> values = new LinkedList<>();
for (Class aClass : disallowedReceivers) {
values.add(aClass.getName());
}
setReceiversBlackList(values);
}
/**
* Legacy alias for {@link #setDisallowedReceiversClasses(List)}.
*/
@Deprecated
public void setReceiversClassesBlackList(final List<Class> disallowedReceivers) {
setDisallowedReceiversClasses(disallowedReceivers);
}
public List<String> getAllowedReceivers() {
return allowedReceivers;
}
/**
* Legacy alias for {@link #getAllowedReceivers()}
*/
@Deprecated
public List<String> getReceiversWhiteList() {
return getAllowedReceivers();
}
/**
* Sets the list of classes which may accept method calls.
*
* @param allowedReceivers the list of accepted classes, as fully qualified names
*/
public void setAllowedReceivers(final List<String> allowedReceivers) {
if (disallowedReceivers != null) {
throw new IllegalArgumentException("You are not allowed to set both an allowed list and a disallowed list");
}
this.allowedReceivers = allowedReceivers;
}
/**
* Legacy alias for {@link #setAllowedReceivers(List)}
*/
@Deprecated
public void setReceiversWhiteList(final List<String> allowedReceivers) {
if (disallowedReceivers != null) {
throw new IllegalArgumentException("You are not allowed to set both an allowed list and a disallowed list");
}
this.allowedReceivers = allowedReceivers;
}
/**
* An alternative way of setting {@link #setReceiversWhiteList(java.util.List) receiver classes}.
*
* @param allowedReceivers a list of classes.
*/
public void setAllowedReceiversClasses(final List<Class> allowedReceivers) {
List<String> values = new LinkedList<>();
for (Class aClass : allowedReceivers) {
values.add(aClass.getName());
}
setReceiversWhiteList(values);
}
/**
* Legacy alias for {@link #setAllowedReceiversClasses(List)}
*/
@Deprecated
public void setReceiversClassesWhiteList(final List<Class> allowedReceivers) {
setAllowedReceiversClasses(allowedReceivers);
}
@Override
public void call(final SourceUnit source, final GeneratorContext context, final ClassNode classNode) throws CompilationFailedException {
ModuleNode ast = source.getAST();
if (!isPackageAllowed && ast.getPackage() != null) {
throw new SecurityException("Package definitions are not allowed");
}
checkMethodDefinitionAllowed(classNode);
// verify imports
if (disallowedImports != null || allowedImports != null || disallowedStarImports != null || allowedStarImports != null) {
for (ImportNode importNode : ast.getImports()) {
assertImportIsAllowed(importNode.getClassName());
}
for (ImportNode importNode : ast.getStarImports()) {
assertStarImportIsAllowed(importNode.getPackageName() + "*");
}
}
// verify static imports
if (disallowedStaticImports != null || allowedStaticImports != null || disallowedStaticStarImports != null || allowedStaticStarImports != null) {
for (Map.Entry<String, ImportNode> entry : ast.getStaticImports().entrySet()) {
final String className = entry.getValue().getClassName();
assertStaticImportIsAllowed(entry.getKey(), className);
}
for (Map.Entry<String, ImportNode> entry : ast.getStaticStarImports().entrySet()) {
final String className = entry.getValue().getClassName();
assertStaticImportIsAllowed(entry.getKey(), className);
}
}
GroovyCodeVisitor visitor = createGroovyCodeVisitor();
ast.getStatementBlock().visit(visitor);
for (ClassNode clNode : ast.getClasses()) {
if (clNode!=classNode) {
checkMethodDefinitionAllowed(clNode);
for (MethodNode methodNode : clNode.getMethods()) {
if (!methodNode.isSynthetic() && methodNode.getCode() != null) {
methodNode.getCode().visit(visitor);
}
}
}
}
List<MethodNode> methods = filterMethods(classNode);
if (isMethodDefinitionAllowed) {
for (MethodNode method : methods) {
if (method.getDeclaringClass() == classNode && method.getCode() != null) {
method.getCode().visit(visitor);
}
}
}
}
protected GroovyCodeVisitor createGroovyCodeVisitor() {
return new SecuringCodeVisitor();
}
protected void checkMethodDefinitionAllowed(ClassNode owner) {
if (isMethodDefinitionAllowed) return;
List<MethodNode> methods = filterMethods(owner);
if (!methods.isEmpty()) throw new SecurityException("Method definitions are not allowed");
}
protected static List<MethodNode> filterMethods(ClassNode owner) {
List<MethodNode> result = new LinkedList<>();
List<MethodNode> methods = owner.getMethods();
for (MethodNode method : methods) {
if (method.getDeclaringClass() == owner && !method.isSynthetic()) {
if (("main".equals(method.getName()) || "run".equals(method.getName())) && method.isScriptBody()) continue;
result.add(method);
}
}
return result;
}
protected void assertStarImportIsAllowed(final String packageName) {
if (allowedStarImports != null && !(allowedStarImports.contains(packageName)
|| allowedStarImports.stream().filter(it -> it.endsWith(".")).anyMatch(packageName::startsWith))) {
throw new SecurityException("Importing [" + packageName + "] is not allowed");
}
if (disallowedStarImports != null && (disallowedStarImports.contains(packageName)
|| disallowedStarImports.stream().filter(it -> it.endsWith(".")).anyMatch(packageName::startsWith))) {
throw new SecurityException("Importing [" + packageName + "] is not allowed");
}
}
protected void assertImportIsAllowed(final String className) {
if (allowedImports != null || allowedStarImports != null) {
if (allowedImports != null && allowedImports.contains(className)) {
return;
}