From 3e230e4ba936bd859d83706ac9bf9e41605476fd Mon Sep 17 00:00:00 2001 From: Shx Guo Date: Tue, 1 Aug 2023 17:14:24 +0800 Subject: [PATCH 1/2] Disallow reading files larger than 15MB in notifyOpenTextDocument --- Core/Sources/Service/Workspace.swift | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Core/Sources/Service/Workspace.swift b/Core/Sources/Service/Workspace.swift index 247fa2f3..021b0c21 100644 --- a/Core/Sources/Service/Workspace.swift +++ b/Core/Sources/Service/Workspace.swift @@ -401,6 +401,13 @@ extension Workspace { refreshUpdateTime() openedFileRecoverableStorage.openFile(fileURL: filespace.fileURL) Task { + // check if file size is larger than 15MB, if so, return immediately + if let attrs = try? FileManager.default + .attributesOfItem(atPath: filespace.fileURL.path), + let fileSize = attrs[FileAttributeKey.size] as? UInt64, + fileSize > 15 * 1024 * 1024 + { return } + try await suggestionService?.notifyOpenTextDocument( fileURL: filespace.fileURL, content: try String(contentsOf: filespace.fileURL, encoding: .utf8) From a211955b588dc1cdd7d285d749f10348ef50721e Mon Sep 17 00:00:00 2001 From: Shx Guo Date: Tue, 1 Aug 2023 17:24:11 +0800 Subject: [PATCH 2/2] Ignore mlmodel files when creating Filespace --- Core/Sources/Service/Workspace.swift | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/Core/Sources/Service/Workspace.swift b/Core/Sources/Service/Workspace.swift index 021b0c21..9d18c0f1 100644 --- a/Core/Sources/Service/Workspace.swift +++ b/Core/Sources/Service/Workspace.swift @@ -129,6 +129,13 @@ final class Workspace { "Suggestion feature is disabled for this project." } } + + struct UnsupportedFileError: Error, LocalizedError { + var extensionName: String + var errorDescription: String? { + "File type \(extensionName) unsupported." + } + } let projectRootURL: URL let openedFileRecoverableStorage: OpenedFileRecoverableStorage @@ -223,6 +230,11 @@ final class Workspace { static func fetchOrCreateWorkspaceIfNeeded(fileURL: URL) async throws -> (workspace: Workspace, filespace: Filespace) { + let ignoreFileExtensions = ["mlmodel"] + if ignoreFileExtensions.contains(fileURL.pathExtension) { + throw UnsupportedFileError(extensionName: fileURL.pathExtension) + } + // If we know which project is opened. if let currentProjectURL = try await Environment.fetchCurrentProjectRootURLFromXcode() { if let existed = workspaces[currentProjectURL] {