1 package io.jawk;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 import java.util.Collection;
26 import java.util.List;
27 import io.jawk.backend.AVM;
28 import io.jawk.backend.SandboxedAVM;
29 import io.jawk.ext.JawkExtension;
30 import io.jawk.util.AwkSettings;
31 import io.jawk.util.ScriptSource;
32
33
34
35
36
37 public final class SandboxedAwk extends Awk {
38
39
40
41
42 public SandboxedAwk() {
43 super();
44 }
45
46
47
48
49
50
51 public SandboxedAwk(AwkSettings settings) {
52 super(settings);
53 }
54
55
56
57
58
59
60 public SandboxedAwk(Collection<? extends JawkExtension> extensions) {
61 super(extensions);
62 }
63
64
65
66
67
68
69
70 public SandboxedAwk(Collection<? extends JawkExtension> extensions, AwkSettings settings) {
71 super(extensions, settings);
72 }
73
74
75
76
77
78
79 @SafeVarargs
80 public SandboxedAwk(JawkExtension... extensions) {
81 super(extensions);
82 }
83
84 @Override
85 public AwkProgram compile(List<ScriptSource> scripts, boolean disableOptimizeParam) throws java.io.IOException {
86 return compileProgram(scripts, disableOptimizeParam, new SandboxedCompiledAwkProgram());
87 }
88
89 @Override
90 public AwkExpression compileExpression(String expression, boolean disableOptimizeParam) throws java.io.IOException {
91 return compileExpression(expression, disableOptimizeParam, new SandboxedCompiledAwkExpression());
92 }
93
94 @Override
95 protected boolean isSourceIncludeAllowed() {
96 return false;
97 }
98
99 @Override
100 public AVM createAvm() {
101 return createAvm(getSettings());
102 }
103
104 @Override
105 public AVM createAvm(boolean profilingEnabled) {
106 return createAvm(getSettings(), profilingEnabled);
107 }
108
109 @Override
110 protected AVM createAvm(AwkSettings settingsParam) {
111 return createAvm(settingsParam, false);
112 }
113
114 @Override
115 protected AVM createAvm(AwkSettings settingsParam, boolean profilingEnabled) {
116 return new SandboxedAVM(settingsParam, getExtensionInstances(), profilingEnabled);
117 }
118 }
119
120 final class SandboxedCompiledAwkProgram extends AwkProgram {
121 private static final long serialVersionUID = 1L;
122
123 @Override
124 public void printToFile(int numExprs, boolean append) {
125 deny("Output redirection is disabled in sandbox mode");
126 }
127
128 @Override
129 public void printToPipe(int numExprs) {
130 deny("Command execution through pipelines is disabled in sandbox mode");
131 }
132
133 @Override
134 public void printfToFile(int numExprs, boolean append) {
135 deny("Output redirection is disabled in sandbox mode");
136 }
137
138 @Override
139 public void printfToPipe(int numExprs) {
140 deny("Command execution through pipelines is disabled in sandbox mode");
141 }
142
143 @Override
144 public void system() {
145 deny("system() is disabled in sandbox mode");
146 }
147
148 @Override
149 public void useAsCommandInput() {
150 deny("Command execution through pipelines is disabled in sandbox mode");
151 }
152
153 @Override
154 public void useAsFileInput() {
155 deny("Input redirection is disabled in sandbox mode");
156 }
157
158 @Override
159 public void assignARGC() {
160 deny("Assigning to ARGC is disabled in sandbox mode");
161 }
162
163 @Override
164 public void argcOffset(int offset) {
165
166
167 }
168
169 @Override
170 public void argvOffset(int offset) {
171
172
173 }
174
175 private static void deny(String message) {
176 throw new AwkSandboxException(message);
177 }
178 }
179
180 final class SandboxedCompiledAwkExpression extends AwkExpression {
181 private static final long serialVersionUID = 1L;
182
183 @Override
184 public void printToFile(int numExprs, boolean append) {
185 deny("Output redirection is disabled in sandbox mode");
186 }
187
188 @Override
189 public void printToPipe(int numExprs) {
190 deny("Command execution through pipelines is disabled in sandbox mode");
191 }
192
193 @Override
194 public void printfToFile(int numExprs, boolean append) {
195 deny("Output redirection is disabled in sandbox mode");
196 }
197
198 @Override
199 public void printfToPipe(int numExprs) {
200 deny("Command execution through pipelines is disabled in sandbox mode");
201 }
202
203 @Override
204 public void system() {
205 deny("system() is disabled in sandbox mode");
206 }
207
208 @Override
209 public void useAsCommandInput() {
210 deny("Command execution through pipelines is disabled in sandbox mode");
211 }
212
213 @Override
214 public void useAsFileInput() {
215 deny("Input redirection is disabled in sandbox mode");
216 }
217
218 @Override
219 public void assignARGC() {
220 deny("Assigning to ARGC is disabled in sandbox mode");
221 }
222
223 @Override
224 public void argcOffset(int offset) {
225
226
227 }
228
229 @Override
230 public void argvOffset(int offset) {
231
232
233 }
234
235 private static void deny(String message) {
236 throw new AwkSandboxException(message);
237 }
238 }