|
| 1 | +package com.bencodez.simpleapi.file; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.nio.file.Files; |
| 5 | +import java.nio.file.LinkOption; |
| 6 | +import java.nio.file.Path; |
| 7 | +import java.nio.file.attribute.AclEntry; |
| 8 | +import java.nio.file.attribute.AclEntryPermission; |
| 9 | +import java.nio.file.attribute.AclEntryType; |
| 10 | +import java.nio.file.attribute.AclFileAttributeView; |
| 11 | +import java.nio.file.attribute.GroupPrincipal; |
| 12 | +import java.nio.file.attribute.PosixFilePermission; |
| 13 | +import java.nio.file.attribute.UserPrincipal; |
| 14 | +import java.util.EnumSet; |
| 15 | +import java.util.List; |
| 16 | +import java.util.Set; |
| 17 | + |
| 18 | +/** Enforces and verifies owner-only access for persisted private material. */ |
| 19 | +public final class PrivateFilePermissions { |
| 20 | + private static final Set<PosixFilePermission> OWNER_FILE = EnumSet.of( |
| 21 | + PosixFilePermission.OWNER_READ, PosixFilePermission.OWNER_WRITE); |
| 22 | + private static final Set<PosixFilePermission> OWNER_DIRECTORY = EnumSet.of( |
| 23 | + PosixFilePermission.OWNER_READ, PosixFilePermission.OWNER_WRITE, PosixFilePermission.OWNER_EXECUTE); |
| 24 | + private static final Set<AclEntryPermission> OWNER_ACL = EnumSet.allOf(AclEntryPermission.class); |
| 25 | + |
| 26 | + private PrivateFilePermissions() { } |
| 27 | + |
| 28 | + /** Enforces 0600-equivalent access, or fails when the provider cannot prove it. */ |
| 29 | + public static void ownerOnlyFile(Path path) throws IOException { |
| 30 | + enforce(path, OWNER_FILE); |
| 31 | + } |
| 32 | + |
| 33 | + /** Enforces 0700-equivalent access, or fails when the provider cannot prove it. */ |
| 34 | + public static void ownerOnlyDirectory(Path path) throws IOException { |
| 35 | + enforce(path, OWNER_DIRECTORY); |
| 36 | + } |
| 37 | + |
| 38 | + private static void enforce(Path path, Set<PosixFilePermission> permissions) throws IOException { |
| 39 | + if (path == null) throw new IllegalArgumentException("Private path is required"); |
| 40 | + if (Files.isSymbolicLink(path)) throw new IOException("Refusing symbolic link for private storage: " + path); |
| 41 | + try { |
| 42 | + Files.setPosixFilePermissions(path, permissions); |
| 43 | + if (!Files.getPosixFilePermissions(path, LinkOption.NOFOLLOW_LINKS).equals(permissions)) |
| 44 | + throw new IOException("Could not verify owner-only POSIX permissions for " + path); |
| 45 | + return; |
| 46 | + } catch (UnsupportedOperationException unsupported) { |
| 47 | + // A non-POSIX provider must expose an ACL that can be reduced and verified. |
| 48 | + } |
| 49 | + enforceWithAcl(path); |
| 50 | + } |
| 51 | + |
| 52 | + private static void enforceWithAcl(Path path) throws IOException { |
| 53 | + try { |
| 54 | + AclFileAttributeView view = Files.getFileAttributeView(path, AclFileAttributeView.class, LinkOption.NOFOLLOW_LINKS); |
| 55 | + if (view == null) throw new IOException("Owner-only private storage is unsupported for " + path); |
| 56 | + enforceWithAcl(view, path); |
| 57 | + } catch (UnsupportedOperationException unsupported) { |
| 58 | + throw new IOException("Owner-only private storage is unsupported for " + path, unsupported); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + /** Package-visible for deterministic ACL-provider tests. */ |
| 63 | + static void enforceWithAcl(AclFileAttributeView view, Path path) throws IOException { |
| 64 | + try { |
| 65 | + if (view == null) throw new IOException("Owner-only private storage is unsupported for " + path); |
| 66 | + UserPrincipal owner = view.getOwner(); |
| 67 | + if (owner == null || owner instanceof GroupPrincipal) |
| 68 | + throw new IOException("Private storage owner is not an individual account for " + path); |
| 69 | + AclEntry ownerEntry = AclEntry.newBuilder().setType(AclEntryType.ALLOW).setPrincipal(owner) |
| 70 | + .setPermissions(OWNER_ACL).build(); |
| 71 | + view.setAcl(List.of(ownerEntry)); |
| 72 | + List<AclEntry> acl = view.getAcl(); |
| 73 | + if (acl.size() != 1 || !isVerifiedOwnerEntry(acl.get(0), owner)) |
| 74 | + throw new IOException("Could not verify owner-only ACL permissions for " + path); |
| 75 | + } catch (UnsupportedOperationException unsupported) { |
| 76 | + throw new IOException("Owner-only private storage is unsupported for " + path, unsupported); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + private static boolean isVerifiedOwnerEntry(AclEntry entry, UserPrincipal owner) { |
| 81 | + return entry.type() == AclEntryType.ALLOW && owner.equals(entry.principal()) && entry.flags().isEmpty() |
| 82 | + && entry.permissions().equals(OWNER_ACL); |
| 83 | + } |
| 84 | +} |
0 commit comments