filename_util_unittest.cc 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  1. // Copyright 2014 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "net/base/filename_util.h"
  5. #include "base/files/file_path.h"
  6. #include "base/files/file_util.h"
  7. #include "base/strings/string_util.h"
  8. #include "base/strings/utf_string_conversions.h"
  9. #include "base/test/test_file_util.h"
  10. #include "build/build_config.h"
  11. #include "build/chromeos_buildflags.h"
  12. #include "net/base/mime_util.h"
  13. #include "testing/gtest/include/gtest/gtest.h"
  14. #include "url/gurl.h"
  15. namespace net {
  16. namespace {
  17. struct FileCase {
  18. const wchar_t* file; // nullptr indicates expected to fail.
  19. const char* url;
  20. };
  21. struct GenerateFilenameCase {
  22. int lineno;
  23. const char* url;
  24. const char* content_disp_header;
  25. const char* referrer_charset;
  26. const char* suggested_filename;
  27. const char* mime_type;
  28. const wchar_t* default_filename;
  29. const wchar_t* expected_filename;
  30. };
  31. // The expected filenames are coded as wchar_t for convenience.
  32. // TODO(https://crbug.com/911896): Make these char16_t once std::u16string is
  33. // std::u16string.
  34. std::wstring FilePathAsWString(const base::FilePath& path) {
  35. #if BUILDFLAG(IS_WIN)
  36. return path.value();
  37. #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
  38. return base::UTF8ToWide(path.value());
  39. #endif
  40. }
  41. base::FilePath WStringAsFilePath(const std::wstring& str) {
  42. #if BUILDFLAG(IS_WIN)
  43. return base::FilePath(str);
  44. #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
  45. return base::FilePath(base::WideToUTF8(str));
  46. #endif
  47. }
  48. std::string GetLocaleWarningString() {
  49. #if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_ANDROID)
  50. return "";
  51. #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
  52. // The generate filename tests can fail on certain OS_POSIX platforms when
  53. // LC_CTYPE is not "utf8" or "utf-8" because some of the string conversions
  54. // fail.
  55. // This warning text is appended to any test failures to save people time if
  56. // this happens to be the cause of failure :)
  57. // Note: some platforms (MACOSX, Chromecast) don't have this problem:
  58. // setlocale returns "c" but it functions as utf8. And Android doesn't
  59. // have setlocale at all.
  60. std::string locale = setlocale(LC_CTYPE, nullptr);
  61. return " this test may have failed because the current LC_CTYPE locale is "
  62. "not utf8 (currently set to " +
  63. locale + ")";
  64. #endif
  65. }
  66. void RunGenerateFileNameTestCase(const GenerateFilenameCase* test_case) {
  67. std::string default_filename(base::WideToUTF8(test_case->default_filename));
  68. base::FilePath file_path = GenerateFileName(
  69. GURL(test_case->url), test_case->content_disp_header,
  70. test_case->referrer_charset, test_case->suggested_filename,
  71. test_case->mime_type, default_filename);
  72. EXPECT_EQ(test_case->expected_filename, FilePathAsWString(file_path))
  73. << "test case at line number: " << test_case->lineno << "; "
  74. << GetLocaleWarningString();
  75. }
  76. constexpr const base::FilePath::CharType* kSafePortableBasenames[] = {
  77. FILE_PATH_LITERAL("a"), FILE_PATH_LITERAL("a.txt"),
  78. FILE_PATH_LITERAL("a b.txt"), FILE_PATH_LITERAL("a-b.txt"),
  79. FILE_PATH_LITERAL("My Computer"),
  80. };
  81. constexpr const base::FilePath::CharType* kUnsafePortableBasenames[] = {
  82. FILE_PATH_LITERAL(""),
  83. FILE_PATH_LITERAL("."),
  84. FILE_PATH_LITERAL(".."),
  85. FILE_PATH_LITERAL("..."),
  86. FILE_PATH_LITERAL("con"),
  87. FILE_PATH_LITERAL("con.zip"),
  88. FILE_PATH_LITERAL("NUL"),
  89. FILE_PATH_LITERAL("NUL.zip"),
  90. FILE_PATH_LITERAL(".a"),
  91. FILE_PATH_LITERAL("a."),
  92. FILE_PATH_LITERAL("a\"a"),
  93. FILE_PATH_LITERAL("a<a"),
  94. FILE_PATH_LITERAL("a>a"),
  95. FILE_PATH_LITERAL("a?a"),
  96. FILE_PATH_LITERAL("a/"),
  97. FILE_PATH_LITERAL("a\\"),
  98. FILE_PATH_LITERAL("a "),
  99. FILE_PATH_LITERAL("a . ."),
  100. FILE_PATH_LITERAL(" Computer"),
  101. FILE_PATH_LITERAL("My Computer.{a}"),
  102. FILE_PATH_LITERAL("My Computer.{20D04FE0-3AEA-1069-A2D8-08002B30309D}"),
  103. #if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
  104. FILE_PATH_LITERAL("a\\a"),
  105. #endif
  106. };
  107. constexpr const base::FilePath::CharType* kUnsafePortableBasenamesForWin[] = {
  108. FILE_PATH_LITERAL("con"), FILE_PATH_LITERAL("con.zip"),
  109. FILE_PATH_LITERAL("NUL"), FILE_PATH_LITERAL("NUL.zip"),
  110. };
  111. constexpr const base::FilePath::CharType* kSafePortableRelativePaths[] = {
  112. FILE_PATH_LITERAL("a/a"),
  113. #if BUILDFLAG(IS_WIN)
  114. FILE_PATH_LITERAL("a\\a"),
  115. #endif
  116. };
  117. } // namespace
  118. TEST(FilenameUtilTest, IsSafePortablePathComponent) {
  119. for (auto* basename : kSafePortableBasenames) {
  120. EXPECT_TRUE(IsSafePortablePathComponent(base::FilePath(basename)))
  121. << basename;
  122. }
  123. for (auto* basename : kUnsafePortableBasenames) {
  124. EXPECT_FALSE(IsSafePortablePathComponent(base::FilePath(basename)))
  125. << basename;
  126. }
  127. for (auto* path : kSafePortableRelativePaths) {
  128. EXPECT_FALSE(IsSafePortablePathComponent(base::FilePath(path))) << path;
  129. }
  130. }
  131. TEST(FilenameUtilTest, IsSafePortableRelativePath) {
  132. base::FilePath safe_dirname(FILE_PATH_LITERAL("a"));
  133. for (auto* basename : kSafePortableBasenames) {
  134. EXPECT_TRUE(IsSafePortableRelativePath(base::FilePath(basename)))
  135. << basename;
  136. EXPECT_TRUE(IsSafePortableRelativePath(
  137. safe_dirname.Append(base::FilePath(basename))))
  138. << basename;
  139. }
  140. for (auto* path : kSafePortableRelativePaths) {
  141. EXPECT_TRUE(IsSafePortableRelativePath(base::FilePath(path))) << path;
  142. EXPECT_TRUE(
  143. IsSafePortableRelativePath(safe_dirname.Append(base::FilePath(path))))
  144. << path;
  145. }
  146. for (auto* basename : kUnsafePortableBasenames) {
  147. EXPECT_FALSE(IsSafePortableRelativePath(base::FilePath(basename)))
  148. << basename;
  149. if (!base::FilePath::StringType(basename).empty()) {
  150. EXPECT_FALSE(IsSafePortableRelativePath(
  151. safe_dirname.Append(base::FilePath(basename))))
  152. << basename;
  153. }
  154. }
  155. }
  156. TEST(FilenameUtilTest, FileURLConversion) {
  157. // a list of test file names and the corresponding URLs
  158. const FileCase round_trip_cases[] = {
  159. #if BUILDFLAG(IS_WIN)
  160. {L"C:\\foo\\bar.txt", "file:///C:/foo/bar.txt"},
  161. {L"\\\\some computer\\foo\\bar.txt",
  162. "file://some%20computer/foo/bar.txt"}, // UNC
  163. {L"D:\\Name;with%some symbols*#",
  164. "file:///D:/Name%3Bwith%25some%20symbols*%23"},
  165. // issue 14153: To be tested with the OS default codepage other than 1252.
  166. {L"D:\\latin1\\caf\x00E9\x00DD.txt",
  167. "file:///D:/latin1/caf%C3%A9%C3%9D.txt"},
  168. {L"D:\\otherlatin\\caf\x0119.txt", "file:///D:/otherlatin/caf%C4%99.txt"},
  169. {L"D:\\greek\\\x03B1\x03B2\x03B3.txt",
  170. "file:///D:/greek/%CE%B1%CE%B2%CE%B3.txt"},
  171. {L"D:\\Chinese\\\x6240\x6709\x4e2d\x6587\x7f51\x9875.doc",
  172. "file:///D:/Chinese/%E6%89%80%E6%9C%89%E4%B8%AD%E6%96%87%E7%BD%91"
  173. "%E9%A1%B5.doc"},
  174. {L"D:\\plane1\\\xD835\xDC00\xD835\xDC01.txt", // Math alphabet "AB"
  175. "file:///D:/plane1/%F0%9D%90%80%F0%9D%90%81.txt"},
  176. // Other percent-encoded characters that are left alone when displaying a
  177. // URL are decoded in a file path (https://crbug.com/585422).
  178. {L"C:\\foo\\\U0001F512.txt",
  179. "file:///C:/foo/%F0%9F%94%92.txt"}, // Blocked.
  180. {L"C:\\foo\\\u2001.txt", "file:///C:/foo/%E2%80%81.txt"}, // Blocked.
  181. {L"C:\\foo\\\a\tbar\n ", "file:///C:/foo/%07%09bar%0A%20"}, // Blocked.
  182. #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
  183. {L"/foo/bar.txt", "file:///foo/bar.txt"},
  184. {L"/foo/BAR.txt", "file:///foo/BAR.txt"},
  185. {L"/C:/foo/bar.txt", "file:///C:/foo/bar.txt"},
  186. {L"/foo/bar?.txt", "file:///foo/bar%3F.txt"},
  187. {L"/foo/\a\tbar\n ", "file:///foo/%07%09bar%0A%20"},
  188. // %5C ('\\') is not special on POSIX, and is therefore decoded as normal.
  189. {L"/foo/..\\bar", "file:///foo/..%5Cbar"},
  190. {L"/some computer/foo/bar.txt", "file:///some%20computer/foo/bar.txt"},
  191. {L"/Name;with%some symbols*#", "file:///Name%3Bwith%25some%20symbols*%23"},
  192. {L"/latin1/caf\x00E9\x00DD.txt", "file:///latin1/caf%C3%A9%C3%9D.txt"},
  193. {L"/otherlatin/caf\x0119.txt", "file:///otherlatin/caf%C4%99.txt"},
  194. {L"/greek/\x03B1\x03B2\x03B3.txt", "file:///greek/%CE%B1%CE%B2%CE%B3.txt"},
  195. {L"/Chinese/\x6240\x6709\x4e2d\x6587\x7f51\x9875.doc",
  196. "file:///Chinese/%E6%89%80%E6%9C%89%E4%B8%AD%E6%96%87%E7%BD"
  197. "%91%E9%A1%B5.doc"},
  198. {L"/plane1/\x1D400\x1D401.txt", // Math alphabet "AB"
  199. "file:///plane1/%F0%9D%90%80%F0%9D%90%81.txt"},
  200. // Other percent-encoded characters that are left alone when displaying a
  201. // URL are decoded in a file path (https://crbug.com/585422).
  202. {L"/foo/\U0001F512.txt", "file:///foo/%F0%9F%94%92.txt"}, // Blocked.
  203. {L"/foo/\u2001.txt", "file:///foo/%E2%80%81.txt"}, // Blocked.
  204. #endif
  205. };
  206. // First, we'll test that we can round-trip all of the above cases of URLs
  207. base::FilePath output;
  208. for (const auto& test_case : round_trip_cases) {
  209. // convert to the file URL
  210. GURL file_url(FilePathToFileURL(WStringAsFilePath(test_case.file)));
  211. EXPECT_EQ(test_case.url, file_url.spec());
  212. // Back to the filename.
  213. EXPECT_TRUE(FileURLToFilePath(file_url, &output));
  214. EXPECT_EQ(test_case.file, FilePathAsWString(output));
  215. }
  216. // Test that various file: URLs get decoded into the correct file type
  217. FileCase url_cases[] = {
  218. {nullptr, "http://foo/bar.txt"},
  219. {nullptr, "http://localhost/foo/bar.txt"},
  220. {nullptr, "https://localhost/foo/bar.txt"},
  221. #if BUILDFLAG(IS_WIN)
  222. {L"C:\\foo\\bar.txt", "file:c|/foo\\bar.txt"},
  223. {L"C:\\foo\\bar.txt", "file:/c:/foo/bar.txt"},
  224. {L"\\\\foo\\bar.txt", "file://foo\\bar.txt"},
  225. {L"C:\\foo\\bar.txt", "file:///c:/foo/bar.txt"},
  226. {L"\\\\foo\\bar.txt", "file:////foo\\bar.txt"},
  227. {L"\\\\foo\\bar.txt", "file:/foo/bar.txt"},
  228. {L"\\\\foo\\bar.txt", "file://foo\\bar.txt"},
  229. {L"C:\\foo\\bar.txt", "file:\\\\\\c:/foo/bar.txt"},
  230. // %2F ('/') should fail, because it might otherwise be interpreted as a
  231. // path separator on Windows.
  232. {nullptr, "file:///C:\\foo%2f..\\bar"},
  233. // %5C ('\\') should fail, because it can't be represented in a Windows
  234. // filename (and should not be considered a path separator).
  235. {nullptr, "file:///foo\\..%5cbar"},
  236. // %00 should fail, because it represents a null byte in a filename.
  237. {nullptr, "file:///foo/%00bar.txt"},
  238. // Other percent-encoded characters that are left alone when displaying a
  239. // URL are decoded in a file path (https://crbug.com/585422).
  240. {L"C:\\foo\\\n.txt", "file:///c:/foo/%0A.txt"}, // Control char.
  241. {L"C:\\foo\\a=$b.txt", "file:///c:/foo/a%3D%24b.txt"}, // Reserved.
  242. // Make sure that '+' isn't converted into ' '.
  243. {L"C:\\foo\\romeo+juliet.txt", "file:/c:/foo/romeo+juliet.txt"},
  244. // SAMBA share case.
  245. {L"\\\\computername\\ShareName\\Path\\Foo.txt",
  246. "file://computername/ShareName/Path/Foo.txt"},
  247. #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
  248. {L"/c:/foo/bar.txt", "file:/c:/foo/bar.txt"},
  249. {L"/c:/foo/bar.txt", "file:///c:/foo/bar.txt"},
  250. {L"/foo/bar.txt", "file:/foo/bar.txt"},
  251. {L"/c:/foo/bar.txt", "file:\\\\\\c:/foo/bar.txt"},
  252. {L"/foo/bar.txt", "file:foo/bar.txt"},
  253. {L"/foo/bar.txt", "file:///foo/bar.txt"},
  254. {L"/foo/bar.txt", "file:////foo/bar.txt"},
  255. {L"/foo/bar.txt", "file:////foo//bar.txt"},
  256. {L"/foo/bar.txt", "file:////foo///bar.txt"},
  257. {L"/foo/bar.txt", "file:////foo////bar.txt"},
  258. {L"/c:/foo/bar.txt", "file:\\\\\\c:/foo/bar.txt"},
  259. {L"/c:/foo/bar.txt", "file:c:/foo/bar.txt"},
  260. // %2F ('/') should fail, because it can't be represented in a POSIX
  261. // filename (and should not be considered a path separator).
  262. {nullptr, "file:///foo%2f../bar"},
  263. // %00 should fail, because it represents a null byte in a filename.
  264. {nullptr, "file:///foo/%00bar.txt"},
  265. // Other percent-encoded characters that are left alone when displaying a
  266. // URL are decoded in a file path (https://crbug.com/585422).
  267. {L"/foo/\n.txt", "file:///foo/%0A.txt"}, // Control char.
  268. {L"/foo/a=$b.txt", "file:///foo/a%3D%24b.txt"}, // Reserved.
  269. // Make sure that '+' isn't converted into ' '.
  270. {L"/foo/romeo+juliet.txt", "file:///foo/romeo+juliet.txt"},
  271. // Backslashes in a file URL are normalized as forward slashes.
  272. {L"/bar.txt", "file://\\bar.txt"},
  273. {L"/c|/foo/bar.txt", "file:c|/foo\\bar.txt"},
  274. {L"/foo/bar.txt", "file:////foo\\bar.txt"},
  275. // Accept obviously-local file URLs.
  276. {L"/foo/bar.txt", "file:///foo/bar.txt"},
  277. {L"/foo/bar.txt", "file://localhost/foo/bar.txt"},
  278. {L"/foo/bar.txt", "file://127.0.0.1/foo/bar.txt"},
  279. {L"/foo/bar.txt", "file://[::1]/foo/bar.txt"},
  280. // Reject non-local file URLs.
  281. {nullptr, "file://foo/bar.txt"},
  282. {nullptr, "file://example.com/bar.txt"},
  283. {nullptr, "file://192.168.1.1/foo/bar.txt"},
  284. {nullptr, "file://[2001:0db8:85a3:0000:0000:8a2e:0370:7334]/foo/bar.txt"},
  285. #endif
  286. };
  287. for (const auto& test_case : url_cases) {
  288. EXPECT_EQ(test_case.file != nullptr,
  289. FileURLToFilePath(GURL(test_case.url), &output));
  290. if (test_case.file) {
  291. EXPECT_EQ(test_case.file, FilePathAsWString(output));
  292. } else {
  293. EXPECT_EQ(L"", FilePathAsWString(output));
  294. }
  295. }
  296. // Invalid UTF-8 tests can't be tested above because FilePathAsWString assumes
  297. // the output is valid UTF-8.
  298. // Invalid UTF-8 bytes in input.
  299. {
  300. const char invalid_utf8[] = "file:///d:/Blah/\x85\x99.doc";
  301. EXPECT_TRUE(FileURLToFilePath(GURL(invalid_utf8), &output));
  302. #if BUILDFLAG(IS_WIN)
  303. // On Windows, invalid UTF-8 bytes are interpreted using the default ANSI
  304. // code page. This defaults to Windows-1252 (which we assume here).
  305. const base::FilePath::CharType expected_output[] =
  306. FILE_PATH_LITERAL("D:\\Blah\\\u2026\u2122.doc");
  307. EXPECT_EQ(expected_output, output.value());
  308. #elif BUILDFLAG(IS_POSIX)
  309. // No conversion should happen, and the invalid UTF-8 should be preserved.
  310. const char expected_output[] = "/d:/Blah/\x85\x99.doc";
  311. EXPECT_EQ(expected_output, output.value());
  312. #endif
  313. }
  314. // Invalid UTF-8 percent-encoded bytes in input.
  315. {
  316. const char invalid_utf8[] = "file:///d:/Blah/%85%99.doc";
  317. EXPECT_TRUE(FileURLToFilePath(GURL(invalid_utf8), &output));
  318. #if BUILDFLAG(IS_WIN)
  319. // On Windows, invalid UTF-8 bytes are interpreted using the default ANSI
  320. // code page. This defaults to Windows-1252 (which we assume here).
  321. const base::FilePath::CharType expected_output[] =
  322. FILE_PATH_LITERAL("D:\\Blah\\\u2026\u2122.doc");
  323. EXPECT_EQ(expected_output, output.value());
  324. #elif BUILDFLAG(IS_POSIX)
  325. // No conversion should happen, and the invalid UTF-8 should be preserved.
  326. const char expected_output[] = "/d:/Blah/\x85\x99.doc";
  327. EXPECT_EQ(expected_output, output.value());
  328. #endif
  329. }
  330. // Test that if a file URL is malformed, we get a failure
  331. EXPECT_FALSE(FileURLToFilePath(GURL("filefoobar"), &output));
  332. }
  333. TEST(FilenameUtilTest, GenerateSafeFileName) {
  334. const struct {
  335. int line;
  336. const char* mime_type;
  337. const char* filename;
  338. const char* expected_filename;
  339. } safe_tests[] = {
  340. {__LINE__, "text/html", "bar.htm", "bar.htm"},
  341. {__LINE__, "text/html", "bar.html", "bar.html"},
  342. {__LINE__, "application/x-chrome-extension", "bar", "bar.crx"},
  343. {__LINE__, "image/png", "bar.html", "bar.html"},
  344. {__LINE__, "text/html", "bar.exe", "bar.exe"},
  345. {__LINE__, "image/gif", "bar.exe", "bar.exe"},
  346. {__LINE__, "text/html", "google.com", "google.com"},
  347. // Allow extension synonyms.
  348. {__LINE__, "image/jpeg", "bar.jpg", "bar.jpg"},
  349. {__LINE__, "image/jpeg", "bar.jpeg", "bar.jpeg"},
  350. #if BUILDFLAG(IS_WIN)
  351. // Device names
  352. {__LINE__, "text/html", "con.htm", "_con.htm"},
  353. {__LINE__, "text/html", "lpt1.htm", "_lpt1.htm"},
  354. {__LINE__, "application/x-chrome-extension", "con", "_con.crx"},
  355. // Looks like foo.{GUID} which get treated as namespace mounts on Windows.
  356. {__LINE__, "text/html", "harmless.{not-really-this-may-be-a-guid}",
  357. "harmless.download"},
  358. {__LINE__, "text/html", "harmless.{mismatched-", "harmless.{mismatched-"},
  359. // Dangerous extensions
  360. {__LINE__, "text/html", "harmless.local", "harmless.download"},
  361. {__LINE__, "text/html", "harmless.lnk", "harmless.download"},
  362. #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
  363. // On Posix, none of the above set is particularly dangerous.
  364. {__LINE__, "text/html", "con.htm", "con.htm"},
  365. {__LINE__, "text/html", "lpt1.htm", "lpt1.htm"},
  366. {__LINE__, "application/x-chrome-extension", "con", "con.crx"},
  367. {__LINE__, "text/html", "harmless.{not-really-this-may-be-a-guid}",
  368. "harmless.{not-really-this-may-be-a-guid}"},
  369. {__LINE__, "text/html", "harmless.{mismatched-", "harmless.{mismatched-"},
  370. {__LINE__, "text/html", "harmless.local", "harmless.local"},
  371. {__LINE__, "text/html", "harmless.lnk", "harmless.lnk"},
  372. #endif // BUILDFLAG(IS_WIN)
  373. };
  374. #if BUILDFLAG(IS_WIN)
  375. base::FilePath base_path(FILE_PATH_LITERAL("C:\\foo"));
  376. #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
  377. base::FilePath base_path("/foo");
  378. #endif
  379. for (const auto& test : safe_tests) {
  380. base::FilePath file_path = base_path.AppendASCII(test.filename);
  381. base::FilePath expected_path =
  382. base_path.AppendASCII(test.expected_filename);
  383. GenerateSafeFileName(test.mime_type, false, &file_path);
  384. EXPECT_EQ(expected_path.value(), file_path.value())
  385. << "Test case at line " << test.line;
  386. }
  387. }
  388. TEST(FilenameUtilTest, GenerateFileName_Assumptions) {
  389. base::FilePath::StringType extension;
  390. EXPECT_TRUE(GetPreferredExtensionForMimeType("application/x-chrome-extension",
  391. &extension));
  392. EXPECT_EQ(base::FilePath::StringType(FILE_PATH_LITERAL("crx")), extension);
  393. }
  394. TEST(FilenameUtilTest, GenerateFileName) {
  395. // Tests whether the correct filename is selected from the the given
  396. // parameters and that Content-Disposition headers are properly
  397. // handled including failovers when the header is malformed.
  398. const GenerateFilenameCase selection_tests[] = {
  399. {// Picks the filename from the C-D header.
  400. __LINE__, "http://www.google.com/", "attachment; filename=test.html", "",
  401. "", "", L"", L"test.html"},
  402. {// Ditto. The C-D header uses a quoted string.
  403. __LINE__, "http://www.google.com/", "attachment; filename=\"test.html\"",
  404. "", "", "", L"", L"test.html"},
  405. {// Ditto. Extra whilespace after the '=' sign.
  406. __LINE__, "http://www.google.com/",
  407. "attachment; filename= \"test.html\"", "", "", "", L"", L"test.html"},
  408. {// Ditto. Whitespace before and after '=' sign.
  409. __LINE__, "http://www.google.com/",
  410. "attachment; filename = \"test.html\"", "", "", "", L"",
  411. L"test.html"},
  412. {// Filename is whitespace. Should failover to URL host
  413. __LINE__, "http://www.google.com/", "attachment; filename= ", "", "",
  414. "", L"", L"www.google.com"},
  415. {// No filename.
  416. __LINE__, "http://www.google.com/path/test.html", "attachment", "", "",
  417. "", L"", L"test.html"},
  418. {// Ditto
  419. __LINE__, "http://www.google.com/path/test.html", "attachment;", "", "",
  420. "", L"", L"test.html"},
  421. {// No C-D, and no URL path.
  422. __LINE__, "http://www.google.com/", "", "", "", "", L"",
  423. L"www.google.com"},
  424. {// No C-D. URL has a path.
  425. __LINE__, "http://www.google.com/test.html", "", "", "", "", L"",
  426. L"test.html"},
  427. {// No C-D. URL's path ends in a slash which results in an empty final
  428. // component.
  429. __LINE__, "http://www.google.com/path/", "", "", "", "", L"",
  430. L"www.google.com"},
  431. {// No C-D. URL has a path, but the path has no extension.
  432. __LINE__, "http://www.google.com/path", "", "", "", "", L"", L"path"},
  433. {// No C-D. URL gives no filename hints.
  434. __LINE__, "file:///", "", "", "", "", L"", L"download"},
  435. {// file:// URL.
  436. __LINE__, "file:///path/testfile", "", "", "", "", L"", L"testfile"},
  437. {// Unknown scheme.
  438. __LINE__, "non-standard-scheme:", "", "", "", "", L"", L"download"},
  439. {// C-D overrides default
  440. __LINE__, "http://www.google.com/",
  441. "attachment; filename =\"test.html\"", "", "", "", L"download",
  442. L"test.html"},
  443. {// But the URL doesn't
  444. __LINE__, "http://www.google.com/", "", "", "", "", L"download",
  445. L"download"},
  446. // Below is a small subset of cases taken from HttpContentDisposition
  447. // tests.
  448. {__LINE__, "http://www.google.com/",
  449. "attachment; filename=\"%EC%98%88%EC%88%A0%20"
  450. "%EC%98%88%EC%88%A0.jpg\"",
  451. "", "", "", L"", L"\uc608\uc220 \uc608\uc220.jpg"},
  452. {__LINE__,
  453. "http://www.google.com/%EC%98%88%EC%88%A0%20%EC%98%88%EC%88%A0.jpg", "",
  454. "", "", "", L"download", L"\uc608\uc220 \uc608\uc220.jpg"},
  455. {__LINE__, "http://www.google.com/", "attachment;", "", "", "",
  456. L"\uB2E4\uC6B4\uB85C\uB4DC", L"\uB2E4\uC6B4\uB85C\uB4DC"},
  457. {__LINE__, "http://www.google.com/",
  458. "attachment; filename=\"=?EUC-JP?Q?=B7=DD=BD="
  459. "D13=2Epng?=\"",
  460. "", "", "", L"download", L"\u82b8\u88533.png"},
  461. {__LINE__, "http://www.example.com/images?id=3",
  462. "attachment; filename=caf\xc3\xa9.png", "iso-8859-1", "", "", L"",
  463. L"caf\u00e9.png"},
  464. {__LINE__, "http://www.example.com/images?id=3",
  465. "attachment; filename=caf\xe5.png", "windows-1253", "", "", L"",
  466. L"caf\u03b5.png"},
  467. {// Invalid C-D header. Name value is skipped now.
  468. __LINE__, "http://www.example.com/file?id=3",
  469. "attachment; name=\xcf\xc2\xd4\xd8.zip", "GBK", "", "", L"", L"file"},
  470. {// Invalid C-D header. Extracts filename from url.
  471. __LINE__, "http://www.google.com/test.html",
  472. "attachment; filename==?iiso88591?Q?caf=EG?=", "", "", "", L"",
  473. L"test.html"},
  474. // about: and data: URLs
  475. {__LINE__, "about:chrome", "", "", "", "", L"", L"download"},
  476. {__LINE__, "data:,looks/like/a.path", "", "", "", "", L"", L"download"},
  477. {__LINE__, "data:text/plain;base64,VG8gYmUgb3Igbm90IHRvIGJlLg=", "", "",
  478. "", "", L"", L"download"},
  479. {__LINE__, "data:,looks/like/a.path", "", "", "", "",
  480. L"default_filename_is_given", L"default_filename_is_given"},
  481. {__LINE__, "data:,looks/like/a.path", "", "", "", "",
  482. L"\u65e5\u672c\u8a9e", // Japanese Kanji.
  483. L"\u65e5\u672c\u8a9e"},
  484. {// The filename encoding is specified by the referrer charset.
  485. __LINE__, "http://example.com/V%FDvojov%E1%20psychologie.doc", "",
  486. "iso-8859-1", "", "", L"", L"V\u00fdvojov\u00e1 psychologie.doc"},
  487. {// Suggested filename takes precedence over URL
  488. __LINE__, "http://www.google.com/test", "", "", "suggested", "", L"",
  489. L"suggested"},
  490. {// The content-disposition has higher precedence over the suggested name.
  491. __LINE__, "http://www.google.com/test", "attachment; filename=test.html",
  492. "", "suggested", "", L"", L"test.html"},
  493. {__LINE__, "http://www.google.com/test", "attachment; filename=test",
  494. "utf-8", "", "image/png", L"", L"test"},
  495. // Raw 8bit characters in C-D
  496. {__LINE__, "http://www.example.com/images?id=3",
  497. "attachment; filename=caf\xc3\xa9.png", "iso-8859-1", "", "image/png",
  498. L"", L"caf\u00e9.png"},
  499. {__LINE__, "http://www.example.com/images?id=3",
  500. "attachment; filename=caf\xe5.png", "windows-1253", "", "image/png", L"",
  501. L"caf\u03b5.png"},
  502. {// No 'filename' keyword in the disposition, use the URL
  503. __LINE__, "http://www.evil.com/my_download.txt", "a_file_name.txt", "",
  504. "", "text/plain", L"download", L"my_download.txt"},
  505. {// Spaces in the disposition file name
  506. __LINE__, "http://www.frontpagehacker.com/a_download.exe",
  507. "filename=My Downloaded File.exe", "", "", "application/octet-stream",
  508. L"download", L"My Downloaded File.exe"},
  509. {// % encoded
  510. __LINE__, "http://www.examples.com/",
  511. "attachment; "
  512. "filename=\"%EC%98%88%EC%88%A0%20%EC%98%88%EC%88%A0.jpg\"",
  513. "", "", "application/x-chrome-extension", L"download",
  514. L"\uc608\uc220 \uc608\uc220.jpg"},
  515. {// Invalid C-D header. Name value is skipped now.
  516. __LINE__, "http://www.examples.com/q.cgi?id=abc",
  517. "attachment; name=abc de.pdf", "", "", "application/octet-stream",
  518. L"download", L"q.cgi"},
  519. {__LINE__, "http://www.example.com/path",
  520. "filename=\"=?EUC-JP?Q?=B7=DD=BD=D13=2Epng?=\"", "", "", "image/png",
  521. L"download",
  522. L"\x82b8\x8853"
  523. L"3.png"},
  524. {// The following two have invalid CD headers and filenames come from the
  525. // URL.
  526. __LINE__, "http://www.example.com/test%20123",
  527. "attachment; filename==?iiso88591?Q?caf=EG?=", "", "", "", L"download",
  528. L"test 123"},
  529. {__LINE__,
  530. "http://www.google.com/%EC%98%88%EC%88%A0%20%EC%98%88%EC%88%A0.jpg",
  531. "malformed_disposition", "", "", "", L"download",
  532. L"\uc608\uc220 \uc608\uc220.jpg"},
  533. {// Invalid C-D. No filename from URL. Falls back to 'download'.
  534. __LINE__, "http://www.google.com/path1/path2/",
  535. "attachment; filename==?iso88591?Q?caf=E3?", "", "", "", L"download",
  536. L"download"},
  537. };
  538. // Tests filename generation. Once the correct filename is
  539. // selected, they should be passed through the validation steps and
  540. // a correct extension should be added if necessary.
  541. const GenerateFilenameCase generation_tests[] = {
  542. // Dotfiles. Ensures preceeding period(s) stripped.
  543. {__LINE__, "http://www.google.com/.test.html", "", "", "", "", L"",
  544. L"test.html"},
  545. {__LINE__, "http://www.google.com/.test", "", "", "", "", L"", L"test"},
  546. {__LINE__, "http://www.google.com/..test", "", "", "", "", L"", L"test"},
  547. {// Disposition has relative paths, remove directory separators
  548. __LINE__, "", "filename=../../../../././../a_file_name.txt", "", "",
  549. "text/plain", L"download", L"_.._.._.._._._.._a_file_name.txt"},
  550. {// Disposition has parent directories, remove directory separators
  551. __LINE__, "", "filename=dir1/dir2/a_file_name.txt", "", "", "text/plain",
  552. L"download", L"dir1_dir2_a_file_name.txt"},
  553. {// Disposition has relative paths, remove directory separators
  554. __LINE__, "", "filename=..\\..\\..\\..\\.\\.\\..\\a_file_name.txt", "", "",
  555. "text/plain", L"download", L"_.._.._.._._._.._a_file_name.txt"},
  556. {// Disposition has parent directories, remove directory separators
  557. __LINE__, "", "filename=dir1\\dir2\\a_file_name.txt", "", "", "text/plain",
  558. L"download", L"dir1_dir2_a_file_name.txt"},
  559. {// Filename looks like HTML?
  560. __LINE__, "", "filename=\"<blink>Hello kitty</blink>\"", "", "",
  561. "text/plain", L"default", L"_blink_Hello kitty__blink_"},
  562. {// A normal avi should get .avi and not .avi.avi
  563. __LINE__, "https://example.com/misc/2.avi", "", "", "", "video/x-msvideo",
  564. L"download", L"2.avi"},
  565. {// Slashes are illegal, and should be replaced with underscores.
  566. __LINE__, "http://example.com/foo%2f..%2fbar.jpg", "", "", "",
  567. "text/plain", L"download", L"foo_.._bar.jpg"},
  568. {// "%00" decodes to the NUL byte, which is illegal and should be replaced
  569. // with an underscore. (Note: This can't be tested with a URL, since "%00"
  570. // is illegal in a URL. Only applies to Content-Disposition.)
  571. __LINE__, "http://example.com/download.py", "filename=foo%00bar.jpg", "",
  572. "", "text/plain", L"download", L"foo_bar.jpg"},
  573. {// Extension generation for C-D derived filenames.
  574. __LINE__, "", "filename=my-cat", "", "", "image/jpeg", L"download",
  575. L"my-cat"},
  576. {// Unknown MIME type
  577. __LINE__, "", "filename=my-cat", "", "", "dance/party", L"download",
  578. L"my-cat"},
  579. {// Known MIME type.
  580. __LINE__, "", "filename=my-cat.jpg", "", "", "text/plain", L"download",
  581. L"my-cat.jpg"},
  582. #if BUILDFLAG(IS_WIN)
  583. // Test truncation of trailing dots and spaces (Windows)
  584. {__LINE__, "", "filename=evil.exe ", "", "", "binary/octet-stream",
  585. L"download", L"evil.exe"},
  586. {__LINE__, "", "filename=evil.exe.", "", "", "binary/octet-stream",
  587. L"download", L"evil.exe_"},
  588. {__LINE__, "", "filename=evil.exe. . .", "", "", "binary/octet-stream",
  589. L"download", L"evil.exe_______"},
  590. {__LINE__, "", "filename=evil.", "", "", "binary/octet-stream", L"download",
  591. L"evil_"},
  592. {__LINE__, "", "filename=. . . . .", "", "", "binary/octet-stream",
  593. L"download", L"download"},
  594. #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
  595. // Test truncation of trailing dots and spaces (non-Windows)
  596. {__LINE__, "", "filename=evil.exe ", "", "", "binary/octet-stream",
  597. L"download", L"evil.exe"},
  598. {__LINE__, "", "filename=evil.exe.", "", "", "binary/octet-stream",
  599. L"download", L"evil.exe"},
  600. {__LINE__, "", "filename=evil.exe. . .", "", "", "binary/octet-stream",
  601. L"download", L"evil.exe. . _"},
  602. {__LINE__, "", "filename=evil.", "", "", "binary/octet-stream", L"download",
  603. L"evil"},
  604. {__LINE__, "", "filename=. . . . .", "", "", "binary/octet-stream",
  605. L"download", L"_. . ._"},
  606. #endif
  607. {__LINE__, "", "attachment; filename=\"meh.exe\xC2\xA0\"", "", "",
  608. "binary/octet-stream", L"", L"meh.exe_"},
  609. // Disappearing directory references:
  610. {__LINE__, "", "filename=.", "", "", "dance/party", L"download",
  611. L"download"},
  612. {__LINE__, "", "filename=..", "", "", "dance/party", L"download",
  613. L"download"},
  614. {__LINE__, "", "filename=...", "", "", "dance/party", L"download",
  615. L"download"},
  616. // Reserved words on Windows
  617. {__LINE__, "", "filename=COM1", "", "", "application/foo-bar", L"download",
  618. #if BUILDFLAG(IS_WIN)
  619. L"_COM1"
  620. #else
  621. L"COM1"
  622. #endif
  623. },
  624. {__LINE__, "", "filename=COM4.txt", "", "", "text/plain", L"download",
  625. #if BUILDFLAG(IS_WIN)
  626. L"_COM4.txt"
  627. #else
  628. L"COM4.txt"
  629. #endif
  630. },
  631. {__LINE__, "", "filename=lpt1.TXT", "", "", "text/plain", L"download",
  632. #if BUILDFLAG(IS_WIN)
  633. L"_lpt1.TXT"
  634. #else
  635. L"lpt1.TXT"
  636. #endif
  637. },
  638. {__LINE__, "", "filename=clock$.txt", "", "", "text/plain", L"download",
  639. #if BUILDFLAG(IS_WIN)
  640. L"_clock$.txt"
  641. #else
  642. L"clock$.txt"
  643. #endif
  644. },
  645. {// Validation should also apply to sugested name
  646. __LINE__, "", "", "", "clock$.txt", "text/plain", L"download",
  647. #if BUILDFLAG(IS_WIN)
  648. L"_clock$.txt"
  649. #else
  650. L"clock$.txt"
  651. #endif
  652. },
  653. {// Device names only work when present at the start of the string.
  654. __LINE__, "", "filename=mycom1.foo", "", "", "", L"download",
  655. L"mycom1.foo"},
  656. {__LINE__, "", "filename=Setup.exe.local", "", "", "", L"download",
  657. #if BUILDFLAG(IS_WIN)
  658. L"Setup.exe.download"
  659. #else
  660. L"Setup.exe.local"
  661. #endif
  662. },
  663. {__LINE__, "", "filename=Setup.exe.local.local", "", "", "", L"download",
  664. #if BUILDFLAG(IS_WIN)
  665. L"Setup.exe.local.download"
  666. #else
  667. L"Setup.exe.local.local"
  668. #endif
  669. },
  670. {__LINE__, "", "filename=Setup.exe.lnk", "", "", "", L"download",
  671. #if BUILDFLAG(IS_WIN)
  672. L"Setup.exe.download"
  673. #else
  674. L"Setup.exe.lnk"
  675. #endif
  676. },
  677. {__LINE__, "", "filename=Desktop.ini", "", "", "", L"download",
  678. #if BUILDFLAG(IS_WIN)
  679. L"_Desktop.ini"
  680. #else
  681. L"Desktop.ini"
  682. #endif
  683. },
  684. {__LINE__, "", "filename=Thumbs.db", "", "", "", L"download",
  685. #if BUILDFLAG(IS_WIN)
  686. L"_Thumbs.db"
  687. #else
  688. L"Thumbs.db"
  689. #endif
  690. },
  691. // Regression tests for older issues:
  692. {// http://crbug.com/5772.
  693. __LINE__, "http://www.example.com/foo.tar.gz", "", "", "",
  694. "application/x-tar", L"download", L"foo.tar.gz"},
  695. {// http://crbug.com/52250.
  696. __LINE__, "http://www.example.com/foo.tgz", "", "", "",
  697. "application/x-tar", L"download", L"foo.tgz"},
  698. {// http://crbug.com/7337.
  699. __LINE__, "http://maged.lordaeron.org/blank.reg", "", "", "",
  700. "text/x-registry", L"download", L"blank.reg"},
  701. {__LINE__, "http://www.example.com/bar.tar", "", "", "",
  702. "application/x-tar", L"download", L"bar.tar"},
  703. {__LINE__, "http://www.example.com/bar.bogus", "", "", "",
  704. "application/x-tar", L"download", L"bar.bogus"},
  705. {// http://crbug.com/20337
  706. __LINE__, "http://www.example.com/.download.txt", "filename=.download.txt",
  707. "", "", "text/plain", L"-download", L"download.txt"},
  708. {// http://crbug.com/56855.
  709. __LINE__, "http://www.example.com/bar.sh", "", "", "", "application/x-sh",
  710. L"download", L"bar.sh"},
  711. {// http://crbug.com/61571
  712. __LINE__, "http://www.example.com/npdf.php?fn=foobar.pdf", "", "", "",
  713. "application/x-chrome-extension", L"download", L"npdf.crx"},
  714. {// Shouldn't overwrite C-D specified extension.
  715. __LINE__, "http://www.example.com/npdf.php?fn=foobar.pdf",
  716. "filename=foobar.jpg", "", "", "text/plain", L"download", L"foobar.jpg"},
  717. {// http://crbug.com/87719
  718. __LINE__, "http://www.example.com/image.aspx?id=blargh", "", "", "",
  719. "application/x-chrome-extension", L"download", L"image.crx"},
  720. {__LINE__, "http://www.example.com/image.aspx?id=blargh", "", "", " .foo",
  721. "", L"download", L"_.foo"},
  722. // Note that the next 4 tests will not fail on all platforms on regression.
  723. // They only fail if application/[x-]gzip has a default extension, which
  724. // can vary across platforms (And even by OS install).
  725. {__LINE__, "http://www.example.com/goat.tar.gz?wearing_hat=true", "", "",
  726. "", "application/gzip", L"", L"goat.tar.gz"},
  727. {__LINE__, "http://www.example.com/goat.tar.gz?wearing_hat=true", "", "",
  728. "", "application/x-gzip", L"", L"goat.tar.gz"},
  729. {__LINE__, "http://www.example.com/goat.tgz?wearing_hat=true", "", "", "",
  730. "application/gzip", L"", L"goat.tgz"},
  731. {__LINE__, "http://www.example.com/goat.tgz?wearing_hat=true", "", "", "",
  732. "application/x-gzip", L"", L"goat.tgz"},
  733. #if BUILDFLAG(IS_CHROMEOS_ASH)
  734. {// http://crosbug.com/26028
  735. __LINE__, "http://www.example.com/fooa%cc%88.txt", "", "", "",
  736. "image/jpeg", L"foo\xe4", L"foo\xe4.txt"},
  737. #endif
  738. // U+3000 IDEOGRAPHIC SPACE (http://crbug.com/849794): In URL file name.
  739. {__LINE__, "http://www.example.com/%E5%B2%A1%E3%80%80%E5%B2%A1.txt", "", "",
  740. "", "text/plain", L"", L"\u5ca1\u3000\u5ca1.txt"},
  741. // U+3000 IDEOGRAPHIC SPACE (http://crbug.com/849794): In
  742. // Content-Disposition filename.
  743. {__LINE__, "http://www.example.com/download.py",
  744. "filename=%E5%B2%A1%E3%80%80%E5%B2%A1.txt", "utf-8", "", "text/plain", L"",
  745. L"\u5ca1\u3000\u5ca1.txt"},
  746. };
  747. for (const auto& selection_test : selection_tests)
  748. RunGenerateFileNameTestCase(&selection_test);
  749. for (const auto& generation_test : generation_tests)
  750. RunGenerateFileNameTestCase(&generation_test);
  751. for (const auto& generation_test : generation_tests) {
  752. GenerateFilenameCase test_case = generation_test;
  753. test_case.referrer_charset = "GBK";
  754. RunGenerateFileNameTestCase(&test_case);
  755. }
  756. }
  757. TEST(FilenameUtilTest, IsReservedNameOnWindows) {
  758. for (auto* basename : kSafePortableBasenames) {
  759. EXPECT_FALSE(IsReservedNameOnWindows(base::FilePath(basename).value()))
  760. << basename;
  761. }
  762. for (auto* basename : kUnsafePortableBasenamesForWin) {
  763. EXPECT_TRUE(IsReservedNameOnWindows(base::FilePath(basename).value()))
  764. << basename;
  765. }
  766. }
  767. } // namespace net