Skip to content

Fastest and only macOS Dictation app with on-device STT and custom trained AI enhancement model. Windows, iOS testflight! Android, Linux waitlist open! A local Wispr Flow alternative. Follow https://x.com/fluidvoiceapp for more

fix: validate downloaded model content + surface onboarding errors (#353, #355)

postoso84f5e6a5fbca
diff --git a/Sources/Fluid/Networking/ModelDownloader.swift b/Sources/Fluid/Networking/ModelDownloader.swift
index e7b067a9..cfd32f05 100644
--- a/Sources/Fluid/Networking/ModelDownloader.swift
+++ b/Sources/Fluid/Networking/ModelDownloader.swift
@@ -196,6 +196,10 @@ final class HuggingFaceModelDownloader {
continuation.resume(throwing: NSError(domain: "HF", code: http.statusCode))
return
}
+ // Reject HTML error/block pages (e.g. a corporate proxy returning its
+ // notification page with HTTP 200) before persisting them as a model
+ // file, otherwise a corrupt payload is cached permanently. See #353.
+ try Self.validateDownloadedFile(at: tempUrl, response: response, relativePath: relativePath)
try FileManager.default.createDirectory(at: destination.deletingLastPathComponent(), withIntermediateDirectories: true)
if FileManager.default.fileExists(atPath: destination.path) {
try FileManager.default.removeItem(at: destination)
@@ -203,6 +207,8 @@ final class HuggingFaceModelDownloader {
try FileManager.default.moveItem(at: tempUrl, to: destination)
continuation.resume()
} catch {
+ // Never leave a rejected/partial payload behind.
+ try? FileManager.default.removeItem(at: tempUrl)
continuation.resume(throwing: error)
}
}
@@ -213,6 +219,82 @@ final class HuggingFaceModelDownloader {
}
}
+ // MARK: - Content Validation
+
+ /// Validates a freshly-downloaded artifact before it is persisted as a model file.
+ ///
+ /// A network proxy / secure web gateway can return an HTML (or XML) block page with
+ /// HTTP 200 in place of the real file. Persisting that markup (e.g. as `coremldata.bin`)
+ /// permanently caches a corrupt model. We reject any payload that looks like HTML/XML
+ /// markup — by its `Content-Type` or by its leading bytes — since no model artifact
+ /// (CoreML binary, JSON vocab, `.mil`) is a markup document. See issue #353.
+ static func validateDownloadedFile(at fileURL: URL, response: URLResponse?, relativePath: String) throws {
+ if let http = response as? HTTPURLResponse,
+ let contentType = http.value(forHTTPHeaderField: "Content-Type") {
+ let lowered = contentType.lowercased()
+ if lowered.contains("text/html") || lowered.contains("text/xml") || lowered.contains("application/xml") {
+ throw Self.invalidContentError(
+ relativePath: relativePath,
+ detail: "the server returned a markup page (Content-Type: \(contentType))"
+ )
+ }
+ }
+
+ // Sniff the leading bytes in case markup was returned without a markup Content-Type.
+ // Read a small prefix only — model files can be gigabytes.
+ let handle = try FileHandle(forReadingFrom: fileURL)
+ defer { try? handle.close() }
+ let prefix = (try? handle.read(upToCount: 512)) ?? Data()
+ if Self.looksLikeHTML(prefix) {
+ throw Self.invalidContentError(
+ relativePath: relativePath,
+ detail: "the downloaded file is an HTML/markup document, not the expected model data"
+ )
+ }
+ }
+
+ /// Returns `true` if `data` begins with an HTML / XML markup marker, ignoring a leading
+ /// UTF-8 BOM and ASCII whitespace.
+ ///
+ /// No artifact this downloader fetches legitimately begins with `<`: CoreML compiled
+ /// `.mlmodelc` / `.mlpackage` payloads are binary (`coremldata.bin`, `weights/weight.bin`,
+ /// `model.mlmodel` protobuf) or JSON (`metadata.json`, `Manifest.json`) starting with
+ /// `{` / `[`; the MIL program text (`model.mil`) starts with `program`; the vocab JSON
+ /// starts with `{`; and `tokenizer.model` is a SentencePiece binary. So any payload that,
+ /// after BOM + whitespace stripping, starts with `<` followed by a markup-ish byte is a
+ /// proxy/block page or a markup document standing in for the real file — reject it. This
+ /// catches `<!doctype`, `<html`, `<head>`, `<body>`, `<script>`, `<meta>`, comments
+ /// (`<!-- -->`) and XML / `<?xml` declarations, not just the two prefixes we used to
+ /// match. See issue #353.
+ static func looksLikeHTML(_ data: Data) -> Bool {
+ var bytes = [UInt8](data.prefix(512))
+ if bytes.starts(with: [0xEF, 0xBB, 0xBF]) {
+ bytes.removeFirst(3)
+ }
+ while let first = bytes.first,
+ first == 0x20 || first == 0x09 || first == 0x0A || first == 0x0D {
+ bytes.removeFirst()
+ }
+ // Must begin with `<` (0x3C)…
+ guard bytes.first == 0x3C, bytes.count >= 2 else {
+ return false
+ }
+ // …immediately followed by a markup-ish byte: an ASCII letter (a tag such as
+ // `<html`), `!` (0x21 — `<!doctype`, `<!--`), `?` (0x3F — `<?xml`), or `/` (0x2F —
+ // a stray closing tag). Requiring this second byte avoids over-rejecting a
+ // hypothetical text artifact that merely contains a stray `<` not followed by markup.
+ let second = bytes[1]
+ let isAsciiLetter = (second >= 0x41 && second <= 0x5A) || (second >= 0x61 && second <= 0x7A)
+ return isAsciiLetter || second == 0x21 || second == 0x3F || second == 0x2F
+ }
+
+ private static func invalidContentError(relativePath: String, detail: String) -> NSError {
+ NSError(domain: "HF", code: -3, userInfo: [
+ NSLocalizedDescriptionKey:
+ "Could not download \(relativePath): \(detail). A network proxy or firewall may be blocking model downloads.",
+ ])
+ }
+
private final class DownloadProgressDelegate: NSObject, URLSessionDownloadDelegate {
private let onProgress: ((Double) -> Void)?
var onFinish: ((URL, URLResponse) -> Void)?
diff --git a/Sources/Fluid/UI/WelcomeView.swift b/Sources/Fluid/UI/WelcomeView.swift
index 96c02372..86281924 100644
--- a/Sources/Fluid/UI/WelcomeView.swift
+++ b/Sources/Fluid/UI/WelcomeView.swift
@@ -1903,6 +1903,12 @@ struct OnboardingFlowView: View {
try await self.asr.ensureAsrReady()
} catch {
DebugLogger.shared.error("Failed to prepare onboarding voice model \(route.model.displayName): \(error)", source: "OnboardingFlowView")
+ // Surface the failure in the UI instead of only logging it, so the user
+ // isn't stuck at a disabled button. The shared ContentView alert (bound to
+ // asr.showError) presents this during onboarding. See #355.
+ self.asr.errorTitle = "Voice Model Setup Failed"
+ self.asr.errorMessage = error.localizedDescription
+ self.asr.showError = true
}
await self.asr.checkIfModelsExistAsync()
}
diff --git a/Tests/FluidDictationIntegrationTests/DictationE2ETests.swift b/Tests/FluidDictationIntegrationTests/DictationE2ETests.swift
index ec528172..818c1c1e 100644
--- a/Tests/FluidDictationIntegrationTests/DictationE2ETests.swift
+++ b/Tests/FluidDictationIntegrationTests/DictationE2ETests.swift
@@ -519,6 +519,96 @@ final class DictationE2ETests: XCTestCase {
XCTAssertFalse(SimpleUpdater.isRollbackVersion(nil, differentFrom: "1.5.11-beta.3"))
}
+ // MARK: - Model download HTML/markup rejection (#353)
+
+ func testLooksLikeHTML_rejectsMarkupVariants() {
+ // A proxy/block page or stand-in markup document must be rejected regardless of
+ // which markup token it opens with — not just <!doctype / <html.
+ let rejected = [
+ "<!DOCTYPE html><html lang=\"en\"><head></head></html>",
+ "<html><body>Blocked by corporate proxy</body></html>",
+ "<script>window.location='https://proxy'</script>",
+ "<head><title>Access Denied</title></head>",
+ "<body>Forbidden</body>",
+ "<meta http-equiv=\"refresh\" content=\"0\">",
+ "<!-- corporate gateway notice -->",
+ "<?xml version=\"1.0\" encoding=\"UTF-8\"?><error>blocked</error>",
+ "</html>",
+ "<!doctype HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\">",
+ ]
+ for markup in rejected {
+ XCTAssertTrue(
+ HuggingFaceModelDownloader.looksLikeHTML(Data(markup.utf8)),
+ "Expected markup to be rejected: \(markup)"
+ )
+ }
+ }
+
+ func testLooksLikeHTML_rejectsLeadingWhitespaceAndBOMVariants() {
+ let bom: [UInt8] = [0xEF, 0xBB, 0xBF]
+
+ // Leading ASCII whitespace before the markup token.
+ XCTAssertTrue(HuggingFaceModelDownloader.looksLikeHTML(Data(" \n\t<!DOCTYPE html>".utf8)))
+ XCTAssertTrue(HuggingFaceModelDownloader.looksLikeHTML(Data("\r\n <html>".utf8)))
+
+ // UTF-8 BOM, then markup.
+ XCTAssertTrue(HuggingFaceModelDownloader.looksLikeHTML(Data(bom + Array("<html>".utf8))))
+
+ // BOM, then whitespace, then an XML declaration.
+ XCTAssertTrue(
+ HuggingFaceModelDownloader.looksLikeHTML(Data(bom + Array(" \n<?xml version=\"1.0\"?>".utf8)))
+ )
+ }
+
+ func testLooksLikeHTML_acceptsModelArtifacts() {
+ // JSON object (vocab / metadata / Manifest) — note the embedded `<pad>` must NOT
+ // trip the detector; only a LEADING `<` does.
+ XCTAssertFalse(HuggingFaceModelDownloader.looksLikeHTML(Data("{\"0\": \"<pad>\", \"1\": \"a\"}".utf8)))
+ // JSON array body.
+ XCTAssertFalse(HuggingFaceModelDownloader.looksLikeHTML(Data("[1, 2, 3]".utf8)))
+ // MIL program text (`model.mil`).
+ XCTAssertFalse(HuggingFaceModelDownloader.looksLikeHTML(Data("program(1.0)\n[buildInfo = ...]".utf8)))
+ // Binary CoreML / Mach-O magic prefix.
+ XCTAssertFalse(HuggingFaceModelDownloader.looksLikeHTML(Data([0xCF, 0xFA, 0xED, 0xFE, 0x07, 0x00])))
+ // Leading-NUL binary (e.g. coremldata.bin / weight.bin style payloads).
+ XCTAssertFalse(HuggingFaceModelDownloader.looksLikeHTML(Data([0x00, 0x00, 0x01, 0x3C, 0x68])))
+ // Empty payload.
+ XCTAssertFalse(HuggingFaceModelDownloader.looksLikeHTML(Data()))
+ // A stray `<` NOT followed by a markup-ish byte must not be over-rejected.
+ XCTAssertFalse(HuggingFaceModelDownloader.looksLikeHTML(Data("< not markup".utf8)))
+ XCTAssertFalse(HuggingFaceModelDownloader.looksLikeHTML(Data("<".utf8)))
+ }
+
+ func testValidateDownloadedFile_rejectsHTMLBodyAndAcceptsJSON() throws {
+ let dir = FileManager.default.temporaryDirectory
+ .appendingPathComponent("FluidVoice-ValidateTest-\(UUID().uuidString)", isDirectory: true)
+ try FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true)
+ defer { try? FileManager.default.removeItem(at: dir) }
+
+ // HTML body written without an HTML Content-Type (response: nil) must still be
+ // rejected by the byte-sniff path.
+ let htmlURL = dir.appendingPathComponent("coremldata.bin")
+ try Data("<!DOCTYPE html><html><body>Blocked</body></html>".utf8).write(to: htmlURL)
+ XCTAssertThrowsError(
+ try HuggingFaceModelDownloader.validateDownloadedFile(
+ at: htmlURL,
+ response: nil,
+ relativePath: "coremldata.bin"
+ )
+ )
+
+ // A real JSON vocab payload must pass validation.
+ let jsonURL = dir.appendingPathComponent("parakeet_v3_vocab.json")
+ try Data("{\"0\": \"<pad>\", \"1\": \"the\"}".utf8).write(to: jsonURL)
+ XCTAssertNoThrow(
+ try HuggingFaceModelDownloader.validateDownloadedFile(
+ at: jsonURL,
+ response: nil,
+ relativePath: "parakeet_v3_vocab.json"
+ )
+ )
+ }
+
private static func modelDirectoryForRun() -> URL {
// Use a stable path on CI so GitHub Actions cache can speed up runs.
if ProcessInfo.processInfo.environment["GITHUB_ACTIONS"] == "true" ||