Skip to content

Commit

Permalink
Merge pull request #229 from lexize/file_api_fix
Browse files Browse the repository at this point in the history
Quick fix of FileAPI so it always uses actual path
  • Loading branch information
UnlikePaladin committed Jun 22, 2024
2 parents 35d1b59 + 7141071 commit bb6edda
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions common/src/main/java/org/figuramc/figura/lua/api/FileAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
@LuaTypeDoc(name = "FileAPI", value = "file")
public class FileAPI {
private final Avatar parent;
private static final Path rootFolderPath = FiguraMod.getFiguraDirectory().resolve("data").toAbsolutePath()
.normalize();
private static final String WRITE_NOT_ALLOWED = "You are only allowed to write in the data folder! Anything else is read only!";

public FileAPI(Avatar parent) {
Expand All @@ -41,10 +39,15 @@ private Path securityCheck(String path) {
return p;
}

private static Path dataPath() {
return FiguraMod.getFiguraDirectory().resolve("data").toAbsolutePath()
.normalize();
}

private Path relativizePath(String path) {
Path p = Path.of(path);
if (p.isAbsolute()) return p.normalize();
return rootFolderPath.resolve(path).toAbsolutePath().normalize();
return dataPath().resolve(path).toAbsolutePath().normalize();
}

@LuaWhitelist
Expand All @@ -61,7 +64,7 @@ public boolean isPathAllowed(@LuaNotNil String path) {
}

public boolean isPathAllowed(Path path) {
return !Files.isSymbolicLink(path) && path.toAbsolutePath().startsWith(rootFolderPath);
return !Files.isSymbolicLink(path) && path.toAbsolutePath().startsWith(dataPath());
}

@LuaWhitelist
Expand Down Expand Up @@ -152,7 +155,7 @@ public FiguraInputStream openReadStream(@LuaNotNil String path) {
public FiguraOutputStream openWriteStream(@LuaNotNil String path) {
try {
Path p = securityCheck(path);
if (!p.startsWith(rootFolderPath)) {
if (!p.startsWith(dataPath())) {
throw new LuaError(WRITE_NOT_ALLOWED);
}
File f = p.toFile();
Expand Down Expand Up @@ -226,7 +229,7 @@ public void writeString(@LuaNotNil String path, @LuaNotNil String data, String e
)
public boolean mkdir(@LuaNotNil String path) {
Path p = securityCheck(path);
if (!p.startsWith(rootFolderPath)) {
if (!p.startsWith(dataPath())) {
throw new LuaError(WRITE_NOT_ALLOWED);
}
File f = p.toFile();
Expand All @@ -244,7 +247,7 @@ public boolean mkdir(@LuaNotNil String path) {
)
public boolean mkdirs(@LuaNotNil String path) {
Path p = securityCheck(path);
if (!p.startsWith(rootFolderPath)) {
if (!p.startsWith(dataPath())) {
throw new LuaError(WRITE_NOT_ALLOWED);
}
File f = p.toFile();
Expand All @@ -262,7 +265,7 @@ public boolean mkdirs(@LuaNotNil String path) {
)
public boolean delete(@LuaNotNil String path) {
Path p = securityCheck(path);
if (!p.startsWith(rootFolderPath)) {
if (!p.startsWith(dataPath())) {
throw new LuaError(WRITE_NOT_ALLOWED);
}
File f = p.toFile();
Expand Down

0 comments on commit bb6edda

Please sign in to comment.