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: harden api audio inputs
altic-dev10652e9c0a35
diff --git a/Sources/Fluid/Persistence/DictationAudioHistoryStore.swift b/Sources/Fluid/Persistence/DictationAudioHistoryStore.swiftindex dfe747f5..c3457842 100644--- a/Sources/Fluid/Persistence/DictationAudioHistoryStore.swift+++ b/Sources/Fluid/Persistence/DictationAudioHistoryStore.swift@@ -74,7 +74,7 @@ final nonisolated class DictationAudioHistoryStore: @unchecked Sendable {func audioFileURL(for entry: TranscriptionHistoryEntry) -> URL? {guard let audio = entry.audio else { return nil }- return try? self.audioDirectory(createIfNeeded: false).appendingPathComponent(audio.fileName, isDirectory: false)+ return self.audioFileURL(fileName: audio.fileName, createIfNeeded: false)}func audioFileExists(for entry: TranscriptionHistoryEntry) -> Bool {@@ -84,7 +84,7 @@ final nonisolated class DictationAudioHistoryStore: @unchecked Sendable {@discardableResultfunc deleteAudio(fileName: String) -> Int64 {- guard let url = try? self.audioDirectory(createIfNeeded: false).appendingPathComponent(fileName, isDirectory: false) else {+ guard let url = self.audioFileURL(fileName: fileName, createIfNeeded: false) else {return 0}@@ -261,6 +261,31 @@ final nonisolated class DictationAudioHistoryStore: @unchecked Sendable {return directory}+ private func audioFileURL(fileName: String, createIfNeeded: Bool) -> URL? {+ guard let safeFileName = self.safeAudioFileName(fileName),+ let directory = try? self.audioDirectory(createIfNeeded: createIfNeeded)+ else {+ return nil+ }+ return directory.appendingPathComponent(safeFileName, isDirectory: false)+ }++ private func safeAudioFileName(_ fileName: String) -> String? {+ let trimmed = fileName.trimmingCharacters(in: .whitespacesAndNewlines)+ guard !trimmed.isEmpty else { return nil }++ let lastPathComponent = URL(fileURLWithPath: trimmed).lastPathComponent+ guard trimmed == lastPathComponent,+ lastPathComponent != ".",+ lastPathComponent != "..",+ URL(fileURLWithPath: lastPathComponent).pathExtension.lowercased() == "wav"+ else {+ return nil+ }++ return lastPathComponent+ }+private func audioFileName(entryID: UUID, timestamp: Date) -> String {"\(Self.fileTimestampFormatter.string(from: timestamp))_\(entryID.uuidString.prefix(8)).wav"}diff --git a/Sources/Fluid/Services/ASRService.swift b/Sources/Fluid/Services/ASRService.swiftindex b1b6f78b..70b82adf 100644--- a/Sources/Fluid/Services/ASRService.swift+++ b/Sources/Fluid/Services/ASRService.swift@@ -1203,6 +1203,8 @@ final class ASRService: ObservableObject {)}+ let estimatedSamples = try LocalAPIAudioDecoder.validateDurationWithinLimit(for: fileURL)+try await self.ensureAsrReady()let provider = self.transcriptionProviderguard provider.isReady else {@@ -1213,7 +1215,6 @@ final class ASRService: ObservableObject {)}- let estimatedSamples = Self.estimatedMono16kSampleCount(for: fileURL)guard provider.prefersNativeFileTranscription else {let samples = try LocalAPIAudioDecoder.samples(from: fileURL)let result = try await self.transcribeSamplesForAPI(samples)@@ -1234,16 +1235,6 @@ final class ASRService: ObservableObject {return (ASRTranscriptionResult(text: cleanedText, confidence: result.confidence), estimatedSamples)}- private static func estimatedMono16kSampleCount(for fileURL: URL) -> Int {- guard- let file = try? AVAudioFile(forReading: fileURL),- file.processingFormat.sampleRate > 0- else {- return 0- }- return Int((Double(file.length) * 16_000.0 / file.processingFormat.sampleRate).rounded())- }-func stopWithoutTranscription() async {guard self.isRunning else { return }defer { self.applyPendingParakeetVocabularyReloadIfNeeded() }diff --git a/Sources/Fluid/Services/LocalAPI/LocalAPIAudioDecoder.swift b/Sources/Fluid/Services/LocalAPI/LocalAPIAudioDecoder.swiftindex b6ee73c0..83b0b5a4 100644--- a/Sources/Fluid/Services/LocalAPI/LocalAPIAudioDecoder.swift+++ b/Sources/Fluid/Services/LocalAPI/LocalAPIAudioDecoder.swift@@ -3,7 +3,7 @@ import Foundationenum LocalAPIAudioDecoder {static let sampleRate: Double = 16_000- private static let maxDurationSeconds: Double = 300+ static let maxDurationSeconds: Double = 300static func samples(from fileURL: URL) throws -> [Float] {let file = try AVAudioFile(forReading: fileURL)@@ -35,6 +35,25 @@ enum LocalAPIAudioDecoder {return try self.samples(from: url)}+ static func validateDurationWithinLimit(for fileURL: URL) throws -> Int {+ let file = try AVAudioFile(forReading: fileURL)+ let sourceFormat = file.processingFormat+ guard sourceFormat.sampleRate > 0 else {+ throw NSError(domain: "LocalAPIAudioDecoder", code: -6, userInfo: [NSLocalizedDescriptionKey: "Audio file has an invalid sample rate."])+ }++ let maxFrames = AVAudioFramePosition(sourceFormat.sampleRate * self.maxDurationSeconds)+ guard file.length <= maxFrames else {+ throw NSError(+ domain: "LocalAPIAudioDecoder",+ code: -5,+ userInfo: [NSLocalizedDescriptionKey: "Audio file exceeds the \(Int(self.maxDurationSeconds)) second API limit."]+ )+ }++ return Int((Double(file.length) * self.sampleRate / sourceFormat.sampleRate).rounded())+ }+private static func convertToMono16k(_ sourceBuffer: AVAudioPCMBuffer) throws -> [Float] {guard let targetFormat = AVAudioFormat(commonFormat: .pcmFormatFloat32,