MASTG-DEMO-0098: References to File Access in WebViews with radare2
Download MASTG-DEMO-0098 IPA Open MASTG-DEMO-0098 Folder Build MASTG-DEMO-0098 IPA
Sample¶
This sample demonstrates a WKWebView with the undocumented allowFileAccessFromFileURLs property enabled, allowing JavaScript to read other local file:// URLs even when loadFileURL:allowingReadAccessToURL: is intentionally granting broad access to the demoRoot directory. This is required because in the end, the sandbox always applies and this API is the one controlling it.
| MastgTest.swift | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 | |
The sample sets up two sibling directories under cachesDirectory/demoRoot/:
app/: containsindex.html(the loaded page) andapi-key.txt(a sensitive secret).other/: containsother.html(a page in a separate directory).
Using KVC, allowFileAccessFromFileURLs is set to true and allowUniversalAccessFromFileURLs is left false. The WebView loads index.html with loadFileURL(_:allowingReadAccessTo:), intentionally passing demoRoot to the allowingReadAccessTo parameter. This means that JavaScript running in the WebView should be able to read any file in demoRoot (including index.html and api-key.txt).
index.html contains JavaScript that demonstrates three access methods:
fetch("./api-key.txt"): readsapi-key.txtfrom the sameapp/directory.XMLHttpRequestto../other/other.html: reads a file from the siblingother/directory.<iframe src="../other/other.html">: embeds the sibling file directly.
The allowingReadAccessTo: demoRoot choice is intentional: it shows that simply setting allowFileAccessFromFileURLs = true is not enough for JavaScript to reach any local file:// URL via fetch or XHR. The allowingReadAccessTo parameter is not the target of this demo but it's required to demonstrate the impact in terms of the sandbox boundary. If the WebView were loaded with allowingReadAccessTo: indexURL instead, then JavaScript would only be able to read index.html and not api-key.txt, even with allowFileAccessFromFileURLs = true.
The app logs would show:
0x1110180c0 - [PID=709] WebProcessProxy::checkURLReceivedFromWebProcess: Received an unexpected URL from the web process
0x103870c18 - [pageProxyID=6, webPageID=7, PID=709] WebPageProxy::Ignoring request to load this main resource because it is outside the sandbox
Steps¶
- Extract the app binary from the IPA ( Obtaining and Extracting Apps).
- Run radare2 (iOS) (radare2) using the provided script to search for references to the relevant WebView methods.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | |
1 2 3 | |
The script searches for:
- References to
allowFileAccessFromFileURLsandallowUniversalAccessFromFileURLs. - References to the
loadFileURL:allowingReadAccessToURL:method.
Observation¶
The output shows all cross-references and disassembled snippets.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | |
Evaluation¶
The test fails because the binary contains a reference to allowFileAccessFromFileURLs that is set to true, and the binary also calls loadFileURL:allowingReadAccessToURL:.
In sym.func.100004ad8 (the showWebView function), around 0x100004b54, we can see that the app sets allowFileAccessFromFileURLs = true. This follows the classic Swift-to-Objective-C bridging pattern:
mov w0, 1loads booleantrue, then bridges it to anNSNumberviaobjc_retainAutoreleasedReturnValue.- At
0x100004b64,"allowFileAccessFromFileURLs"is constructed as anNSString. setValue:forKey:is called viafcn.10000c780at0x100004b8c.
Immediately after, around 0x100004b9c, the app sets allowUniversalAccessFromFileURLs = false using the same pattern:
mov w0, 0loads booleanfalse, bridged toNSNumber.- At
0x100004bac,"allowUniversalAccessFromFileURLs"is constructed as anNSString. setValue:forKey:is called at0x100004bd0.
The actual call to loadFileURL:allowingReadAccessToURL: is inside the presenter.present completion closure, compiled into sym.func.1000050c0. Earlier in this function, around 0x100005130, the demoRoot static property is resolved and stored in x23:
0x100005130 adrp x1, segment.__DATA ; 0x100014000
0x100005134 add x1, x1, 0x340 ; demoRoot storage
0x100005138 mov x0, x21
0x10000513c bl sym.func.100007be4 ; resolve demoRoot static
0x100005140 mov x23, x0 ; x23 = demoRoot
Then the key sequence leading to the loadFileURL:allowingReadAccessToURL: call at 0x10000520c is:
0x1000051a4 adrp x1, segment.__DATA ; 0x100014000
0x1000051a8 add x1, x1, 0x300 ; indexURL storage
0x1000051ac mov x0, x21
0x1000051b0 bl sym.func.100007be4 ; resolve indexURL static
0x1000051b4 mov x1, x0 ; x1 = indexURL (passed directly)
0x1000051b8 mov x0, x22
0x1000051bc mov x2, x21
0x1000051c0 blr x27 ; initialize Swift URL in x22 from indexURL
0x1000051c8 bl Foundation.URL._bridgeToObjectiveC.NSURL ; bridge to NSURL
0x1000051cc mov x24, x0 ; x24 = NSURL(indexURL) [fileURL arg]
...
0x1000051d8 blr x28 ; destroy URL buffer
0x1000051dc mov x0, x22
0x1000051e0 mov x1, x23 ; x1 = x23 = demoRoot (from earlier)
0x1000051e4 mov x2, x21
0x1000051e8 blr x27 ; initialize Swift URL in x22 from demoRoot
0x1000051ec bl Foundation.URL._bridgeToObjectiveC.NSURL ; bridge to NSURL
0x1000051f0 mov x20, x0 ; x20 = NSURL(demoRoot) [readAccess arg]
...
0x100005204 mov x2, x24 ; fileURL = NSURL(indexURL)
0x100005208 mov x3, x20 ; allowingReadAccessTo = NSURL(demoRoot)
0x10000520c bl fcn.10000c680 ; loadFileURL:allowingReadAccessToURL:
x2 and x3 are produced from two different static properties. The first URL (x24) comes from the indexURL static (resolved at 0x1000051b0 from [segment.__DATA + 0x300]), while the second URL (x20) comes from the demoRoot static (resolved earlier at 0x10000513c from [segment.__DATA + 0x340] and stored in x23). This confirms that loadFileURL is called with indexURL as the first argument (the file to load) and demoRoot as the second argument (the read-access boundary), as written in the Swift source.
Because allowingReadAccessTo: demoRoot grants WebKit native file access to the entire demoRoot directory, and allowFileAccessFromFileURLs = true independently grants JavaScript the ability to issue fetch() and XMLHttpRequest calls to file:// URLs within that boundary, JavaScript running in the WebView can read both api-key.txt and other.html.
iOS Simulator vs. Physical Device
The iOS Simulator is more permissive than a physical device when it comes to file:// access in WebViews. On the Simulator, JavaScript can reach local files via fetch or XHR even when allowingReadAccessTo is restricted to a single file (e.g., indexURL). On a physical device, WebKit strictly enforces the sandbox boundary set by allowingReadAccessTo, so JavaScript can only access files within the directory (or file) specified by that parameter. Always validate WebView file-access behavior on a real device to get accurate results.