forked from swagger-api/swagger-codegen
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
configure security manager to write protect container filesystem
- Loading branch information
Showing
8 changed files
with
173 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
grant { | ||
permission java.security.AllPermission; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
grant { | ||
permission java.security.AllPermission; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
...agger-generator/src/main/java/io/swagger/v3/generator/util/FileAccessSecurityManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package io.swagger.v3.generator.util; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.security.AccessControlException; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
public class FileAccessSecurityManager extends SecurityManager { | ||
|
||
static Logger LOGGER = LoggerFactory.getLogger(FileAccessSecurityManager.class); | ||
|
||
static List<String> allowedDirectories= StringUtils.isBlank(System.getProperty("generatorWriteDirs")) ? new ArrayList<>() : Arrays.asList(System.getProperty("generatorWriteDirs").split(",")); | ||
|
||
@Override | ||
public void checkWrite(String file) { | ||
super.checkWrite(file); | ||
|
||
if (allowedDirectories.isEmpty()) { | ||
return; | ||
} | ||
if (!StringUtils.isBlank(file)) { | ||
boolean granted = false; | ||
|
||
for (String dir : allowedDirectories) { | ||
try { | ||
String dirPath = new File(dir).getCanonicalPath(); | ||
if (new File(file).getCanonicalPath().startsWith(dirPath)) { | ||
granted = true; | ||
} | ||
} catch (IOException e) { | ||
LOGGER.error("Exception getting absolute path for file {} and/or allowed dir ", file, e); | ||
throw new SecurityException("Exception getting absolute path for allowed dir " + dir + " and/or file " + file); | ||
} | ||
|
||
} | ||
if (!granted) { | ||
LOGGER.error("Blocking attempt to write to not allowed directory for file " + file); | ||
throw new AccessControlException("Error writing file to " + file + " as target dir is not allowed"); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters