@@ -61,18 +61,27 @@ public static void intercept(@Advice.AllArguments Object[] args, @Advice.Origin
6161 final Collection <ProtectionDomain > callers = walker .walk (StackCallerProtectionDomainChainExtractor .INSTANCE );
6262
6363 final String name = method .getName ();
64- boolean isMutating = name .equals ("copy" ) || name . equals ( " move" ) || name .equals ("write" ) || name .startsWith ("create" );
64+ boolean isMutating = name .equals ("move" ) || name .equals ("write" ) || name .startsWith ("create" );
6565 final boolean isDelete = isMutating == false ? name .startsWith ("delete" ) : false ;
6666
67- if (isMutating == false && isDelete == false && name .equals ("newByteChannel" ) == true ) {
68- if (args .length > 1 && args [1 ] instanceof OpenOption [] opts ) {
69- for (final OpenOption opt : opts ) {
70- if (opt != StandardOpenOption .READ ) {
71- isMutating = true ;
72- break ;
67+ String targetFilePath = null ;
68+ if (isMutating == false && isDelete == false ) {
69+ if (name .equals ("newByteChannel" ) == true ) {
70+ if (args .length > 1 && args [1 ] instanceof OpenOption [] opts ) {
71+ for (final OpenOption opt : opts ) {
72+ if (opt != StandardOpenOption .READ ) {
73+ isMutating = true ;
74+ break ;
75+ }
7376 }
74- }
7577
78+ }
79+ } else if (name .equals ("copy" ) == true ) {
80+ if (args .length > 1 && args [1 ] instanceof String pathStr ) {
81+ targetFilePath = Paths .get (pathStr ).toAbsolutePath ().toString ();
82+ } else if (args .length > 1 && args [1 ] instanceof Path path ) {
83+ targetFilePath = path .toAbsolutePath ().toString ();
84+ }
7685 }
7786 }
7887
@@ -85,6 +94,19 @@ public static void intercept(@Advice.AllArguments Object[] args, @Advice.Origin
8594 }
8695 }
8796
97+ // Handle Files.copy() separately to check read/write permissions properly
98+ if (method .getName ().equals ("copy" )) {
99+ if (!policy .implies (domain , new FilePermission (filePath , "read" ))) {
100+ throw new SecurityException ("Denied OPEN access to file: " + filePath + ", domain: " + domain );
101+ }
102+
103+ if (targetFilePath != null ) {
104+ if (!policy .implies (domain , new FilePermission (targetFilePath , "write" ))) {
105+ throw new SecurityException ("Denied OPEN access to file: " + targetFilePath + ", domain: " + domain );
106+ }
107+ }
108+ }
109+
88110 // File mutating operations
89111 if (isMutating && !policy .implies (domain , new FilePermission (filePath , "write" ))) {
90112 throw new SecurityException ("Denied WRITE access to file: " + filePath + ", domain: " + domain );
0 commit comments