|
| 1 | +/* |
| 2 | + * The MIT License |
| 3 | + * |
| 4 | + * Copyright (c) 2018, CloudBees, Inc. |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in |
| 14 | + * all copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | + * THE SOFTWARE. |
| 23 | + */ |
| 24 | + |
| 25 | +package org.jenkinsci.plugins.scriptsecurity.sandbox.groovy; |
| 26 | + |
| 27 | +import com.google.common.collect.ImmutableList; |
| 28 | +import groovy.lang.Grab; |
| 29 | +import groovy.transform.ASTTest; |
| 30 | +import org.codehaus.groovy.ast.AnnotatedNode; |
| 31 | +import org.codehaus.groovy.ast.AnnotationNode; |
| 32 | +import org.codehaus.groovy.ast.ClassCodeVisitorSupport; |
| 33 | +import org.codehaus.groovy.ast.ClassNode; |
| 34 | +import org.codehaus.groovy.classgen.GeneratorContext; |
| 35 | +import org.codehaus.groovy.control.CompilationFailedException; |
| 36 | +import org.codehaus.groovy.control.CompilePhase; |
| 37 | +import org.codehaus.groovy.control.SourceUnit; |
| 38 | +import org.codehaus.groovy.control.customizers.CompilationCustomizer; |
| 39 | + |
| 40 | +import java.lang.annotation.Annotation; |
| 41 | +import java.util.List; |
| 42 | + |
| 43 | +public class RejectASTTransformsCustomizer extends CompilationCustomizer { |
| 44 | + private static final List<Class<? extends Annotation>> BLOCKED_TRANSFORMS = ImmutableList.of(ASTTest.class, Grab.class); |
| 45 | + |
| 46 | + public RejectASTTransformsCustomizer() { |
| 47 | + super(CompilePhase.CONVERSION); |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public void call(final SourceUnit source, GeneratorContext context, ClassNode classNode) throws CompilationFailedException { |
| 52 | + new RejectASTTransformsVisitor(source).visitClass(classNode); |
| 53 | + } |
| 54 | + |
| 55 | + private static class RejectASTTransformsVisitor extends ClassCodeVisitorSupport { |
| 56 | + private SourceUnit source; |
| 57 | + |
| 58 | + public RejectASTTransformsVisitor(SourceUnit source) { |
| 59 | + this.source = source; |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + protected SourceUnit getSourceUnit() { |
| 64 | + return source; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * If the node is annotated with one of the blocked transform annotations, throw a security exception. |
| 69 | + * |
| 70 | + * @param node the node to process |
| 71 | + */ |
| 72 | + @Override |
| 73 | + public void visitAnnotations(AnnotatedNode node) { |
| 74 | + for (AnnotationNode an : node.getAnnotations()) { |
| 75 | + for (Class<? extends Annotation> blockedAnnotation : BLOCKED_TRANSFORMS) { |
| 76 | + if (blockedAnnotation.getSimpleName().equals(an.getClassNode().getName())) { |
| 77 | + throw new SecurityException("Annotation " + blockedAnnotation.getSimpleName() + " cannot be used in the sandbox."); |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | +} |
0 commit comments