Skip to content

Commit

Permalink
Merge pull request #719 from Infomaniak/update-swiftsoup
Browse files Browse the repository at this point in the history
feat: Clean head and body from html document
  • Loading branch information
PhilippeWeidmann committed May 1, 2023
2 parents 7896849 + f2669d0 commit c18c34d
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 80 deletions.
6 changes: 3 additions & 3 deletions .package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -236,10 +236,10 @@
{
"identity" : "swiftsoup",
"kind" : "remoteSourceControl",
"location" : "https://github.com/scinfu/SwiftSoup",
"location" : "https://github.com/valentinperignon/SwiftSoup",
"state" : {
"revision" : "f707b8680cddb96dc1855632340a572ef37bbb98",
"version" : "2.5.3"
"branch" : "try-headcleaner",
"revision" : "05c6c880a5056421de271012faa8bd0a42c2e0a8"
}
},
{
Expand Down
1 change: 0 additions & 1 deletion MailCore/Utils/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ public enum Constants {
try! NSRegularExpression(pattern: ">\\s*<|>?\\s+<?")
}()

public static let extendedWhitelist = Whitelist.extendedWhitelist
public static let customCss = (try? String(contentsOfFile: Bundle.main.path(forResource: "style", ofType: "css") ?? "",
encoding: .utf8).replacingOccurrences(of: "\n", with: "")) ?? ""

Expand Down
19 changes: 17 additions & 2 deletions MailCore/Utils/MessageBodyUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,25 @@ public enum MessageBodyUtils {
}
return nil
}

public static func cleanHtmlContent(rawHtml: String) -> String? {
do {
return try SwiftSoup.clean(rawHtml, Constants.extendedWhitelist)
let dirtyDocument = try SwiftSoup.parse(rawHtml)
let cleanedDocument = try SwiftSoup.Cleaner(headWhitelist: .headWhitelist, bodyWhitelist: .extendedBodyWhitelist)
.clean(dirtyDocument)

// We need to remove the tag <meta http-equiv="refresh" content="x">
let metaRefreshTags = try cleanedDocument.select("meta[http-equiv='refresh']")
for metaRefreshTag in metaRefreshTags {
try metaRefreshTag.parent()?.removeChild(metaRefreshTag)
}

// If `<body>` has a style attribute, keep it
if let bodyStyleAttribute = try dirtyDocument.body()?.attr("style") {
try cleanedDocument.body()?.attr("style", bodyStyleAttribute)
}

return try cleanedDocument.outerHtml()
} catch {
DDLogError("An error occurred while parsing body \(error)")
return nil
Expand Down
93 changes: 20 additions & 73 deletions MailCore/Utils/Whitelist+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,87 +20,34 @@ import Foundation
import SwiftSoup

extension Whitelist {
static var extendedWhitelist: Whitelist {
static var headWhitelist: Whitelist {
do {
let customWhitelist = Whitelist.none()
let allowedTags = [
"a",
"b",
"blockquote",
"body",
"br",
"caption",
"center",
"cite",
"code",
"col",
"colgroup",
"dd",
"div",
"dl",
"dt",
"em",
"h1",
"h2",
"h3",
"h4",
"h5",
"h6",
"head",
"hr",
"html",
"i",
"img",
"li",
"meta",
"ol",
"p",
"pre",
"q",
"small",
"span",
"strike",
"strong",
"style",
"sub",
"sup",
"table",
"tbody",
"td",
"tfoot",
"th",
"thead",
"title",
"tr",
"u",
"ul",
]
try customWhitelist
.addTags("base", "meta", "style", "title")
.addAttributes("style", "media", "type")
.addAttributes("meta", "charset", "content", "http-equiv", "name")
.addAttributes("base", "href", "target")
.addProtocols("base", "href", "http", "https")

for tag in allowedTags {
try customWhitelist.addTags(tag)
try customWhitelist.addAttributes(tag, "style", "width", "height", "class", "align")
}
return customWhitelist
} catch {
fatalError("Couldn't init head whitelist")
}
}

static var extendedBodyWhitelist: Whitelist {
do {
let customWhitelist = try Whitelist.relaxed()
try customWhitelist
.addAttributes("a", "href", "title")
.addAttributes("blockquote", "cite")
.addAttributes("col", "span")
.addAttributes("colgroup", "span")
.addAttributes("img", "align", "alt", "src", "title")
.addAttributes("ol", "start", "type")
.addAttributes("q", "cite")
.addAttributes("table", "summary")
.addAttributes("td", "abbr", "axis", "colspan", "rowspan")
.addAttributes("th", "abbr", "axis", "colspan", "rowspan", "scope")
.addAttributes("ul", "type")
.addTags("center", "hr", "style")
.addAttributes(":all", "align", "bgcolor", "border", "class", "dir", "height", "id", "style", "width")
.addAttributes("td", "valign")
.addProtocols("img", "src", "cid", "data")

.addProtocols("a", "href", "http", "https", "mailto")
.addProtocols("blockquote", "cite", "http", "https")
.addProtocols("cite", "cite", "http", "https")
.addProtocols("q", "cite", "http", "https")
return customWhitelist
} catch {
fatalError("Couldn't init html whitelist")
fatalError("Couldn't init body whitelist")
}
}
}
2 changes: 1 addition & 1 deletion Project.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ let project = Project(name: "Mail",
.package(url: "https://github.com/SCENEE/FloatingPanel", .upToNextMajor(from: "2.0.0")),
.package(url: "https://github.com/kean/Nuke", .upToNextMajor(from: "12.0.0")),
.package(url: "https://github.com/airbnb/lottie-ios", .exact("3.5.0")),
.package(url: "https://github.com/scinfu/SwiftSoup", .upToNextMajor(from: "2.5.3")),
.package(url: "https://github.com/valentinperignon/SwiftSoup", .branch("try-headcleaner")),
.package(url: "https://github.com/johnpatrickmorgan/NavigationBackport", .upToNextMajor(from: "0.7.2")),
.package(url: "https://github.com/aheze/Popovers", .upToNextMajor(from: "1.3.2"))
],
Expand Down

0 comments on commit c18c34d

Please sign in to comment.