Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(21)

Unified Diff: chrome/android/java/src/org/chromium/chrome/browser/download/ChromeDownloadDelegate.java

Issue 1717783002: Fix an issue that download filename from content disposition is not sanitized (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: pass localized default download file name Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/android/java/src/org/chromium/chrome/browser/download/ChromeDownloadDelegate.java
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/download/ChromeDownloadDelegate.java b/chrome/android/java/src/org/chromium/chrome/browser/download/ChromeDownloadDelegate.java
index 4d0b01787575a9c56665b44ea6d5b33c091593c9..f9b18e8736eb2029591345bf4fe2070cf216d60f 100644
--- a/chrome/android/java/src/org/chromium/chrome/browser/download/ChromeDownloadDelegate.java
+++ b/chrome/android/java/src/org/chromium/chrome/browser/download/ChromeDownloadDelegate.java
@@ -19,12 +19,10 @@ import android.text.TextUtils;
import android.util.Pair;
import android.view.View;
import android.webkit.MimeTypeMap;
-import android.webkit.URLUtil;
import android.widget.TextView;
import org.chromium.base.Log;
import org.chromium.base.ThreadUtils;
-import org.chromium.base.VisibleForTesting;
import org.chromium.base.annotations.CalledByNative;
import org.chromium.chrome.R;
import org.chromium.chrome.browser.infobar.InfoBarIdentifier;
@@ -177,64 +175,16 @@ public class ChromeDownloadDelegate implements ContentViewDownloadDelegate {
}
/**
- * Decide the file name of the final download. The file extension is derived
- * from the MIME type.
- * @param url The full URL to the content that should be downloaded.
- * @param mimeType The MIME type of the content reported by the server.
- * @param contentDisposition Content-Disposition HTTP header, if present.
- * @return The best guess of the file name for the downloaded object.
- */
- @VisibleForTesting
- public static String fileName(String url, String mimeType, String contentDisposition) {
- // URLUtil#guessFileName will prefer the MIME type extension over
- // the file extension only if the latter is of a known MIME type.
- // Therefore for things like "file.php" with Content-Type PDF, it will
- // still generate file names like "file.php" instead of "file.pdf".
- // If that's the case, rebuild the file extension from the MIME type.
- String fileName = URLUtil.guessFileName(url, contentDisposition, mimeType);
- int dotIndex = fileName.lastIndexOf('.');
- if (mimeType != null
- && !mimeType.isEmpty()
- && dotIndex > 1 // at least one char before the '.'
- && dotIndex < fileName.length()) { // '.' should not be the last char
- MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
-
- String fileRoot = fileName.substring(0, dotIndex);
- String fileExtension = fileName.substring(dotIndex + 1);
- String fileExtensionMimeType =
- mimeTypeMap.getMimeTypeFromExtension(fileExtension);
-
- // If the file extension's official MIME type and {@code mimeType}
- // are the same, simply use the file extension.
- // If not, extension derived from {@code mimeType} is preferred.
- if (mimeType.equals(fileExtensionMimeType)) {
- fileName = fileRoot + "." + fileExtension;
- } else {
- String mimeExtension =
- mimeTypeMap.getExtensionFromMimeType(mimeType);
-
- if (mimeExtension != null && !mimeExtension.equals(fileExtension)) {
- fileName = fileRoot + "." + mimeExtension;
- }
- }
- }
- return fileName;
- }
-
- /**
* Notify the host application a download should be done, even if there is a
* streaming viewer available for this type.
*
* @param downloadInfo Information about the download.
*/
protected void onDownloadStartNoStream(final DownloadInfo downloadInfo) {
- final String newMimeType = remapGenericMimeType(
- downloadInfo.getMimeType(),
- downloadInfo.getUrl(),
- downloadInfo.getFileName());
- final String fileName = TextUtils.isEmpty(downloadInfo.getFileName())
- ? fileName(downloadInfo.getUrl(), newMimeType, downloadInfo.getContentDisposition())
- : downloadInfo.getFileName();
+ final String fileName = downloadInfo.getFileName();
+ assert !TextUtils.isEmpty(fileName);
+ final String newMimeType =
+ remapGenericMimeType(downloadInfo.getMimeType(), downloadInfo.getUrl(), fileName);
new AsyncTask<Void, Void, Object[]>() {
@Override
protected Object[] doInBackground(Void... params) {

Powered by Google App Engine
This is Rietveld 408576698