|
| 1 | +import { describe, expect, it, vi } from "vitest"; |
| 2 | +import { |
| 3 | + type DarwinMountEntry, |
| 4 | + isMacosAppTranslocationPath, |
| 5 | + isMacosPackagedUnsafeBundleLocation, |
| 6 | + isMacosPathOnReadOnlyNonRootMountFromTable, |
| 7 | + parseDarwinMountTable, |
| 8 | + type ReadDarwinMountTable, |
| 9 | +} from "./macos-packaged-install-guard"; |
| 10 | + |
| 11 | +describe("isMacosAppTranslocationPath", () => { |
| 12 | + it.each([ |
| 13 | + { |
| 14 | + case: "appPath is translocated", |
| 15 | + appPath: |
| 16 | + "/private/var/folders/yf/xx/AppTranslocation/C6283C3C-9D6E-4D81-A7D5-8BA2567ED486/d/PostHog Code.app/Contents/Resources/app.asar", |
| 17 | + exePath: "/Applications/PostHog Code.app/Contents/MacOS/PostHog Code", |
| 18 | + expected: true, |
| 19 | + }, |
| 20 | + { |
| 21 | + case: "exePath is translocated", |
| 22 | + appPath: "/Applications/PostHog Code.app/Contents/Resources/app.asar", |
| 23 | + exePath: |
| 24 | + "/private/var/folders/yf/xx/AppTranslocation/C6283C3C/d/PostHog Code.app/Contents/MacOS/PostHog Code", |
| 25 | + expected: true, |
| 26 | + }, |
| 27 | + { |
| 28 | + case: "neither path is translocated (/Applications)", |
| 29 | + appPath: "/Applications/PostHog Code.app/Contents/Resources/app.asar", |
| 30 | + exePath: "/Applications/PostHog Code.app/Contents/MacOS/PostHog Code", |
| 31 | + expected: false, |
| 32 | + }, |
| 33 | + { |
| 34 | + case: "neither path is translocated (/Users)", |
| 35 | + appPath: "/Users/dev/PostHog Code.app/Contents/Resources/app.asar", |
| 36 | + exePath: "/Users/dev/PostHog Code.app/Contents/MacOS/PostHog Code", |
| 37 | + expected: false, |
| 38 | + }, |
| 39 | + ])("$case → $expected", ({ appPath, exePath, expected }) => { |
| 40 | + expect(isMacosAppTranslocationPath(appPath, exePath)).toBe(expected); |
| 41 | + }); |
| 42 | +}); |
| 43 | + |
| 44 | +describe("parseDarwinMountTable", () => { |
| 45 | + it.each<{ |
| 46 | + case: string; |
| 47 | + input: string; |
| 48 | + expected: DarwinMountEntry[]; |
| 49 | + }>([ |
| 50 | + { |
| 51 | + case: "standard macOS mount lines", |
| 52 | + input: `/dev/disk3s1s1 on / (apfs, sealed, local, read-only, journaled) |
| 53 | +/dev/disk7s1 on /Volumes/My Dmg (apfs, local, read-only, journaled) |
| 54 | +/dev/disk5s1 on /Volumes/Writable (apfs, local, journaled) |
| 55 | +`, |
| 56 | + expected: [ |
| 57 | + { |
| 58 | + mountPoint: "/", |
| 59 | + options: "apfs, sealed, local, read-only, journaled", |
| 60 | + }, |
| 61 | + { |
| 62 | + mountPoint: "/Volumes/My Dmg", |
| 63 | + options: "apfs, local, read-only, journaled", |
| 64 | + }, |
| 65 | + { mountPoint: "/Volumes/Writable", options: "apfs, local, journaled" }, |
| 66 | + ], |
| 67 | + }, |
| 68 | + { |
| 69 | + case: "mount point name contains ' (' — anchors to trailing options", |
| 70 | + input: |
| 71 | + "/dev/disk9s1 on /Volumes/My Backup (2) (apfs, local, read-only, journaled)\n", |
| 72 | + expected: [ |
| 73 | + { |
| 74 | + mountPoint: "/Volumes/My Backup (2)", |
| 75 | + options: "apfs, local, read-only, journaled", |
| 76 | + }, |
| 77 | + ], |
| 78 | + }, |
| 79 | + ])("parses: $case", ({ input, expected }) => { |
| 80 | + expect(parseDarwinMountTable(input)).toEqual(expected); |
| 81 | + }); |
| 82 | +}); |
| 83 | + |
| 84 | +describe("isMacosPathOnReadOnlyNonRootMountFromTable", () => { |
| 85 | + const baseTable = `/dev/disk3s1s1 on / (apfs, sealed, local, read-only, journaled) |
| 86 | +/dev/disk7s1 on /Volumes/ReadOnlyVol (apfs, local, read-only, journaled) |
| 87 | +/dev/disk5s1 on /Volumes/Writable (apfs, local, journaled) |
| 88 | +`; |
| 89 | + const nestedTable = `/dev/x on / (apfs, read-only) |
| 90 | +/dev/y on /Volumes/RW (apfs, local, journaled) |
| 91 | +/dev/z on /Volumes/RW/nested (apfs, local, read-only) |
| 92 | +`; |
| 93 | + |
| 94 | + it.each([ |
| 95 | + { |
| 96 | + case: "path under read-only / is ignored (Users)", |
| 97 | + table: baseTable, |
| 98 | + path: "/Users/me/app", |
| 99 | + expected: false, |
| 100 | + }, |
| 101 | + { |
| 102 | + case: "path under read-only / is ignored (Applications)", |
| 103 | + table: baseTable, |
| 104 | + path: "/Applications/Foo.app", |
| 105 | + expected: false, |
| 106 | + }, |
| 107 | + { |
| 108 | + case: "read-only non-root volume", |
| 109 | + table: baseTable, |
| 110 | + path: "/Volumes/ReadOnlyVol/PostHog Code.app/Contents/MacOS/PostHog Code", |
| 111 | + expected: true, |
| 112 | + }, |
| 113 | + { |
| 114 | + case: "writable non-root volume", |
| 115 | + table: baseTable, |
| 116 | + path: "/Volumes/Writable/out/PostHog Code.app/Contents/MacOS/PostHog Code", |
| 117 | + expected: false, |
| 118 | + }, |
| 119 | + { |
| 120 | + case: "nested read-only mount wins over writable parent", |
| 121 | + table: nestedTable, |
| 122 | + path: "/Volumes/RW/nested/app", |
| 123 | + expected: true, |
| 124 | + }, |
| 125 | + { |
| 126 | + case: "writable parent wins when no deeper match", |
| 127 | + table: nestedTable, |
| 128 | + path: "/Volumes/RW/other/app", |
| 129 | + expected: false, |
| 130 | + }, |
| 131 | + ])("$case → $expected", ({ table, path, expected }) => { |
| 132 | + expect(isMacosPathOnReadOnlyNonRootMountFromTable(path, table)).toBe( |
| 133 | + expected, |
| 134 | + ); |
| 135 | + }); |
| 136 | +}); |
| 137 | + |
| 138 | +describe("isMacosPackagedUnsafeBundleLocation", () => { |
| 139 | + const writableMountTable = `/dev/disk3s1s1 on / (apfs, sealed, local, read-only, journaled) |
| 140 | +/dev/disk5s1 on /Volumes/build (apfs, local, journaled) |
| 141 | +/dev/disk6s1 on /Applications (apfs, local, journaled) |
| 142 | +`; |
| 143 | + const readOnlyMountTable = `/dev/disk3s1s1 on / (apfs, sealed, local, read-only, journaled) |
| 144 | +/dev/disk7s1 on /Volumes/ReadOnlyVol (apfs, local, read-only, journaled) |
| 145 | +`; |
| 146 | + |
| 147 | + it.each<{ |
| 148 | + case: string; |
| 149 | + appPath: string; |
| 150 | + exePath: string; |
| 151 | + readMountTable: ReadDarwinMountTable; |
| 152 | + expected: boolean; |
| 153 | + }>([ |
| 154 | + { |
| 155 | + case: "translocated bundle", |
| 156 | + appPath: |
| 157 | + "/private/var/.../AppTranslocation/UUID/d/PostHog Code.app/Contents/Resources/app.asar", |
| 158 | + exePath: "/Applications/PostHog Code.app/Contents/MacOS/PostHog Code", |
| 159 | + readMountTable: () => writableMountTable, |
| 160 | + expected: true, |
| 161 | + }, |
| 162 | + { |
| 163 | + case: "ordinary non-translocated path on a writable mount", |
| 164 | + appPath: |
| 165 | + "/Volumes/build/out/PostHog Code.app/Contents/Resources/app.asar", |
| 166 | + exePath: |
| 167 | + "/Volumes/build/out/PostHog Code.app/Contents/MacOS/PostHog Code", |
| 168 | + readMountTable: () => writableMountTable, |
| 169 | + expected: false, |
| 170 | + }, |
| 171 | + { |
| 172 | + case: "bundle on a read-only non-root volume", |
| 173 | + appPath: |
| 174 | + "/Volumes/ReadOnlyVol/PostHog Code.app/Contents/Resources/app.asar", |
| 175 | + exePath: |
| 176 | + "/Volumes/ReadOnlyVol/PostHog Code.app/Contents/MacOS/PostHog Code", |
| 177 | + readMountTable: () => readOnlyMountTable, |
| 178 | + expected: true, |
| 179 | + }, |
| 180 | + { |
| 181 | + case: "mount table cannot be read (degrade to non-blocking)", |
| 182 | + appPath: "/Applications/PostHog Code.app/Contents/Resources/app.asar", |
| 183 | + exePath: "/Applications/PostHog Code.app/Contents/MacOS/PostHog Code", |
| 184 | + readMountTable: () => null, |
| 185 | + expected: false, |
| 186 | + }, |
| 187 | + ])("$case → $expected", ({ appPath, exePath, readMountTable, expected }) => { |
| 188 | + expect( |
| 189 | + isMacosPackagedUnsafeBundleLocation(appPath, exePath, readMountTable), |
| 190 | + ).toBe(expected); |
| 191 | + }); |
| 192 | + |
| 193 | + it("short-circuits on translocation without reading the mount table", () => { |
| 194 | + const readMountTable = vi.fn(() => writableMountTable); |
| 195 | + isMacosPackagedUnsafeBundleLocation( |
| 196 | + "/private/var/.../AppTranslocation/UUID/d/PostHog Code.app/Contents/Resources/app.asar", |
| 197 | + "/Applications/PostHog Code.app/Contents/MacOS/PostHog Code", |
| 198 | + readMountTable, |
| 199 | + ); |
| 200 | + expect(readMountTable).not.toHaveBeenCalled(); |
| 201 | + }); |
| 202 | +}); |
0 commit comments