From 46d55a8ada05179e99a7ce2e7a89edd017eabe54 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Sat, 6 Dec 2025 05:21:07 +0100 Subject: [PATCH] fix: harden model catalog parsing --- apps/macos/Sources/Clawdis/AppMain.swift | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/apps/macos/Sources/Clawdis/AppMain.swift b/apps/macos/Sources/Clawdis/AppMain.swift index 466689363..4fc53b8be 100644 --- a/apps/macos/Sources/Clawdis/AppMain.swift +++ b/apps/macos/Sources/Clawdis/AppMain.swift @@ -1096,15 +1096,19 @@ enum ModelCatalogLoader { } private static func sanitize(source: String) -> String { - var text = source - text = text.replacingOccurrences(of: #"(?m)^import[^\n]*\n"#, with: "", options: .regularExpression) - text = text.replacingOccurrences( - of: #"export\s+const\s+MODELS"#, - with: "var MODELS", + guard let exportRange = source.range(of: "export const MODELS"), + let firstBrace = source[exportRange.upperBound...].firstIndex(of: "{"), + let lastBrace = source.lastIndex(of: "}") + else { + return "var MODELS = {}" + } + var body = String(source[firstBrace...lastBrace]) + body = body.replacingOccurrences( + of: #"satisfies\s+[A-Za-z0-9_<>.,\-\s]+"#, + with: "", options: .regularExpression) - text = text.replacingOccurrences(of: #"satisfies\s+Model<[^>]+>"#, with: "", options: .regularExpression) - text = text.replacingOccurrences(of: #"as\s+Model<[^>]+>"#, with: "", options: .regularExpression) - return text + body = body.replacingOccurrences(of: #"as\s+[A-Za-z0-9_<>.,\-\s]+"#, with: "", options: .regularExpression) + return "var MODELS = \(body);" } }