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.
Code
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
Unix Permissions CalculatorConvert between octal and symbolic Unix file permissions and explain read/write/execute plus setuid, setgid, and sticky bits.Argon2id Hash InfoEstimate Argon2id password-hash latency and memory usage, and check your parameters against OWASP 2024 minimum and recommended settings.bcrypt Cost EstimatorEstimate bcrypt cost factor latency and brute-force resistance, and get a recommended cost for your target hash time and attacker throughput.CORS Header Policy DebuggerSimulate browser CORS preflight and credential policies against server response headers to pinpoint blocked origins, methods, or headers and fix them.
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.
Report a problem with this exampleOpens a pre-filled issue on GitHub.