This repository was archived by the owner on Jul 21, 2026. It is now read-only.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAether.java
More file actions
Latest commit
79 lines (65 loc) · 2.63 KB
/
Copy pathAether.java
File metadata and controls
79 lines (65 loc) · 2.63 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
packageaether;
importaether.lexer.Lexer;
importjava.io.BufferedReader;
importjava.io.IOException;
importjava.io.InputStreamReader;
importjava.nio.file.Files;
importjava.nio.file.Paths;
publicclassAether {
privatestaticfinalInterpreterinterpreter = newInterpreter();
// Define your custom language extension here
privatestaticfinalStringFILE_EXTENSION = ".aether";
staticvoidmain(String[] args) throwsIOException {
if (args.length > 1) {
System.out.println("Usage: java -jar target/java-project-aether-1.0-SNAPSHOT.jar [script" + FILE_EXTENSION + "]");
System.exit(64);
} elseif (args.length == 1) {
runFile(args[0]);
} else {
runPrompt();
}
}
privatestaticvoidrunFile(Stringpath) throwsIOException {
// Enforce the custom file extension constraint
if (!path.endsWith(FILE_EXTENSION)) {
System.err.println("Error: Invalid file format. Aether source files must end with '" + FILE_EXTENSION + "'.");
System.exit(65); // EX_DATAERR: Input data was incorrect in some way
}
// 2. Define the base directory where your scripts are allowed
varbaseDir = Paths.get("scripts").toAbsolutePath().normalize();
// 3. Resolve the requested path against the base directory
vartargetPath = baseDir.resolve(path).normalize();
// 4. Vulnerability Check: Ensure the resolved path starts with the base directory
if (!targetPath.startsWith(baseDir)) {
thrownewSecurityException("Access denied: File path outside of allowed directory.");
}
// 5. Proceed with execution
varbytes = Files.readAllBytes(targetPath);
run(newString(bytes));
}
privatestaticvoidrunPrompt() throwsIOException {
varinput = newInputStreamReader(System.in);
varreader = newBufferedReader(input);
for (;;) {
System.out.print("aether> ");
varline = reader.readLine();
if (line == null) break;
run(line);
}
}
privatestaticvoidrun(Stringsource) {
if (source.trim().isEmpty()) return;
try {
varlexer = newLexer(source);
vartokens = lexer.scanTokens();
varparser = newParser(tokens);
varstatements = parser.parse();
if (statements == null || statements.contains(null)) {
return;
}
interpreter.interpret(statements);
} catch (Exceptione) {
System.err.println("Error: " + e.getMessage());
}
}
}