@capacitor/android 6.2.2 → 6.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/capacitor/build.gradle
CHANGED
|
@@ -91,6 +91,6 @@ dependencies {
|
|
|
91
91
|
androidTestImplementation "androidx.test.espresso:espresso-core:$androidxEspressoCoreVersion"
|
|
92
92
|
implementation "org.apache.cordova:framework:$cordovaAndroidVersion"
|
|
93
93
|
testImplementation 'org.json:json:20231013'
|
|
94
|
-
testImplementation 'org.mockito:mockito-
|
|
94
|
+
testImplementation 'org.mockito:mockito-core:5.14.1'
|
|
95
95
|
}
|
|
96
96
|
|
|
@@ -7,16 +7,20 @@ package com.getcapacitor;
|
|
|
7
7
|
import android.content.Context;
|
|
8
8
|
import android.content.res.AssetManager;
|
|
9
9
|
import android.net.Uri;
|
|
10
|
+
import android.os.Environment;
|
|
10
11
|
import android.util.TypedValue;
|
|
11
12
|
import java.io.File;
|
|
12
13
|
import java.io.FileInputStream;
|
|
14
|
+
import java.io.FileNotFoundException;
|
|
13
15
|
import java.io.IOException;
|
|
14
16
|
import java.io.InputStream;
|
|
17
|
+
import java.util.ArrayList;
|
|
15
18
|
import java.util.List;
|
|
16
19
|
|
|
17
20
|
public class AndroidProtocolHandler {
|
|
18
21
|
|
|
19
22
|
private Context context;
|
|
23
|
+
private List<File> appStorageRoots;
|
|
20
24
|
|
|
21
25
|
public AndroidProtocolHandler(Context context) {
|
|
22
26
|
this.context = context;
|
|
@@ -69,6 +73,125 @@ public class AndroidProtocolHandler {
|
|
|
69
73
|
return new FileInputStream(localFile);
|
|
70
74
|
}
|
|
71
75
|
|
|
76
|
+
/**
|
|
77
|
+
* Opens a file served from the base path the app is hosted from (see {@code setServerBasePath}).
|
|
78
|
+
* The request path is decoded by {@link Uri#getPath()}, so an encoded slash ({@code %2F}) can carry
|
|
79
|
+
* {@code ..} segments that the browser did not resolve. The resolved file must therefore sit inside
|
|
80
|
+
* the base path, or the request would read any file the app can read.
|
|
81
|
+
*
|
|
82
|
+
* @param basePath the directory the app is hosted from
|
|
83
|
+
* @param filePath the base path followed by the request path
|
|
84
|
+
* @throws FileNotFoundException if the resolved file is outside of the base path
|
|
85
|
+
*/
|
|
86
|
+
public InputStream openBasePathFile(String basePath, String filePath) throws IOException {
|
|
87
|
+
String base = new File(basePath).getCanonicalPath();
|
|
88
|
+
File file = new File(filePath).getCanonicalFile();
|
|
89
|
+
String resolved = file.getPath();
|
|
90
|
+
if (!resolved.equals(base) && !resolved.startsWith(base.endsWith(File.separator) ? base : base + File.separator)) {
|
|
91
|
+
throw new FileNotFoundException("File is outside of the base path: " + filePath);
|
|
92
|
+
}
|
|
93
|
+
return new FileInputStream(file);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Opens a file whose location was chosen by a {@link RouteProcessor}. The processor may map requests
|
|
98
|
+
* outside the base path on purpose, so containment is not checked here. The request path is decoded
|
|
99
|
+
* by {@link Uri#getPath()}, so an encoded slash ({@code %2F}) can still carry {@code ..} segments that
|
|
100
|
+
* the browser did not resolve and that a processor would pass on; such requests are refused.
|
|
101
|
+
*
|
|
102
|
+
* @param requestPath the decoded request path
|
|
103
|
+
* @param processedPath the file path returned by the route processor
|
|
104
|
+
* @throws FileNotFoundException if the request path contains a parent-directory segment
|
|
105
|
+
*/
|
|
106
|
+
public InputStream openRoutedFile(String requestPath, String processedPath) throws IOException {
|
|
107
|
+
for (String segment : requestPath.split("/")) {
|
|
108
|
+
if (segment.equals("..")) {
|
|
109
|
+
throw new FileNotFoundException("Parent-directory segment in request path: " + requestPath);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
return openFile(processedPath);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Opens a file requested through the {@link Bridge#CAPACITOR_FILE_START} proxy route.
|
|
117
|
+
* Unlike {@link #openFile(String)}, the resolved path must sit inside the app's own storage or
|
|
118
|
+
* shared/external storage, so the route cannot expose app-private data (shared_prefs, databases)
|
|
119
|
+
* or system files such as /proc or /etc.
|
|
120
|
+
*
|
|
121
|
+
* @param filePath the request path, starting with {@link Bridge#CAPACITOR_FILE_START}
|
|
122
|
+
* @throws IOException if the file cannot be opened or its location is not allowed
|
|
123
|
+
*/
|
|
124
|
+
public InputStream openProxiedFile(String filePath) throws IOException {
|
|
125
|
+
String realPath = filePath.startsWith(Bridge.CAPACITOR_FILE_START)
|
|
126
|
+
? filePath.substring(Bridge.CAPACITOR_FILE_START.length())
|
|
127
|
+
: filePath;
|
|
128
|
+
// Canonicalizing resolves ".." segments and symlinks; open the resolved file so the checked path is the read one.
|
|
129
|
+
File canonicalFile = new File(realPath).getCanonicalFile();
|
|
130
|
+
if (!isInAllowedLocation(canonicalFile)) {
|
|
131
|
+
throw new IOException("Path is outside the locations served by the file proxy: " + realPath);
|
|
132
|
+
}
|
|
133
|
+
return new FileInputStream(canonicalFile);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
private boolean isInAllowedLocation(File canonicalFile) throws IOException {
|
|
137
|
+
String path = canonicalFile.getPath();
|
|
138
|
+
for (File root : getAllowedRoots()) {
|
|
139
|
+
String rootPath = root.getCanonicalPath();
|
|
140
|
+
if (path.equals(rootPath) || path.startsWith(rootPath + File.separator)) {
|
|
141
|
+
return true;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
return false;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
@SuppressWarnings("deprecation")
|
|
148
|
+
/**
|
|
149
|
+
* The app's files and cache directories (Filesystem Data/Library/Cache, Camera temporary images). They do not
|
|
150
|
+
* change while the app runs, unlike the external storage roots, so they are looked up once.
|
|
151
|
+
*/
|
|
152
|
+
private List<File> getAppStorageRoots() {
|
|
153
|
+
List<File> roots = appStorageRoots;
|
|
154
|
+
if (roots == null) {
|
|
155
|
+
roots = new ArrayList<>();
|
|
156
|
+
addIfNotNull(roots, context.getFilesDir());
|
|
157
|
+
addIfNotNull(roots, context.getCacheDir());
|
|
158
|
+
appStorageRoots = roots;
|
|
159
|
+
}
|
|
160
|
+
return roots;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
private List<File> getAllowedRoots() {
|
|
164
|
+
List<File> roots = new ArrayList<>(getAppStorageRoots());
|
|
165
|
+
// Primary shared storage (Filesystem Documents/ExternalStorage, gallery and media picks)
|
|
166
|
+
addIfNotNull(roots, Environment.getExternalStorageDirectory());
|
|
167
|
+
// App-specific external dirs, and the root of every other volume (e.g. SD cards) they live on
|
|
168
|
+
List<File> externalDirs = new ArrayList<>();
|
|
169
|
+
for (File dir : context.getExternalFilesDirs(null)) {
|
|
170
|
+
externalDirs.add(dir);
|
|
171
|
+
}
|
|
172
|
+
for (File dir : context.getExternalCacheDirs()) {
|
|
173
|
+
externalDirs.add(dir);
|
|
174
|
+
}
|
|
175
|
+
for (File dir : externalDirs) {
|
|
176
|
+
if (dir == null) {
|
|
177
|
+
continue;
|
|
178
|
+
}
|
|
179
|
+
roots.add(dir);
|
|
180
|
+
String dirPath = dir.getPath();
|
|
181
|
+
int androidDirIndex = dirPath.indexOf("/Android/");
|
|
182
|
+
if (androidDirIndex > 0) {
|
|
183
|
+
roots.add(new File(dirPath.substring(0, androidDirIndex)));
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
return roots;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
private static void addIfNotNull(List<File> roots, File dir) {
|
|
190
|
+
if (dir != null) {
|
|
191
|
+
roots.add(dir);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
72
195
|
public InputStream openContentUrl(Uri uri) throws IOException {
|
|
73
196
|
Integer port = uri.getPort();
|
|
74
197
|
String baseUrl = uri.getScheme() + "://" + uri.getHost();
|
|
@@ -80,7 +203,10 @@ public class AndroidProtocolHandler {
|
|
|
80
203
|
InputStream stream = null;
|
|
81
204
|
try {
|
|
82
205
|
stream = context.getContentResolver().openInputStream(Uri.parse(realPath));
|
|
83
|
-
} catch (
|
|
206
|
+
} catch (RuntimeException e) {
|
|
207
|
+
// The target provider runs in this process and may throw unchecked exceptions for a malformed or
|
|
208
|
+
// unknown path (e.g. FileProvider throws IllegalArgumentException, older versions
|
|
209
|
+
// StringIndexOutOfBoundsException). Left uncaught they crash the app, so report "not found" instead.
|
|
84
210
|
Logger.error("Unable to open content URL: " + uri, e);
|
|
85
211
|
}
|
|
86
212
|
return stream;
|
|
@@ -371,10 +371,23 @@ public class Bridge {
|
|
|
371
371
|
return 0;
|
|
372
372
|
}
|
|
373
373
|
|
|
374
|
+
/**
|
|
375
|
+
* Whether the path targets the local file or content proxy routes.
|
|
376
|
+
*/
|
|
377
|
+
static boolean isLocalFilePath(String path) {
|
|
378
|
+
return path != null && (path.startsWith(CAPACITOR_FILE_START) || path.startsWith(CAPACITOR_CONTENT_START));
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
/**
|
|
382
|
+
* Whether the path targets one of the internal proxy routes (file, content, http interceptor).
|
|
383
|
+
*/
|
|
384
|
+
static boolean isInternalProxyPath(String path) {
|
|
385
|
+
return isLocalFilePath(path) || (path != null && path.startsWith(CAPACITOR_HTTP_INTERCEPTOR_START));
|
|
386
|
+
}
|
|
387
|
+
|
|
374
388
|
public boolean launchIntent(Uri url) {
|
|
375
|
-
// The proxy
|
|
376
|
-
|
|
377
|
-
if (path != null && path.startsWith(CAPACITOR_HTTP_INTERCEPTOR_START)) {
|
|
389
|
+
// The proxy routes return data at the app origin, so block navigating to them before plugins can allow it.
|
|
390
|
+
if (isInternalProxyPath(url.getPath())) {
|
|
378
391
|
return true;
|
|
379
392
|
}
|
|
380
393
|
|
|
@@ -25,6 +25,7 @@ import android.webkit.WebResourceRequest;
|
|
|
25
25
|
import android.webkit.WebResourceResponse;
|
|
26
26
|
import com.getcapacitor.plugin.util.CapacitorHttpUrlConnection;
|
|
27
27
|
import com.getcapacitor.plugin.util.HttpRequestHandler;
|
|
28
|
+
import java.io.ByteArrayInputStream;
|
|
28
29
|
import java.io.IOException;
|
|
29
30
|
import java.io.InputStream;
|
|
30
31
|
import java.net.HttpURLConnection;
|
|
@@ -171,10 +172,15 @@ public class WebViewLocalServer {
|
|
|
171
172
|
public WebResourceResponse shouldInterceptRequest(WebResourceRequest request) {
|
|
172
173
|
Uri loadingUrl = request.getUrl();
|
|
173
174
|
|
|
175
|
+
if (Bridge.isInternalProxyPath(loadingUrl.getPath()) && !isProxyRequestAllowed(request)) {
|
|
176
|
+
// Internal proxy routes are only for sub-resources requested at the app's own origin.
|
|
177
|
+
Logger.warn("Blocked request to internal proxy route: " + loadingUrl);
|
|
178
|
+
return buildBlockedResponse();
|
|
179
|
+
}
|
|
180
|
+
|
|
174
181
|
if (null != loadingUrl.getPath() && loadingUrl.getPath().startsWith(Bridge.CAPACITOR_HTTP_INTERCEPTOR_START)) {
|
|
175
|
-
// Only fetch/XHR should reach the proxy; a document would run remote content at the app origin.
|
|
176
182
|
boolean httpEnabled = bridge.getConfig().getPluginConfiguration("CapacitorHttp").getBoolean("enabled", false);
|
|
177
|
-
if (!httpEnabled
|
|
183
|
+
if (!httpEnabled) {
|
|
178
184
|
return null;
|
|
179
185
|
}
|
|
180
186
|
Logger.debug("Handling CapacitorHttp request: " + loadingUrl);
|
|
@@ -202,6 +208,47 @@ public class WebViewLocalServer {
|
|
|
202
208
|
}
|
|
203
209
|
}
|
|
204
210
|
|
|
211
|
+
/**
|
|
212
|
+
* Internal proxy routes (file, content, http interceptor) return data at the app origin, so they must only
|
|
213
|
+
* serve sub-resource requests (img, media, fetch/XHR) made to the app's own origin. A document would run
|
|
214
|
+
* the returned content with the app's privileges, and a request to any other origin has no business here.
|
|
215
|
+
*/
|
|
216
|
+
private boolean isProxyRequestAllowed(WebResourceRequest request) {
|
|
217
|
+
return isAppOrigin(request.getUrl()) && !isDocumentRequest(request);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
private boolean isAppOrigin(Uri uri) {
|
|
221
|
+
String localUrl = bridge.getLocalUrl();
|
|
222
|
+
if (localUrl == null || uri.getScheme() == null || uri.getHost() == null) {
|
|
223
|
+
return false;
|
|
224
|
+
}
|
|
225
|
+
Uri appOrigin = Uri.parse(localUrl);
|
|
226
|
+
return (
|
|
227
|
+
uri.getScheme().equalsIgnoreCase(appOrigin.getScheme()) &&
|
|
228
|
+
uri.getHost().equalsIgnoreCase(appOrigin.getHost()) &&
|
|
229
|
+
getEffectivePort(uri) == getEffectivePort(appOrigin)
|
|
230
|
+
);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* The WebView hands over request URLs without a default port, while the configured app URL may spell it out
|
|
235
|
+
* (e.g. server.url "http://host:80"), so a missing port is treated as the scheme's default.
|
|
236
|
+
*/
|
|
237
|
+
private static int getEffectivePort(Uri uri) {
|
|
238
|
+
int port = uri.getPort();
|
|
239
|
+
if (port != -1) {
|
|
240
|
+
return port;
|
|
241
|
+
}
|
|
242
|
+
String scheme = uri.getScheme();
|
|
243
|
+
if ("https".equalsIgnoreCase(scheme)) {
|
|
244
|
+
return 443;
|
|
245
|
+
}
|
|
246
|
+
if ("http".equalsIgnoreCase(scheme)) {
|
|
247
|
+
return 80;
|
|
248
|
+
}
|
|
249
|
+
return -1;
|
|
250
|
+
}
|
|
251
|
+
|
|
205
252
|
/** isForMainFrame() is false for an iframe and a fetch alike; only navigations send this header. */
|
|
206
253
|
private boolean isDocumentRequest(WebResourceRequest request) {
|
|
207
254
|
if (request.isForMainFrame()) {
|
|
@@ -221,8 +268,7 @@ public class WebViewLocalServer {
|
|
|
221
268
|
}
|
|
222
269
|
|
|
223
270
|
private boolean isLocalFile(Uri uri) {
|
|
224
|
-
|
|
225
|
-
return path.startsWith(capacitorContentStart) || path.startsWith(capacitorFileStart);
|
|
271
|
+
return Bridge.isLocalFilePath(uri.getPath());
|
|
226
272
|
}
|
|
227
273
|
|
|
228
274
|
private boolean isErrorUrl(Uri uri) {
|
|
@@ -317,7 +363,9 @@ public class WebViewLocalServer {
|
|
|
317
363
|
encoding = encodingParts[1].trim();
|
|
318
364
|
}
|
|
319
365
|
}
|
|
320
|
-
} else {
|
|
366
|
+
} else if (!"Access-Control-Allow-Origin".equalsIgnoreCase(entry.getKey())) {
|
|
367
|
+
// The target's CORS grant is not forwarded: the request is made natively (with the app's cookies), so a
|
|
368
|
+
// grant meant for the target's own callers would let another origin read the response.
|
|
321
369
|
responseHeaders.put(entry.getKey(), builder.toString());
|
|
322
370
|
}
|
|
323
371
|
}
|
|
@@ -340,26 +388,54 @@ public class WebViewLocalServer {
|
|
|
340
388
|
return new WebResourceResponse(mimeType, encoding, responseCode, reasonPhrase, responseHeaders, inputStream);
|
|
341
389
|
}
|
|
342
390
|
|
|
391
|
+
/**
|
|
392
|
+
* A fixed rejection for a blocked proxy route request. Returning null would let the WebView send the request
|
|
393
|
+
* to the network itself, which for a remote server.url puts the local path in a request to that server.
|
|
394
|
+
*/
|
|
395
|
+
private WebResourceResponse buildBlockedResponse() {
|
|
396
|
+
Map<String, String> headers = new HashMap<>();
|
|
397
|
+
headers.put("Content-Security-Policy", "sandbox; frame-ancestors 'none'");
|
|
398
|
+
headers.put("Cache-Control", "no-store");
|
|
399
|
+
return new WebResourceResponse("text/plain", "UTF-8", 403, "Forbidden", headers, new ByteArrayInputStream(new byte[0]));
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
/**
|
|
403
|
+
* Files served through the file/content proxy routes are data, not pages. If anything ends up rendering one,
|
|
404
|
+
* the sandbox keeps it inert and off the app origin, and it cannot be framed.
|
|
405
|
+
*/
|
|
406
|
+
void addProxyRouteSecurityHeaders(Uri uri, Map<String, String> responseHeaders) {
|
|
407
|
+
if (isLocalFile(uri)) {
|
|
408
|
+
responseHeaders.put("Content-Security-Policy", "sandbox; frame-ancestors 'none'");
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
|
|
343
412
|
private WebResourceResponse handleLocalRequest(WebResourceRequest request, PathHandler handler) {
|
|
344
413
|
String path = request.getUrl().getPath();
|
|
345
414
|
|
|
346
415
|
if (request.getRequestHeaders().get("Range") != null) {
|
|
347
416
|
InputStream responseStream = new LollipopLazyInputStream(handler, request);
|
|
348
417
|
String mimeType = getMimeType(path, responseStream);
|
|
349
|
-
Map<String, String> tempResponseHeaders = handler.getResponseHeaders();
|
|
418
|
+
Map<String, String> tempResponseHeaders = new LinkedHashMap<>(handler.getResponseHeaders());
|
|
350
419
|
int statusCode = 206;
|
|
351
420
|
try {
|
|
352
421
|
int totalRange = responseStream.available();
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
422
|
+
if (totalRange == -1) {
|
|
423
|
+
// No stream (missing or refused file): not a partial response.
|
|
424
|
+
statusCode = 404;
|
|
425
|
+
addProxyRouteSecurityHeaders(request.getUrl(), tempResponseHeaders);
|
|
426
|
+
} else {
|
|
427
|
+
String rangeString = request.getRequestHeaders().get("Range");
|
|
428
|
+
String[] parts = rangeString.split("=");
|
|
429
|
+
String[] streamParts = parts[1].split("-");
|
|
430
|
+
String fromRange = streamParts[0];
|
|
431
|
+
int range = totalRange - 1;
|
|
432
|
+
if (streamParts.length > 1) {
|
|
433
|
+
range = Integer.parseInt(streamParts[1]);
|
|
434
|
+
}
|
|
435
|
+
tempResponseHeaders.put("Accept-Ranges", "bytes");
|
|
436
|
+
tempResponseHeaders.put("Content-Range", "bytes " + fromRange + "-" + range + "/" + totalRange);
|
|
437
|
+
addProxyRouteSecurityHeaders(request.getUrl(), tempResponseHeaders);
|
|
360
438
|
}
|
|
361
|
-
tempResponseHeaders.put("Accept-Ranges", "bytes");
|
|
362
|
-
tempResponseHeaders.put("Content-Range", "bytes " + fromRange + "-" + range + "/" + totalRange);
|
|
363
439
|
} catch (IOException e) {
|
|
364
440
|
statusCode = 404;
|
|
365
441
|
}
|
|
@@ -377,12 +453,14 @@ public class WebViewLocalServer {
|
|
|
377
453
|
InputStream responseStream = new LollipopLazyInputStream(handler, request);
|
|
378
454
|
String mimeType = getMimeType(request.getUrl().getPath(), responseStream);
|
|
379
455
|
int statusCode = getStatusCode(responseStream, handler.getStatusCode());
|
|
456
|
+
Map<String, String> localResponseHeaders = new LinkedHashMap<>(handler.getResponseHeaders());
|
|
457
|
+
addProxyRouteSecurityHeaders(request.getUrl(), localResponseHeaders);
|
|
380
458
|
return new WebResourceResponse(
|
|
381
459
|
mimeType,
|
|
382
460
|
handler.getEncoding(),
|
|
383
461
|
statusCode,
|
|
384
462
|
handler.getReasonPhrase(),
|
|
385
|
-
|
|
463
|
+
localResponseHeaders,
|
|
386
464
|
responseStream
|
|
387
465
|
);
|
|
388
466
|
}
|
|
@@ -656,13 +734,14 @@ public class WebViewLocalServer {
|
|
|
656
734
|
if (path.startsWith(capacitorContentStart)) {
|
|
657
735
|
stream = protocolHandler.openContentUrl(url);
|
|
658
736
|
} else if (path.startsWith(capacitorFileStart)) {
|
|
659
|
-
stream = protocolHandler.
|
|
737
|
+
stream = protocolHandler.openProxiedFile(path);
|
|
660
738
|
} else if (!isAsset) {
|
|
661
739
|
if (routeProcessor == null) {
|
|
662
|
-
path
|
|
740
|
+
// The decoded path can contain ".." that must not leave the base path.
|
|
741
|
+
stream = protocolHandler.openBasePathFile(basePath, basePath + url.getPath());
|
|
742
|
+
} else {
|
|
743
|
+
stream = protocolHandler.openRoutedFile(url.getPath(), path);
|
|
663
744
|
}
|
|
664
|
-
|
|
665
|
-
stream = protocolHandler.openFile(path);
|
|
666
745
|
} else if (ignoreAssetPath) {
|
|
667
746
|
stream = protocolHandler.openAsset(path);
|
|
668
747
|
} else {
|
package/package.json
CHANGED