Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugzilla 1649160: RTL char bug in downloaded file name #6942

Merged
merged 1 commit into from Jul 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion Client/Frontend/Browser/DownloadQueue.swift
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
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)
}
}