Compare commits

...
Author SHA1 Message Date
liutianyou ed44234f37 Merge branch 'master' into alert-autofix-45 2025-05-20 12:09:49 +08:00
aias00 d85e285582 Merge branch 'master' into alert-autofix-45 2025-05-18 20:41:44 +08:00
aias00 5f29d948fa Merge branch 'master' into alert-autofix-45 2025-05-18 17:02:18 +08:00
Calvin e58e2d30a5 Merge branch 'master' into alert-autofix-45 2025-05-18 13:47:00 +08:00
liuhy c4a2a8f4ce fix alert 2025-05-17 15:44:50 +08:00
aias00andCopilot Autofix powered by AI 48cc09b94e add path validation for pluginservice
Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
Signed-off-by: aias00 <liuhongyu@apache.org>
2025-05-17 15:38:09 +08:00
@@ -224,7 +224,9 @@ public class PluginServiceImpl implements PluginService {
List<PluginItem> pluginItems = new ArrayList<>();
AtomicInteger pluginImplementationCount = new AtomicInteger(0);
try {
validateFilePath(jarFile);
URL jarUrl = new URL("file:" + jarFile.getAbsolutePath());
validateJarUrl(jarUrl);
try (URLClassLoader classLoader = new URLClassLoader(new URL[]{jarUrl}, this.getClass().getClassLoader());
JarFile jar = new JarFile(jarFile)) {
Enumeration<JarEntry> entries = jar.entries();
@@ -272,6 +274,35 @@ public class PluginServiceImpl implements PluginService {
return metadata;
}
/**
* Validate that the file resides within the expected directory.
*
* @param file the file to validate
*/
private void validateFilePath(File file) {
try {
String canonicalPath = file.getCanonicalPath();
String expectedDir = new File("plugin-lib").getCanonicalPath();
if (!canonicalPath.startsWith(expectedDir)) {
throw new CommonException("File is outside the allowed directory: " + canonicalPath);
}
} catch (IOException e) {
log.error("Error validating file path: {}", file.getAbsolutePath(), e);
throw new CommonException("Error validating file path: " + file.getAbsolutePath());
}
}
/**
* Validate that the URL uses the 'file:' protocol and does not point to an external resource.
*
* @param url the URL to validate
*/
private void validateJarUrl(URL url) {
if (!"file".equals(url.getProtocol())) {
throw new CommonException("Invalid URL protocol: " + url.getProtocol());
}
}
private void validateMetadata(PluginMetadata metadata) {
if (metadataDao.countPluginMetadataByName(metadata.getName()) != 0) {
throw new CommonException("A plugin named " + metadata.getName() + " already exists");