Skip to content

Commit

Permalink
Bugzilla 1649159: RTL char bug in downloaded file name
Browse files Browse the repository at this point in the history
  • Loading branch information
garvankeeley committed Jul 9, 2020
1 parent 6cd37d1 commit c9f07c9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
8 changes: 7 additions & 1 deletion Client/Frontend/Browser/DownloadQueue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,20 @@ class HTTPDownload: Download {

private var resumeData: Data?

// Used to avoid name spoofing using Unicode RTL char to change file extension
public static func stripUnicode(fromFilename string: String) -> String {
let allowed = CharacterSet.alphanumerics.union(CharacterSet.punctuationCharacters)
return string.components(separatedBy: allowed.inverted).joined()
}

init(preflightResponse: URLResponse, request: URLRequest) {
self.preflightResponse = preflightResponse
self.request = request

super.init()

if let filename = preflightResponse.suggestedFilename {
self.filename = filename
self.filename = HTTPDownload.stripUnicode(fromFilename: filename)
}

if let mimeType = preflightResponse.mimeType {
Expand Down
8 changes: 8 additions & 0 deletions ClientTests/StringExtensionsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import Foundation
import XCTest
@testable import Client

class StringExtensionsTests: XCTestCase {

Expand Down Expand Up @@ -51,4 +52,11 @@ class StringExtensionsTests: XCTestCase {
roundtripTest("http://mozilla.com/?a=foo&b=bar", "http://mozilla.com/%3Fa%3Dfoo%26b%3Dbar")
}

func testRemoveUnicodeFromFilename() {
let file = "foo-\u{200F}cod.jpg" // Unicode RTL-switch code, becomes "foo-gpj.doc"
let nounicode = "foo-cod.jpg"
XCTAssert(file != nounicode)
let strip = HTTPDownload.stripUnicode(fromFilename: file)
XCTAssert(strip == nounicode)
}
}

0 comments on commit c9f07c9

Please sign in to comment.