Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,27 @@
final Collection<ProtectionDomain> callers = walker.walk(StackCallerProtectionDomainChainExtractor.INSTANCE);

final String name = method.getName();
boolean isMutating = name.equals("copy") || name.equals("move") || name.equals("write") || name.startsWith("create");
boolean isMutating = name.equals("move") || name.equals("write") || name.startsWith("create");
final boolean isDelete = isMutating == false ? name.startsWith("delete") : false;

if (isMutating == false && isDelete == false && name.equals("newByteChannel") == true) {
if (args.length > 1 && args[1] instanceof OpenOption[] opts) {
for (final OpenOption opt : opts) {
if (opt != StandardOpenOption.READ) {
isMutating = true;
break;
String targetFilePath = null;

Check warning on line 67 in libs/agent-sm/agent/src/main/java/org/opensearch/javaagent/FileInterceptor.java

View check run for this annotation

Codecov / codecov/patch

libs/agent-sm/agent/src/main/java/org/opensearch/javaagent/FileInterceptor.java#L67

Added line #L67 was not covered by tests
if (isMutating == false && isDelete == false) {
if (name.equals("newByteChannel") == true) {
if (args.length > 1 && args[1] instanceof OpenOption[] opts) {
for (final OpenOption opt : opts) {
if (opt != StandardOpenOption.READ) {
isMutating = true;
break;

Check warning on line 74 in libs/agent-sm/agent/src/main/java/org/opensearch/javaagent/FileInterceptor.java

View check run for this annotation

Codecov / codecov/patch

libs/agent-sm/agent/src/main/java/org/opensearch/javaagent/FileInterceptor.java#L73-L74

Added lines #L73 - L74 were not covered by tests
}
}
}

}
} else if (name.equals("copy") == true) {
if (args.length > 1 && args[1] instanceof String pathStr) {
targetFilePath = Paths.get(pathStr).toAbsolutePath().toString();

Check warning on line 81 in libs/agent-sm/agent/src/main/java/org/opensearch/javaagent/FileInterceptor.java

View check run for this annotation

Codecov / codecov/patch

libs/agent-sm/agent/src/main/java/org/opensearch/javaagent/FileInterceptor.java#L81

Added line #L81 was not covered by tests
} else if (args.length > 1 && args[1] instanceof Path path) {
targetFilePath = path.toAbsolutePath().toString();

Check warning on line 83 in libs/agent-sm/agent/src/main/java/org/opensearch/javaagent/FileInterceptor.java

View check run for this annotation

Codecov / codecov/patch

libs/agent-sm/agent/src/main/java/org/opensearch/javaagent/FileInterceptor.java#L83

Added line #L83 was not covered by tests
}
}
}

Expand All @@ -85,6 +94,19 @@
}
}

// Handle Files.copy() separately to check read/write permissions properly
if (method.getName().equals("copy")) {
if (!policy.implies(domain, new FilePermission(filePath, "read"))) {
throw new SecurityException("Denied OPEN access to file: " + filePath + ", domain: " + domain);

Check warning on line 100 in libs/agent-sm/agent/src/main/java/org/opensearch/javaagent/FileInterceptor.java

View check run for this annotation

Codecov / codecov/patch

libs/agent-sm/agent/src/main/java/org/opensearch/javaagent/FileInterceptor.java#L100

Added line #L100 was not covered by tests
}

if (targetFilePath != null) {
if (!policy.implies(domain, new FilePermission(targetFilePath, "write"))) {
throw new SecurityException("Denied OPEN access to file: " + targetFilePath + ", domain: " + domain);

Check warning on line 105 in libs/agent-sm/agent/src/main/java/org/opensearch/javaagent/FileInterceptor.java

View check run for this annotation

Codecov / codecov/patch

libs/agent-sm/agent/src/main/java/org/opensearch/javaagent/FileInterceptor.java#L105

Added line #L105 was not covered by tests
}
}
}

// File mutating operations
if (isMutating && !policy.implies(domain, new FilePermission(filePath, "write"))) {
throw new SecurityException("Denied WRITE access to file: " + filePath + ", domain: " + domain);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;

@SuppressWarnings("removal")
Expand Down Expand Up @@ -144,6 +145,10 @@ public void testCopy() throws Exception {

// Test copy operation
Files.copy(sourcePath, targetPath);
assertThrows(
SecurityException.class,
() -> Files.copy(sourcePath, tmpDir.getRoot().resolve("test-target-" + randomAlphaOfLength(8) + ".txt"))
);

// Verify copy
assertTrue("Target file should exist", Files.exists(targetPath));
Expand Down
Loading