Files Owner Principal Probe

Check file identity before acting on it: read the owner UserPrincipal without following symlinks, then decide whether the sensitive read may proceed.

JavaSecurity Boundaries

Code

snippet
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.attribute.UserPrincipal;

public class OwnershipProbe {

    public static void main(String[] args) throws IOException {
        Path secret = Files.createTempFile("credentials-", ".txt");
        Files.writeString(secret, "token=change-me");

        // NOFOLLOW_LINKS:不解析符号链接,防止路径被链接调包
        UserPrincipal owner = Files.getOwner(secret, LinkOption.NOFOLLOW_LINKS);
        System.out.println("owner = " + owner.getName());
        System.out.println("is symlink = " + Files.isSymbolicLink(secret));

        // 探测通过后再做敏感操作
        System.out.println("readable = " + Files.isReadable(secret));
        Files.deleteIfExists(secret);
    }
}

How it works

Count the class OwnershipProbe as the entire surface area of Files Owner Principal Probe.

Using it

Running the file prints labeled results such as owner =, is symlink =, readable = — each operation’s effect is visible in the output.

Key statements

UserPrincipal owner = Files.getOwner(secret, LinkOption.NOFOLLOW_LINKS);
Path secret = Files.createTempFile("credentials-", ".txt");
Files.writeString(secret, "token=change-me");

Interactive Tools for This Topic

FAQ

What does Files Owner Principal Probe print when run?
No setup needed: run the file and look for labeled lines like `owner =`, `is symlink =`, `readable =` in the output.