1 // Copyright 2023 Code Intelligence GmbH 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package com.example; 16 17 import com.code_intelligence.jazzer.api.FuzzedDataProvider; 18 import java.io.Reader; 19 import java.io.StringReader; 20 import java.io.Writer; 21 import java.util.List; 22 import javax.script.Bindings; 23 import javax.script.ScriptContext; 24 import javax.script.ScriptEngine; 25 import javax.script.ScriptEngineFactory; 26 27 public class ScriptEngineInjection { 28 private final static ScriptEngine engine = new DummyScriptEngine(); 29 private final static ScriptContext context = new DummyScriptContext(); 30 insecureScriptEval(String input)31 private static void insecureScriptEval(String input) throws Exception { 32 engine.eval(new StringReader(input), context); 33 } 34 fuzzerTestOneInput(FuzzedDataProvider data)35 public static void fuzzerTestOneInput(FuzzedDataProvider data) throws Exception { 36 try { 37 insecureScriptEval(data.consumeRemainingAsAsciiString()); 38 } catch (Exception ignored) { 39 } 40 } 41 42 private static class DummyScriptEngine implements ScriptEngine { 43 @Override createBindings()44 public Bindings createBindings() { 45 return null; 46 } 47 48 @Override eval(String script)49 public Object eval(String script) { 50 return null; 51 } 52 53 @Override eval(Reader reader)54 public Object eval(Reader reader) { 55 return null; 56 } 57 58 @Override eval(String script, ScriptContext context)59 public Object eval(String script, ScriptContext context) { 60 return null; 61 } 62 63 @Override eval(Reader reader, ScriptContext context)64 public Object eval(Reader reader, ScriptContext context) { 65 return null; 66 } 67 68 @Override eval(String script, Bindings n)69 public Object eval(String script, Bindings n) { 70 return null; 71 } 72 73 @Override eval(Reader reader, Bindings n)74 public Object eval(Reader reader, Bindings n) { 75 return null; 76 } 77 78 @Override get(String key)79 public Object get(String key) { 80 return null; 81 } 82 83 @Override getBindings(int scope)84 public Bindings getBindings(int scope) { 85 return null; 86 } 87 88 @Override getContext()89 public ScriptContext getContext() { 90 return null; 91 } 92 93 @Override getFactory()94 public ScriptEngineFactory getFactory() { 95 return null; 96 } 97 98 @Override put(String key, Object value)99 public void put(String key, Object value) {} 100 101 @Override setBindings(Bindings bindings, int scope)102 public void setBindings(Bindings bindings, int scope) {} 103 104 @Override setContext(ScriptContext context)105 public void setContext(ScriptContext context) {} 106 DummyScriptEngine()107 public DummyScriptEngine() {} 108 } 109 110 private static class DummyScriptContext implements ScriptContext { 111 @Override setBindings(Bindings bindings, int scope)112 public void setBindings(Bindings bindings, int scope) {} 113 114 @Override getBindings(int scope)115 public Bindings getBindings(int scope) { 116 return null; 117 } 118 119 @Override setAttribute(String name, Object value, int scope)120 public void setAttribute(String name, Object value, int scope) {} 121 122 @Override getAttribute(String name, int scope)123 public Object getAttribute(String name, int scope) { 124 return null; 125 } 126 127 @Override removeAttribute(String name, int scope)128 public Object removeAttribute(String name, int scope) { 129 return null; 130 } 131 132 @Override getAttribute(String name)133 public Object getAttribute(String name) { 134 return null; 135 } 136 137 @Override getAttributesScope(String name)138 public int getAttributesScope(String name) { 139 return 0; 140 } 141 142 @Override getWriter()143 public Writer getWriter() { 144 return null; 145 } 146 147 @Override getErrorWriter()148 public Writer getErrorWriter() { 149 return null; 150 } 151 152 @Override setWriter(Writer writer)153 public void setWriter(Writer writer) {} 154 155 @Override setErrorWriter(Writer writer)156 public void setErrorWriter(Writer writer) {} 157 158 @Override getReader()159 public Reader getReader() { 160 return null; 161 } 162 163 @Override setReader(Reader reader)164 public void setReader(Reader reader) {} 165 166 @Override getScopes()167 public List<Integer> getScopes() { 168 return null; 169 } 170 } 171 } 172