如果读取的文件不属于启动 JVM 的用户所有,我想显示警告。
在我的应用中,这个文件可以让程序写入任意文件。所以我想降低权限升级攻击的风险,如果管理员处理这个文件。
Files.getOwner(path)
retruns
UserPrincipal
代表文件所有者。但是我找不到一种独立于平台的方法来检查这个UserPrincipal
是否与当前用户匹配。
我可以用
System.getProperty("user.name")
获取当前用户并尝试匹配名称。但是可以操纵这些属性,例如,如果文件所有者是一个组,这将不起作用。
如果您已经有了文件所有者的委托人,问题仍然是如何可靠地让当前用户运行应用程序。
也许这个解决方案可以提供帮助?然而,它似乎利用了 Sprint 安全性:https://www.javaguides.net/2019/06/spring-security-get-current-logged-in-user-details.html
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
UserDetails userDetails = (UserDetails) authentication.getPrincipal();
// getUsername() - Returns the username used to authenticate the user.
System.out.println("User name: " + userDetails.getUsername());
// getAuthorities() - Returns the authorities granted to the user.
System.out.println("User has authorities: " + userDetails.getAuthorities());