From 776aa22602bb0760988d1e2b0e8be32ee0b6be40 Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Thu, 12 Nov 2026 05:51:46 +0700 Subject: [PATCH 1/7] fix(macos): hold Read-Only Safe Mode at the server, bound MCP and assistant answers, narrow the assistant's tools and refuse MCP file access --- .../SQLFileNamedAsTable.swift | 81 ++- .../PluginReadOnlyModeTests.swift | 131 ++++ .../SQLFileNamedAsTableTests.swift | 29 + .../SQLOutsideReachNamesTests.swift | 35 + .../TableProMCP/Auth/MCPPrincipal.swift | 11 +- .../Protocol/Tools/MCPToolSchema.swift | 8 +- .../Auth/MCPPrincipalToolAllowlistTests.swift | 56 ++ .../ClickHousePlugin.swift | 119 +++- .../ClickHousePluginDriver+Http.swift | 58 +- .../ClickHouseReadOnlyRequestTests.swift | 136 +++- .../DuckDBDriverPlugin/DuckDBConnection.swift | 57 +- .../DuckDBDriverPlugin/DuckDBPlugin.swift | 43 +- .../DuckDBReadOnlyStatement.swift | 24 +- .../MSSQLDriverPlugin/MSSQLPlugin.swift | 20 + .../MariaDBPluginConnection.swift | 99 ++- .../MySQLDriverPlugin/MySQLPluginDriver.swift | 240 ++++--- .../MySQLReadOnlyTransaction.swift | 63 +- .../MySQLSelectLimitStatement.swift | 23 +- .../MySQLReadOnlyTransactionTests.swift | 121 +++- .../MySQLSelectLimitTests.swift | 27 + .../CockroachPluginDriver.swift | 1 + .../LibPQDriverCore.swift | 127 +++- .../LibPQPluginConnection.swift | 300 ++++++-- .../LibPQReadBound.swift | 49 ++ .../LibPQReadOnlyTransaction.swift | 60 +- .../PostgreSQLPluginDriver.swift | 3 +- .../LibPQBoundedReadOnlyTests.swift | 218 ++++++ .../LibPQReadBoundTests.swift | 57 ++ .../LibPQReadOnlyTransactionTests.swift | 101 ++- .../LibPQScriptedServer.swift | 215 +++++- .../SQLiteAgentBackend.swift | 40 +- .../SQLiteExecutionBackend.swift | 152 ++++- .../SQLiteDriverPlugin/SQLitePlugin.swift | 85 ++- .../SQLiteReadOnlyStatementTests.swift | 175 ++++- .../PluginCapabilities.swift | 3 + .../PluginDatabaseDriver.swift | 22 + .../PluginStatementContext.swift | 18 +- .../Core/AI/CLI/AgentCLIProcess.swift | 35 +- .../Core/AI/Chat/ChatToolCallRepair.swift | 27 +- .../TablePro/Core/AI/Chat/ChatTransport.swift | 15 +- .../Core/AI/Chat/ToolApprovalCenter.swift | 7 + .../ConfirmDestructiveOperationChatTool.swift | 3 +- .../AI/Chat/Tools/ExecuteQueryChatTool.swift | 3 +- .../Chat/Tools/ListConnectionsChatTool.swift | 5 +- .../AI/ClaudeAgent/ClaudeAgentMCPBridge.swift | 24 +- .../AI/ClaudeAgent/ClaudeAgentProvider.swift | 17 +- .../Core/Coordinators/ExactRowCounter.swift | 18 +- .../Coordinators/PaginationCoordinator.swift | 59 +- .../QueryExecutionCoordinator+Helpers.swift | 29 +- ...yExecutionCoordinator+MultiStatement.swift | 31 +- ...QueryExecutionCoordinator+Parameters.swift | 9 +- .../Access/DatabaseAccessBridge+Scripts.swift | 123 +++- .../Access/DatabaseAccessBridge.swift | 54 +- .../Database/Access/DatabaseAccessError.swift | 4 + .../Access/ExternalAnswerBudget.swift | 210 ++++++ .../Core/Database/DatabaseDriver.swift | 26 + apple/TablePro/Core/LSP/LSPTransport.swift | 35 +- .../Auth/MCPBearerTokenAuthenticator.swift | 11 +- .../Legacy/Handlers/InitializeHandler.swift | 19 +- .../Core/MCP/Legacy/MCPLegacyEraAdapter.swift | 2 +- apple/TablePro/Core/MCP/MCPAuthPolicy.swift | 15 +- .../Core/MCP/MCPConnectionBridge+Data.swift | 54 +- .../Core/MCP/MCPConnectionBridge+Server.swift | 36 +- .../Core/MCP/MCPConnectionBridge.swift | 57 +- apple/TablePro/Core/MCP/MCPTokenStore.swift | 77 ++- .../MCP/Prompts/MCPPromptSchemaReader.swift | 2 +- .../Handlers/ResourcesReadHandler.swift | 2 +- .../Protocol/Handlers/ToolsCallHandler.swift | 12 + .../Protocol/Handlers/ToolsListHandler.swift | 5 +- .../MCP/Protocol/Tools/ExplainQueryTool.swift | 33 +- .../MCP/Protocol/Tools/ExportDataTool.swift | 25 +- .../Protocol/Tools/FocusQueryTabTool.swift | 25 +- .../Protocol/Tools/ListRecentTabsTool.swift | 35 +- .../Tools/MCPTabSnapshotProvider.swift | 26 +- .../Tools/MCPToolExecutionError.swift | 10 +- .../Tools/MCPToolImplementation.swift | 10 + .../Tools/SearchQueryHistoryTool.swift | 39 +- .../Protocol/Tools/ToolQueryExecutor.swift | 64 +- .../Plugins/ExportDataSourceAdapter.swift | 23 +- .../Plugins/PluginDriverAdapter+Export.swift | 17 +- .../PluginDriverAdapter+ReadOnlyMode.swift | 26 + .../Core/Plugins/PluginDriverAdapter.swift | 6 +- ...uginMetadataRegistry+CuratedDefaults.swift | 12 + .../StreamingQueryExportDataSource.swift | 10 +- .../Core/Process/PipeLineSplitter.swift | 34 + apple/TablePro/Core/Process/PipeReader.swift | 57 +- .../Process/SupervisedProcessRunner.swift | 15 +- .../Core/Scripting/ScriptingError.swift | 2 +- .../Execution/BatchTransactionPolicy.swift | 8 +- .../Execution/DefaultExecutionGate.swift | 47 ++ .../Execution/ExecutionGateProvider.swift | 62 +- .../Execution/ExternalStatementGate.swift | 9 + .../Execution/ReadOnlyFunctionCalls.swift | 374 ++++++++++ .../Core/Services/Export/ExportService.swift | 40 +- .../Export/TableTransferService.swift | 20 +- .../Core/Services/Query/QueryExecutor.swift | 53 +- apple/TablePro/Models/AI/AIModels.swift | 1 + apple/TablePro/Models/AI/AgentSession.swift | 18 - .../AIChatViewModel+Persistence.swift | 4 +- .../AIChatViewModel+Streaming.swift | 3 +- .../AIChatViewModel+ToolApproval.swift | 78 ++- .../TablePro/ViewModels/AIChatViewModel.swift | 3 +- .../ViewModels/DashboardTileBoard.swift | 11 +- .../ViewModels/ServerDashboardViewModel.swift | 17 +- .../Views/AIChat/AIChatPanelView.swift | 6 +- .../Views/AIChat/AIChatToolUseBlockView.swift | 9 +- .../ConnectionFormCoordinator.swift | 3 +- .../Panes/OptionsPaneView.swift | 18 + .../Support/ConnectionFormEdits.swift | 3 + .../ViewModels/AdvancedPaneViewModel.swift | 9 + .../MainContentCoordinator+Explain.swift | 3 +- ...nContentCoordinator+ReadOnlySafeMode.swift | 53 ++ ...ainContentCoordinator+UnloadedValues.swift | 14 +- .../Views/Main/MainContentCoordinator.swift | 12 +- .../Drivers/DuckDBSafeModeReadOnlyTests.swift | 117 ++++ .../Core/AI/AgentCLIProcessTests.swift | 80 +++ .../Chat/ListConnectionsChatToolTests.swift | 97 +++ .../Core/AI/ClaudeAgentAskProfileTests.swift | 225 ++++++ .../AI/ClaudeAgentConnectionScopeTests.swift | 31 +- .../Core/AI/ClaudeAgentProviderTests.swift | 37 +- .../Core/AI/ToolApprovalCenterTests.swift | 19 + ...atabaseAccessBridgeAnswerBudgetTests.swift | 257 +++++++ .../Database/ExternalAnswerBudgetTests.swift | 174 +++++ .../ProtectedWriteDeadlineTests.swift | 16 +- .../MCP/Legacy/InitializeHandlerTests.swift | 43 +- .../MCP/MCPAggregateReadApprovalTests.swift | 315 +++++++++ .../MCP/MCPAnswerBudgetPayloadTests.swift | 159 +++++ .../MCP/MCPHistoryAnswerBudgetTests.swift | 97 +++ .../Handlers/ToolsCallHandlerTests.swift | 98 +++ .../Handlers/ToolsListHandlerTests.swift | 24 + .../Tools/ExportDataBudgetTests.swift | 70 ++ .../Tools/MCPToolErrorSurfaceTests.swift | 6 + .../Protocol/Tools/MCPToolTestHarness.swift | 17 +- .../Tools/StoppedWriteOutcomeTests.swift | 218 ++++++ ...inDriverAdapterStatementContextTests.swift | 139 +++- ...luginMetadataRegistryKingbaseESTests.swift | 9 + .../Core/Process/PipeLineSplitterTests.swift | 30 + .../BatchTransactionPolicyTests.swift | 12 + .../Execution/ExecutionGateTests.swift | 305 +++++++++ .../Helpers/AnsweringPluginDriver.swift | 92 +++ .../ViewModels/AIChatAlwaysAllowTests.swift | 136 ++++ .../AIChatStoppedToolRoundTests.swift | 205 ++++++ .../ViewModels/AIChatStoppedWriteTests.swift | 130 ++++ .../ViewModels/ConnectionFormEditsTests.swift | 29 +- .../AdvancedPaneViewModelTests.swift | 28 + .../ReadOnlySafeModeEntryPointTests.swift | 544 +++++++++++++++ .../check-postgresql-read-only-functions.sh | 59 ++ core/crates/core/src/external_access.rs | 3 + core/crates/core/src/sql_syntax/mod.rs | 2 + .../core/src/sql_syntax/outside_reach.rs | 640 ++++++++++++++++++ .../core/src/sql_syntax/statement_access.rs | 24 +- .../core/tests/fixtures/outside_reach.json | 138 ++++ core/crates/mcp/src/query_tools.rs | 10 +- core/crates/mcp/tests/mcp_server.rs | 22 + 154 files changed, 9205 insertions(+), 832 deletions(-) create mode 100644 apple/Packages/TableProCore/Tests/TableProPluginKitTests/PluginReadOnlyModeTests.swift create mode 100644 apple/Packages/TableProCore/Tests/TableProSQLGrammarTests/SQLOutsideReachNamesTests.swift create mode 100644 apple/Packages/TableProMacCore/Tests/TableProMCPTests/Auth/MCPPrincipalToolAllowlistTests.swift create mode 100644 apple/Plugins/PostgreSQLDriverPlugin/LibPQReadBound.swift create mode 100644 apple/Plugins/PostgreSQLDriverTests/LibPQBoundedReadOnlyTests.swift create mode 100644 apple/Plugins/PostgreSQLDriverTests/LibPQReadBoundTests.swift create mode 100644 apple/TablePro/Core/Database/Access/ExternalAnswerBudget.swift create mode 100644 apple/TablePro/Core/Plugins/PluginDriverAdapter+ReadOnlyMode.swift create mode 100644 apple/TablePro/Core/Process/PipeLineSplitter.swift create mode 100644 apple/TablePro/Core/Services/Execution/ReadOnlyFunctionCalls.swift create mode 100644 apple/TablePro/Views/Main/Extensions/MainContentCoordinator+ReadOnlySafeMode.swift create mode 100644 apple/TableProMobile/TableProMobileTests/Drivers/DuckDBSafeModeReadOnlyTests.swift create mode 100644 apple/TableProTests/Core/AI/Chat/ListConnectionsChatToolTests.swift create mode 100644 apple/TableProTests/Core/AI/ClaudeAgentAskProfileTests.swift create mode 100644 apple/TableProTests/Core/Database/DatabaseAccessBridgeAnswerBudgetTests.swift create mode 100644 apple/TableProTests/Core/Database/ExternalAnswerBudgetTests.swift create mode 100644 apple/TableProTests/Core/MCP/MCPAggregateReadApprovalTests.swift create mode 100644 apple/TableProTests/Core/MCP/MCPAnswerBudgetPayloadTests.swift create mode 100644 apple/TableProTests/Core/MCP/MCPHistoryAnswerBudgetTests.swift create mode 100644 apple/TableProTests/Core/MCP/Protocol/Tools/ExportDataBudgetTests.swift create mode 100644 apple/TableProTests/Core/MCP/Protocol/Tools/StoppedWriteOutcomeTests.swift create mode 100644 apple/TableProTests/Core/Process/PipeLineSplitterTests.swift create mode 100644 apple/TableProTests/Helpers/AnsweringPluginDriver.swift create mode 100644 apple/TableProTests/ViewModels/AIChatAlwaysAllowTests.swift create mode 100644 apple/TableProTests/ViewModels/AIChatStoppedWriteTests.swift create mode 100644 apple/TableProTests/Views/Main/ReadOnlySafeModeEntryPointTests.swift create mode 100755 apple/scripts/probes/check-postgresql-read-only-functions.sh create mode 100644 core/crates/core/src/sql_syntax/outside_reach.rs create mode 100644 core/crates/core/tests/fixtures/outside_reach.json diff --git a/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLFileNamedAsTable.swift b/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLFileNamedAsTable.swift index 6029d61d64..98708c33b7 100644 --- a/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLFileNamedAsTable.swift +++ b/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLFileNamedAsTable.swift @@ -1,7 +1,7 @@ import Foundation // DuckDB reads a file named as a table. A quoted identifier, in `"` or a backtick, counts only with a `/`, a `.` or a -// `\`. +// `\`. So does a dotted name ending in a file extension, and a table function DuckDB does not keep inside the database. public enum SQLFileNamedAsTable { private static let tableIntroducers: Set = [ "FROM", "JOIN", "USING", "TABLE", "DESCRIBE", "DESC", "SHOW", "SUMMARIZE", "PIVOT", "UNPIVOT", @@ -16,6 +16,23 @@ public enum SQLFileNamedAsTable { // `extract(year FROM '2026-01-01'::DATE)` separates arguments with FROM. private static let functionsTakingFrom: Set = ["EXTRACT", "TRIM", "SUBSTRING", "OVERLAY"] + // Only a FROM or JOIN list holds table functions; PIVOT's and USING's parentheses hold expressions. + private static let callingIntroducers: Set = ["FROM", "JOIN"] + + // An allowlist: an extension DuckDB loads on its own brings table functions no list here knows. + static let tableFunctionsInsideDatabase: Set = [ + "RANGE", "GENERATE_SERIES", "UNNEST", "REPEAT", "JSON_EACH", "JSON_TREE", "DUCKDB_COLUMNS", + "DUCKDB_CONSTRAINTS", "DUCKDB_DATABASES", "DUCKDB_DEPENDENCIES", "DUCKDB_FUNCTIONS", "DUCKDB_INDEXES", + "DUCKDB_KEYWORDS", "DUCKDB_SCHEMAS", "DUCKDB_SEQUENCES", "DUCKDB_SETTINGS", "DUCKDB_TABLES", "DUCKDB_TYPES", + "DUCKDB_VARIABLES", "DUCKDB_VIEWS", "PRAGMA_DATABASE_SIZE", "PRAGMA_PLATFORM", "PRAGMA_SHOW", + "PRAGMA_STORAGE_INFO", "PRAGMA_TABLE_INFO", "PRAGMA_VERSION", + ] + + // DuckDB joins a dotted table name with `.` and reads it as a file in the working folder. + static let fileExtensions: Set = [ + "CSV", "TSV", "GZ", "ZST", "PARQUET", "JSON", "JSONL", "NDJSON", "GEOJSON", "GEOJSONL", "DB", "DDB", "DUCKDB", + ] + private static let nameContinuations = Set(".:&".utf16) private static let pathMarks = CharacterSet(charactersIn: "/.\\") @@ -31,6 +48,8 @@ public enum SQLFileNamedAsTable { var separatesArgumentsWithFrom = false var listsTables = false var expects = Expectation.nothing + var introducer = "" + var nameParts = 0 } public static func appears(in statement: String, grammar: SQLLexicalGrammar) -> Bool { @@ -46,32 +65,53 @@ public enum SQLFileNamedAsTable { break case .word(let word) where tableIntroducers.contains(word): levels[top].expects = .table + levels[top].introducer = word + levels[top].nameParts = 0 if word == "FROM" { levels[top].listsTables = true } case .word(let word) where tableListEnders.contains(word) && expected != .table: levels[top].listsTables = false - case .word("ONLY") where expected == .table: + case .word("ONLY") where expected == .table, .word("LATERAL") where expected == .table: levels[top].expects = .table - case .word, .name: + case .word(let word): + guard expected == .table else { break } + levels[top].nameParts += 1 + if levels[top].nameParts > 1, fileExtensions.contains(word) { return true } + levels[top].expects = .restOfName + case .name: if expected == .table { levels[top].expects = .restOfName } case .quoted: guard expected == .table else { break } if namesFile(at: token.location, in: source, grammar: grammar) { return true } + levels[top].nameParts += 1 + let quotedName = identifier(at: token.location, in: source, grammar: grammar)?.uppercased() + if levels[top].nameParts > 1, let quotedName, fileExtensions.contains(quotedName) { return true } levels[top].expects = .restOfName case .open: + if expected == .restOfName, callingIntroducers.contains(levels[top].introducer), + !callsTableFunctionInsideDatabase(before: index, in: tokens, source: source, grammar: grammar) { + return true + } // `FROM (notes JOIN 'payroll.csv' USING (id))` groups tables, and any other `(` starts afresh. levels.append( Parentheses( separatesArgumentsWithFrom: callsFunctionTakingFrom(before: index, in: tokens, source: source), - expects: expected == .table ? .table : .nothing + expects: expected == .table ? .table : .nothing, + introducer: expected == .table ? levels[top].introducer : "" ) ) case .close: if top > 0 { levels.removeLast() } case .comma: - if levels[top].listsTables { levels[top].expects = .table } + if levels[top].listsTables { + levels[top].expects = .table + levels[top].nameParts = 0 + } case .other: - let continuesName = nameContinuations.contains(source.character(at: token.location)) - if expected == .restOfName, continuesName { levels[top].expects = .table } + let mark = source.character(at: token.location) + if expected == .restOfName, nameContinuations.contains(mark) { + levels[top].expects = .table + if mark != period { levels[top].nameParts = 0 } + } case .terminator: break } @@ -106,6 +146,33 @@ public enum SQLFileNamedAsTable { return tableIntroducers.first(where: word.hasSuffix).map { .word($0) } ?? token.kind } + // A name the scanner cannot read counts as unknown. + private static func callsTableFunctionInsideDatabase( + before index: Int, + in tokens: [TSQLTokens.Token], + source: NSString, + grammar: SQLLexicalGrammar + ) -> Bool { + guard index >= 1 else { return false } + let callee = tokens[index - 1] + let name: String? = switch callee.kind { + case .word(let word): word + case .quoted: identifier(at: callee.location, in: source, grammar: grammar)?.uppercased() + default: nil + } + guard let name else { return false } + return name == "VALUES" || tableFunctionsInsideDatabase.contains(name) + } + + private static func identifier(at location: Int, in source: NSString, grammar: SQLLexicalGrammar) -> String? { + guard [SqlLexer.doubleQuote, SqlLexer.backtick].contains(source.character(at: location)), + let span = SQLNonCodeSpan.span(at: location, in: source, grammar: grammar) + else { + return nil + } + return source.substring(with: NSRange(location: location + 1, length: max(0, span.contentEnd - location - 1))) + } + private static func namesFile(at location: Int, in source: NSString, grammar: SQLLexicalGrammar) -> Bool { guard [SqlLexer.doubleQuote, SqlLexer.backtick].contains(source.character(at: location)), let span = SQLNonCodeSpan.span(at: location, in: source, grammar: grammar) diff --git a/apple/Packages/TableProCore/Tests/TableProPluginKitTests/PluginReadOnlyModeTests.swift b/apple/Packages/TableProCore/Tests/TableProPluginKitTests/PluginReadOnlyModeTests.swift new file mode 100644 index 0000000000..c5a0183869 --- /dev/null +++ b/apple/Packages/TableProCore/Tests/TableProPluginKitTests/PluginReadOnlyModeTests.swift @@ -0,0 +1,131 @@ +import Foundation +import TableProPluginKit +import Testing + +struct PluginReadOnlyModeTests { + @Test("A new context asks for no Read-Only mode and no byte limit") + func contextDefaults() { + let context = PluginStatementContext() + + #expect(!context.enforcesReadOnly) + #expect(context.resultByteLimit == nil) + #expect(context == PluginStatementContext(enforcesReadOnly: false, resultByteLimit: nil)) + } + + @Test("The initializer sets both fields, and equality reads them") + func contextInitializerSetsTheFields() { + let context = PluginStatementContext(enforcesReadOnly: true, resultByteLimit: 4_096) + + #expect(context.enforcesReadOnly) + #expect(context.resultByteLimit == 4_096) + #expect(context != PluginStatementContext()) + #expect(PluginStatementContext(resultByteLimit: 1) != PluginStatementContext(resultByteLimit: 2)) + } + + @Test("The Read-Only capability is bit 24 and shares no bit with another capability") + func capabilityBit() { + let others: [PluginCapabilities] = [ + .materializedViews, .foreignTables, .storedProcedures, .userFunctions, .alterTableDDL, .foreignKeyToggle, + .truncateTable, .multiSchema, .parameterizedQueries, .cancelQuery, .batchExecute, .transactions, + .userManagement, .schemaCompare, .dataCompare, .resultSetBatches, .cellExpressions, .uuidCellText, + .temporaryTables, .rowWrites, .repeatingPrimaryKey, .readOnlyStatements, .statementPolicy, + .columnDefaultMarks, + ] + + #expect(PluginCapabilities.enforcesReadOnlyMode.rawValue == 1 << 24) + for other in others { + #expect(other.isDisjoint(with: .enforcesReadOnlyMode), "\(other.rawValue)") + } + } + + @Test("The default bounded read with a context runs the context-free one for a plain read") + func defaultForwardsAPlainRead() async throws { + let driver = BoundedReadDriver() + let context = PluginStatementContext(resultByteLimit: 1_024) + + let result = try await driver.executeBoundedQuery(query: "SELECT 1", rowCap: 5, context: context) + + #expect(result?.rows.count == 1) + #expect(driver.calls == [5]) + } + + @Test( + "The default bounded read with a context hands back what the context-free one cannot honor", + arguments: Restriction.allCases + ) + func defaultRefusesARestrictedRead(restriction: Restriction) async throws { + let driver = BoundedReadDriver() + + let result = try await driver.executeBoundedQuery(query: "SELECT 1", rowCap: 5, context: restriction.context) + + #expect(result == nil) + #expect(driver.calls.isEmpty) + } + + enum Restriction: CaseIterable, Sendable { + case enforcesReadOnly + case readOnly + case readsCatalog + case refusesDestructive + case refusesServerCode + + var context: PluginStatementContext { + var context = PluginStatementContext() + switch self { + case .enforcesReadOnly: context.enforcesReadOnly = true + case .readOnly: context.readOnly = true + case .readsCatalog: context.readsCatalog = true + case .refusesDestructive: context.allowsDestructive = false + case .refusesServerCode: context.allowsServerCode = false + } + return context + } + } +} + +private final class BoundedReadDriver: PluginDatabaseDriver, @unchecked Sendable { + let supportsSchemas = false + let supportsTransactions = false + let currentSchema: String? = nil + let serverVersion: String? = nil + + private let lock = NSLock() + private var rowCaps: [Int] = [] + + var calls: [Int] { + lock.withLock { rowCaps } + } + + func connect() async throws {} + func disconnect() {} + func ping() async throws {} + + func execute(query: String) async throws -> PluginQueryResult { + PluginQueryResult(columns: [], columnTypeNames: [], rows: [], rowsAffected: 0, executionTime: 0) + } + + func executeBoundedQuery(query: String, rowCap: Int) async throws -> PluginQueryResult? { + lock.withLock { rowCaps.append(rowCap) } + return PluginQueryResult( + columns: ["one"], + columnTypeNames: ["INTEGER"], + rows: [[.text("1")]], + rowsAffected: 0, + executionTime: 0 + ) + } + + func fetchTables(schema: String?) async throws -> [PluginTableInfo] { [] } + func fetchColumns(table: String, schema: String?) async throws -> [PluginColumnInfo] { [] } + func fetchIndexes(table: String, schema: String?) async throws -> [PluginIndexInfo] { [] } + func fetchForeignKeys(table: String, schema: String?) async throws -> [PluginForeignKeyInfo] { [] } + func fetchTableDDL(table: String, schema: String?) async throws -> String { "" } + func fetchViewDefinition(view: String, schema: String?) async throws -> String { "" } + func fetchTableMetadata(table: String, schema: String?) async throws -> PluginTableMetadata { + PluginTableMetadata(tableName: table) + } + func fetchDatabases() async throws -> [String] { [] } + func fetchDatabaseMetadata(_ database: String) async throws -> PluginDatabaseMetadata { + PluginDatabaseMetadata(name: database) + } +} diff --git a/apple/Packages/TableProCore/Tests/TableProSQLGrammarTests/SQLFileNamedAsTableTests.swift b/apple/Packages/TableProCore/Tests/TableProSQLGrammarTests/SQLFileNamedAsTableTests.swift index dc69bc15ba..4b9532357e 100644 --- a/apple/Packages/TableProCore/Tests/TableProSQLGrammarTests/SQLFileNamedAsTableTests.swift +++ b/apple/Packages/TableProCore/Tests/TableProSQLGrammarTests/SQLFileNamedAsTableTests.swift @@ -66,6 +66,30 @@ struct SQLFileNamedAsTableTests { #expect(namesFile(sql)) } + @Test("A table function DuckDB does not keep inside the database counts as one", arguments: [ + "SELECT * FROM quack_serve('x')", + "SELECT * FROM odbc_query('DSN=x', 'DROP TABLE t')", + "SELECT * FROM notes, cloudformation_create_stack('x')", + "SELECT * FROM notes JOIN avro_metadata('x') ON true", + "SELECT * FROM notes, LATERAL quack_query('x')", + "SELECT * FROM (FROM quack_serve('x'))", + "SELECT * FROM system.main.quack_serve('x')", + "SELECT * FROM \"quack_serve\"('x')", + ]) + func unknownTableFunctions(sql: String) { + #expect(namesFile(sql)) + } + + @Test("A dotted name ending in a file extension is a file in the working folder", arguments: [ + "SELECT * FROM finances.csv", + "SELECT * FROM notes.\"csv\"", + "SELECT * FROM backup.duckdb", + "SELECT * FROM notes, data.parquet", + ]) + func dottedFileNames(sql: String) { + #expect(namesFile(sql)) + } + @Test("A FROM that runs on from a number or an operator still lists tables", arguments: [ "SELECT 1FROM 'payroll.csv'", "SELECT 1e5FROM 'payroll.csv'", @@ -122,6 +146,11 @@ struct SQLFileNamedAsTableTests { "COMMENT ON TABLE notes IS 'a.csv'", "CREATE TABLE notes (path VARCHAR DEFAULT 'a.csv')", "SELECT _from, _desc, 'a.csv' FROM notes", + "SELECT * FROM main.range(1, 10)", + "SELECT * FROM duckdb_tables()", + "PIVOT notes ON year USING sum(salary)", + "SELECT * FROM notes JOIN tags ON f(tags.id) = notes.id", + "SELECT * FROM notes.csv_rows", ]) func valuesAndTableNames(sql: String) { #expect(!namesFile(sql)) diff --git a/apple/Packages/TableProCore/Tests/TableProSQLGrammarTests/SQLOutsideReachNamesTests.swift b/apple/Packages/TableProCore/Tests/TableProSQLGrammarTests/SQLOutsideReachNamesTests.swift new file mode 100644 index 0000000000..11a06fa769 --- /dev/null +++ b/apple/Packages/TableProCore/Tests/TableProSQLGrammarTests/SQLOutsideReachNamesTests.swift @@ -0,0 +1,35 @@ +import Foundation +@testable import TableProSQLGrammar +import Testing + +// The core keeps its own copy of these names for its MCP server, and both test suites compare theirs with one file. +struct SQLOutsideReachNamesTests { + private static let sharedNames = URL(fileURLWithPath: #filePath) + .deletingLastPathComponent() + .deletingLastPathComponent() + .deletingLastPathComponent() + .deletingLastPathComponent() + .deletingLastPathComponent() + .deletingLastPathComponent() + .appendingPathComponent("core/crates/core/tests/fixtures/outside_reach.json") + + @Test("Each list names what the core lists") + func listsMatchTheSharedNames() throws { + let data = try Data(contentsOf: Self.sharedNames) + let shared = try JSONDecoder().decode([String: [String]].self, from: data) + .mapValues { Set($0.map { $0.uppercased() }) } + let duckDB = SQLStatementTiering.duckDB + let clickHouse = SQLStatementTiering.clickHouse + let listed: [String: Set] = [ + "filesystem_word_runs": Set(SQLStatementTiering.filesystemWordRuns.map { $0.joined(separator: " ") }), + "filesystem_names": SQLStatementTiering.filesystemNames, + "filesystem_or_code_keywords": SQLStatementTiering.filesystemOrCodeKeywords, + "filesystem_functions": SQLStatementTiering.filesystemFunctions, + "duckdb_functions": SQLStatementTiering.functionsReachingOutside[duckDB] ?? [], + "duckdb_families": Set(SQLStatementTiering.functionFamiliesReachingOutside[duckDB] ?? []), + "clickhouse_functions": SQLStatementTiering.functionsReachingOutside[clickHouse] ?? [], + "clickhouse_families": Set(SQLStatementTiering.functionFamiliesReachingOutside[clickHouse] ?? []), + ] + #expect(listed == shared) + } +} diff --git a/apple/Packages/TableProMacCore/Sources/TableProMCP/Auth/MCPPrincipal.swift b/apple/Packages/TableProMacCore/Sources/TableProMCP/Auth/MCPPrincipal.swift index fe6855968c..c5b5fed9ee 100644 --- a/apple/Packages/TableProMacCore/Sources/TableProMCP/Auth/MCPPrincipal.swift +++ b/apple/Packages/TableProMacCore/Sources/TableProMCP/Auth/MCPPrincipal.swift @@ -28,19 +28,23 @@ public struct MCPPrincipal: Sendable, Equatable, Hashable { public let scopes: Set public let connectionAccess: ConnectionAccess public let metadata: MCPPrincipalMetadata + // Nil means every tool the scopes allow. + public let toolAllowlist: Set? public init( tokenFingerprint: String, tokenId: UUID, scopes: Set, connectionAccess: ConnectionAccess = .all, - metadata: MCPPrincipalMetadata + metadata: MCPPrincipalMetadata, + toolAllowlist: Set? = nil ) { self.tokenFingerprint = tokenFingerprint self.tokenId = tokenId self.scopes = scopes self.connectionAccess = connectionAccess self.metadata = metadata + self.toolAllowlist = toolAllowlist } public static let inAppAssistantTokenId = UUID(uuid: (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xA1, 0x1A)) @@ -69,6 +73,10 @@ public struct MCPPrincipal: Sendable, Equatable, Hashable { scopes.contains(scope) } + public func allowsTool(_ name: String) -> Bool { + toolAllowlist?.contains(name) ?? true + } + public func missingScopes(from required: Set) -> Set { required.subtracting(scopes) } @@ -86,6 +94,7 @@ public struct MCPPrincipal: Sendable, Equatable, Hashable { && lhs.scopes == rhs.scopes && lhs.connectionAccess == rhs.connectionAccess && lhs.metadata == rhs.metadata + && lhs.toolAllowlist == rhs.toolAllowlist } public func hash(into hasher: inout Hasher) { diff --git a/apple/Packages/TableProMacCore/Sources/TableProMCP/Protocol/Tools/MCPToolSchema.swift b/apple/Packages/TableProMacCore/Sources/TableProMCP/Protocol/Tools/MCPToolSchema.swift index 3e8afd4e6e..28d43ff89c 100644 --- a/apple/Packages/TableProMacCore/Sources/TableProMCP/Protocol/Tools/MCPToolSchema.swift +++ b/apple/Packages/TableProMacCore/Sources/TableProMCP/Protocol/Tools/MCPToolSchema.swift @@ -95,7 +95,13 @@ public extension MCPToolSchema { "row_count": integer(String(localized: "Number of rows returned")), "rows_affected": integer(String(localized: "Rows the statement changed")), "execution_time_ms": number(String(localized: "Server round trip in milliseconds")), - "is_truncated": boolean(String(localized: "Whether the row limit clipped the result")), + "is_truncated": boolean( + String(localized: "Whether the row limit or the answer's size limit left rows out of the result") + ), + "notes": array( + String(localized: "Why rows were left out or values cut, when they were"), + of: stringItem + ), "status_message": string(String(localized: "Driver status message, when the engine sent one")), "database": string(String(localized: "Database the statement ran against")), "schema": string(String(localized: "Schema the statement ran against")) diff --git a/apple/Packages/TableProMacCore/Tests/TableProMCPTests/Auth/MCPPrincipalToolAllowlistTests.swift b/apple/Packages/TableProMacCore/Tests/TableProMCPTests/Auth/MCPPrincipalToolAllowlistTests.swift new file mode 100644 index 0000000000..d83aae0b91 --- /dev/null +++ b/apple/Packages/TableProMacCore/Tests/TableProMCPTests/Auth/MCPPrincipalToolAllowlistTests.swift @@ -0,0 +1,56 @@ +import Foundation +import TableProMCP +import Testing + +struct MCPPrincipalToolAllowlistTests { + private let tokenId = UUID() + + private func principal(toolAllowlist: Set? = nil) -> MCPPrincipal { + MCPPrincipal( + tokenFingerprint: "fp", + tokenId: tokenId, + scopes: [.toolsRead], + metadata: MCPPrincipalMetadata(label: "test", issuedAt: Date(timeIntervalSince1970: 0), expiresAt: nil), + toolAllowlist: toolAllowlist + ) + } + + @Test("A principal without an allowlist allows every tool its scopes reach") + func noAllowlistAllowsEveryTool() { + let open = principal() + + #expect(open.toolAllowlist == nil) + #expect(open.allowsTool("list_tables")) + #expect(open.allowsTool("execute_query")) + } + + @Test("An allowlist allows only the tools it names") + func allowlistIsMembership() { + let narrow = principal(toolAllowlist: ["list_tables", "describe_table"]) + + #expect(narrow.allowsTool("list_tables")) + #expect(narrow.allowsTool("describe_table")) + #expect(!narrow.allowsTool("execute_query")) + #expect(!narrow.allowsTool("")) + } + + @Test("An empty allowlist allows no tool") + func emptyAllowlistAllowsNothing() { + #expect(!principal(toolAllowlist: []).allowsTool("list_tables")) + } + + @Test("Two principals differing only in their allowlist are not equal") + func equalityIncludesTheAllowlist() { + let tables = principal(toolAllowlist: ["list_tables"]) + let sameTables = principal(toolAllowlist: ["list_tables"]) + + #expect(principal() != tables) + #expect(tables != principal(toolAllowlist: ["describe_table"])) + #expect(tables == sameTables) + } + + @Test("The in-app assistant carries no allowlist") + func inAppAssistantIsUnrestricted() { + #expect(MCPPrincipal.inAppAssistant.toolAllowlist == nil) + } +} diff --git a/apple/Plugins/ClickHouseDriverPlugin/ClickHousePlugin.swift b/apple/Plugins/ClickHouseDriverPlugin/ClickHousePlugin.swift index 5166531fd1..69b6a98efd 100644 --- a/apple/Plugins/ClickHouseDriverPlugin/ClickHousePlugin.swift +++ b/apple/Plugins/ClickHouseDriverPlugin/ClickHousePlugin.swift @@ -223,6 +223,7 @@ nonisolated final class ClickHousePluginDriver: PluginDatabaseDriver, @unchecked .uuidCellText, .repeatingPrimaryKey, .readOnlyStatements, + .enforcesReadOnlyMode, .columnDefaultMarks, ] } @@ -340,15 +341,15 @@ nonisolated final class ClickHousePluginDriver: PluginDatabaseDriver, @unchecked // MARK: - Query Execution func execute(query: String) async throws -> PluginQueryResult { - try await runStatement(query, parameters: [], readOnly: false) + try await runStatement(query, parameters: [], readOnly: nil) } func execute(query: String, context: PluginStatementContext) async throws -> PluginQueryResult { - try await runStatement(query, parameters: [], readOnly: context.readOnly && context.origin == .external) + try await runStatement(query, parameters: [], readOnly: ClickHouseReadOnlyReason(context)) } func executeParameterized(query: String, parameters: [PluginCellValue]) async throws -> PluginQueryResult { - try await runStatement(query, parameters: parameters, readOnly: false) + try await runStatement(query, parameters: parameters, readOnly: nil) } func executeParameterized( @@ -356,19 +357,24 @@ nonisolated final class ClickHousePluginDriver: PluginDatabaseDriver, @unchecked parameters: [PluginCellValue], context: PluginStatementContext ) async throws -> PluginQueryResult { - try await runStatement(query, parameters: parameters, readOnly: context.readOnly && context.origin == .external) + try await runStatement(query, parameters: parameters, readOnly: ClickHouseReadOnlyReason(context)) } + // A capped read-only read with no parameters is the bounded stream, which ends on the server one row past the cap. + // Parameters ride only on the buffered request, so a read that has them is read whole and cut here. func executeUserQuery( query: String, rowCap: Int?, parameters: [PluginCellValue]?, context: PluginStatementContext ) async throws -> PluginQueryResult { - guard context.readOnly, context.origin == .external else { + guard let reason = ClickHouseReadOnlyReason(context) else { return try await executeUserQuery(query: query, rowCap: rowCap, parameters: parameters) } - let raw = try await runStatement(query, parameters: parameters ?? [], readOnly: true) + if let cap = rowCap, cap > 0, parameters?.isEmpty ?? true { + return try await boundedRead(query: query, rowCap: cap, readOnly: reason, byteLimit: context.resultByteLimit) + } + let raw = try await runStatement(query, parameters: parameters ?? [], readOnly: reason) guard let cap = rowCap, cap > 0, raw.rows.count > cap else { return raw } return PluginQueryResult( columns: raw.columns, @@ -380,7 +386,11 @@ nonisolated final class ClickHousePluginDriver: PluginDatabaseDriver, @unchecked ) } - private func runStatement(_ query: String, parameters: [PluginCellValue], readOnly: Bool) async throws -> PluginQueryResult { + private func runStatement( + _ query: String, + parameters: [PluginCellValue], + readOnly: ClickHouseReadOnlyReason? + ) async throws -> PluginQueryResult { let startTime = Date() let queryId = UUID().uuidString let result: CHQueryResult @@ -579,6 +589,30 @@ nonisolated final class ClickHousePluginDriver: PluginDatabaseDriver, @unchecked /// both ask for names its columns, so neither pays a `LIMIT 0` probe to learn them. That probe /// appended to the statement, which a query already carrying a `LIMIT` rejects outright. func executeBoundedQuery(query: String, rowCap: Int) async throws -> PluginQueryResult? { + try await boundedRead(query: query, rowCap: rowCap, readOnly: nil, byteLimit: nil) + } + + // A context `readonly` does not cover goes back to the host as PluginKit's default sends it. + func executeBoundedQuery( + query: String, + rowCap: Int, + context: PluginStatementContext + ) async throws -> PluginQueryResult? { + guard let reason = ClickHouseReadOnlyReason(context) else { + guard !context.enforcesReadOnly, !context.readOnly, !context.readsCatalog, + context.allowsDestructive, context.allowsServerCode + else { return nil } + return try await executeBoundedQuery(query: query, rowCap: rowCap) + } + return try await boundedRead(query: query, rowCap: rowCap, readOnly: reason, byteLimit: context.resultByteLimit) + } + + private func boundedRead( + query: String, + rowCap: Int, + readOnly: ClickHouseReadOnlyReason?, + byteLimit: Int? + ) async throws -> PluginQueryResult { let started = Date() let isOutputCut = OSAllocatedUnfairLock(initialState: false) let stream = PluginRowStream.make { continuation, abort in @@ -587,6 +621,8 @@ nonisolated final class ClickHousePluginDriver: PluginDatabaseDriver, @unchecked try await self.performBoundedStreamRows( query: query, rowCap: rowCap, + readOnly: readOnly, + byteLimit: byteLimit, isOutputCut: isOutputCut, continuation: continuation ) @@ -611,6 +647,8 @@ nonisolated final class ClickHousePluginDriver: PluginDatabaseDriver, @unchecked private func performBoundedStreamRows( query: String, rowCap: Int, + readOnly: ClickHouseReadOnlyReason?, + byteLimit: Int?, isOutputCut: OSAllocatedUnfairLock, continuation: AsyncThrowingStream.Continuation ) async throws { @@ -621,17 +659,23 @@ nonisolated final class ClickHousePluginDriver: PluginDatabaseDriver, @unchecked database: database, queryId: queryId, executionTimeLimit: executionTimeLimit, - shape: .streamed(rowCap: rowCap) + shape: .streamed(rowCap: rowCap), + readOnly: readOnly != nil ) request.timeoutInterval = _queryTimeout.requestTimeoutInterval try await killingOnCancel(queryId: queryId) { - try await streamTabSeparatedRows( - request: request, - session: session, - batchSize: min(5_000, rowCap + 1), - boundedOutputCut: isOutputCut, - continuation: continuation - ) + do { + try await streamTabSeparatedRows( + request: request, + session: session, + batchSize: min(5_000, rowCap + 1), + boundedOutputCut: isOutputCut, + byteLimit: byteLimit, + continuation: continuation + ) + } catch { + throw Self.readOnlyRefusal(error, reason: readOnly) + } } } @@ -649,6 +693,7 @@ nonisolated final class ClickHousePluginDriver: PluginDatabaseDriver, @unchecked session: URLSession, batchSize: Int, boundedOutputCut: OSAllocatedUnfairLock? = nil, + byteLimit: Int? = nil, continuation: AsyncThrowingStream.Continuation ) async throws { var decoder = ClickHouseTabSeparatedRowDecoder() @@ -657,6 +702,7 @@ nonisolated final class ClickHousePluginDriver: PluginDatabaseDriver, @unchecked var headerSent = false var batch: [PluginRow] = [] batch.reserveCapacity(batchSize) + var bytes = 0 func sendHeader(_ header: ClickHouseTabSeparatedRowDecoder.Header) { guard !headerSent else { return } @@ -668,6 +714,20 @@ nonisolated final class ClickHousePluginDriver: PluginDatabaseDriver, @unchecked ))) } + // Keeps the row that takes the decoded rows past `byteLimit`, and says it came; the rest is never read. + func take(_ rows: [PluginRow]) -> Bool { + guard let byteLimit else { + batch.append(contentsOf: rows) + return false + } + for row in rows { + batch.append(row) + bytes += Self.decodedByteCount(of: row) + if bytes > byteLimit { return true } + } + return false + } + let chunks = ClickHouseHTTPChunks(session: session, request: request) do { try await chunks.forEachChunk { chunk in @@ -694,11 +754,18 @@ nonisolated final class ClickHousePluginDriver: PluginDatabaseDriver, @unchecked if let header = decoder.header { sendHeader(header) } - batch.append(contentsOf: rows) + if take(rows) { throw ByteLimitReached() } guard batch.count >= batchSize else { return } continuation.yield(.rows(batch)) batch.removeAll(keepingCapacity: true) } + } catch is ByteLimitReached { + if !batch.isEmpty { + continuation.yield(.rows(batch)) + } + boundedOutputCut?.withLock { $0 = true } + continuation.finish() + return } catch is OtherFormatBodyFull {} if let otherFormatBody { @@ -723,12 +790,14 @@ nonisolated final class ClickHousePluginDriver: PluginDatabaseDriver, @unchecked exceptionTag: chunks.headerValue("X-ClickHouse-Exception-Tag"), header: decoder.header ) - batch.append(contentsOf: decoder.consume(rest)) - batch.append(contentsOf: decoder.finish()) + let stoppedAtByteLimit = take(decoder.consume(rest)) || take(decoder.finish()) sendHeader(decoder.header ?? ClickHouseTabSeparatedRowDecoder.Header(columns: [], columnTypeNames: [])) if !batch.isEmpty { continuation.yield(.rows(batch)) } + if stoppedAtByteLimit { + boundedOutputCut?.withLock { $0 = true } + } continuation.finish() } @@ -738,15 +807,17 @@ nonisolated final class ClickHousePluginDriver: PluginDatabaseDriver, @unchecked private struct OtherFormatBodyFull: Error {} + private struct ByteLimitReached: Error {} + func streamRows(query: String) -> AsyncThrowingStream { - stream(query, readOnly: false) + stream(query, readOnly: nil) } func streamRows(query: String, context: PluginStatementContext) -> AsyncThrowingStream { - stream(query, readOnly: context.readOnly && context.origin == .external) + stream(query, readOnly: ClickHouseReadOnlyReason(context)) } - private func stream(_ query: String, readOnly: Bool) -> AsyncThrowingStream { + private func stream(_ query: String, readOnly: ClickHouseReadOnlyReason?) -> AsyncThrowingStream { AsyncThrowingStream(bufferingPolicy: .unbounded) { continuation in let streamTask = Task { do { @@ -763,7 +834,7 @@ nonisolated final class ClickHousePluginDriver: PluginDatabaseDriver, @unchecked private func performStreamRows( query: String, - readOnly: Bool, + readOnly: ClickHouseReadOnlyReason?, continuation: AsyncThrowingStream.Continuation ) async throws { let queryId = UUID().uuidString @@ -774,7 +845,7 @@ nonisolated final class ClickHousePluginDriver: PluginDatabaseDriver, @unchecked queryId: queryId, executionTimeLimit: executionTimeLimit, shape: .streamed(rowCap: nil), - readOnly: readOnly + readOnly: readOnly != nil ) request.timeoutInterval = _queryTimeout.requestTimeoutInterval try await killingOnCancel(queryId: queryId) { @@ -786,7 +857,7 @@ nonisolated final class ClickHousePluginDriver: PluginDatabaseDriver, @unchecked continuation: continuation ) } catch { - throw readOnly ? Self.readOnlyRefusal(error) : error + throw Self.readOnlyRefusal(error, reason: readOnly) } } } diff --git a/apple/Plugins/ClickHouseDriverPlugin/ClickHousePluginDriver+Http.swift b/apple/Plugins/ClickHouseDriverPlugin/ClickHousePluginDriver+Http.swift index d95fde6e89..242463a761 100644 --- a/apple/Plugins/ClickHouseDriverPlugin/ClickHousePluginDriver+Http.swift +++ b/apple/Plugins/ClickHouseDriverPlugin/ClickHousePluginDriver+Http.swift @@ -18,7 +18,7 @@ nonisolated extension ClickHousePluginDriver { _ query: String, queryId: String? = nil, requestTimeout: TimeInterval? = nil, - readOnly: Bool = false + readOnly: ClickHouseReadOnlyReason? = nil ) async throws -> CHQueryResult { try await sendStatement( query, @@ -34,7 +34,7 @@ nonisolated extension ClickHousePluginDriver { _ query: String, params: [String: String?], queryId: String? = nil, - readOnly: Bool = false + readOnly: ClickHouseReadOnlyReason? = nil ) async throws -> CHQueryResult { try await sendStatement( query, @@ -51,7 +51,7 @@ nonisolated extension ClickHousePluginDriver { params: [String: String?]?, executionTimeLimit: Int?, requestTimeout: TimeInterval? = nil, - readOnly: Bool = false + readOnly: ClickHouseReadOnlyReason? = nil ) async throws -> CHQueryResult { let (session, database) = try lock.withLock { () throws -> (URLSession, String) in guard let session = self.session else { throw ClickHouseError.notConnected } @@ -68,26 +68,34 @@ nonisolated extension ClickHousePluginDriver { queryId: queryId, executionTimeLimit: executionTimeLimit, shape: .buffered(parameters: params), - readOnly: readOnly + readOnly: readOnly != nil ) request.timeoutInterval = requestTimeout ?? _queryTimeout.requestTimeoutInterval return try await killingOnCancel(queryId: queryId) { do { return try await perform(request: request, session: session) } catch { - throw readOnly ? Self.readOnlyRefusal(error) : error + throw Self.readOnlyRefusal(error, reason: readOnly) } } } // 164 is READONLY: the request's own `readonly` refused the statement. - static func readOnlyRefusal(_ error: Error) -> Error { - guard let failure = error as? ClickHouseError, failure.exceptionCode == 164 else { return error } - return ClickHouseError( - message: String(localized: "This request is read only, and the statement is not a read."), - exceptionCode: failure.exceptionCode, - detail: failure.message - ) + static func readOnlyRefusal(_ error: Error, reason: ClickHouseReadOnlyReason?) -> Error { + guard let reason, let failure = error as? ClickHouseError, failure.exceptionCode == 164 else { return error } + return ClickHouseError(message: reason.refusalMessage, exceptionCode: failure.exceptionCode, detail: failure.message) + } + + // Text as UTF-8, bytes as they are, null as nothing: never more than the host counts for the same row, so a read + // never stops before the host's own budget is full. + static func decodedByteCount(of row: PluginRow) -> Int { + row.reduce(0) { total, cell in + switch cell { + case .null: total + case .text(let text): total + text.utf8.count + case .bytes(let data): total + data.count + } + } } // Two callers can share this driver, so a cancelled statement stops its own query, never the one sent last. @@ -330,6 +338,32 @@ nonisolated extension ClickHousePluginDriver { } } +// Who asked for `readonly`, which is what a refusal names. ClickHouse has no transaction a request could find open. +nonisolated enum ClickHouseReadOnlyReason: Sendable, Equatable { + case externalClients + case safeMode + + // An app read marked `readOnly` alone, a catalog read or a dashboard tile, runs as it is. + init?(_ context: PluginStatementContext) { + if context.readOnly, context.origin == .external { + self = .externalClients + } else if context.enforcesReadOnly { + self = .safeMode + } else { + return nil + } + } + + var refusalMessage: String { + switch self { + case .externalClients: + String(localized: "This request is read only, and the statement is not a read.") + case .safeMode: + String(localized: "Safe Mode is set to Read-Only for this connection, and the database refused the statement.") + } + } +} + /// How the server hands a result back. nonisolated enum ClickHouseResponseShape: Equatable { /// The server holds the whole result until the query ends, so a failure is an HTTP status and diff --git a/apple/Plugins/ClickHouseDriverTests/ClickHouseReadOnlyRequestTests.swift b/apple/Plugins/ClickHouseDriverTests/ClickHouseReadOnlyRequestTests.swift index 9396f7bd61..59f34fd5c3 100644 --- a/apple/Plugins/ClickHouseDriverTests/ClickHouseReadOnlyRequestTests.swift +++ b/apple/Plugins/ClickHouseDriverTests/ClickHouseReadOnlyRequestTests.swift @@ -145,8 +145,140 @@ struct ClickHouseReadOnlyRequestTests { } } - @Test("The driver says it runs a read-only statement in ClickHouse's own read-only mode") + @Test("The driver says it runs a read-only statement in ClickHouse's own read-only mode, from outside and at Read-Only") func declaresReadOnlyStatements() { - #expect(ClickHouseStubServer().connectedDriver().capabilities.contains(.readOnlyStatements)) + let capabilities = ClickHouseStubServer().connectedDriver().capabilities + #expect(capabilities.contains(.readOnlyStatements)) + #expect(capabilities.contains(.enforcesReadOnlyMode)) + } + + private static let safeMode = PluginStatementContext(enforcesReadOnly: true) + + @Test("An external read names external clients, Read-Only from any origin names Safe Mode, an app read alone neither") + func reasonFollowsTheContext() { + var appRead = PluginStatementContext() + appRead.readOnly = true + var externalWrite = PluginStatementContext() + externalWrite.origin = .external + externalWrite.enforcesReadOnly = true + var externalSafeModeRead = Self.externalRead + externalSafeModeRead.enforcesReadOnly = true + + #expect(ClickHouseReadOnlyReason(Self.externalRead) == .externalClients) + #expect(ClickHouseReadOnlyReason(externalSafeModeRead) == .externalClients) + #expect(ClickHouseReadOnlyReason(Self.safeMode) == .safeMode) + #expect(ClickHouseReadOnlyReason(externalWrite) == .safeMode) + #expect(ClickHouseReadOnlyReason(appRead) == nil) + #expect(ClickHouseReadOnlyReason(PluginStatementContext()) == nil) + } + + @Test("At Read-Only every entry point carries readonly=1, and an app read marked readOnly alone carries none") + func safeModeEntryPointsCarryIt() async throws { + let server = ClickHouseStubServer() + let driver = server.connectedDriver() + var appRead = PluginStatementContext() + appRead.readOnly = true + + _ = try await driver.execute(query: "SELECT 1", context: Self.safeMode) + _ = try await driver.executeUserQuery(query: "SELECT 2", rowCap: 10, parameters: nil, context: Self.safeMode) + _ = try await driver.executeUserQuery(query: "SELECT ?", rowCap: nil, parameters: [.text("3")], context: Self.safeMode) + _ = try await driver.executeParameterized(query: "SELECT ?", parameters: [.text("4")], context: Self.safeMode) + for try await _ in driver.streamRows(query: "SELECT 5", context: Self.safeMode) {} + _ = try await driver.executeBoundedQuery(query: "SELECT 6", rowCap: 10, context: Self.safeMode) + _ = try await driver.executeBoundedQuery(query: "SELECT 7", rowCap: 10, context: Self.externalRead) + + _ = try await driver.execute(query: "SELECT 8", context: appRead) + _ = try await driver.executeUserQuery(query: "SELECT 9", rowCap: 10, parameters: nil, context: appRead) + + let statements = server.requests.filter { !$0.body.hasPrefix("KILL QUERY") } + let guarded = statements.filter { $0.item("readonly") == "1" }.map(\.body) + let unguarded = statements.filter { $0.item("readonly") == nil }.map(\.body) + #expect(guarded == ["SELECT 1", "SELECT 2", "SELECT {p1:String}", "SELECT {p1:String}", "SELECT 5", "SELECT 6", "SELECT 7"]) + #expect(unguarded == ["SELECT 8", "SELECT 9"]) + } + + @Test("A bounded read the read-only mode does not cover goes back to the host") + func boundedReadGoesBackWhenNotCovered() async throws { + let driver = ClickHouseStubServer().connectedDriver() + var appRead = PluginStatementContext() + appRead.readOnly = true + var catalogRead = appRead + catalogRead.readsCatalog = true + + #expect(try await driver.executeBoundedQuery(query: "SELECT 1", rowCap: 10, context: appRead) == nil) + #expect(try await driver.executeBoundedQuery(query: "SELECT 1", rowCap: 10, context: catalogRead) == nil) + let plain = try await driver.executeBoundedQuery(query: "SELECT 1", rowCap: 10, context: PluginStatementContext()) + #expect(plain?.rows.count == 1) + } + + @Test("At Read-Only a statement the server refuses names Safe Mode, and the server's text stays") + func safeModeRefusalNamesSafeMode() async throws { + let serverText = "Code: 164. DB::Exception: default: Cannot execute query in readonly mode. (READONLY) (version 26.8.36.3 (official build))" + let server = ClickHouseStubServer { _ in + ClickHouseStubReply(statusCode: 500, body: serverText, headers: ["X-ClickHouse-Exception-Code": "164"]) + } + let driver = server.connectedDriver() + + do { + _ = try await driver.execute(query: "SELECT purge_orders()", context: Self.safeMode) + Issue.record("expected a refusal") + } catch let error as ClickHouseError { + #expect(error.message == ClickHouseReadOnlyReason.safeMode.refusalMessage) + #expect(error.message != ClickHouseReadOnlyReason.externalClients.refusalMessage) + #expect(error.exceptionCode == 164) + #expect(error.pluginErrorDetail == serverText) + } + + do { + _ = try await driver.executeBoundedQuery(query: "SELECT purge_orders()", rowCap: 10, context: Self.safeMode) + Issue.record("expected a refusal") + } catch let error as ClickHouseError { + #expect(error.message == ClickHouseReadOnlyReason.safeMode.refusalMessage) + #expect(error.exceptionCode == 164) + } + } + + @Test("An external capped read is the bounded stream: the server stops one row past the cap, and readonly stays") + func externalCappedReadEndsOnTheServer() async throws { + let server = ClickHouseStubServer() + let driver = server.connectedDriver() + driver.serverProfile = Self.server258 + + _ = try await driver.executeUserQuery(query: "SELECT 2", rowCap: 10, parameters: nil, context: Self.externalRead) + _ = try await driver.executeUserQuery(query: "SELECT ?", rowCap: 10, parameters: [.text("3")], context: Self.externalRead) + + let capped = server.requests.first { $0.body == "SELECT 2" } + #expect(capped?.item("max_result_rows") == "11") + #expect(capped?.item("result_overflow_mode") == "break") + #expect(capped?.item("readonly") == "1") + let bound = server.requests.first { $0.body == "SELECT {p1:String}" } + #expect(bound?.item("max_result_rows") == nil) + #expect(bound?.item("readonly") == "1") + } + + @Test("A read-only read stops at the first row past the byte limit and keeps that row") + func readOnlyReadStopsAtTheByteLimit() async throws { + let server = ClickHouseStubServer { _ in + ClickHouseStubReply(statusCode: 200, body: "s\nString\naaaaaa\nbbbbbb\ncccccc\n") + } + let driver = server.connectedDriver() + var limited = Self.externalRead + limited.resultByteLimit = 10 + var safeModeLimited = Self.safeMode + safeModeLimited.resultByteLimit = 10 + + let external = try await driver.executeUserQuery(query: "SELECT s", rowCap: 100, parameters: nil, context: limited) + #expect(external.rows.compactMap { $0.first?.asText } == ["aaaaaa", "bbbbbb"]) + #expect(external.isTruncated) + + let bounded = try await driver.executeBoundedQuery(query: "SELECT s", rowCap: 100, context: safeModeLimited) + #expect(bounded?.rows.count == 2) + #expect(bounded?.isTruncated == true) + + var roomy = Self.externalRead + roomy.resultByteLimit = 1_000 + let whole = try await driver.executeUserQuery(query: "SELECT s", rowCap: 100, parameters: nil, context: roomy) + #expect(whole.rows.count == 3) + #expect(!whole.isTruncated) } } diff --git a/apple/Plugins/DuckDBDriverPlugin/DuckDBConnection.swift b/apple/Plugins/DuckDBDriverPlugin/DuckDBConnection.swift index 321389ab22..051d2d65c8 100644 --- a/apple/Plugins/DuckDBDriverPlugin/DuckDBConnection.swift +++ b/apple/Plugins/DuckDBDriverPlugin/DuckDBConnection.swift @@ -221,22 +221,32 @@ actor DuckDBConnectionActor { // The state check, the BEGIN, the statement and the ROLLBACK share one actor turn, so nothing lands in between. // The statement text is not tracked: the ROLLBACK leaves the session as idle as it was found. - func executeReadOnly(_ query: String, parameters: [PluginCellValue], rowCap: Int? = nil) throws -> DuckDBRawResult { + // A stop at `byteLimit` ends only the read's own read-only transaction. + func executeReadOnly( + _ query: String, + parameters: [PluginCellValue], + rowCap: Int? = nil, + byteLimit: Int? = nil, + reason: DuckDBReadOnlyStatement.Reason + ) throws -> DuckDBRawResult { let conn = try readOnlyConnection() let startTime = Date() - return try DuckDBReadOnlyStatement.run(on: conn, session: sessionTransactionState()) { + return try DuckDBReadOnlyStatement.run(on: conn, session: sessionTransactionState(), reason: reason) { var opened = try openResult(query: query, parameters: parameters, connection: conn, unread: .abandon) defer { duckdb_destroy_result(&opened.result) } - return try extractResult(from: opened, limit: rowCap ?? PluginRowLimits.emergencyMax, startTime: startTime) + return try extractResult( + from: opened, limit: rowCap ?? PluginRowLimits.emergencyMax, byteLimit: byteLimit, startTime: startTime + ) } } func streamReadOnly( _ query: String, + reason: DuckDBReadOnlyStatement.Reason, continuation: AsyncThrowingStream.Continuation ) throws { let conn = try readOnlyConnection() - try DuckDBReadOnlyStatement.run(on: conn, session: sessionTransactionState()) { + try DuckDBReadOnlyStatement.run(on: conn, session: sessionTransactionState(), reason: reason) { var opened = try openResult(query: query, parameters: [], connection: conn, unread: .abandon) defer { duckdb_destroy_result(&opened.result) } try streamResultRows(opened, continuation: continuation) @@ -655,17 +665,36 @@ actor DuckDBConnectionActor { return ColumnSchema(names: names, logicalTypes: logicalTypes) } - private func extractResult(from opened: OpenedResult, limit: Int, startTime: Date) throws -> DuckDBRawResult { + // With `byteLimit` the read stops at the row that takes the decoded rows past it and keeps that row. A chunk is + // decoded whole before it is measured. + private func extractResult( + from opened: OpenedResult, + limit: Int, + byteLimit: Int? = nil, + startTime: Date + ) throws -> DuckDBRawResult { var rows: [[PluginCellValue]] = [] if let rowCount = opened.rowCount { rows.reserveCapacity(Int(min(rowCount, UInt64(max(0, limit))))) } var budget = DuckDBFetchBudget(limit: limit) + var bytes = 0 + var stoppedAtByteLimit = false let reachedEnd = try readRows(of: opened) { batch in let kept = budget.take(batch.count) - rows.append(contentsOf: kept == batch.count ? batch[...] : batch.prefix(kept)) - return !budget.isExceeded + var slice = kept == batch.count ? batch[...] : batch.prefix(kept) + if let byteLimit { + for (offset, row) in slice.enumerated() { + bytes += Self.decodedByteCount(of: row) + guard bytes > byteLimit else { continue } + slice = slice.prefix(offset + 1) + stoppedAtByteLimit = true + break + } + } + rows.append(contentsOf: slice) + return !budget.isExceeded && !stoppedAtByteLimit } if !reachedEnd, opened.isStreaming, !opened.abandonsUnreadRows { try Self.drain(opened.result) @@ -677,10 +706,22 @@ actor DuckDBConnectionActor { rows: rows, rowsAffected: Int(opened.rowsChanged), executionTime: Date().timeIntervalSince(startTime), - isTruncated: budget.isExceeded + isTruncated: budget.isExceeded || stoppedAtByteLimit ) } + // Text as UTF-8, bytes as they are, null as nothing: never more than the host counts for the same row, so a read + // never stops before the host's own budget is full. + static func decodedByteCount(of row: [PluginCellValue]) -> Int { + row.reduce(0) { total, cell in + switch cell { + case .null: total + case .text(let text): total + text.utf8.count + case .bytes(let data): total + data.count + } + } + } + private func streamResultRows( _ opened: OpenedResult, continuation: AsyncThrowingStream.Continuation diff --git a/apple/Plugins/DuckDBDriverPlugin/DuckDBPlugin.swift b/apple/Plugins/DuckDBDriverPlugin/DuckDBPlugin.swift index 19da1b5586..c9aefc6f4c 100644 --- a/apple/Plugins/DuckDBDriverPlugin/DuckDBPlugin.swift +++ b/apple/Plugins/DuckDBDriverPlugin/DuckDBPlugin.swift @@ -277,6 +277,7 @@ final class DuckDBPluginDriver: PluginDatabaseDriver, @unchecked Sendable { .schemaCompare, .dataCompare, .readOnlyStatements, + .enforcesReadOnlyMode, ] } @@ -695,8 +696,10 @@ final class DuckDBPluginDriver: PluginDatabaseDriver, @unchecked Sendable { } func execute(query: String, context: PluginStatementContext) async throws -> PluginQueryResult { - guard context.readOnly, context.origin == .external else { return try await execute(query: query) } - return Self.queryResult(try await connectionActor.executeReadOnly(query, parameters: [])) + guard let reason = DuckDBReadOnlyStatement.reason(for: context) else { return try await execute(query: query) } + return Self.queryResult(try await connectionActor.executeReadOnly( + query, parameters: [], byteLimit: context.resultByteLimit, reason: reason + )) } func executeParameterized( @@ -711,8 +714,12 @@ final class DuckDBPluginDriver: PluginDatabaseDriver, @unchecked Sendable { parameters: [PluginCellValue], context: PluginStatementContext ) async throws -> PluginQueryResult { - guard context.readOnly, context.origin == .external else { return try await executeParameterized(query: query, parameters: parameters) } - return Self.queryResult(try await connectionActor.executeReadOnly(query, parameters: parameters)) + guard let reason = DuckDBReadOnlyStatement.reason(for: context) else { + return try await executeParameterized(query: query, parameters: parameters) + } + return Self.queryResult(try await connectionActor.executeReadOnly( + query, parameters: parameters, byteLimit: context.resultByteLimit, reason: reason + )) } func executeBoundedQuery(query: String, rowCap: Int) async throws -> PluginQueryResult? { @@ -720,6 +727,24 @@ final class DuckDBPluginDriver: PluginDatabaseDriver, @unchecked Sendable { return Self.queryResult(try await connectionActor.executeBounded(query, parameters: [], rowCap: rowCap)) } + // A context the read-only mode does not cover goes back to the host as PluginKit's default sends it. + func executeBoundedQuery( + query: String, + rowCap: Int, + context: PluginStatementContext + ) async throws -> PluginQueryResult? { + guard let reason = DuckDBReadOnlyStatement.reason(for: context) else { + guard !context.enforcesReadOnly, !context.readOnly, !context.readsCatalog, + context.allowsDestructive, context.allowsServerCode + else { return nil } + return try await executeBoundedQuery(query: query, rowCap: rowCap) + } + guard rowCap > 0 else { return nil } + return Self.queryResult(try await connectionActor.executeReadOnly( + query, parameters: [], rowCap: rowCap, byteLimit: context.resultByteLimit, reason: reason + )) + } + func executeUserQuery(query: String, rowCap: Int?, parameters: [PluginCellValue]?) async throws -> PluginQueryResult { guard let cap = rowCap, cap > 0 else { return Self.queryResult(try await connectionActor.executePrepared(query, parameters: parameters ?? [])) @@ -733,11 +758,13 @@ final class DuckDBPluginDriver: PluginDatabaseDriver, @unchecked Sendable { parameters: [PluginCellValue]?, context: PluginStatementContext ) async throws -> PluginQueryResult { - guard context.readOnly, context.origin == .external else { + guard let reason = DuckDBReadOnlyStatement.reason(for: context) else { return try await executeUserQuery(query: query, rowCap: rowCap, parameters: parameters) } let cap = rowCap.flatMap { $0 > 0 ? $0 : nil } - return Self.queryResult(try await connectionActor.executeReadOnly(query, parameters: parameters ?? [], rowCap: cap)) + return Self.queryResult(try await connectionActor.executeReadOnly( + query, parameters: parameters ?? [], rowCap: cap, byteLimit: context.resultByteLimit, reason: reason + )) } private static func queryResult(_ rawResult: DuckDBRawResult) -> PluginQueryResult { @@ -772,12 +799,12 @@ final class DuckDBPluginDriver: PluginDatabaseDriver, @unchecked Sendable { } func streamRows(query: String, context: PluginStatementContext) -> AsyncThrowingStream { - guard context.readOnly, context.origin == .external else { return streamRows(query: query) } + guard let reason = DuckDBReadOnlyStatement.reason(for: context) else { return streamRows(query: query) } let actor = connectionActor return AsyncThrowingStream(bufferingPolicy: .bufferingOldest(DuckDBBoundedStream.bufferedBatches)) { continuation in Task { do { - try await actor.streamReadOnly(query, continuation: continuation) + try await actor.streamReadOnly(query, reason: reason, continuation: continuation) } catch { continuation.finish(throwing: error) } diff --git a/apple/Plugins/DuckDBDriverPlugin/DuckDBReadOnlyStatement.swift b/apple/Plugins/DuckDBDriverPlugin/DuckDBReadOnlyStatement.swift index 802331d537..e0e502323e 100644 --- a/apple/Plugins/DuckDBDriverPlugin/DuckDBReadOnlyStatement.swift +++ b/apple/Plugins/DuckDBDriverPlugin/DuckDBReadOnlyStatement.swift @@ -9,29 +9,51 @@ import TableProPluginKit // Depends on nothing else in the plugin, so TableProMobileTests compiles it against a DuckDB connection of its own. nonisolated enum DuckDBReadOnlyStatement { + // Who asked for the read-only mode, which is what a refusal names. + nonisolated enum Reason: Sendable, Equatable { + case externalClients + case safeMode + } + nonisolated enum Refusal: Error, PluginDriverError, Equatable { case transactionOpen + case safeModeTransactionOpen case beginFailed(String) var pluginErrorMessage: String { switch self { case .transactionOpen: String(localized: "This connection has a transaction open. Commit or roll it back in TablePro, then try again.") + // Read-Only offers no Commit, so the way out is a rollback. + case .safeModeTransactionOpen: + String( + localized: "Safe Mode is set to Read-Only for this connection, and it has a transaction open. Roll it back, then try again." + ) case .beginFailed(let message): message } } } + // An app read marked `readOnly` alone, a catalog read or a dashboard tile, runs as it is. + static func reason(for context: PluginStatementContext) -> Reason? { + if context.readOnly, context.origin == .external { return .externalClients } + if context.enforcesReadOnly { return .safeMode } + return nil + } + // DuckDB refuses every database write inside a read-only transaction. A BEGIN inside the user's own transaction // fails and aborts it (measured on v2.0.0), so only a session known to be idle gets one, and the caller reads that // state in the same actor turn. static func run( on connection: duckdb_connection, session: PluginSessionTransactionState, + reason: Reason = .externalClients, _ statement: () throws -> T ) throws -> T { - guard session == .idle else { throw Refusal.transactionOpen } + guard session == .idle else { + throw reason == .safeMode ? Refusal.safeModeTransactionOpen : Refusal.transactionOpen + } if let failure = send("BEGIN TRANSACTION READ ONLY", on: connection) { throw Refusal.beginFailed(failure) } diff --git a/apple/Plugins/MSSQLDriverPlugin/MSSQLPlugin.swift b/apple/Plugins/MSSQLDriverPlugin/MSSQLPlugin.swift index 60fb612887..f788df65c2 100644 --- a/apple/Plugins/MSSQLDriverPlugin/MSSQLPlugin.swift +++ b/apple/Plugins/MSSQLDriverPlugin/MSSQLPlugin.swift @@ -635,6 +635,26 @@ final class MSSQLPluginDriver: PluginDatabaseDriver, @unchecked Sendable { // MARK: - Streaming func executeBoundedQuery(query: String, rowCap: Int) async throws -> PluginQueryResult? { + try await boundedRead(query: query, rowCap: rowCap) + } + + // An external read with a cap ends the request one row past it, as the bounded read does, instead of reading up to + // the emergency ceiling and cutting here. Parameters ride in `sp_executesql`, which only the default path writes. + func executeUserQuery( + query: String, + rowCap: Int?, + parameters: [PluginCellValue]?, + context: PluginStatementContext + ) async throws -> PluginQueryResult { + guard context.readOnly, context.origin == .external, parameters?.isEmpty ?? true, let cap = rowCap, cap > 0 else { + return try await PluginParameterBinder.withHostContext(context) { + try await executeUserQuery(query: query, rowCap: rowCap, parameters: parameters) + } + } + return try await boundedRead(query: query, rowCap: cap) + } + + private func boundedRead(query: String, rowCap: Int) async throws -> PluginQueryResult { guard let conn = freeTDSConn else { throw MSSQLPluginError.notConnected } diff --git a/apple/Plugins/MySQLDriverPlugin/MariaDBPluginConnection.swift b/apple/Plugins/MySQLDriverPlugin/MariaDBPluginConnection.swift index ba69a63dc3..ee757fb21c 100644 --- a/apple/Plugins/MySQLDriverPlugin/MariaDBPluginConnection.swift +++ b/apple/Plugins/MySQLDriverPlugin/MariaDBPluginConnection.swift @@ -893,35 +893,36 @@ final class MariaDBPluginConnection: @unchecked Sendable { // MARK: - Query Execution - func executeQuery(_ query: String, rowCap: Int? = nil) async throws -> MariaDBPluginQueryResult { + func executeQuery(_ query: String, rowCap: Int? = nil, byteLimit: Int? = nil) async throws -> MariaDBPluginQueryResult { let queryToRun = String(query) return try await pluginDispatchAsync(on: queue) { [self] in guard !isShuttingDown else { throw MariaDBPluginError.notConnected } - return try executeQuerySync(queryToRun, rowCap: rowCap) + return try executeQuerySync(queryToRun, rowCap: rowCap, byteLimit: byteLimit) } } func executeParameterizedQuery( _ query: String, parameters: [PluginCellValue], - rowCap: Int? = nil + rowCap: Int? = nil, + byteLimit: Int? = nil ) async throws -> MariaDBPluginQueryResult { let queryToRun = String(query) let params = parameters return try await pluginDispatchAsync(on: queue) { [self] in guard !isShuttingDown else { throw MariaDBPluginError.notConnected } - return try executeParameterizedQuerySync(queryToRun, parameters: params, rowCap: rowCap) + return try executeParameterizedQuerySync(queryToRun, parameters: params, rowCap: rowCap, byteLimit: byteLimit) } } - // `sessionState` runs in this same block, so nothing queued behind it can open a transaction before the wrap. func executeReadOnly( _ query: String, parameters: [PluginCellValue]?, rowCap: Int?, - sessionState: @escaping @Sendable (Bool) -> PluginSessionTransactionState + byteLimit: Int?, + scope: MySQLReadOnlyTransaction.Scope ) async throws -> MariaDBPluginQueryResult { let queryToRun = String(query) let params = parameters @@ -929,20 +930,29 @@ final class MariaDBPluginConnection: @unchecked Sendable { return try await pluginDispatchAsync(on: queue) { [self] in guard !isShuttingDown else { throw MariaDBPluginError.notConnected } return try MySQLReadOnlyTransaction.run( - state: sessionState(isInTransaction), + state: scope.sessionState(isInTransaction), + reason: scope.reason, send: { _ = try executeQuerySync($0) }, statement: { - guard let params else { return try executeQuerySync(queryToRun, rowCap: rowCap) } - return try executeParameterizedQuerySync(queryToRun, parameters: params, rowCap: rowCap) + guard let params else { + return try executeQuerySync(queryToRun, rowCap: rowCap, byteLimit: byteLimit) + } + return try executeParameterizedQuerySync( + queryToRun, parameters: params, rowCap: rowCap, byteLimit: byteLimit + ) } ) } } - private func executeQuerySync(_ query: String, rowCap: Int? = nil) throws -> MariaDBPluginQueryResult { + private func executeQuerySync( + _ query: String, + rowCap: Int? = nil, + byteLimit: Int? = nil + ) throws -> MariaDBPluginQueryResult { try runStatement(query) { try self.runCancellableStatement { mysql, generation in - try self.runTextStatement(query, rowCap: rowCap, on: mysql, generation: generation) + try self.runTextStatement(query, rowCap: rowCap, byteLimit: byteLimit, on: mysql, generation: generation) } } } @@ -980,6 +990,7 @@ final class MariaDBPluginConnection: @unchecked Sendable { private func runTextStatement( _ query: String, rowCap: Int?, + byteLimit: Int?, on mysql: UnsafeMutablePointer, generation: Int ) throws -> MariaDBPluginQueryResult { @@ -1046,6 +1057,8 @@ final class MariaDBPluginConnection: @unchecked Sendable { let maxRows = mysqlClampedRowCap(rowCap) ?? PluginRowLimits.emergencyMax let fetchLimit = maxRows == PluginRowLimits.emergencyMax ? maxRows : maxRows + 1 var serverSentMore = false + var bytes = 0 + var stoppedAtByteLimit = false var firstRowTime: TimeInterval? while let rowPtr = mysql_fetch_row(resultPtr) { @@ -1062,20 +1075,30 @@ final class MariaDBPluginConnection: @unchecked Sendable { break } - rows.append(textProtocolRow(rowPtr, lengths: mysql_fetch_lengths(resultPtr), columns: columns)) + let row = textProtocolRow(rowPtr, lengths: mysql_fetch_lengths(resultPtr), columns: columns) + rows.append(row) + if let byteLimit { + bytes += mysqlDecodedByteCount(of: row) + if bytes > byteLimit { + stoppedAtByteLimit = true + break + } + } } let outcome = mysqlBoundedFetchOutcome( fetchedRows: rows.count, rowCap: maxRows, - serverSentMore: serverSentMore + serverSentMore: serverSentMore, + stoppedAtByteLimit: stoppedAtByteLimit ) let truncated = outcome.isTruncated + let stoppedEarly = outcome.serverIgnoredLimit || stoppedAtByteLimit if truncated { - logger.warning("Result set truncated at \(maxRows) rows") + logger.warning("Result set truncated at \(outcome.keptRows) rows") rows.removeLast(rows.count - outcome.keptRows) } - if outcome.serverIgnoredLimit { + if stoppedEarly { if !sessionFlavor.dropsIdleSessionOnKillQuery { killStatementOnServer(on: mysql, generation: generation) } @@ -1091,9 +1114,7 @@ final class MariaDBPluginConnection: @unchecked Sendable { let fetchErrno = mysql_errno(mysql) if fetchErrno != 0 { let error = noting(getError(), generation: generation) - if !isExpectedInterruption( - errno: fetchErrno, message: error.message, wasTruncated: outcome.serverIgnoredLimit - ) { + if !isExpectedInterruption(errno: fetchErrno, message: error.message, wasTruncated: stoppedEarly) { mysql_free_result(resultPtr) throw error } @@ -1103,7 +1124,7 @@ final class MariaDBPluginConnection: @unchecked Sendable { do { try consumeTrailingResults(on: mysql, generation: generation) } catch let error as MariaDBPluginError where isExpectedInterruption( - errno: error.code, message: error.message, wasTruncated: outcome.serverIgnoredLimit + errno: error.code, message: error.message, wasTruncated: stoppedEarly ) { // The row cap's own kill stopped the routine after its first result set, the truncation already reported. } @@ -1207,6 +1228,7 @@ final class MariaDBPluginConnection: @unchecked Sendable { metadata: UnsafeMutablePointer, columns: MySQLResultColumns, rowCap: Int? = nil, + byteLimit: Int?, generation: Int, sentAt: Date ) throws -> (rows: [[PluginCellValue]], isTruncated: Bool, firstRowTime: TimeInterval) { @@ -1246,6 +1268,8 @@ final class MariaDBPluginConnection: @unchecked Sendable { let maxRows = mysqlClampedRowCap(rowCap) ?? PluginRowLimits.emergencyMax let fetchLimit = maxRows == PluginRowLimits.emergencyMax ? maxRows : maxRows + 1 var serverSentMore = false + var bytes = 0 + var stoppedAtByteLimit = false /// `mysql_stmt_execute` returns once the server has answered with a header, which on an /// unbuffered statement can be long before the first tuple exists. Only a fetch that /// returns a row proves the server produced one. @@ -1291,20 +1315,30 @@ final class MariaDBPluginConnection: @unchecked Sendable { } } - rows.append(columns.row(encoding: connectionEncoding) { index in + let row = columns.row(encoding: connectionEncoding) { index in guard resultBinds[index].is_null?.pointee != 1 else { return nil } let length = Int(resultBinds[index].length?.pointee ?? 0) return UnsafeRawBufferPointer(start: resultBuffers[index], count: length) - }) + } + rows.append(row) + // `mysql_stmt_next_result` and `mysql_stmt_close` skip the rows left unread. + if let byteLimit { + bytes += mysqlDecodedByteCount(of: row) + if bytes > byteLimit { + stoppedAtByteLimit = true + break + } + } } let outcome = mysqlBoundedFetchOutcome( fetchedRows: rows.count, rowCap: maxRows, - serverSentMore: serverSentMore + serverSentMore: serverSentMore, + stoppedAtByteLimit: stoppedAtByteLimit ) if outcome.isTruncated { - logger.warning("Prepared statement result truncated at \(maxRows) rows") + logger.warning("Prepared statement result truncated at \(outcome.keptRows) rows") rows.removeLast(rows.count - outcome.keptRows) } @@ -1318,18 +1352,19 @@ final class MariaDBPluginConnection: @unchecked Sendable { private func executeParameterizedQuerySync( _ query: String, parameters: [PluginCellValue], - rowCap: Int? = nil + rowCap: Int? = nil, + byteLimit: Int? = nil ) throws -> MariaDBPluginQueryResult { guard flavor.preparesOnServer else { let inlined = flavor.isDatabend ? try DatabendLiteral.inline(query, parameters: parameters, serverVersion: serverVersion()) : try StarRocksLiteral.inline(query, parameters: parameters) - return try executeQuerySync(inlined, rowCap: rowCap) + return try executeQuerySync(inlined, rowCap: rowCap, byteLimit: byteLimit) } return try runStatement(query) { try self.runCancellableStatement { mysql, generation in try self.runPreparedStatement( - query, parameters: parameters, rowCap: rowCap, on: mysql, generation: generation + query, parameters: parameters, rowCap: rowCap, byteLimit: byteLimit, on: mysql, generation: generation ) } } @@ -1339,6 +1374,7 @@ final class MariaDBPluginConnection: @unchecked Sendable { _ query: String, parameters: [PluginCellValue], rowCap: Int?, + byteLimit: Int?, on mysql: UnsafeMutablePointer, generation: Int ) throws -> MariaDBPluginQueryResult { @@ -1419,7 +1455,7 @@ final class MariaDBPluginConnection: @unchecked Sendable { let fetchResult = try fetchResultSet( from: stmt, metadata: metadata, - columns: columns, rowCap: rowCap, generation: generation, sentAt: sentAt + columns: columns, rowCap: rowCap, byteLimit: byteLimit, generation: generation, sentAt: sentAt ) try consumeTrailingResults(of: stmt, generation: generation) @@ -1441,10 +1477,10 @@ final class MariaDBPluginConnection: @unchecked Sendable { // MARK: - Streaming Query - // With `readOnlySessionState` the whole stream runs inside `executeReadOnly`'s transaction. + // With `readOnly` the whole stream runs inside `executeReadOnly`'s transaction. func streamQuery( _ query: String, - readOnlySessionState: (@Sendable (Bool) -> PluginSessionTransactionState)? = nil + readOnly: MySQLReadOnlyTransaction.Scope? = nil ) -> AsyncThrowingStream { let queryToRun = String(query) @@ -1463,13 +1499,14 @@ final class MariaDBPluginConnection: @unchecked Sendable { } } } - if let readOnlySessionState { + if let readOnly { guard !abort.isAborted else { continuation.finish() return } try MySQLReadOnlyTransaction.run( - state: readOnlySessionState(isInTransaction), + state: readOnly.sessionState(isInTransaction), + reason: readOnly.reason, send: { _ = try executeQuerySync($0) }, statement: stream ) diff --git a/apple/Plugins/MySQLDriverPlugin/MySQLPluginDriver.swift b/apple/Plugins/MySQLDriverPlugin/MySQLPluginDriver.swift index 6778d15221..903c707dfb 100644 --- a/apple/Plugins/MySQLDriverPlugin/MySQLPluginDriver.swift +++ b/apple/Plugins/MySQLDriverPlugin/MySQLPluginDriver.swift @@ -178,7 +178,7 @@ final class MySQLPluginDriver: PluginDatabaseDriver, @unchecked Sendable { capabilities.insert(.temporaryTables) } if flavor.enforcesReadOnlyTransactions { - capabilities.insert(.readOnlyStatements) + capabilities.formUnion([.readOnlyStatements, .enforcesReadOnlyMode]) } return capabilities } @@ -384,14 +384,23 @@ final class MySQLPluginDriver: PluginDatabaseDriver, @unchecked Sendable { } func executeUserQuery(query: String, rowCap: Int?, parameters: [PluginCellValue]?) async throws -> PluginQueryResult { + try await runUserQuery(query: query, rowCap: rowCap, parameters: parameters, byteLimit: nil) + } + + private func runUserQuery( + query: String, + rowCap: Int?, + parameters: [PluginCellValue]?, + byteLimit: Int? + ) async throws -> PluginQueryResult { let cap = rowCap.flatMap { $0 > 0 ? $0 : nil } guard let parameters else { return try await retryingAfterStop { - try await self.executeWithReconnect(query: query, isRetry: false, rowCap: cap) + try await self.executeWithReconnect(query: query, isRetry: false, rowCap: cap, byteLimit: byteLimit) } } return try await retryingAfterStop { - try await self.executeParameterizedOnce(query: query, parameters: parameters, rowCap: cap) + try await self.executeParameterizedOnce(query: query, parameters: parameters, rowCap: cap, byteLimit: byteLimit) } } @@ -403,85 +412,7 @@ final class MySQLPluginDriver: PluginDatabaseDriver, @unchecked Sendable { func executeParameterized(query: String, parameters: [PluginCellValue]) async throws -> PluginQueryResult { try await retryingAfterStop { - try await self.executeParameterizedOnce(query: query, parameters: parameters, rowCap: nil) - } - } - - func executeUserQuery( - query: String, - rowCap: Int?, - parameters: [PluginCellValue]?, - context: PluginStatementContext - ) async throws -> PluginQueryResult { - guard runsReadOnly(context) else { - return try await executeUserQuery(query: query, rowCap: rowCap, parameters: parameters) - } - let cap = rowCap.flatMap { $0 > 0 ? $0 : nil } - return try await retryingAfterStop { - try await self.executeReadOnly(query: query, parameters: parameters, rowCap: cap, isRetry: false) - } - } - - func execute(query: String, context: PluginStatementContext) async throws -> PluginQueryResult { - guard runsReadOnly(context) else { return try await execute(query: query) } - return try await retryingAfterStop { - try await self.executeReadOnly(query: query, parameters: nil, rowCap: nil, isRetry: false) - } - } - - func executeParameterized( - query: String, - parameters: [PluginCellValue], - context: PluginStatementContext - ) async throws -> PluginQueryResult { - guard runsReadOnly(context) else { return try await executeParameterized(query: query, parameters: parameters) } - return try await retryingAfterStop { - try await self.executeReadOnly(query: query, parameters: parameters, rowCap: nil, isRetry: false) - } - } - - private func runsReadOnly(_ context: PluginStatementContext) -> Bool { - MySQLReadOnlyTransaction.applies(to: context, flavor: flavor) - } - - // The lock comes from the footprint, because the status flags never show it. - private var readOnlySessionState: @Sendable (Bool) -> PluginSessionTransactionState { - { isInTransaction in - self.sessionLock.withLock { self.footprint.transactionState(isInTransaction: isInTransaction) } - } - } - - // Replays on a lost connection exactly when `executeWithReconnect` would. - private func executeReadOnly( - query: String, - parameters: [PluginCellValue]?, - rowCap: Int?, - isRetry: Bool - ) async throws -> PluginQueryResult { - let startTime = Date() - - let conn = try await requireConnection() - defer { endOperation(on: conn) } - noteActivity(query) - - do { - let result = try await conn.executeReadOnly( - query, - parameters: parameters, - rowCap: rowCap, - sessionState: readOnlySessionState - ) - return Self.pluginResult(result, startedAt: startTime) - } catch let error as MariaDBPluginError where !isRetry - && parameters == nil - && mysqlConnectionLossMayReplay(code: error.code, outlastedSocketTimeout: error.outlastedSocketTimeout) - && mayReplay(query) { - try await reconnect() - return try await executeReadOnly(query: query, parameters: nil, rowCap: rowCap, isRetry: true) - } catch { - noteFailure(query) - await refreshTransactionFlag(after: error, on: conn) - throw error + try await self.executeParameterizedOnce(query: query, parameters: parameters, rowCap: nil, byteLimit: nil) } } @@ -499,7 +430,8 @@ final class MySQLPluginDriver: PluginDatabaseDriver, @unchecked Sendable { private func executeParameterizedOnce( query: String, parameters: [PluginCellValue], - rowCap: Int? + rowCap: Int?, + byteLimit: Int? ) async throws -> PluginQueryResult { let conn = try await requireConnection() defer { endOperation(on: conn) } @@ -507,7 +439,9 @@ final class MySQLPluginDriver: PluginDatabaseDriver, @unchecked Sendable { let startTime = Date() let result: MariaDBPluginQueryResult do { - result = try await conn.executeParameterizedQuery(query, parameters: parameters, rowCap: rowCap) + result = try await conn.executeParameterizedQuery( + query, parameters: parameters, rowCap: rowCap, byteLimit: byteLimit + ) } catch { noteFailure(query) await refreshTransactionFlag(after: error, on: conn) @@ -562,6 +496,7 @@ final class MySQLPluginDriver: PluginDatabaseDriver, @unchecked Sendable { query: String, isRetry: Bool, rowCap: Int? = nil, + byteLimit: Int? = nil, countsAsActivity: Bool = true ) async throws -> PluginQueryResult { let startTime = Date() @@ -573,7 +508,7 @@ final class MySQLPluginDriver: PluginDatabaseDriver, @unchecked Sendable { } do { - let result = try await conn.executeQuery(query, rowCap: rowCap) + let result = try await conn.executeQuery(query, rowCap: rowCap, byteLimit: byteLimit) return Self.pluginResult(result, startedAt: startTime) } catch let error as MariaDBPluginError where !isRetry && mysqlConnectionLossMayReplay(code: error.code, outlastedSocketTimeout: error.outlastedSocketTimeout) @@ -583,6 +518,7 @@ final class MySQLPluginDriver: PluginDatabaseDriver, @unchecked Sendable { query: query, isRetry: true, rowCap: rowCap, + byteLimit: byteLimit, countsAsActivity: countsAsActivity ) } catch { @@ -1084,12 +1020,12 @@ final class MySQLPluginDriver: PluginDatabaseDriver, @unchecked Sendable { // MARK: - Streaming func streamRows(query: String) -> AsyncThrowingStream { - streamRows(query: query, readOnlySessionState: nil) + streamRows(query: query, readOnly: nil) } func streamRows(query: String, context: PluginStatementContext) -> AsyncThrowingStream { - guard runsReadOnly(context) else { return streamRows(query: query) } - return streamRows(query: query, readOnlySessionState: readOnlySessionState) + guard let scope = readOnlyScope(context) else { return streamRows(query: query) } + return streamRows(query: query, readOnly: scope) } /// The bridge task is retained and cancelled from `onTermination`. Without that, a consumer @@ -1098,7 +1034,7 @@ final class MySQLPluginDriver: PluginDatabaseDriver, @unchecked Sendable { /// nothing ever cancels the task awaiting it. private func streamRows( query: String, - readOnlySessionState: (@Sendable (Bool) -> PluginSessionTransactionState)? + readOnly: MySQLReadOnlyTransaction.Scope? ) -> AsyncThrowingStream { AsyncThrowingStream { continuation in let task = Task { @@ -1107,7 +1043,7 @@ final class MySQLPluginDriver: PluginDatabaseDriver, @unchecked Sendable { let conn = try await requireConnection() defer { self.endOperation(on: conn) } noteActivity(query) - for try await element in conn.streamQuery(query, readOnlySessionState: readOnlySessionState) { + for try await element in conn.streamQuery(query, readOnly: readOnly) { continuation.yield(element) } } @@ -1493,3 +1429,127 @@ final class MySQLPluginDriver: PluginDatabaseDriver, @unchecked Sendable { flavor.hasMySQLObjectCatalogs ? ["SET FOREIGN_KEY_CHECKS=1"] : nil } } + +// MARK: - Statement Context + +// An app read marked `readOnly` alone, a catalog read or a dashboard tile, runs as it is. `enforcesReadOnly` from any +// origin, and `readOnly` from an external one, run in `START TRANSACTION READ ONLY` on a flavor that enforces it. +extension MySQLPluginDriver { + func executeUserQuery( + query: String, + rowCap: Int?, + parameters: [PluginCellValue]?, + context: PluginStatementContext + ) async throws -> PluginQueryResult { + guard let scope = readOnlyScope(context) else { + return try await runUserQuery( + query: query, rowCap: rowCap, parameters: parameters, byteLimit: context.resultByteLimit + ) + } + return try await readOnlyRead( + query: query, parameters: parameters, rowCap: rowCap, byteLimit: context.resultByteLimit, scope: scope + ) + } + + func execute(query: String, context: PluginStatementContext) async throws -> PluginQueryResult { + guard let scope = readOnlyScope(context) else { + return try await runUserQuery(query: query, rowCap: nil, parameters: nil, byteLimit: context.resultByteLimit) + } + return try await readOnlyRead( + query: query, parameters: nil, rowCap: nil, byteLimit: context.resultByteLimit, scope: scope + ) + } + + func executeParameterized( + query: String, + parameters: [PluginCellValue], + context: PluginStatementContext + ) async throws -> PluginQueryResult { + guard let scope = readOnlyScope(context) else { + return try await runUserQuery( + query: query, rowCap: nil, parameters: parameters, byteLimit: context.resultByteLimit + ) + } + return try await readOnlyRead( + query: query, parameters: parameters, rowCap: nil, byteLimit: context.resultByteLimit, scope: scope + ) + } + + // A context the read-only mode does not cover goes back to the host as PluginKit's default sends it, so TiDB, + // OceanBase, Databend and StarRocks never run an `enforcesReadOnly` read without the mode. + func executeBoundedQuery( + query: String, + rowCap: Int, + context: PluginStatementContext + ) async throws -> PluginQueryResult? { + guard let scope = readOnlyScope(context) else { + guard !context.enforcesReadOnly, !context.readOnly, !context.readsCatalog, + context.allowsDestructive, context.allowsServerCode + else { return nil } + return try await runUserQuery( + query: query, rowCap: rowCap, parameters: nil, byteLimit: context.resultByteLimit + ) + } + return try await readOnlyRead( + query: query, parameters: nil, rowCap: rowCap, byteLimit: context.resultByteLimit, scope: scope + ) + } + + private func readOnlyScope(_ context: PluginStatementContext) -> MySQLReadOnlyTransaction.Scope? { + guard let reason = MySQLReadOnlyTransaction.reason(for: context, flavor: flavor) else { return nil } + // The lock comes from the footprint, because the status flags never show it. + return MySQLReadOnlyTransaction.Scope(reason: reason, sessionState: { isInTransaction in + self.sessionLock.withLock { self.footprint.transactionState(isInTransaction: isInTransaction) } + }) + } + + private func readOnlyRead( + query: String, + parameters: [PluginCellValue]?, + rowCap: Int?, + byteLimit: Int?, + scope: MySQLReadOnlyTransaction.Scope + ) async throws -> PluginQueryResult { + let cap = rowCap.flatMap { $0 > 0 ? $0 : nil } + return try await retryingAfterStop { + try await self.executeReadOnly( + query: query, parameters: parameters, rowCap: cap, byteLimit: byteLimit, scope: scope, isRetry: false + ) + } + } + + // Replays on a lost connection exactly when `executeWithReconnect` would. + private func executeReadOnly( + query: String, + parameters: [PluginCellValue]?, + rowCap: Int?, + byteLimit: Int?, + scope: MySQLReadOnlyTransaction.Scope, + isRetry: Bool + ) async throws -> PluginQueryResult { + let startTime = Date() + + let conn = try await requireConnection() + defer { endOperation(on: conn) } + noteActivity(query) + + do { + let result = try await conn.executeReadOnly( + query, parameters: parameters, rowCap: rowCap, byteLimit: byteLimit, scope: scope + ) + return Self.pluginResult(result, startedAt: startTime) + } catch let error as MariaDBPluginError where !isRetry + && parameters == nil + && mysqlConnectionLossMayReplay(code: error.code, outlastedSocketTimeout: error.outlastedSocketTimeout) + && mayReplay(query) { + try await reconnect() + return try await executeReadOnly( + query: query, parameters: nil, rowCap: rowCap, byteLimit: byteLimit, scope: scope, isRetry: true + ) + } catch { + noteFailure(query) + await refreshTransactionFlag(after: error, on: conn) + throw error + } + } +} diff --git a/apple/Plugins/MySQLDriverPlugin/MySQLReadOnlyTransaction.swift b/apple/Plugins/MySQLDriverPlugin/MySQLReadOnlyTransaction.swift index 2527e87292..2abe7697c8 100644 --- a/apple/Plugins/MySQLDriverPlugin/MySQLReadOnlyTransaction.swift +++ b/apple/Plugins/MySQLDriverPlugin/MySQLReadOnlyTransaction.swift @@ -7,6 +7,18 @@ import Foundation import TableProPluginKit internal enum MySQLReadOnlyTransaction { + // Who asked for the read-only mode, which is what a refusal names. + enum Reason: Sendable, Equatable { + case externalClients + case safeMode + } + + // The session state is read on the statement's own queue, so nothing queued behind it can open a transaction first. + struct Scope: Sendable { + let reason: Reason + let sessionState: @Sendable (Bool) -> PluginSessionTransactionState + } + // Not the `/*!50605 ... */` spelling: a server older than 5.6.5 would run that as a plain read-write transaction. static let begin = "START TRANSACTION READ ONLY" static let end = "ROLLBACK" @@ -24,17 +36,36 @@ internal enum MySQLReadOnlyTransaction { ) } + // Read-Only offers no Commit, so the way out is a rollback. + static var safeModeSessionNotIdle: MariaDBPluginError { + MariaDBPluginError( + code: 0, + message: String( + localized: "Safe Mode is set to Read-Only for this connection, and it has a transaction open. Roll it back, then try again." + ), + sqlState: nil + ) + } + + static func reason(for context: PluginStatementContext, flavor: MySQLServerFlavor) -> Reason? { + guard flavor.enforcesReadOnlyTransactions else { return nil } + if context.readOnly, context.origin == .external { return .externalClients } + if context.enforcesReadOnly { return .safeMode } + return nil + } + static func applies(to context: PluginStatementContext, flavor: MySQLServerFlavor) -> Bool { - context.readOnly && context.origin == .external && flavor.enforcesReadOnlyTransactions + reason(for: context, flavor: flavor) != nil } // `START TRANSACTION` would commit the user's open transaction and release their `LOCK TABLES`. static func run( state: PluginSessionTransactionState, + reason: Reason, send: (String) throws -> Void, statement: () throws -> Result ) throws -> Result { - guard state == .idle else { throw sessionNotIdle } + guard state == .idle else { throw notIdle(reason) } do { try send(begin) } catch { @@ -46,20 +77,30 @@ internal enum MySQLReadOnlyTransaction { result = try statement() } catch { try? send(end) - throw refusal(from: error) + throw refusal(from: error, reason: reason) } try send(end) return result } - static func refusal(from error: Error) -> Error { + static func notIdle(_ reason: Reason) -> MariaDBPluginError { + switch reason { + case .externalClients: sessionNotIdle + case .safeMode: safeModeSessionNotIdle + } + } + + static func refusal(from error: Error, reason: Reason) -> Error { guard let failure = error as? MariaDBPluginError, failure.code == refusalCode else { return error } - return MariaDBPluginError( - code: failure.code, - message: String( - localized: "This connection is read only for external clients, and the database refused the statement." - ), - sqlState: failure.sqlState - ) + return MariaDBPluginError(code: failure.code, message: refusalMessage(reason), sqlState: failure.sqlState) + } + + static func refusalMessage(_ reason: Reason) -> String { + switch reason { + case .externalClients: + String(localized: "This connection is read only for external clients, and the database refused the statement.") + case .safeMode: + String(localized: "Safe Mode is set to Read-Only for this connection, and the database refused the statement.") + } } } diff --git a/apple/Plugins/MySQLDriverPlugin/MySQLSelectLimitStatement.swift b/apple/Plugins/MySQLDriverPlugin/MySQLSelectLimitStatement.swift index eaf152013a..606703975d 100644 --- a/apple/Plugins/MySQLDriverPlugin/MySQLSelectLimitStatement.swift +++ b/apple/Plugins/MySQLDriverPlugin/MySQLSelectLimitStatement.swift @@ -121,15 +121,28 @@ internal func mysqlStatementReturnsAtMostOneRow(_ sql: String) -> Bool { return mysqlBodyMatches(String(body[fromRange.lowerBound...]), "^FROM\\s+DUAL\\s*$") } +// A read that stopped at the byte limit keeps the row that crossed it, so the host sees its own limit was reached. internal func mysqlBoundedFetchOutcome( fetchedRows: Int, rowCap: Int, - serverSentMore: Bool + serverSentMore: Bool, + stoppedAtByteLimit: Bool = false ) -> MySQLBoundedFetchOutcome { - let truncated = serverSentMore || fetchedRows > rowCap - return MySQLBoundedFetchOutcome( - keptRows: truncated ? rowCap : fetchedRows, - isTruncated: truncated, + MySQLBoundedFetchOutcome( + keptRows: min(fetchedRows, rowCap), + isTruncated: serverSentMore || stoppedAtByteLimit || fetchedRows > rowCap, serverIgnoredLimit: serverSentMore ) } + +// Text as UTF-8, bytes as they are, null as nothing: never more than the host counts for the same row, so the read never +// stops before the host's own budget is full. +internal func mysqlDecodedByteCount(of row: [PluginCellValue]) -> Int { + row.reduce(0) { total, cell in + switch cell { + case .null: total + case .text(let text): total + text.utf8.count + case .bytes(let data): total + data.count + } + } +} diff --git a/apple/Plugins/MySQLDriverTests/MySQLReadOnlyTransactionTests.swift b/apple/Plugins/MySQLDriverTests/MySQLReadOnlyTransactionTests.swift index 2d42d93758..ed69a8c3fa 100644 --- a/apple/Plugins/MySQLDriverTests/MySQLReadOnlyTransactionTests.swift +++ b/apple/Plugins/MySQLDriverTests/MySQLReadOnlyTransactionTests.swift @@ -20,14 +20,20 @@ struct MySQLReadOnlyTransactionTests { private static let statement = "SELECT * FROM orders" - private static func context(readOnly: Bool, origin: PluginStatementOrigin) -> PluginStatementContext { + private static func context( + readOnly: Bool, + origin: PluginStatementOrigin, + enforcesReadOnly: Bool = false + ) -> PluginStatementContext { var context = PluginStatementContext() context.readOnly = readOnly context.origin = origin + context.enforcesReadOnly = enforcesReadOnly return context } private static let externalRead = context(readOnly: true, origin: .external) + private static let safeModeRead = context(readOnly: false, origin: .app, enforcesReadOnly: true) private static func driver(variant: String? = nil) -> MySQLPluginDriver { MySQLPluginDriver(config: DriverConnectionConfig( @@ -36,11 +42,15 @@ struct MySQLReadOnlyTransactionTests { )) } + private static let nonEnforcingVariants = [ + MySQLServerFlavor.oceanbaseVariant, MySQLServerFlavor.databendVariant, MySQLServerFlavor.starrocksVariant, + ] + @Test("An external read runs between a plain START TRANSACTION READ ONLY and a ROLLBACK") func wrapsTheStatement() throws { let wire = Wire() - let rows = try MySQLReadOnlyTransaction.run(state: .idle, send: wire.send) { + let rows = try MySQLReadOnlyTransaction.run(state: .idle, reason: .externalClients, send: wire.send) { try wire.send(Self.statement) return 3 } @@ -58,7 +68,7 @@ struct MySQLReadOnlyTransactionTests { let wire = Wire() let error = #expect(throws: MariaDBPluginError.self) { - try MySQLReadOnlyTransaction.run(state: state, send: wire.send) { + try MySQLReadOnlyTransaction.run(state: state, reason: .externalClients, send: wire.send) { try wire.send(Self.statement) } } @@ -67,6 +77,24 @@ struct MySQLReadOnlyTransactionTests { #expect(error?.message == MySQLReadOnlyTransaction.sessionNotIdle.message) } + @Test( + "At Read-Only a session that is not idle is refused before anything is sent, naming Safe Mode", + arguments: [PluginSessionTransactionState.inTransaction, .holdsSessionLocks, .abortedTransaction, .unknown] + ) + func safeModeRefusesUnlessIdle(state: PluginSessionTransactionState) { + let wire = Wire() + + let error = #expect(throws: MariaDBPluginError.self) { + try MySQLReadOnlyTransaction.run(state: state, reason: .safeMode, send: wire.send) { + try wire.send(Self.statement) + } + } + + #expect(wire.sent.isEmpty) + #expect(error?.message == MySQLReadOnlyTransaction.safeModeSessionNotIdle.message) + #expect(error?.message != MySQLReadOnlyTransaction.sessionNotIdle.message) + } + @Test("A session holding LOCK TABLES is refused although the server reports no transaction") func tableLockIsNotIdle() { let wire = Wire() @@ -76,6 +104,7 @@ struct MySQLReadOnlyTransactionTests { #expect(throws: MariaDBPluginError.self) { try MySQLReadOnlyTransaction.run( state: footprint.transactionState(isInTransaction: false), + reason: .externalClients, send: wire.send ) { try wire.send(Self.statement) @@ -92,7 +121,7 @@ struct MySQLReadOnlyTransactionTests { wire.failing[Self.statement] = MariaDBPluginError(code: 1_792, message: serverMessage, sqlState: "25006") let error = #expect(throws: MariaDBPluginError.self) { - try MySQLReadOnlyTransaction.run(state: .idle, send: wire.send) { + try MySQLReadOnlyTransaction.run(state: .idle, reason: .externalClients, send: wire.send) { try wire.send(Self.statement) } } @@ -100,7 +129,28 @@ struct MySQLReadOnlyTransactionTests { #expect(wire.sent == ["START TRANSACTION READ ONLY", Self.statement, "ROLLBACK"]) #expect(error?.code == 1_792) #expect(error?.sqlState == "25006") - #expect(error?.message != serverMessage) + #expect(error?.message == MySQLReadOnlyTransaction.refusalMessage(.externalClients)) + } + + // A stored function that writes fails inside the read-only transaction with 1792, which is the EX-1 case. + @Test("At Read-Only error 1792 names Safe Mode, and the rollback still goes out") + func safeModeRefusalNamesSafeMode() { + let wire = Wire() + let call = "SELECT purge_orders()" + wire.failing[call] = MariaDBPluginError( + code: 1_792, message: "Cannot execute statement in a READ ONLY transaction.", sqlState: "25006" + ) + + let error = #expect(throws: MariaDBPluginError.self) { + try MySQLReadOnlyTransaction.run(state: .idle, reason: .safeMode, send: wire.send) { + try wire.send(call) + } + } + + #expect(wire.sent == ["START TRANSACTION READ ONLY", call, "ROLLBACK"]) + #expect(error?.code == 1_792) + #expect(error?.message == MySQLReadOnlyTransaction.refusalMessage(.safeMode)) + #expect(error?.message != MySQLReadOnlyTransaction.refusalMessage(.externalClients)) } @Test("Any other failure keeps its own message, and the rollback still goes out") @@ -110,7 +160,7 @@ struct MySQLReadOnlyTransactionTests { wire.failing[Self.statement] = missing let error = #expect(throws: MariaDBPluginError.self) { - try MySQLReadOnlyTransaction.run(state: .idle, send: wire.send) { + try MySQLReadOnlyTransaction.run(state: .idle, reason: .externalClients, send: wire.send) { try wire.send(Self.statement) } } @@ -128,7 +178,7 @@ struct MySQLReadOnlyTransactionTests { ) let error = #expect(throws: MariaDBPluginError.self) { - try MySQLReadOnlyTransaction.run(state: .idle, send: wire.send) { + try MySQLReadOnlyTransaction.run(state: .idle, reason: .externalClients, send: wire.send) { try wire.send(Self.statement) } } @@ -137,7 +187,7 @@ struct MySQLReadOnlyTransactionTests { #expect(error?.code == 1_064) } - @Test("Only an external read on MySQL or MariaDB is wrapped", arguments: [ + @Test("Only an external read or a Read-Only statement on MySQL or MariaDB is wrapped", arguments: [ (MySQLServerFlavor.mysql, true), (.mariadb, true), (.tidb(version: MySQLEngineVersion(major: 8, minor: 5, patch: 1)), false), @@ -147,17 +197,60 @@ struct MySQLReadOnlyTransactionTests { ]) func wrapsOnlyEnforcingFlavors(flavor: MySQLServerFlavor, wraps: Bool) { #expect(MySQLReadOnlyTransaction.applies(to: Self.externalRead, flavor: flavor) == wraps) + #expect(MySQLReadOnlyTransaction.applies(to: Self.safeModeRead, flavor: flavor) == wraps) #expect(!MySQLReadOnlyTransaction.applies(to: PluginStatementContext(), flavor: flavor)) #expect(!MySQLReadOnlyTransaction.applies(to: Self.context(readOnly: true, origin: .app), flavor: flavor)) } - @Test("The driver declares read-only statements only for a flavor that enforces them") + @Test("The reason follows who asked: an external read names external clients, anything else at Read-Only names Safe Mode") + func reasonFollowsTheContext() { + var catalogRead = Self.context(readOnly: true, origin: .app) + catalogRead.readsCatalog = true + + #expect(MySQLReadOnlyTransaction.reason(for: Self.externalRead, flavor: .mysql) == .externalClients) + #expect(MySQLReadOnlyTransaction.reason(for: Self.safeModeRead, flavor: .mariadb) == .safeMode) + #expect( + MySQLReadOnlyTransaction.reason( + for: Self.context(readOnly: true, origin: .external, enforcesReadOnly: true), flavor: .mysql + ) == .externalClients + ) + #expect( + MySQLReadOnlyTransaction.reason( + for: Self.context(readOnly: false, origin: .external, enforcesReadOnly: true), flavor: .mysql + ) == .safeMode + ) + #expect(MySQLReadOnlyTransaction.reason(for: catalogRead, flavor: .mysql) == nil) + } + + @Test("The driver declares both read-only bits only for a flavor that enforces them") func capabilityFollowsFlavor() { - #expect(Self.driver().capabilities.contains(.readOnlyStatements)) - for variant in [ - MySQLServerFlavor.oceanbaseVariant, MySQLServerFlavor.databendVariant, MySQLServerFlavor.starrocksVariant, - ] { - #expect(!Self.driver(variant: variant).capabilities.contains(.readOnlyStatements), "\(variant)") + let capabilities = Self.driver().capabilities + #expect(capabilities.contains(.readOnlyStatements)) + #expect(capabilities.contains(.enforcesReadOnlyMode)) + for variant in Self.nonEnforcingVariants { + let other = Self.driver(variant: variant).capabilities + #expect(!other.contains(.readOnlyStatements), "\(variant)") + #expect(!other.contains(.enforcesReadOnlyMode), "\(variant)") + } + } + + // Nil sends the host back to a call that takes the context; none of these touch the connection. + @Test("A bounded read the read-only mode cannot cover goes back to the host") + func boundedReadGoesBackWhenNotCovered() async throws { + for variant in Self.nonEnforcingVariants { + let answer = try await Self.driver(variant: variant).executeBoundedQuery( + query: Self.statement, rowCap: 10, context: Self.safeModeRead + ) + #expect(answer == nil, "\(variant)") } + + var catalogRead = Self.context(readOnly: true, origin: .app) + catalogRead.readsCatalog = true + #expect(try await Self.driver().executeBoundedQuery(query: Self.statement, rowCap: 10, context: catalogRead) == nil) + #expect( + try await Self.driver().executeBoundedQuery( + query: Self.statement, rowCap: 10, context: Self.context(readOnly: true, origin: .app) + ) == nil + ) } } diff --git a/apple/Plugins/MySQLDriverTests/MySQLSelectLimitTests.swift b/apple/Plugins/MySQLDriverTests/MySQLSelectLimitTests.swift index b58dcb6868..24c6b5e607 100644 --- a/apple/Plugins/MySQLDriverTests/MySQLSelectLimitTests.swift +++ b/apple/Plugins/MySQLDriverTests/MySQLSelectLimitTests.swift @@ -3,6 +3,7 @@ // TableProTests // +import Foundation import TableProPluginKit import Testing @@ -106,6 +107,32 @@ struct MySQLSelectLimitTests { #expect(outcome.isTruncated == false) } + @Test("A read stopped at the byte limit keeps the row that crossed it and reports truncated") + func byteLimitKeepsTheCrossingRow() { + let outcome = mysqlBoundedFetchOutcome( + fetchedRows: 3, rowCap: 1_000, serverSentMore: false, stoppedAtByteLimit: true + ) + #expect(outcome.keptRows == 3) + #expect(outcome.isTruncated == true) + #expect(outcome.serverIgnoredLimit == false) + } + + @Test("A byte stop on the row past the cap still drops that row") + func byteLimitOnTheExtraRowDropsIt() { + let outcome = mysqlBoundedFetchOutcome( + fetchedRows: 1_001, rowCap: 1_000, serverSentMore: false, stoppedAtByteLimit: true + ) + #expect(outcome.keptRows == 1_000) + #expect(outcome.isTruncated == true) + } + + @Test("A row's size is its text as UTF-8 and its bytes, with null counting nothing") + func decodedByteCountMeasuresTheCells() { + let row: [PluginCellValue] = [.null, .text("héllo"), .bytes(Data(count: 4)), .text("")] + #expect(mysqlDecodedByteCount(of: row) == 10) + #expect(mysqlDecodedByteCount(of: []) == 0) + } + /// Installing the cap costs a `SET`, and a `SET` resets what `ROW_COUNT()` reports, so /// `UPDATE t SET ...; SELECT ROW_COUNT()` answered 0 instead of the UPDATE's count. Measured on /// MariaDB 12.3.2 and MySQL 8.4.11. diff --git a/apple/Plugins/PostgreSQLDriverPlugin/CockroachPluginDriver.swift b/apple/Plugins/PostgreSQLDriverPlugin/CockroachPluginDriver.swift index 0ea3e26c4a..64b0932131 100644 --- a/apple/Plugins/PostgreSQLDriverPlugin/CockroachPluginDriver.swift +++ b/apple/Plugins/PostgreSQLDriverPlugin/CockroachPluginDriver.swift @@ -32,6 +32,7 @@ final class CockroachPluginDriver: LibPQBackedDriver, @unchecked Sendable { .schemaCompare, .dataCompare, .readOnlyStatements, + .enforcesReadOnlyMode, ] } diff --git a/apple/Plugins/PostgreSQLDriverPlugin/LibPQDriverCore.swift b/apple/Plugins/PostgreSQLDriverPlugin/LibPQDriverCore.swift index 9b6c67a479..b24bd76545 100644 --- a/apple/Plugins/PostgreSQLDriverPlugin/LibPQDriverCore.swift +++ b/apple/Plugins/PostgreSQLDriverPlugin/LibPQDriverCore.swift @@ -274,47 +274,64 @@ final class LibPQDriverCore: @unchecked Sendable { ) } - func executeReadOnly(query: String, parameters: [PluginCellValue]?) async throws -> PluginQueryResult { + func executeReadOnly( + query: String, + parameters: [PluginCellValue]?, + reason: LibPQReadOnlyTransaction.Reason + ) async throws -> PluginQueryResult { let pqConn = try connection() let startTime = Date() - let result = try await pqConn.executeReadOnlyStatement(query, parameters: parameters) - return PluginQueryResult( - columns: result.columns, - columnTypeNames: result.columnTypeNames, - rows: result.rows, - rowsAffected: result.affectedRows, - executionTime: Date().timeIntervalSince(startTime), - isTruncated: result.isTruncated - ) + let result = try await pqConn.executeReadOnlyStatement(query, parameters: parameters, reason: reason) + return Self.answer(result, since: startTime) } - // The statement an external caller approved, sent as `executeReadOnly`, `executeParameterized` or - // `executeSingleStatement` sends it, with the relation column each result column reads. + func executeBoundedReadOnly( + query: String, + parameters: [PluginCellValue]?, + bound: LibPQReadBound, + reason: LibPQReadOnlyTransaction.Reason + ) async throws -> PluginQueryResult { + let pqConn = try connection() + let startTime = Date() + let result = try await pqConn.boundedReadOnlyStatement(query, parameters: parameters, bound: bound, reason: reason) + return Self.answer(result, since: startTime) + } + + // The statement an external caller approved, sent as `executeReadOnly`, `executeBoundedReadOnly`, + // `executeParameterized` or `executeSingleStatement` sends it, with the relation column each result column reads. + // Only a read-only read takes `bound`: stopping a write would cancel what the caller approved to run to its end. func executeExternalStatement( query: String, parameters: [PluginCellValue]?, - readOnly: Bool + readOnly: LibPQReadOnlyTransaction.Reason?, + bound: LibPQReadBound? ) async throws -> PluginQueryResult { let pqConn = try connection() let startTime = Date() let result: LibPQPluginQueryResult - if readOnly { - result = try await pqConn.executeReadOnlyStatement(query, parameters: parameters) + if let readOnly, let bound { + result = try await pqConn.boundedReadOnlyStatement(query, parameters: parameters, bound: bound, reason: readOnly) + } else if let readOnly { + result = try await pqConn.executeReadOnlyStatement(query, parameters: parameters, reason: readOnly) } else if let parameters { result = try await pqConn.executeParameterizedQuery(query, parameters: parameters) } else { result = try await pqConn.executeSingleStatement(query) } - var answer = PluginQueryResult( + var answer = Self.answer(result, since: startTime) + answer.columnOrigins = await columnOrigins(for: result.columnSources) + return answer + } + + private static func answer(_ result: LibPQPluginQueryResult, since startTime: Date) -> PluginQueryResult { + PluginQueryResult( columns: result.columns, columnTypeNames: result.columnTypeNames, rows: result.rows, rowsAffected: result.affectedRows, - executionTime: Date().timeIntervalSince(startTime), + timing: PluginQueryTiming(total: Date().timeIntervalSince(startTime), firstRow: result.firstRowTime), isTruncated: result.isTruncated ) - answer.columnOrigins = await columnOrigins(for: result.columnSources) - return answer } // Unpinned (Redshift, CockroachDB) the lookup would run unscoped inside the user's transaction, so it reports none. @@ -344,7 +361,10 @@ final class LibPQDriverCore: @unchecked Sendable { ) } - func streamRows(query: String, readOnly: Bool = false) -> AsyncThrowingStream { + func streamRows( + query: String, + readOnly: LibPQReadOnlyTransaction.Reason? = nil + ) -> AsyncThrowingStream { guard let pqConn = libpqConnection else { return AsyncThrowingStream { $0.finish(throwing: LibPQPluginError.notConnected) } } @@ -529,20 +549,31 @@ extension LibPQBackedDriver { parameters: [PluginCellValue]?, context: PluginStatementContext ) async throws -> PluginQueryResult { + let readOnly = readOnlyReason(context) if context.origin == .external { let answer = try await core.executeExternalStatement( - query: query, parameters: parameters, readOnly: runsReadOnly(context) + query: query, + parameters: parameters, + readOnly: readOnly, + bound: LibPQReadBound.ifSet(rowCap: rowCap, byteLimit: context.resultByteLimit) ) return Self.capped(answer, rowCap: rowCap) } - guard runsReadOnly(context) else { + guard let readOnly else { return try await executeUserQuery(query: query, rowCap: rowCap, parameters: parameters) } - return Self.capped(try await core.executeReadOnly(query: query, parameters: parameters), rowCap: rowCap) + let answer = try await readOnlyRead( + query: query, parameters: parameters, rowCap: rowCap, context: context, reason: readOnly + ) + return Self.capped(answer, rowCap: rowCap) } + // An app read the host marked `readOnly` alone, a catalog read among them, has no read-only reason and keeps its pin. + // One that also sets `enforcesReadOnly` runs read-only and unpinned. func execute(query: String, context: PluginStatementContext) async throws -> PluginQueryResult { - if runsReadOnly(context) { return try await core.executeReadOnly(query: query, parameters: nil) } + if let readOnly = readOnlyReason(context) { + return try await readOnlyRead(query: query, parameters: nil, rowCap: nil, context: context, reason: readOnly) + } // The catalog entry runs its text as a multi-statement string, so it takes only the app's own SQL. if context.readsCatalog, context.origin == .app { return try await catalogRead(query) } return try await execute(query: query) @@ -553,12 +584,28 @@ extension LibPQBackedDriver { parameters: [PluginCellValue], context: PluginStatementContext ) async throws -> PluginQueryResult { - guard runsReadOnly(context) else { return try await executeParameterized(query: query, parameters: parameters) } - return try await core.executeReadOnly(query: query, parameters: parameters) + guard let readOnly = readOnlyReason(context) else { + return try await executeParameterized(query: query, parameters: parameters) + } + return try await readOnlyRead(query: query, parameters: parameters, rowCap: nil, context: context, reason: readOnly) } - private func runsReadOnly(_ context: PluginStatementContext) -> Bool { - LibPQReadOnlyTransaction.applies(to: context, capabilities: capabilities) + private func readOnlyReason(_ context: PluginStatementContext) -> LibPQReadOnlyTransaction.Reason? { + LibPQReadOnlyTransaction.reason(for: context, capabilities: capabilities) + } + + // Bounded at the source when the caller set a row cap or a byte limit, whole otherwise. + private func readOnlyRead( + query: String, + parameters: [PluginCellValue]?, + rowCap: Int?, + context: PluginStatementContext, + reason: LibPQReadOnlyTransaction.Reason + ) async throws -> PluginQueryResult { + guard let bound = LibPQReadBound.ifSet(rowCap: rowCap, byteLimit: context.resultByteLimit) else { + return try await core.executeReadOnly(query: query, parameters: parameters, reason: reason) + } + return try await core.executeBoundedReadOnly(query: query, parameters: parameters, bound: bound, reason: reason) } private static func capped(_ raw: PluginQueryResult, rowCap: Int?) -> PluginQueryResult { @@ -579,13 +626,33 @@ extension LibPQBackedDriver { try await core.executeBoundedQuery(query: query, rowCap: rowCap) } + // A context the read-only mode does not cover goes back to the host as PluginKit's default sends it, so Redshift, + // which declares neither bit, never runs an `enforcesReadOnly` read without the mode. + func executeBoundedQuery( + query: String, + rowCap: Int, + context: PluginStatementContext + ) async throws -> PluginQueryResult? { + guard let readOnly = readOnlyReason(context) else { + guard !context.enforcesReadOnly, !context.readOnly, !context.readsCatalog, + context.allowsDestructive, context.allowsServerCode + else { return nil } + return try await executeBoundedQuery(query: query, rowCap: rowCap) + } + let bound = LibPQReadBound(rowCap: rowCap, byteLimit: context.resultByteLimit) + guard context.origin == .app else { + return try await core.executeExternalStatement(query: query, parameters: nil, readOnly: readOnly, bound: bound) + } + return try await core.executeBoundedReadOnly(query: query, parameters: nil, bound: bound, reason: readOnly) + } + func streamRows(query: String) -> AsyncThrowingStream { core.streamRows(query: query) } func streamRows(query: String, context: PluginStatementContext) -> AsyncThrowingStream { - guard runsReadOnly(context) else { return streamRows(query: query) } - return core.streamRows(query: query, readOnly: true) + guard let readOnly = readOnlyReason(context) else { return streamRows(query: query) } + return core.streamRows(query: query, readOnly: readOnly) } func cancelQuery() throws { diff --git a/apple/Plugins/PostgreSQLDriverPlugin/LibPQPluginConnection.swift b/apple/Plugins/PostgreSQLDriverPlugin/LibPQPluginConnection.swift index a57fb8a048..c5e1d6832b 100644 --- a/apple/Plugins/PostgreSQLDriverPlugin/LibPQPluginConnection.swift +++ b/apple/Plugins/PostgreSQLDriverPlugin/LibPQPluginConnection.swift @@ -776,7 +776,8 @@ final class LibPQPluginConnection: @unchecked Sendable { // One dispatch for the state check, the transaction and the statement, as `executeTransactionScopedRead` does. func executeReadOnlyStatement( _ statement: String, - parameters: [PluginCellValue]? + parameters: [PluginCellValue]?, + reason: LibPQReadOnlyTransaction.Reason ) async throws -> LibPQPluginQueryResult { let statementToRun = String(statement) let params = parameters @@ -786,6 +787,7 @@ final class LibPQPluginConnection: @unchecked Sendable { if let ended = sessionEndedBeforeSending(conn) { throw ended } return try LibPQReadOnlyTransaction.run( state: transactionStateOnQueue().sessionTransactionState, + reason: reason, send: { try runReadOnlyControl($0, on: conn) } ) { guard let params else { return try executeQuerySync(statementToRun, singleStatement: true) } @@ -1356,67 +1358,11 @@ final class LibPQPluginConnection: @unchecked Sendable { defer { refreshStandardConformingStrings(from: conn) } let cancelsOutput = cancelsAbandonedOutput(conn) - var paramValues: [UnsafePointer?] = [] - var paramLengths: [Int32] = [] - var paramFormats: [Int32] = [] - var allocations: [UnsafeMutableRawPointer] = [] - - defer { - for ptr in allocations { - free(ptr) - } - } - - paramValues.reserveCapacity(parameters.count) - paramLengths.reserveCapacity(parameters.count) - paramFormats.reserveCapacity(parameters.count) - - for param in parameters { - switch param { - case .null: - paramValues.append(nil) - paramLengths.append(0) - paramFormats.append(0) - case .text(let str): - guard let cStr = strdup(str) else { - throw LibPQPluginError(message: "Failed to allocate parameter buffer", sqlState: nil, detail: nil) - } - allocations.append(UnsafeMutableRawPointer(cStr)) - paramValues.append(UnsafePointer(cStr)) - paramLengths.append(0) - paramFormats.append(0) - case .bytes(let data): - let byteCount = data.count - guard let raw = malloc(max(byteCount, 1)) else { - throw LibPQPluginError(message: "Failed to allocate parameter buffer", sqlState: nil, detail: nil) - } - allocations.append(raw) - if byteCount > 0 { - data.copyBytes(to: raw.assumingMemoryBound(to: UInt8.self), count: byteCount) - } - paramValues.append(UnsafePointer(raw.assumingMemoryBound(to: CChar.self))) - paramLengths.append(Int32(byteCount)) - paramFormats.append(1) - } - } - - if let ended = sessionEndedBeforeSending(conn) { throw ended } - let localQuery = String(query) - let result: OpaquePointer? = localQuery.withCString { queryPtr in - paramLengths.withUnsafeBufferPointer { lengthsBuf in - paramFormats.withUnsafeBufferPointer { formatsBuf in - PQexecParams( - conn, - queryPtr, - Int32(parameters.count), - nil, - paramValues, - lengthsBuf.baseAddress, - formatsBuf.baseAddress, - 0 - ) - } + let result: OpaquePointer? = try Self.withParameterBuffers(parameters) { values, lengths, formats in + if let ended = sessionEndedBeforeSending(conn) { throw ended } + return localQuery.withCString { queryPtr in + PQexecParams(conn, queryPtr, Int32(parameters.count), nil, values, lengths, formats, 0) } } @@ -1520,7 +1466,10 @@ final class LibPQPluginConnection: @unchecked Sendable { // The abort is polled by the producer rather than acted on from `onTermination`, because both // run on one serial queue: a drain enqueued from the handler sits behind the producer and runs // only once the whole result has been read, which is no abort at all. - func streamQuery(_ query: String, readOnly: Bool = false) -> AsyncThrowingStream { + func streamQuery( + _ query: String, + readOnly: LibPQReadOnlyTransaction.Reason? = nil + ) -> AsyncThrowingStream { let queryToRun = String(query) return PluginRowStream.make { continuation, abort in @@ -1561,10 +1510,11 @@ final class LibPQPluginConnection: @unchecked Sendable { cancelsOutput: cancelsOutput ) do { - if readOnly { + if let readOnly { // The extended protocol takes one statement, so no `COMMIT; INSERT` can follow the read. try LibPQReadOnlyTransaction.run( state: transactionStateOnQueue().sessionTransactionState, + reason: readOnly, send: { try runReadOnlyControl($0, on: conn) } ) { try streamResults(stream, singleStatement: true) @@ -1879,3 +1829,227 @@ extension LibPQPluginConnection { requestServerCancelOnCurrentConnection() } } + +// MARK: - Bounded Read-Only Read + +// Outside the class body, which is at its length limit. An extension in this file still reaches its private members. +extension LibPQPluginConnection { + // `executeReadOnlyStatement` bounded at the source: the statement goes out in single-row mode and stops at `bound`, + // so no more than that is ever held. Stopping cancels only the read-only transaction this opened. Unknown column + // types are looked up after its ROLLBACK, since a cancel can leave it aborted. + func boundedReadOnlyStatement( + _ statement: String, + parameters: [PluginCellValue]?, + bound: LibPQReadBound, + reason: LibPQReadOnlyTransaction.Reason + ) async throws -> LibPQPluginQueryResult { + let statementToRun = String(statement) + let params = parameters ?? [] + + return try await pluginDispatchAsync(on: queue) { [self] in + guard !isShuttingDown, let conn = connectionHandle else { throw LibPQPluginError.notConnected } + if let ended = sessionEndedBeforeSending(conn) { throw ended } + let read = try LibPQReadOnlyTransaction.run( + state: transactionStateOnQueue().sessionTransactionState, + reason: reason, + send: { try runReadOnlyControl($0, on: conn) } + ) { + try boundedReadSync(statementToRun, parameters: params, bound: bound, conn: conn) + } + let metadata = read.metadata.map { resolvingUnknownTypes($0, conn: conn) } + var result = PostGISTypes.rendering( + LibPQPluginQueryResult( + columns: metadata?.columns ?? [], + columnOids: metadata?.columnOids ?? [], + columnTypeNames: metadata?.columnTypeNames ?? [], + rows: read.rows, + affectedRows: read.affectedRows, + commandTag: read.commandTag, + isTruncated: read.isTruncated, + firstRowTime: read.firstRowTime + ), + types: typeNames.postgisTypes + ) + result.columnSources = read.columnSources + return result + } + } + + private struct BoundedRead { + var metadata: ColumnMetadata? + var columnSources: [PostgreSQLColumnOrigins.Source?] = [] + var rows: [[PluginCellValue]] = [] + var affectedRows = 0 + var commandTag: String? + var isTruncated = false + var firstRowTime: TimeInterval? + } + + // Runs inside the read-only transaction `boundedReadOnlyStatement` opened. The session reports a block open, but + // the block is this read's own and ends in ROLLBACK, so the server is cancelled wherever it takes a cancel, which + // `cancelsAbandonedOutput` would refuse inside a block the user opened. PGlite takes none, so its rest is drained. + // The column sources come from the first row description, as every single-row result carries one. + private func boundedReadSync( + _ query: String, + parameters: [PluginCellValue], + bound: LibPQReadBound, + conn: OpaquePointer + ) throws -> BoundedRead { + let generation = cancellationGate.beginQuery() + defer { cancellationGate.endQuery(generation) } + defer { refreshStandardConformingStrings(from: conn) } + + let sentAt = Date() + let suppressCancel = suppressServerSideCancel + let localQuery = String(query) + let sent: Int32 = try Self.withParameterBuffers(parameters) { values, lengths, formats in + if let ended = sessionEndedBeforeSending(conn) { throw ended } + return localQuery.withCString { queryPtr in + PQsendQueryParams(conn, queryPtr, Int32(parameters.count), nil, values, lengths, formats, 0) + } + } + guard sent != 0 else { + throw lostConnection(getError(from: conn), on: conn, sent: false) + } + guard PQsetSingleRowMode(conn) != 0 else { + _ = cancelAndDrain(conn, suppressCancel: suppressCancel) + recordTransactionState(of: conn) + throw LibPQPluginError(message: "Failed to enter single-row mode", sqlState: nil, detail: nil) + } + + var read = BoundedRead() + read.rows.reserveCapacity(min(bound.rowCap, 10_000)) + var bytes = 0 + var stopped = false + var failedStatement: LibPQPluginError? + var pendingError: Error? + + while let result = PQgetResult(conn) { + let status = PQresultStatus(result) + if read.firstRowTime == nil { read.firstRowTime = Date().timeIntervalSince(sentAt) } + if read.metadata == nil, Self.carriesRowDescription(status) { + read.metadata = readColumnMetadata(from: result) + read.columnSources = PostgreSQLColumnOrigins.sources(of: result) + } + + if status == PGRES_SINGLE_TUPLE { + let oids = read.metadata?.columnOids ?? [] + var row: [PluginCellValue] = [] + row.reserveCapacity(oids.count) + for (columnIndex, oid) in oids.enumerated() { + row.append(Self.decodeCell(from: result, row: 0, column: Int32(columnIndex), oid: oid)) + } + PQclear(result) + bytes += LibPQReadBound.byteCount(of: row) + read.rows.append(row) + + if cancellationGate.isCancelled(generation) { + _ = cancelAndDrain(conn, suppressCancel: suppressCancel) + recordTransactionState(of: conn) + throw CancellationError() + } + if bound.isReached(rows: read.rows.count, bytes: bytes) { + stopped = true + break + } + continue + } + + if status == PGRES_TUPLES_OK { + PQclear(result) + continue + } + + if status == PGRES_COMMAND_OK { + read.affectedRows = getAffectedRows(from: result) + read.commandTag = getCommandTag(from: result) + PQclear(result) + continue + } + + if let copy = LibPQCopyState.copy(of: result) { + pendingError = Self.unsupportedCopyError(copy) + PQclear(result) + break + } + + failedStatement = getResultError(from: result) + PQclear(result) + break + } + + let outcome = stopped + ? cancelAndDrain(conn, suppressCancel: suppressCancel) + : finishPendingResults(conn, cancellingOutput: !suppressCancel) + recordTransactionState(of: conn) + + if let failedStatement { + if cancellationGate.isCancelled(generation) { throw CancellationError() } + throw lostConnection(failedStatement, on: conn, sent: true) + } + if let pendingError { + if cancellationGate.isCancelled(generation) { throw CancellationError() } + throw pendingError + } + if cancellationGate.isCancelled(generation) { throw CancellationError() } + if let abandoned = abandonedCopyError(outcome, generation: generation) { throw abandoned } + + read.isTruncated = stopped + read.rows = bound.kept(read.rows) + return read + } + + // libpq reads the parameter buffers only while the call sends them, so they live for `body` alone. + private static func withParameterBuffers( + _ parameters: [PluginCellValue], + _ body: (_ values: [UnsafePointer?], _ lengths: UnsafePointer?, _ formats: UnsafePointer?) throws -> T + ) throws -> T { + var values: [UnsafePointer?] = [] + var lengths: [Int32] = [] + var formats: [Int32] = [] + var allocations: [UnsafeMutableRawPointer] = [] + defer { + for pointer in allocations { + free(pointer) + } + } + values.reserveCapacity(parameters.count) + lengths.reserveCapacity(parameters.count) + formats.reserveCapacity(parameters.count) + + for parameter in parameters { + switch parameter { + case .null: + values.append(nil) + lengths.append(0) + formats.append(0) + case .text(let text): + guard let cString = strdup(text) else { + throw LibPQPluginError(message: "Failed to allocate parameter buffer", sqlState: nil, detail: nil) + } + allocations.append(UnsafeMutableRawPointer(cString)) + values.append(UnsafePointer(cString)) + lengths.append(0) + formats.append(0) + case .bytes(let data): + let byteCount = data.count + guard let raw = malloc(max(byteCount, 1)) else { + throw LibPQPluginError(message: "Failed to allocate parameter buffer", sqlState: nil, detail: nil) + } + allocations.append(raw) + if byteCount > 0 { + data.copyBytes(to: raw.assumingMemoryBound(to: UInt8.self), count: byteCount) + } + values.append(UnsafePointer(raw.assumingMemoryBound(to: CChar.self))) + lengths.append(Int32(byteCount)) + formats.append(1) + } + } + + return try lengths.withUnsafeBufferPointer { lengthsBuffer in + try formats.withUnsafeBufferPointer { formatsBuffer in + try body(values, lengthsBuffer.baseAddress, formatsBuffer.baseAddress) + } + } + } +} diff --git a/apple/Plugins/PostgreSQLDriverPlugin/LibPQReadBound.swift b/apple/Plugins/PostgreSQLDriverPlugin/LibPQReadBound.swift new file mode 100644 index 0000000000..9bee15c2a2 --- /dev/null +++ b/apple/Plugins/PostgreSQLDriverPlugin/LibPQReadBound.swift @@ -0,0 +1,49 @@ +// +// LibPQReadBound.swift +// PostgreSQLDriverPlugin +// + +import Foundation +import TableProPluginKit + +// Where a read-only read stops: past `rowCap` rows, the extra one telling "exactly" from "more", or at the first row that +// takes the decoded rows past `byteLimit`. That row is kept, so the host sees its own limit was reached. +struct LibPQReadBound: Equatable, Sendable { + let rowCap: Int + let byteLimit: Int? + + init(rowCap: Int, byteLimit: Int?) { + self.rowCap = max(rowCap, 1) + self.byteLimit = byteLimit.map { max($0, 0) } + } + + // Nil when neither bound is set, and the caller reads the result whole as it always has. + static func ifSet(rowCap: Int?, byteLimit: Int?) -> LibPQReadBound? { + let cap = rowCap.flatMap { $0 > 0 ? $0 : nil } + guard cap != nil || byteLimit != nil else { return nil } + return LibPQReadBound(rowCap: cap ?? PluginRowLimits.emergencyMax, byteLimit: byteLimit) + } + + func isReached(rows: Int, bytes: Int) -> Bool { + if rows > rowCap { return true } + guard let byteLimit else { return false } + return bytes > byteLimit + } + + // The row that only told "more" goes; a row that crossed the byte limit within the cap stays. + func kept(_ rows: [[PluginCellValue]]) -> [[PluginCellValue]] { + rows.count > rowCap ? Array(rows.prefix(rowCap)) : rows + } + + // The decoded size, at most what the host counts for the same row, so the read never stops before the host's own + // budget is full. + static func byteCount(of row: [PluginCellValue]) -> Int { + row.reduce(0) { total, cell in + switch cell { + case .null: total + case .text(let text): total + text.utf8.count + case .bytes(let data): total + data.count + } + } + } +} diff --git a/apple/Plugins/PostgreSQLDriverPlugin/LibPQReadOnlyTransaction.swift b/apple/Plugins/PostgreSQLDriverPlugin/LibPQReadOnlyTransaction.swift index f4806f707b..81204295f1 100644 --- a/apple/Plugins/PostgreSQLDriverPlugin/LibPQReadOnlyTransaction.swift +++ b/apple/Plugins/PostgreSQLDriverPlugin/LibPQReadOnlyTransaction.swift @@ -7,6 +7,12 @@ import Foundation import TableProPluginKit internal enum LibPQReadOnlyTransaction { + // Who asked for the read-only mode, which is what a refusal names. + enum Reason: Sendable, Equatable { + case externalClients + case safeMode + } + // `SELECT 1` takes the snapshot, after which the server refuses any statement that makes the transaction writable. static let begin = "START TRANSACTION READ ONLY; SELECT 1" // Not COMMIT, so a `SET`, `set_config` or `NOTIFY` the statement ran is undone too. @@ -25,17 +31,39 @@ internal enum LibPQReadOnlyTransaction { ) } + // Read-Only offers no Commit, so the way out is a rollback. + static var safeModeSessionNotIdle: LibPQPluginError { + LibPQPluginError( + message: String( + localized: "Safe Mode is set to Read-Only for this connection, and it has a transaction open. Roll it back, then try again." + ), + sqlState: nil, + detail: nil + ) + } + + static func reason(for context: PluginStatementContext, capabilities: PluginCapabilities) -> Reason? { + if context.readOnly, context.origin == .external, capabilities.contains(.readOnlyStatements) { + return .externalClients + } + if context.enforcesReadOnly, capabilities.contains(.enforcesReadOnlyMode) { + return .safeMode + } + return nil + } + static func applies(to context: PluginStatementContext, capabilities: PluginCapabilities) -> Bool { - context.readOnly && context.origin == .external && capabilities.contains(.readOnlyStatements) + reason(for: context, capabilities: capabilities) != nil } // Inside an open transaction the wrap would make the user's own transaction read-only. static func run( state: PluginSessionTransactionState, + reason: Reason, send: (String) throws -> Void, statement: () throws -> Result ) throws -> Result { - guard state == .idle else { throw sessionNotIdle } + guard state == .idle else { throw notIdle(reason) } do { try send(begin) } catch { @@ -47,23 +75,33 @@ internal enum LibPQReadOnlyTransaction { result = try statement() } catch { try? send(end) - throw refusal(from: error) + throw refusal(from: error, reason: reason) } try send(end) return result } - static func refusal(from error: Error) -> Error { + static func notIdle(_ reason: Reason) -> LibPQPluginError { + switch reason { + case .externalClients: sessionNotIdle + case .safeMode: safeModeSessionNotIdle + } + } + + static func refusal(from error: Error, reason: Reason) -> Error { guard let failure = error as? LibPQPluginError, let sqlState = failure.sqlState, refusalStates.contains(sqlState) else { return error } - return LibPQPluginError( - message: String( - localized: "This connection is read only for external clients, and the database refused the statement." - ), - sqlState: sqlState, - detail: failure.message - ) + return LibPQPluginError(message: refusalMessage(reason), sqlState: sqlState, detail: failure.message) + } + + static func refusalMessage(_ reason: Reason) -> String { + switch reason { + case .externalClients: + String(localized: "This connection is read only for external clients, and the database refused the statement.") + case .safeMode: + String(localized: "Safe Mode is set to Read-Only for this connection, and the database refused the statement.") + } } } diff --git a/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPluginDriver.swift b/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPluginDriver.swift index 332e0df495..f7035e8507 100644 --- a/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPluginDriver.swift +++ b/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPluginDriver.swift @@ -51,7 +51,8 @@ class PostgreSQLPluginDriver: LibPQBackedDriver, @unchecked Sendable { .schemaCompare, .dataCompare, .temporaryTables, - .readOnlyStatements + .readOnlyStatements, + .enforcesReadOnlyMode ] } diff --git a/apple/Plugins/PostgreSQLDriverTests/LibPQBoundedReadOnlyTests.swift b/apple/Plugins/PostgreSQLDriverTests/LibPQBoundedReadOnlyTests.swift new file mode 100644 index 0000000000..e886af72bf --- /dev/null +++ b/apple/Plugins/PostgreSQLDriverTests/LibPQBoundedReadOnlyTests.swift @@ -0,0 +1,218 @@ +// +// LibPQBoundedReadOnlyTests.swift +// PostgreSQLDriverTests +// + +import Foundation +import TableProPluginKit +import Testing + +// A scripted server sends every row at once and, where the answer says so, holds the statement open until a cancel +// arrives, so a read that does not stop and cancel would end only after the server gives up waiting. +@Suite(.timeLimit(.minutes(1))) +struct LibPQBoundedReadOnlyTests { + private static let events = "SELECT * FROM events" + + private static let columns = [ + LibPQScriptedServer.Column(name: "id", table: 16_400, attribute: 1, type: 23), + LibPQScriptedServer.Column(name: "body", table: 16_400, attribute: 3, type: 25), + ] + + private static func rows(_ count: Int, width: Int = 10) -> [[String?]] { + (0.. UInt8 { + let controls = [LibPQReadOnlyTransaction.begin, LibPQReadOnlyTransaction.end, "BEGIN"] + let last = (earlier + [query]).last { controls.contains($0) } + guard let last, last != LibPQReadOnlyTransaction.end else { return LibPQScriptedServer.idle } + return LibPQScriptedServer.inTransaction + } + + private static func server(answering answer: LibPQScriptedServer.Answer) throws -> LibPQScriptedServer { + try LibPQScriptedServer( + status: { query, earlier in Self.status(query, earlier) }, + answer: { query in query == Self.events ? answer : nil } + ) + } + + private static func connected( + to server: LibPQScriptedServer, + suppressingCancel: Bool = false + ) async throws -> LibPQPluginConnection { + let connection = LibPQPluginConnection( + host: "127.0.0.1", + port: server.port, + user: "postgres", + password: nil, + database: "postgres", + suppressServerSideCancel: suppressingCancel + ) + try await connection.connect() + try await connection.finishConnecting() + return connection + } + + private static func read( + _ connection: LibPQPluginConnection, + rowCap: Int, + byteLimit: Int? = nil, + parameters: [PluginCellValue]? = nil, + query: String = LibPQBoundedReadOnlyTests.events + ) async throws -> LibPQPluginQueryResult { + try await connection.boundedReadOnlyStatement( + query, + parameters: parameters, + bound: LibPQReadBound(rowCap: rowCap, byteLimit: byteLimit), + reason: .safeMode + ) + } + + @Test("A read past the row cap keeps the cap, cancels the rest and still rolls back its transaction") + func stopsAtTheRowCap() async throws { + let server = try Self.server(answering: .init(columns: Self.columns, rows: Self.rows(50), waitsForCancel: true)) + defer { server.close() } + server.start() + let connection = try await Self.connected(to: server) + defer { connection.disconnect() } + let before = server.queries.count + + let result = try await Self.read(connection, rowCap: 3) + + let sent = Array(server.queries.dropFirst(before)) + #expect(result.rows.count == 3) + #expect(result.rows.first == [.text("0"), .text("xxxxxxxxxx")]) + #expect(result.isTruncated) + #expect(server.cancelRequests == 1) + #expect(sent == [LibPQReadOnlyTransaction.begin, Self.events, LibPQReadOnlyTransaction.end]) + } + + @Test("A read that passes the byte limit stops there and keeps the row that crossed it") + func stopsAtTheByteLimit() async throws { + let wide = Self.rows(50, width: 100) + let server = try Self.server(answering: .init(columns: Self.columns, rows: wide, waitsForCancel: true)) + defer { server.close() } + server.start() + let connection = try await Self.connected(to: server) + defer { connection.disconnect() } + + let result = try await Self.read(connection, rowCap: 1_000, byteLimit: 250) + + #expect(result.rows.count == 3) + #expect(result.isTruncated) + #expect(server.cancelRequests == 1) + #expect(server.queries.last == LibPQReadOnlyTransaction.end) + } + + @Test("A result within the cap is read to its end with no cancel") + func shortResultReadsWhole() async throws { + let server = try Self.server(answering: .init(columns: Self.columns, rows: Self.rows(2))) + defer { server.close() } + server.start() + let connection = try await Self.connected(to: server) + defer { connection.disconnect() } + let before = server.queries.count + + let result = try await Self.read(connection, rowCap: 3, byteLimit: 1_000) + + let sent = Array(server.queries.dropFirst(before)) + #expect(result.rows.count == 2) + #expect(!result.isTruncated) + #expect(server.cancelRequests == 0) + #expect(sent == [LibPQReadOnlyTransaction.begin, Self.events, LibPQReadOnlyTransaction.end]) + } + + // PGlite has one connection and takes no cancel request. + @Test("Without a server cancel the rest is drained, and the read still stops at the cap") + func drainsWhereNoCancelIsTaken() async throws { + let server = try Self.server(answering: .init(columns: Self.columns, rows: Self.rows(50))) + defer { server.close() } + server.start() + let connection = try await Self.connected(to: server, suppressingCancel: true) + defer { connection.disconnect() } + + let result = try await Self.read(connection, rowCap: 3) + + #expect(result.rows.count == 3) + #expect(result.isTruncated) + #expect(server.cancelRequests == 0) + #expect(server.queries.last == LibPQReadOnlyTransaction.end) + } + + @Test("Each column keeps the relation column the first row description named, so masking by origin still works") + func keepsColumnSources() async throws { + let columns = Self.columns + [LibPQScriptedServer.Column(name: "total")] + let server = try Self.server(answering: .init(columns: columns, rows: [["1", "a", "2"]])) + defer { server.close() } + server.start() + let connection = try await Self.connected(to: server) + defer { connection.disconnect() } + + let result = try await Self.read(connection, rowCap: 10) + + #expect(result.columns == ["id", "body", "total"]) + #expect(result.columnSources == [ + PostgreSQLColumnOrigins.Source(relation: 16_400, attribute: 1), + PostgreSQLColumnOrigins.Source(relation: 16_400, attribute: 3), + nil, + ]) + } + + @Test("A write the server refuses at Read-Only names Safe Mode, and the rollback still goes out") + func refusalNamesSafeMode() async throws { + let refused = LibPQScriptedServer.Failure( + sqlState: "25006", message: "cannot execute DELETE in a read-only transaction" + ) + let server = try Self.server(answering: .init(failure: refused)) + defer { server.close() } + server.start() + let connection = try await Self.connected(to: server) + defer { connection.disconnect() } + + let error = await #expect(throws: LibPQPluginError.self) { + _ = try await Self.read(connection, rowCap: 10) + } + + #expect(error?.sqlState == "25006") + #expect(error?.message == LibPQReadOnlyTransaction.refusalMessage(.safeMode)) + #expect(error?.detail?.contains("read-only transaction") == true) + #expect(server.queries.last == LibPQReadOnlyTransaction.end) + } + + @Test("A session holding a transaction is refused at Read-Only before anything is sent") + func refusesAnOpenTransaction() async throws { + let server = try Self.server(answering: .init(columns: Self.columns, rows: Self.rows(2))) + defer { server.close() } + server.start() + let connection = try await Self.connected(to: server) + defer { connection.disconnect() } + _ = try await connection.executeQuery("BEGIN") + let before = server.queries.count + + let error = await #expect(throws: LibPQPluginError.self) { + _ = try await Self.read(connection, rowCap: 10) + } + + #expect(error?.message == LibPQReadOnlyTransaction.safeModeSessionNotIdle.message) + #expect(server.queries.count == before) + } + + @Test("Parameters go out with the statement, a null as a null") + func bindsParameters() async throws { + let server = try Self.server(answering: .init()) + defer { server.close() } + server.start() + let connection = try await Self.connected(to: server) + defer { connection.disconnect() } + + _ = try await Self.read( + connection, + rowCap: 10, + parameters: [.text("7"), .null], + query: "SELECT * FROM events WHERE id = $1 AND note = $2" + ) + + #expect(server.boundParameters.last == ["7", nil]) + #expect(server.queries.last == LibPQReadOnlyTransaction.end) + } +} diff --git a/apple/Plugins/PostgreSQLDriverTests/LibPQReadBoundTests.swift b/apple/Plugins/PostgreSQLDriverTests/LibPQReadBoundTests.swift new file mode 100644 index 0000000000..644a572321 --- /dev/null +++ b/apple/Plugins/PostgreSQLDriverTests/LibPQReadBoundTests.swift @@ -0,0 +1,57 @@ +// +// LibPQReadBoundTests.swift +// PostgreSQLDriverTests +// + +import Foundation +import TableProPluginKit +import Testing + +struct LibPQReadBoundTests { + @Test("No row cap and no byte limit leaves the read whole") + func noBoundReadsWhole() { + #expect(LibPQReadBound.ifSet(rowCap: nil, byteLimit: nil) == nil) + #expect(LibPQReadBound.ifSet(rowCap: 0, byteLimit: nil) == nil) + #expect(LibPQReadBound.ifSet(rowCap: -5, byteLimit: nil) == nil) + } + + @Test("A byte limit alone bounds the rows at the emergency maximum") + func byteLimitAloneKeepsTheEmergencyCap() { + let bound = LibPQReadBound.ifSet(rowCap: nil, byteLimit: 4_096) + + #expect(bound == LibPQReadBound(rowCap: PluginRowLimits.emergencyMax, byteLimit: 4_096)) + } + + @Test("The read stops at the row past the cap") + func stopsPastTheCap() { + let bound = LibPQReadBound(rowCap: 3, byteLimit: nil) + + #expect(!bound.isReached(rows: 3, bytes: .max)) + #expect(bound.isReached(rows: 4, bytes: 0)) + } + + @Test("The read stops at the first row that takes it past the byte limit, not at the one that reaches it") + func stopsPastTheByteLimit() { + let bound = LibPQReadBound(rowCap: 1_000, byteLimit: 250) + + #expect(!bound.isReached(rows: 2, bytes: 250)) + #expect(bound.isReached(rows: 3, bytes: 251)) + } + + @Test("Only the row that told 'more' is dropped; a row that crossed the byte limit is kept") + func keepsTheCrossingRow() { + let bound = LibPQReadBound(rowCap: 3, byteLimit: 10) + let three: [[PluginCellValue]] = [[.text("a")], [.text("b")], [.text("c")]] + let four = three + [[.text("d")]] + + #expect(bound.kept(three) == three) + #expect(bound.kept(four) == three) + } + + @Test("A row's size is its text in UTF-8 and its bytes, with nulls free") + func measuresDecodedSize() { + let row: [PluginCellValue] = [.text("é"), .bytes(Data([1, 2, 3])), .null, .text("ab")] + + #expect(LibPQReadBound.byteCount(of: row) == 2 + 3 + 0 + 2) + } +} diff --git a/apple/Plugins/PostgreSQLDriverTests/LibPQReadOnlyTransactionTests.swift b/apple/Plugins/PostgreSQLDriverTests/LibPQReadOnlyTransactionTests.swift index e3066bc12c..6d7479e8cc 100644 --- a/apple/Plugins/PostgreSQLDriverTests/LibPQReadOnlyTransactionTests.swift +++ b/apple/Plugins/PostgreSQLDriverTests/LibPQReadOnlyTransactionTests.swift @@ -20,14 +20,20 @@ struct LibPQReadOnlyTransactionTests { private static let statement = "SELECT * FROM orders" - private static func context(readOnly: Bool, origin: PluginStatementOrigin) -> PluginStatementContext { + private static func context( + readOnly: Bool, + origin: PluginStatementOrigin, + enforcesReadOnly: Bool = false + ) -> PluginStatementContext { var context = PluginStatementContext() context.readOnly = readOnly context.origin = origin + context.enforcesReadOnly = enforcesReadOnly return context } private static let externalRead = context(readOnly: true, origin: .external) + private static let safeModeRead = context(readOnly: false, origin: .app, enforcesReadOnly: true) private static let config = DriverConnectionConfig(host: "", port: 0, username: "", password: "", database: "") @@ -35,7 +41,7 @@ struct LibPQReadOnlyTransactionTests { func wrapsTheStatement() throws { let wire = Wire() - let rows = try LibPQReadOnlyTransaction.run(state: .idle, send: wire.send) { + let rows = try LibPQReadOnlyTransaction.run(state: .idle, reason: .externalClients, send: wire.send) { try wire.send(Self.statement) return 3 } @@ -53,7 +59,7 @@ struct LibPQReadOnlyTransactionTests { let wire = Wire() let error = #expect(throws: LibPQPluginError.self) { - try LibPQReadOnlyTransaction.run(state: state, send: wire.send) { + try LibPQReadOnlyTransaction.run(state: state, reason: .externalClients, send: wire.send) { try wire.send(Self.statement) } } @@ -62,6 +68,24 @@ struct LibPQReadOnlyTransactionTests { #expect(error?.message == LibPQReadOnlyTransaction.sessionNotIdle.message) } + @Test( + "At Read-Only a session that is not idle is refused before anything is sent, and told to roll back", + arguments: [PluginSessionTransactionState.inTransaction, .abortedTransaction, .holdsSessionLocks, .unknown] + ) + func safeModeRefusesUnlessIdle(state: PluginSessionTransactionState) { + let wire = Wire() + + let error = #expect(throws: LibPQPluginError.self) { + try LibPQReadOnlyTransaction.run(state: state, reason: .safeMode, send: wire.send) { + try wire.send(Self.statement) + } + } + + #expect(wire.sent.isEmpty) + #expect(error?.message == LibPQReadOnlyTransaction.safeModeSessionNotIdle.message) + #expect(error?.message != LibPQReadOnlyTransaction.sessionNotIdle.message) + } + @Test("A write the read-only transaction refuses comes back as a read-only refusal, and the rollback still goes out", arguments: ["25006", "25001"]) func refusalIsReported(sqlState: String) { @@ -70,14 +94,33 @@ struct LibPQReadOnlyTransactionTests { wire.failing[Self.statement] = LibPQPluginError(message: serverMessage, sqlState: sqlState, detail: nil) let error = #expect(throws: LibPQPluginError.self) { - try LibPQReadOnlyTransaction.run(state: .idle, send: wire.send) { + try LibPQReadOnlyTransaction.run(state: .idle, reason: .externalClients, send: wire.send) { try wire.send(Self.statement) } } #expect(wire.sent == ["START TRANSACTION READ ONLY; SELECT 1", Self.statement, "ROLLBACK"]) #expect(error?.sqlState == sqlState) - #expect(error?.message != serverMessage) + #expect(error?.message == LibPQReadOnlyTransaction.refusalMessage(.externalClients)) + #expect(error?.detail == serverMessage) + } + + @Test("A write refused at Read-Only names Safe Mode and keeps the server's text") + func safeModeRefusalNamesSafeMode() { + let wire = Wire() + let serverMessage = "ERROR: cannot execute DELETE in a read-only transaction" + wire.failing[Self.statement] = LibPQPluginError(message: serverMessage, sqlState: "25006", detail: nil) + + let error = #expect(throws: LibPQPluginError.self) { + try LibPQReadOnlyTransaction.run(state: .idle, reason: .safeMode, send: wire.send) { + try wire.send(Self.statement) + } + } + + #expect(wire.sent == ["START TRANSACTION READ ONLY; SELECT 1", Self.statement, "ROLLBACK"]) + #expect(error?.sqlState == "25006") + #expect(error?.message == LibPQReadOnlyTransaction.refusalMessage(.safeMode)) + #expect(error?.message != LibPQReadOnlyTransaction.refusalMessage(.externalClients)) #expect(error?.detail == serverMessage) } @@ -88,7 +131,7 @@ struct LibPQReadOnlyTransactionTests { wire.failing[Self.statement] = missing let error = #expect(throws: LibPQPluginError.self) { - try LibPQReadOnlyTransaction.run(state: .idle, send: wire.send) { + try LibPQReadOnlyTransaction.run(state: .idle, reason: .safeMode, send: wire.send) { try wire.send(Self.statement) } } @@ -103,7 +146,7 @@ struct LibPQReadOnlyTransactionTests { wire.failing[LibPQReadOnlyTransaction.begin] = LibPQPluginError(message: "refused", sqlState: "0A000", detail: nil) #expect(throws: LibPQPluginError.self) { - try LibPQReadOnlyTransaction.run(state: .idle, send: wire.send) { + try LibPQReadOnlyTransaction.run(state: .idle, reason: .externalClients, send: wire.send) { try wire.send(Self.statement) } } @@ -111,11 +154,12 @@ struct LibPQReadOnlyTransactionTests { #expect(!wire.sent.contains(Self.statement)) } - @Test("Only an external read on a driver that declares read-only statements is wrapped") - func wrapsOnlyExternalReads() { + @Test("An external read and an app read at Read-Only are wrapped; the app's own read-only marks are not") + func wrapsOnlyExternalAndSafeModeReads() { let capabilities = PostgreSQLPluginDriver(config: Self.config).capabilities - #expect(LibPQReadOnlyTransaction.applies(to: Self.externalRead, capabilities: capabilities)) + #expect(LibPQReadOnlyTransaction.reason(for: Self.externalRead, capabilities: capabilities) == .externalClients) + #expect(LibPQReadOnlyTransaction.reason(for: Self.safeModeRead, capabilities: capabilities) == .safeMode) #expect(!LibPQReadOnlyTransaction.applies(to: PluginStatementContext(), capabilities: capabilities)) #expect(!LibPQReadOnlyTransaction.applies( to: Self.context(readOnly: true, origin: .app), capabilities: capabilities @@ -125,7 +169,27 @@ struct LibPQReadOnlyTransactionTests { )) } - @Test("PostgreSQL, PGlite and CockroachDB declare read-only statements") + // `executeCatalogRead` marks its read `readOnly` from the app with `readsCatalog`; a wrap would drop the pin. + @Test("An app catalog read keeps its pin, because only Read-Only wraps an app read") + func catalogReadIsNotWrapped() { + let capabilities = PostgreSQLPluginDriver(config: Self.config).capabilities + var catalogRead = Self.context(readOnly: true, origin: .app) + catalogRead.readsCatalog = true + + #expect(LibPQReadOnlyTransaction.reason(for: catalogRead, capabilities: capabilities) == nil) + } + + @Test("An external read keeps the external clients' wording when Read-Only is also set") + func externalReasonWinsForAnExternalRead() { + let capabilities = PostgreSQLPluginDriver(config: Self.config).capabilities + let both = Self.context(readOnly: true, origin: .external, enforcesReadOnly: true) + let writeAtReadOnly = Self.context(readOnly: false, origin: .external, enforcesReadOnly: true) + + #expect(LibPQReadOnlyTransaction.reason(for: both, capabilities: capabilities) == .externalClients) + #expect(LibPQReadOnlyTransaction.reason(for: writeAtReadOnly, capabilities: capabilities) == .safeMode) + } + + @Test("PostgreSQL, PGlite and CockroachDB declare read-only statements and honor Read-Only") func declaringDrivers() { let drivers: [any PluginDatabaseDriver] = [ PostgreSQLPluginDriver(config: Self.config), @@ -135,16 +199,29 @@ struct LibPQReadOnlyTransactionTests { for driver in drivers { #expect(driver.capabilities.contains(.readOnlyStatements), "\(type(of: driver))") + #expect(driver.capabilities.contains(.enforcesReadOnlyMode), "\(type(of: driver))") #expect(LibPQReadOnlyTransaction.applies(to: Self.externalRead, capabilities: driver.capabilities)) + #expect(LibPQReadOnlyTransaction.applies(to: Self.safeModeRead, capabilities: driver.capabilities)) } } // Redshift documents READ ONLY, but nobody has measured whether it refuses a write there. - @Test("Redshift runs an external read as it always has") + @Test("Redshift runs an external read as it always has, and declares no Read-Only mode") func redshiftDoesNotWrap() { let redshift = RedshiftPluginDriver(config: Self.config) #expect(!redshift.capabilities.contains(.readOnlyStatements)) + #expect(!redshift.capabilities.contains(.enforcesReadOnlyMode)) #expect(!LibPQReadOnlyTransaction.applies(to: Self.externalRead, capabilities: redshift.capabilities)) + #expect(!LibPQReadOnlyTransaction.applies(to: Self.safeModeRead, capabilities: redshift.capabilities)) + } + + @Test("Redshift hands a bounded read at Read-Only back to the host instead of running it without the mode") + func redshiftReturnsBoundedReadAtReadOnly() async throws { + let redshift = RedshiftPluginDriver(config: Self.config) + + let result = try await redshift.executeBoundedQuery(query: Self.statement, rowCap: 10, context: Self.safeModeRead) + + #expect(result == nil) } } diff --git a/apple/Plugins/PostgreSQLDriverTests/LibPQScriptedServer.swift b/apple/Plugins/PostgreSQLDriverTests/LibPQScriptedServer.swift index 4b5eaf6ecd..e41be9da74 100644 --- a/apple/Plugins/PostgreSQLDriverTests/LibPQScriptedServer.swift +++ b/apple/Plugins/PostgreSQLDriverTests/LibPQScriptedServer.swift @@ -7,18 +7,44 @@ import Darwin import Foundation // A loopback PostgreSQL server for one plaintext session. It refuses SSL and GSS encryption, accepts any startup -// without a password, records each simple query, runs `onQuery` before it answers, and answers with an empty -// completion and the transaction status `status` picks from the query and the ones before it. +// without a password, records each simple query and each statement the extended protocol parses, runs `onQuery` before +// it answers, and reports the transaction status `status` picks from the query and the ones before it. A simple query +// gets an empty completion; a parsed statement gets what `answer` returns for it, an empty completion for nil. Cancel +// requests arrive on connections of their own: each is counted, and a statement waiting for one then fails as a +// cancelled statement does. final class LibPQScriptedServer: @unchecked Sendable { struct SetupFailure: Error {} + struct Column: Sendable { + let name: String + var table: UInt32 = 0 + var attribute: Int16 = 0 + var type: UInt32 = 25 + } + + struct Failure: Sendable { + let sqlState: String + let message: String + } + + struct Answer: Sendable { + var columns: [Column] = [] + var rows: [[String?]] = [] + // After the rows, wait for a cancel request and fail with 57014. A statement nobody cancels completes. + var waitsForCancel = false + var failure: Failure? + } + static let idle = UInt8(ascii: "I") static let inTransaction = UInt8(ascii: "T") + static let failedTransaction = UInt8(ascii: "E") private static let protocolVersion: UInt32 = 196_608 private static let sslRequest: UInt32 = 80_877_103 private static let gssEncryptionRequest: UInt32 = 80_877_104 + private static let cancelRequest: UInt32 = 80_877_102 private static let pollMilliseconds: Int32 = 10_000 + private static let cancelWait: DispatchTimeInterval = .seconds(5) private static let parameters: [(String, String)] = [ ("server_version", "17.4"), ("server_encoding", "UTF8"), @@ -33,19 +59,35 @@ final class LibPQScriptedServer: @unchecked Sendable { private let listener: Int32 private let status: @Sendable (_ query: String, _ earlier: [String]) -> UInt8 private let onQuery: @Sendable (String) -> Void + private let answer: @Sendable (String) -> Answer? + private let cancelArrived = DispatchSemaphore(value: 0) private let lock = NSLock() private var received: [String] = [] + private var bindings: [[String?]] = [] + private var cancels = 0 + private var isClosed = false var queries: [String] { lock.withLock { received } } + // Each Bind's parameters as text, nil for a null. + var boundParameters: [[String?]] { + lock.withLock { bindings } + } + + var cancelRequests: Int { + lock.withLock { cancels } + } + init( status: @escaping @Sendable (_ query: String, _ earlier: [String]) -> UInt8 = { _, _ in LibPQScriptedServer.idle }, - onQuery: @escaping @Sendable (String) -> Void = { _ in } + onQuery: @escaping @Sendable (String) -> Void = { _ in }, + answer: @escaping @Sendable (String) -> Answer? = { _ in nil } ) throws { self.status = status self.onQuery = onQuery + self.answer = answer let socketFD = socket(AF_INET, SOCK_STREAM, 0) guard socketFD >= 0 else { throw SetupFailure() } var option: Int32 = 1 @@ -76,23 +118,100 @@ final class LibPQScriptedServer: @unchecked Sendable { } func close() { + lock.withLock { isClosed = true } Darwin.close(listener) } + private struct Statement { + let query: String + let earlier: [String] + let answer: Answer + } + private func runSession() { guard let client = acceptClient() else { return } defer { Darwin.close(client) } + DispatchQueue(label: "com.TablePro.tests.scripted-postgresql.cancel").async { self.acceptCancelRequests() } guard startUp(client) else { return } - while let type = receive(client, 1)?.first, type == UInt8(ascii: "Q"), let body = receiveBody(client) { - let query = String(decoding: body.prefix { $0 != 0 }, as: UTF8.self) - let earlier = lock.withLock { () -> [String] in - defer { received.append(query) } - return received + var statement: Statement? + var failed = false + while let type = receive(client, 1)?.first, let body = receiveBody(client) { + var reply: [UInt8] = [] + switch type { + case UInt8(ascii: "Q"): + let query = Self.cString(body, from: 0).text + let earlier = record(query) + onQuery(query) + reply = Self.message("C", Array("SELECT 0".utf8) + [0]) + Self.message("Z", [status(query, earlier)]) + case UInt8(ascii: "P"): + let query = Self.cString(body, from: Self.cString(body, from: 0).next).text + let earlier = record(query) + onQuery(query) + statement = Statement(query: query, earlier: earlier, answer: answer(query) ?? Answer()) + if !failed { reply = Self.message("1", []) } + case UInt8(ascii: "B"): + lock.withLock { bindings.append(Self.parameters(ofBind: body)) } + if !failed { reply = Self.message("2", []) } + case UInt8(ascii: "D"): + guard !failed, let statement else { break } + let columns = statement.answer.failure == nil ? statement.answer.columns : [] + reply = columns.isEmpty ? Self.message("n", []) : Self.rowDescription(columns) + case UInt8(ascii: "E"): + guard !failed, let statement else { break } + guard execute(statement.answer, on: client, failed: &failed) else { return } + case UInt8(ascii: "S"): + var state = statement.map { status($0.query, $0.earlier) } ?? Self.idle + if failed, state == Self.inTransaction { state = Self.failedTransaction } + reply = Self.message("Z", [state]) + statement = nil + failed = false + case UInt8(ascii: "H"): + break + case UInt8(ascii: "C"): + if !failed { reply = Self.message("3", []) } + default: + return + } + guard reply.isEmpty || send(client, reply) else { return } + } + } + + private func record(_ query: String) -> [String] { + lock.withLock { () -> [String] in + defer { received.append(query) } + return received + } + } + + // The rows go out before any wait, as a server streams them, so the client can stop and cancel while it waits. + private func execute(_ answer: Answer, on client: Int32, failed: inout Bool) -> Bool { + if let failure = answer.failure { + failed = true + return send(client, Self.errorResponse(failure)) + } + guard send(client, answer.rows.flatMap(Self.dataRow)) else { return false } + if answer.waitsForCancel, cancelArrived.wait(timeout: .now() + Self.cancelWait) == .success { + failed = true + return send(client, Self.errorResponse(Failure( + sqlState: "57014", message: "canceling statement due to user request" + ))) + } + return send(client, Self.message("C", Array("SELECT \(answer.rows.count)".utf8) + [0])) + } + + private func acceptCancelRequests() { + while !lock.withLock({ isClosed }) { + var pollFD = pollfd(fd: listener, events: Int16(POLLIN), revents: 0) + guard poll(&pollFD, 1, 100) >= 0 else { return } + guard pollFD.revents & Int16(POLLIN) != 0, !lock.withLock({ isClosed }) else { continue } + let client = accept(listener, nil, nil) + guard client >= 0 else { continue } + if let packet = receiveBody(client), packet.count == 12, Self.uint32(packet.prefix(4)) == Self.cancelRequest { + lock.withLock { cancels += 1 } + cancelArrived.signal() } - onQuery(query) - let reply = Self.message("C", Array("SELECT 0".utf8) + [0]) + Self.message("Z", [status(query, earlier)]) - guard send(client, reply) else { return } + Darwin.close(client) } } @@ -115,6 +234,76 @@ final class LibPQScriptedServer: @unchecked Sendable { return false } + private static func rowDescription(_ columns: [Column]) -> [UInt8] { + var body = bytes(UInt16(columns.count)) + for column in columns { + body += Array(column.name.utf8) + [0] + body += bytes(column.table) + bytes(UInt16(bitPattern: column.attribute)) + bytes(column.type) + body += bytes(UInt16(bitPattern: -1)) + bytes(UInt32(bitPattern: -1)) + bytes(UInt16(0)) + } + return message("T", body) + } + + private static func dataRow(_ row: [String?]) -> [UInt8] { + var body = bytes(UInt16(row.count)) + for value in row { + guard let value else { + body += bytes(UInt32(bitPattern: -1)) + continue + } + body += bytes(UInt32(value.utf8.count)) + Array(value.utf8) + } + return message("D", body) + } + + private static func errorResponse(_ failure: Failure) -> [UInt8] { + let fields: [(UInt8, String)] = [ + (UInt8(ascii: "S"), "ERROR"), + (UInt8(ascii: "V"), "ERROR"), + (UInt8(ascii: "C"), failure.sqlState), + (UInt8(ascii: "M"), failure.message), + ] + var body: [UInt8] = [] + for (code, value) in fields { + body += [code] + Array(value.utf8) + [0] + } + return message("E", body + [0]) + } + + // Portal and statement names, the format codes, then each parameter as a length (-1 for null) and its bytes. + private static func parameters(ofBind body: [UInt8]) -> [String?] { + let afterPortal = cString(body, from: 0).next + var offset = cString(body, from: afterPortal).next + guard let formats = int16(body, at: offset) else { return [] } + offset += 2 + 2 * Int(formats) + guard let count = int16(body, at: offset) else { return [] } + offset += 2 + var values: [String?] = [] + for _ in 0..= 0, offset + Int(length) <= body.count else { + values.append(nil) + continue + } + values.append(String(bytes: body[offset.. (text: String, next: Int) { + guard start < body.count else { return ("", body.count) } + let end = body[start...].firstIndex(of: 0) ?? body.count + return (String(bytes: body[start.. Int16? { + guard offset + 2 <= body.count else { return nil } + return Int16(bitPattern: UInt16(body[offset]) << 8 | UInt16(body[offset + 1])) + } + private static func message(_ type: Character, _ body: [UInt8]) -> [UInt8] { [type.asciiValue ?? 0] + bytes(UInt32(body.count + 4)) + body } @@ -123,6 +312,10 @@ final class LibPQScriptedServer: @unchecked Sendable { [UInt8((value >> 24) & 0xFF), UInt8((value >> 16) & 0xFF), UInt8((value >> 8) & 0xFF), UInt8(value & 0xFF)] } + private static func bytes(_ value: UInt16) -> [UInt8] { + [UInt8((value >> 8) & 0xFF), UInt8(value & 0xFF)] + } + private static func uint32(_ bytes: C) -> UInt32 where C.Element == UInt8 { bytes.reduce(0) { $0 << 8 | UInt32($1) } } diff --git a/apple/Plugins/SQLiteDriverPlugin/SQLiteAgentBackend.swift b/apple/Plugins/SQLiteDriverPlugin/SQLiteAgentBackend.swift index 5ff155c7d4..624a52e18e 100644 --- a/apple/Plugins/SQLiteDriverPlugin/SQLiteAgentBackend.swift +++ b/apple/Plugins/SQLiteDriverPlugin/SQLiteAgentBackend.swift @@ -77,15 +77,27 @@ actor SQLiteAgentBackend: SQLiteExecutionBackend { connection.setBusyTimeout(UInt32(max(0, milliseconds))) } + // The agent stops at the bound's row cap. Its byte limit is not sent: the agent has no such field. func executeQuery( _ query: String, parameters: [PluginCellValue]?, - scope: SQLiteStatementScope + rules: SQLiteStatementRules, + bound: SQLiteReadBound? ) async throws -> SQLiteRawResult { - try await run(query, parameters: (parameters ?? []).map(Self.encode), scope: scope) + try await run( + query, + parameters: (parameters ?? []).map(Self.encode), + rules: rules, + rowCap: bound?.rowCap ?? PluginRowLimits.emergencyMax + ) } - private func run(_ query: String, parameters: [SQLiteAgentValue], scope: SQLiteStatementScope) async throws -> SQLiteRawResult { + private func run( + _ query: String, + parameters: [SQLiteAgentValue], + rules: SQLiteStatementRules, + rowCap: Int + ) async throws -> SQLiteRawResult { let startTime = Date() let accumulator = SQLiteAgentResultAccumulator() @@ -93,10 +105,10 @@ actor SQLiteAgentBackend: SQLiteExecutionBackend { try await connection.execute( sql: query, parameters: parameters, - scope: Self.agentScope(scope), + scope: Self.agentScope(rules), // The buffered path holds the whole result in memory, so it takes the same emergency // ceiling the local backend applies; the streaming path below stays uncapped. - rowCap: UInt32(clamping: PluginRowLimits.emergencyMax), + rowCap: UInt32(clamping: rowCap), onHeader: { header in accumulator.setHeader(header) }, onRows: { columnCount, values in guard columnCount > 0 else { return } @@ -126,14 +138,14 @@ actor SQLiteAgentBackend: SQLiteExecutionBackend { func streamQuery( _ query: String, - scope: SQLiteStatementScope, + rules: SQLiteStatementRules, continuation: AsyncThrowingStream.Continuation ) async throws { _ = try await withTaskCancellationHandler { try await connection.execute( sql: query, parameters: [], - scope: Self.agentScope(scope), + scope: Self.agentScope(rules), rowCap: 0, onHeader: { header in continuation.yield(.header(PluginStreamHeader( @@ -159,12 +171,14 @@ actor SQLiteAgentBackend: SQLiteExecutionBackend { continuation.finish() } - // Nil runs the plain execute, so an unrestricted statement sends the frame it always sent. - private static func agentScope(_ scope: SQLiteStatementScope) -> SQLiteAgentStatementScope? { - switch scope { - case .unrestricted: nil - case .noServerCode: .noServerCode - case .readOnly: .readOnly + // Nil runs the plain execute, so an unrestricted statement sends the frame it always sent. The agent loads no + // extension, so a Safe Mode read loses nothing under its read-only scope. + static func agentScope(_ rules: SQLiteStatementRules) -> SQLiteAgentStatementScope? { + if rules.readsOnly { return .readOnly } + switch rules.scope { + case .unrestricted: return nil + case .noServerCode: return .noServerCode + case .readOnly: return .readOnly } } diff --git a/apple/Plugins/SQLiteDriverPlugin/SQLiteExecutionBackend.swift b/apple/Plugins/SQLiteDriverPlugin/SQLiteExecutionBackend.swift index 5906915c69..da1a8dd201 100644 --- a/apple/Plugins/SQLiteDriverPlugin/SQLiteExecutionBackend.swift +++ b/apple/Plugins/SQLiteDriverPlugin/SQLiteExecutionBackend.swift @@ -43,15 +43,75 @@ protocol SQLiteExecutionBackend: Actor { func executeQuery( _ query: String, parameters: [PluginCellValue]?, - scope: SQLiteStatementScope + rules: SQLiteStatementRules, + bound: SQLiteReadBound? ) async throws -> SQLiteRawResult func streamQuery( _ query: String, - scope: SQLiteStatementScope, + rules: SQLiteStatementRules, continuation: AsyncThrowingStream.Continuation ) async throws } +// How one statement runs: the authorizer scope, and whether read-only, with `PRAGMA query_only` around it and +// `sqlite3_stmt_readonly` checked before it. Safe Mode's Read-Only keeps the session's authorizer scope, so an in-app +// read keeps the extension functions it uses; an external read takes the `.readOnly` scope, which refuses them. +struct SQLiteStatementRules: Sendable, Equatable { + let scope: SQLiteStatementScope + let readsOnly: Bool + + static let unrestricted = SQLiteStatementRules(scope: .unrestricted, readsOnly: false) + + init(scope: SQLiteStatementScope, readsOnly: Bool) { + self.scope = scope + self.readsOnly = readsOnly || scope == .readOnly + } + + init(_ context: PluginStatementContext) { + if context.readOnly, context.origin == .external { + self.init(scope: .readOnly, readsOnly: true) + } else { + self.init(scope: context.allowsServerCode ? .unrestricted : .noServerCode, readsOnly: context.enforcesReadOnly) + } + } + + // What a statement SQLite does not read as read-only is refused with, in the words of whoever asked. + var notReadRefusal: SQLitePluginError { + scope == .readOnly ? .readOnlyStatement : .safeModeReadOnlyStatement + } +} + +// Where a bounded read stops: once `rowCap` rows are read and another is there, or at the first row that takes the +// decoded rows past `byteLimit`. That row is kept, so the host sees its own limit was reached. +struct SQLiteReadBound: Sendable, Equatable { + let rowCap: Int + let byteLimit: Int? + + init(rowCap: Int, byteLimit: Int?) { + self.rowCap = min(max(rowCap, 1), PluginRowLimits.emergencyMax) + self.byteLimit = byteLimit.map { max($0, 0) } + } + + // Nil when neither bound is set, and the statement is read whole as it always was. + static func ifSet(rowCap: Int?, byteLimit: Int?) -> SQLiteReadBound? { + let cap = rowCap.flatMap { $0 > 0 ? $0 : nil } + guard cap != nil || byteLimit != nil else { return nil } + return SQLiteReadBound(rowCap: cap ?? PluginRowLimits.emergencyMax, byteLimit: byteLimit) + } + + // Text as UTF-8, bytes as they are, null as nothing: never more than the host counts for the same row, so the read + // never stops before the host's own budget is full. + static func byteCount(of row: [PluginCellValue]) -> Int { + row.reduce(0) { total, cell in + switch cell { + case .null: total + case .text(let text): total + text.utf8.count + case .bytes(let data): total + data.count + } + } + } +} + /// The one thing about a running query that must be reachable while the query holds the backend /// actor: the request to stop it. The local backend interrupts the SQLite handle; the remote /// backend writes a cancel frame to the agent. @@ -65,18 +125,18 @@ extension SQLiteExecutionBackend { func sessionTransactionState() async -> PluginSessionTransactionState { .unknown } func executeQuery(_ query: String) async throws -> SQLiteRawResult { - try await executeQuery(query, parameters: nil, scope: .unrestricted) + try await executeQuery(query, parameters: nil, rules: .unrestricted, bound: nil) } func executeParameterizedQuery(_ query: String, parameters: [PluginCellValue]) async throws -> SQLiteRawResult { - try await executeQuery(query, parameters: parameters, scope: .unrestricted) + try await executeQuery(query, parameters: parameters, rules: .unrestricted, bound: nil) } func streamQuery( _ query: String, continuation: AsyncThrowingStream.Continuation ) async throws { - try await streamQuery(query, scope: .unrestricted, continuation: continuation) + try await streamQuery(query, rules: .unrestricted, continuation: continuation) } } @@ -282,36 +342,45 @@ actor SQLiteLocalBackend: SQLiteExecutionBackend { func executeQuery( _ query: String, parameters: [PluginCellValue]?, - scope: SQLiteStatementScope + rules: SQLiteStatementRules, + bound: SQLiteReadBound? ) throws -> SQLiteRawResult { - try withStatementHandle(scope) { handle, mustRead in - try runStatement(query, parameters: parameters, on: handle, mustRead: mustRead) + try withStatementHandle(rules) { handle, notReadRefusal in + try runStatement(query, parameters: parameters, on: handle, notReadRefusal: notReadRefusal, bound: bound) } } - // `mustRead` is true when the statement has to be one SQLite reads as read-only. A read-only statement runs on the - // session's handle, so temporary objects, attachments, `last_insert_rowid()` and `changes()` read as they do there. + // `notReadRefusal` is set when the statement has to be one SQLite reads as read-only. A read-only statement runs on + // the session's handle, so temporary objects, attachments, `last_insert_rowid()` and `changes()` read as they do there. private func withStatementHandle( - _ scope: SQLiteStatementScope, - _ body: (OpaquePointer, Bool) throws -> T + _ rules: SQLiteStatementRules, + _ body: (OpaquePointer, SQLitePluginError?) throws -> T ) throws -> T { guard let db, let policy else { throw SQLitePluginError.notConnected } busyState.beginOperation() - switch scope { - case .unrestricted: - return try body(db, false) - case .noServerCode: - policy.scope = .noServerCode - defer { policy.scope = .unrestricted } - return try body(db, false) - case .readOnly: - return try withQueryOnly(db, policy: policy) { try body(db, true) } + guard rules.readsOnly else { + return try Self.within(rules.scope, of: policy) { try body(db, nil) } + } + // The pragmas run before the scope applies: the `.readOnly` scope refuses a pragma that sets anything. + return try withQueryOnly(db) { + try Self.within(rules.scope, of: policy) { try body(db, rules.notReadRefusal) } } } - // query_only stops writes to the database files. It does not stop an extension's function or virtual table from - // reaching other files, and the policy refuses those. - private func withQueryOnly(_ db: OpaquePointer, policy: SQLiteAuthorizerPolicy, _ body: () throws -> T) throws -> T { + private static func within( + _ scope: SQLiteStatementScope, + of policy: SQLiteAuthorizerPolicy, + _ body: () throws -> T + ) rethrows -> T { + guard scope != .unrestricted else { return try body() } + policy.scope = scope + defer { policy.scope = .unrestricted } + return try body() + } + + // query_only stops writes to the database files, a write an application function makes among them. It does not stop + // an extension's function or virtual table from reaching other files, and the `.readOnly` scope refuses those. + private func withQueryOnly(_ db: OpaquePointer, _ body: () throws -> T) throws -> T { let wasQueryOnly = try Self.integerPragma("PRAGMA query_only", on: db) == 1 if !wasQueryOnly { try Self.exec("PRAGMA query_only = 1", on: db) @@ -321,8 +390,6 @@ actor SQLiteLocalBackend: SQLiteExecutionBackend { try? Self.exec("PRAGMA query_only = 0", on: db) } } - policy.scope = .readOnly - defer { policy.scope = .unrestricted } return try body() } @@ -345,14 +412,15 @@ actor SQLiteLocalBackend: SQLiteExecutionBackend { _ query: String, parameters: [PluginCellValue]?, on db: OpaquePointer, - mustRead: Bool + notReadRefusal: SQLitePluginError?, + bound: SQLiteReadBound? ) throws -> SQLiteRawResult { let startTime = Date() let statement = try Self.prepareOnlyStatement(query, db: db) defer { sqlite3_finalize(statement) } // EXPLAIN never runs its statement, but stmt_readonly reports that statement's writes. - if mustRead, sqlite3_stmt_readonly(statement) == 0, sqlite3_stmt_isexplain(statement) == 0 { - throw SQLitePluginError.readOnlyStatement + if let notReadRefusal, sqlite3_stmt_readonly(statement) == 0, sqlite3_stmt_isexplain(statement) == 0 { + throw notReadRefusal } if let parameters { @@ -368,13 +436,23 @@ actor SQLiteLocalBackend: SQLiteExecutionBackend { var rows: [[PluginCellValue]] = [] var truncated = false + let rowLimit = bound?.rowCap ?? PluginRowLimits.emergencyMax + var bytes = 0 var stepResult = firstStep.result while stepResult == SQLITE_ROW { - if rows.count >= PluginRowLimits.emergencyMax { + if rows.count >= rowLimit { truncated = true break } - rows.append(Self.readRow(statement, columnCount: columnCount)) + let row = Self.readRow(statement, columnCount: columnCount) + rows.append(row) + if let byteLimit = bound?.byteLimit { + bytes += SQLiteReadBound.byteCount(of: row) + if bytes > byteLimit { + truncated = true + break + } + } stepResult = sqlite3_step(statement) } @@ -431,25 +509,25 @@ actor SQLiteLocalBackend: SQLiteExecutionBackend { func streamQuery( _ query: String, - scope: SQLiteStatementScope, + rules: SQLiteStatementRules, continuation: AsyncThrowingStream.Continuation ) throws { - try withStatementHandle(scope) { handle, mustRead in - try stream(query, on: handle, mustRead: mustRead, continuation: continuation) + try withStatementHandle(rules) { handle, notReadRefusal in + try stream(query, on: handle, notReadRefusal: notReadRefusal, continuation: continuation) } } private func stream( _ query: String, on db: OpaquePointer, - mustRead: Bool, + notReadRefusal: SQLitePluginError?, continuation: AsyncThrowingStream.Continuation ) throws { let statement = try Self.prepareOnlyStatement(query, db: db) // EXPLAIN never runs its statement, but stmt_readonly reports that statement's writes. - if mustRead, sqlite3_stmt_readonly(statement) == 0, sqlite3_stmt_isexplain(statement) == 0 { + if let notReadRefusal, sqlite3_stmt_readonly(statement) == 0, sqlite3_stmt_isexplain(statement) == 0 { sqlite3_finalize(statement) - throw SQLitePluginError.readOnlyStatement + throw notReadRefusal } let firstStep = SQLiteResultColumns.stepFirst(statement) diff --git a/apple/Plugins/SQLiteDriverPlugin/SQLitePlugin.swift b/apple/Plugins/SQLiteDriverPlugin/SQLitePlugin.swift index 439acba77e..29f8831205 100644 --- a/apple/Plugins/SQLiteDriverPlugin/SQLitePlugin.swift +++ b/apple/Plugins/SQLiteDriverPlugin/SQLitePlugin.swift @@ -152,6 +152,7 @@ final class SQLitePluginDriver: PluginDatabaseDriver, @unchecked Sendable { .dataCompare, .temporaryTables, .readOnlyStatements, + .enforcesReadOnlyMode, ] } @@ -226,9 +227,10 @@ final class SQLitePluginDriver: PluginDatabaseDriver, @unchecked Sendable { } func execute(query: String, context: PluginStatementContext) async throws -> PluginQueryResult { - let scope = SQLiteStatementScope(context) - guard scope != .unrestricted else { return try await execute(query: query) } - return Self.queryResult(try await backend.executeQuery(query, parameters: nil, scope: scope)) + let rules = SQLiteStatementRules(context) + guard rules != .unrestricted else { return try await execute(query: query) } + let bound = Self.bound(rowCap: nil, context: context, rules: rules) + return Self.queryResult(try await backend.executeQuery(query, parameters: nil, rules: rules, bound: bound)) } func executeParameterized(query: String, parameters: [PluginCellValue]) async throws -> PluginQueryResult { @@ -240,9 +242,19 @@ final class SQLitePluginDriver: PluginDatabaseDriver, @unchecked Sendable { parameters: [PluginCellValue], context: PluginStatementContext ) async throws -> PluginQueryResult { - let scope = SQLiteStatementScope(context) - guard scope != .unrestricted else { return try await executeParameterized(query: query, parameters: parameters) } - return Self.queryResult(try await backend.executeQuery(query, parameters: parameters, scope: scope)) + let rules = SQLiteStatementRules(context) + guard rules != .unrestricted else { return try await executeParameterized(query: query, parameters: parameters) } + let bound = Self.bound(rowCap: nil, context: context, rules: rules) + return Self.queryResult(try await backend.executeQuery(query, parameters: parameters, rules: rules, bound: bound)) + } + + // Only a read-only run stops at the byte limit: stopping a write would cancel what the caller approved. + private static func bound( + rowCap: Int?, + context: PluginStatementContext, + rules: SQLiteStatementRules + ) -> SQLiteReadBound? { + SQLiteReadBound.ifSet(rowCap: rowCap, byteLimit: rules.readsOnly ? context.resultByteLimit : nil) } private static func queryResult(_ rawResult: SQLiteRawResult) -> PluginQueryResult { @@ -341,29 +353,39 @@ final class SQLitePluginDriver: PluginDatabaseDriver, @unchecked Sendable { return Self.capped(raw, to: rowCap) } + // A capped statement that returns rows stops in the step loop one row past the cap, a read-only one also at the byte + // limit. Any other runs to its end. func executeUserQuery( query: String, rowCap: Int?, parameters: [PluginCellValue]?, context: PluginStatementContext ) async throws -> PluginQueryResult { - guard SQLiteStatementScope(context) != .unrestricted else { + let rules = SQLiteStatementRules(context) + guard rules != .unrestricted else { return try await executeUserQuery(query: query, rowCap: rowCap, parameters: parameters) } - if parameters == nil, let cap = rowCap, cap > 0, Self.returnsRows(query) { - return try await PluginBoundedStream.collect( - streamRows(query: query, context: context), - rowCap: cap, - startedAt: Date() - ) - } - let raw: PluginQueryResult - if let parameters { - raw = try await executeParameterized(query: query, parameters: parameters, context: context) - } else { - raw = try await execute(query: query, context: context) + let bound = Self.bound(rowCap: Self.returnsRows(query) ? rowCap : nil, context: context, rules: rules) + let raw = try await backend.executeQuery(query, parameters: parameters, rules: rules, bound: bound) + return Self.capped(Self.queryResult(raw), to: rowCap) + } + + // A context the read-only mode does not cover goes back to the host as PluginKit's default sends it. + func executeBoundedQuery( + query: String, + rowCap: Int, + context: PluginStatementContext + ) async throws -> PluginQueryResult? { + let rules = SQLiteStatementRules(context) + guard rules.readsOnly else { + guard !context.enforcesReadOnly, !context.readOnly, !context.readsCatalog, + context.allowsDestructive, context.allowsServerCode + else { return nil } + return try await executeBoundedQuery(query: query, rowCap: rowCap) } - return Self.capped(raw, to: rowCap) + guard Self.returnsRows(query) else { return nil } + let bound = SQLiteReadBound(rowCap: rowCap, byteLimit: context.resultByteLimit) + return Self.queryResult(try await backend.executeQuery(query, parameters: nil, rules: rules, bound: bound)) } private static func capped(_ raw: PluginQueryResult, to rowCap: Int?) -> PluginQueryResult { @@ -683,19 +705,19 @@ final class SQLitePluginDriver: PluginDatabaseDriver, @unchecked Sendable { // MARK: - Streaming func streamRows(query: String) -> AsyncThrowingStream { - stream(query, scope: .unrestricted) + stream(query, rules: .unrestricted) } func streamRows(query: String, context: PluginStatementContext) -> AsyncThrowingStream { - stream(query, scope: SQLiteStatementScope(context)) + stream(query, rules: SQLiteStatementRules(context)) } - private func stream(_ query: String, scope: SQLiteStatementScope) -> AsyncThrowingStream { + private func stream(_ query: String, rules: SQLiteStatementRules) -> AsyncThrowingStream { let queryToRun = String(query) return AsyncThrowingStream(bufferingPolicy: .unbounded) { continuation in let streamTask = Task { do { - try await self.backend.streamQuery(queryToRun, scope: scope, continuation: continuation) + try await self.backend.streamQuery(queryToRun, rules: rules, continuation: continuation) } catch { continuation.finish(throwing: error) } @@ -843,18 +865,6 @@ final class SQLitePluginDriver: PluginDatabaseDriver, @unchecked Sendable { } } -extension SQLiteStatementScope { - init(_ context: PluginStatementContext) { - if context.readOnly, context.origin == .external { - self = .readOnly - } else if !context.allowsServerCode { - self = .noServerCode - } else { - self = .unrestricted - } - } -} - // MARK: - Errors enum SQLitePluginError: Error { @@ -864,6 +874,7 @@ enum SQLitePluginError: Error { case unsupportedOperation case attachedDatabaseStructure case readOnlyStatement + case safeModeReadOnlyStatement } extension SQLitePluginError: PluginDriverError { @@ -877,6 +888,8 @@ extension SQLitePluginError: PluginDriverError { return String(localized: "The structure of a table in an attached database cannot be changed here.") case .readOnlyStatement: return String(localized: "This request is read only, and the statement is not a read.") + case .safeModeReadOnlyStatement: + return String(localized: "Safe Mode is set to Read-Only for this connection, and the database refused the statement.") } } } diff --git a/apple/Plugins/SQLiteDriverTests/SQLiteReadOnlyStatementTests.swift b/apple/Plugins/SQLiteDriverTests/SQLiteReadOnlyStatementTests.swift index 69efdd6ee4..0a3340d1e4 100644 --- a/apple/Plugins/SQLiteDriverTests/SQLiteReadOnlyStatementTests.swift +++ b/apple/Plugins/SQLiteDriverTests/SQLiteReadOnlyStatementTests.swift @@ -36,6 +36,10 @@ struct SQLiteReadOnlyStatementTests { return context } + private static var safeMode: PluginStatementContext { + PluginStatementContext(enforcesReadOnly: true) + } + private func connect(_ database: String) async throws -> SQLitePluginDriver { let driver = SQLitePluginDriver(config: DriverConnectionConfig( host: "", port: 0, username: "", password: "", database: database @@ -54,12 +58,181 @@ struct SQLiteReadOnlyStatementTests { try FileManager.default.contentsOfDirectory(atPath: directory.path).sorted() } - @Test("The driver says it runs a read-only statement in SQLite's own read-only mode") + @Test("The driver says it runs a read-only statement in SQLite's own read-only mode, from outside and at Read-Only") func declaresReadOnlyStatements() { let driver = SQLitePluginDriver(config: DriverConnectionConfig( host: "", port: 0, username: "", password: "", database: path("app.db") )) #expect(driver.capabilities.contains(.readOnlyStatements)) + #expect(driver.capabilities.contains(.enforcesReadOnlyMode)) + } + + @Test("Read-Only reads read-only under the session's own scope, an external read under the read-only scope") + func rulesFollowTheContext() { + var externalSafeModeWrite = Self.external + externalSafeModeWrite.enforcesReadOnly = true + var catalogRead = PluginStatementContext() + catalogRead.readOnly = true + catalogRead.readsCatalog = true + + #expect(SQLiteStatementRules(PluginStatementContext()) == .unrestricted) + #expect(SQLiteStatementRules(catalogRead) == .unrestricted) + #expect(SQLiteStatementRules(Self.safeMode) == SQLiteStatementRules(scope: .unrestricted, readsOnly: true)) + #expect(SQLiteStatementRules(Self.externalRead) == SQLiteStatementRules(scope: .readOnly, readsOnly: true)) + #expect(SQLiteStatementRules(Self.external) == SQLiteStatementRules(scope: .noServerCode, readsOnly: false)) + #expect(SQLiteStatementRules(externalSafeModeWrite) == SQLiteStatementRules(scope: .noServerCode, readsOnly: true)) + #expect(SQLiteAgentBackend.agentScope(SQLiteStatementRules(Self.safeMode)) == .readOnly) + #expect(SQLiteAgentBackend.agentScope(.unrestricted) == nil) + #expect(SQLiteAgentBackend.agentScope(SQLiteStatementRules(Self.external)) == .noServerCode) + } + + // An application function that deletes through its own connection: the shape of a SQL function that writes. + private static let purgeKept: @convention(c) (OpaquePointer?, Int32, UnsafeMutablePointer?) -> Void = { context, _, _ in + let db = sqlite3_context_db_handle(context) + guard sqlite3_exec(db, "DELETE FROM kept", nil, nil, nil) == SQLITE_OK else { + sqlite3_result_error(context, sqlite3_errmsg(db), -1) + return + } + sqlite3_result_int(context, 1) + } + + private typealias ExtensionEntryPoint = @convention(c) ( + OpaquePointer?, UnsafeMutablePointer?>?, UnsafeRawPointer? + ) -> Int32 + + private static let registerPurgeKept: ExtensionEntryPoint = { db, _, _ in + sqlite3_create_function(db, "purge_kept", 0, SQLITE_UTF8, nil, SQLiteReadOnlyStatementTests.purgeKept, nil, nil) + } + + // The driver opens its own handle, so the function goes onto every handle opened while the driver connects. + private func connectWithPurgeKept() async throws -> SQLitePluginDriver { + let entry = unsafeBitCast(Self.registerPurgeKept, to: (@convention(c) () -> Void).self) + #expect(sqlite3_auto_extension(entry) == SQLITE_OK) + defer { sqlite3_cancel_auto_extension(entry) } + return try await connect(path("app.db")) + } + + @Test("At Read-Only a function that writes is refused on every entry point, and the default context lets it write") + func safeModeRefusesAWritingFunction() async throws { + let driver = try await connectWithPurgeKept() + defer { driver.disconnect() } + let call = "SELECT purge_kept()" + + await #expect(throws: (any Error).self) { + _ = try await driver.execute(query: call, context: Self.safeMode) + } + await #expect(throws: (any Error).self) { + _ = try await driver.executeUserQuery(query: call, rowCap: 100, parameters: nil, context: Self.safeMode) + } + await #expect(throws: (any Error).self) { + _ = try await driver.executeParameterized( + query: "SELECT purge_kept(), ?", parameters: [.null], context: Self.safeMode + ) + } + await #expect(throws: (any Error).self) { + _ = try await driver.executeBoundedQuery(query: call, rowCap: 100, context: Self.safeMode) + } + await #expect(throws: (any Error).self) { + for try await _ in driver.streamRows(query: call, context: Self.safeMode) {} + } + await #expect(throws: (any Error).self) { + _ = try await driver.execute(query: call, context: Self.externalRead) + } + #expect(try await count(driver) == "1") + #expect(try await driver.execute(query: "PRAGMA query_only").rows.first?.first?.asText == "0") + + _ = try await driver.execute(query: call, context: PluginStatementContext()) + #expect(try await count(driver) == "0") + } + + @Test("At Read-Only a write is refused, a read runs, and the default context still writes") + func safeModeRefusesWrites() async throws { + let driver = try await connect(path("app.db")) + defer { driver.disconnect() } + + for sql in ["INSERT INTO kept VALUES (2)", "UPDATE kept SET a = 5", "CREATE TABLE added (a INTEGER)"] { + await #expect(throws: (any Error).self, "\(sql)") { + _ = try await driver.execute(query: sql, context: Self.safeMode) + } + } + let safeModeRefusal = await #expect(throws: SQLitePluginError.self) { + _ = try await driver.executeUserQuery( + query: "DELETE FROM kept", rowCap: 10, parameters: nil, context: Self.safeMode + ) + } + #expect(safeModeRefusal?.pluginErrorMessage == SQLitePluginError.safeModeReadOnlyStatement.pluginErrorMessage) + let externalRefusal = await #expect(throws: SQLitePluginError.self) { + _ = try await driver.execute(query: "DELETE FROM kept", context: Self.externalRead) + } + #expect(externalRefusal?.pluginErrorMessage == SQLitePluginError.readOnlyStatement.pluginErrorMessage) + let read = try await driver.executeBoundedQuery(query: "SELECT a FROM kept", rowCap: 10, context: Self.safeMode) + #expect(read?.rows.compactMap { $0.first?.asText } == ["1"]) + #expect(read?.isTruncated == false) + #expect(try await count(driver) == "1") + + _ = try await driver.execute(query: "INSERT INTO kept VALUES (2)", context: PluginStatementContext()) + #expect(try await count(driver) == "2") + } + + @Test("A bounded read the read-only mode does not cover goes back to the host") + func boundedReadGoesBackWhenNotCovered() async throws { + let driver = try await connect(path("app.db")) + defer { driver.disconnect() } + var catalogRead = PluginStatementContext() + catalogRead.readOnly = true + catalogRead.readsCatalog = true + + #expect(try await driver.executeBoundedQuery(query: "SELECT a FROM kept", rowCap: 10, context: catalogRead) == nil) + #expect(try await driver.executeBoundedQuery(query: "SELECT a FROM kept", rowCap: 10, context: Self.external) == nil) + let plain = try await driver.executeBoundedQuery( + query: "SELECT a FROM kept", rowCap: 10, context: PluginStatementContext() + ) + #expect(plain?.rows.count == 1) + } + + @Test("A read-only read stops at the first row past the byte limit and keeps that row") + func readOnlyReadStopsAtTheByteLimit() async throws { + let driver = try await connect(path("app.db")) + defer { driver.disconnect() } + _ = try await driver.execute(query: "DELETE FROM kept") + // Each value reads as six bytes of text. + _ = try await driver.execute(query: "INSERT INTO kept VALUES (100000), (200000), (300000), (400000)") + let sql = "SELECT a FROM kept ORDER BY a" + var limited = Self.externalRead + limited.resultByteLimit = 10 + var safeModeLimited = Self.safeMode + safeModeLimited.resultByteLimit = 10 + + let capped = try await driver.executeUserQuery(query: sql, rowCap: 100, parameters: nil, context: limited) + #expect(capped.rows.compactMap { $0.first?.asText } == ["100000", "200000"]) + #expect(capped.isTruncated) + + let whole = try await driver.execute(query: sql, context: limited) + #expect(whole.rows.count == 2) + #expect(whole.isTruncated) + + let bounded = try await driver.executeBoundedQuery(query: sql, rowCap: 100, context: safeModeLimited) + #expect(bounded?.rows.count == 2) + #expect(bounded?.isTruncated == true) + + let capFirst = try await driver.executeUserQuery(query: sql, rowCap: 1, parameters: nil, context: limited) + #expect(capFirst.rows.compactMap { $0.first?.asText } == ["100000"]) + #expect(capFirst.isTruncated) + + var roomy = Self.externalRead + roomy.resultByteLimit = 1_000 + let all = try await driver.executeUserQuery(query: sql, rowCap: 100, parameters: nil, context: roomy) + #expect(all.rows.count == 4) + #expect(!all.isTruncated) + } + + @Test("A read bound exists only when a cap or a byte limit is set, and a row's size is its decoded bytes") + func readBoundShape() { + #expect(SQLiteReadBound.ifSet(rowCap: nil, byteLimit: nil) == nil) + #expect(SQLiteReadBound.ifSet(rowCap: 0, byteLimit: nil) == nil) + #expect(SQLiteReadBound.ifSet(rowCap: nil, byteLimit: 5)?.rowCap == PluginRowLimits.emergencyMax) + #expect(SQLiteReadBound.ifSet(rowCap: 7, byteLimit: nil) == SQLiteReadBound(rowCap: 7, byteLimit: nil)) + #expect(SQLiteReadBound.byteCount(of: [.null, .text("héllo"), .bytes(Data(count: 4))]) == 10) } @Test("A read-only statement that writes, attaches or copies the database is refused and changes nothing", arguments: [ diff --git a/apple/Plugins/TableProPluginKit/PluginCapabilities.swift b/apple/Plugins/TableProPluginKit/PluginCapabilities.swift index 67ed3dcbf9..1efed41fc8 100644 --- a/apple/Plugins/TableProPluginKit/PluginCapabilities.swift +++ b/apple/Plugins/TableProPluginKit/PluginCapabilities.swift @@ -44,4 +44,7 @@ public struct PluginCapabilities: OptionSet, Sendable { // The driver writes the column default only where `PluginRowChange.defaultColumns` marks one, so the text // `__DEFAULT__` in any other cell is saved as text. public static let columnDefaultMarks = PluginCapabilities(rawValue: 1 << 23) + // A `PluginStatementContext.enforcesReadOnly` statement runs in the engine's read-only mode or throws, from every + // entry point that takes a context, `executeBoundedQuery(query:rowCap:context:)` included. + public static let enforcesReadOnlyMode = PluginCapabilities(rawValue: 1 << 24) } diff --git a/apple/Plugins/TableProPluginKit/PluginDatabaseDriver.swift b/apple/Plugins/TableProPluginKit/PluginDatabaseDriver.swift index b1f03c7154..ad8f52e7fe 100644 --- a/apple/Plugins/TableProPluginKit/PluginDatabaseDriver.swift +++ b/apple/Plugins/TableProPluginKit/PluginDatabaseDriver.swift @@ -127,6 +127,15 @@ public protocol PluginDatabaseDriver: AnyObject, Sendable { /// statement on the server. func executeBoundedQuery(query: String, rowCap: Int) async throws -> PluginQueryResult? + // The same bounded read with what the host decided about it, under the same contract. The default runs the + // requirement above for a context that asks nothing that call cannot honor, and returns nil otherwise, so the host + // falls back to a call that takes the context. + func executeBoundedQuery( + query: String, + rowCap: Int, + context: PluginStatementContext + ) async throws -> PluginQueryResult? + /// Sends `query` to the server as one batch and reads it to the end, returning every result set it produced and /// every error it raised. `rowCap` bounds each result set on its own. A driver that implements this declares /// ``PluginCapabilities/resultSetBatches``; the default returns nil, and the host runs the text statement by @@ -1455,6 +1464,19 @@ public extension PluginDatabaseDriver { func executeBoundedQuery(query: String, rowCap: Int) async throws -> PluginQueryResult? { nil } + // A driver built before the context overload ignores every field, so a read-only mode, a statement policy or a + // catalog pin it was asked for would silently not happen. + func executeBoundedQuery( + query: String, + rowCap: Int, + context: PluginStatementContext + ) async throws -> PluginQueryResult? { + guard !context.enforcesReadOnly, !context.readOnly, !context.readsCatalog, + context.allowsDestructive, context.allowsServerCode + else { return nil } + return try await executeBoundedQuery(query: query, rowCap: rowCap) + } + func executeBatch(query: String, rowCap: Int?, parameters: [PluginCellValue]?) async throws -> PluginBatchResult? { nil } diff --git a/apple/Plugins/TableProPluginKit/PluginStatementContext.swift b/apple/Plugins/TableProPluginKit/PluginStatementContext.swift index ef39c06a8c..90f995e091 100644 --- a/apple/Plugins/TableProPluginKit/PluginStatementContext.swift +++ b/apple/Plugins/TableProPluginKit/PluginStatementContext.swift @@ -9,7 +9,8 @@ import Foundation public struct PluginStatementContext: Sendable, Equatable { // From an external origin, a driver that declares `PluginCapabilities.readOnlyStatements` runs the statement in // the engine's read-only mode and throws when the session state does not allow it. A scripting driver refuses - // every write whatever the origin. + // every write whatever the origin. A SQL driver runs an app statement marked this way as it is, a catalog read + // among them: Safe Mode's Read-Only level is `enforcesReadOnly`. public var readOnly = false public var origin: PluginStatementOrigin = .app @@ -35,7 +36,22 @@ public struct PluginStatementContext: Sendable, Equatable { // `.decimal` as numbers, `.boolean` as TRUE or FALSE. Nil, or a nil entry, where none was given. public var parameterKinds: [PluginColumnKind?]? + // Safe Mode is Read-Only for the connection. A driver that declares `PluginCapabilities.enforcesReadOnlyMode` runs + // the statement in the engine's read-only mode whatever the origin, or throws. + public var enforcesReadOnly = false + + // Stop reading rows once the decoded rows pass this many bytes, keeping the row that crossed it, so the host sees + // the limit was reached. Nil for no limit. The host sets it on reads only, since stopping a write cancels it. + public var resultByteLimit: Int? + + // Kept at its published signature: a defaulted parameter would replace its mangled symbol. + @_disfavoredOverload public init() {} + + public init(enforcesReadOnly: Bool = false, resultByteLimit: Int? = nil) { + self.enforcesReadOnly = enforcesReadOnly + self.resultByteLimit = resultByteLimit + } } public enum PluginStatementOrigin: Sendable, Equatable { diff --git a/apple/TablePro/Core/AI/CLI/AgentCLIProcess.swift b/apple/TablePro/Core/AI/CLI/AgentCLIProcess.swift index c465866ced..3782a91374 100644 --- a/apple/TablePro/Core/AI/CLI/AgentCLIProcess.swift +++ b/apple/TablePro/Core/AI/CLI/AgentCLIProcess.swift @@ -92,35 +92,34 @@ struct AgentCLIProcess: Sendable { process.standardError = stderr let label = self.label - let stderrDrain = Task { - for try await line in stderr.fileHandleForReading.bytes.lines where !line.isEmpty { - Self.logger.error("\(label, privacy: .public) stderr: \(line, privacy: .private)") - } + // Not FileHandle.bytes: Foundation runs every AsyncBytes read on one serial queue, so a quiet + // stderr or an idle language server holds the stdout read until the tool exits. + let stderrReader = PipeReader(stderr.fileHandleForReading) + stderrReader.startDeliveringLines { line in + guard !line.isEmpty else { return } + Self.logger.error("\(label, privacy: .public) stderr: \(line, privacy: .private)") } do { try process.run() } catch { - stderrDrain.cancel() + stderrReader.stop() continuation.finish(throwing: AgentCLIError.launchFailed(error.localizedDescription)) return } - let reader = Task { - do { - for try await line in stdout.fileHandleForReading.bytes.lines { - if Task.isCancelled { break } - continuation.yield(line) - } - continuation.finish() - } catch { - continuation.finish(throwing: error) - } - } + let stdoutReader = PipeReader(stdout.fileHandleForReading) + stdoutReader.startDeliveringLines( + { line in + guard !line.isEmpty else { return } + continuation.yield(line) + }, + onEnd: { continuation.finish() } + ) continuation.onTermination = { _ in - reader.cancel() - stderrDrain.cancel() + stdoutReader.stop() + stderrReader.stop() Self.terminate(process, label: label) } } diff --git a/apple/TablePro/Core/AI/Chat/ChatToolCallRepair.swift b/apple/TablePro/Core/AI/Chat/ChatToolCallRepair.swift index 63302e7804..b499238ae4 100644 --- a/apple/TablePro/Core/AI/Chat/ChatToolCallRepair.swift +++ b/apple/TablePro/Core/AI/Chat/ChatToolCallRepair.swift @@ -37,6 +37,19 @@ enum ChatToolCallRepair { return repaired } + // A stored card has nothing left to answer it, so storage and a loaded transcript never hold `.pending`. + static func settlingPendingApprovals(_ turns: [ChatTurnWire]) -> [ChatTurnWire] { + turns.map { turn in + var settled = turn + settled.blocks = turn.blocks.map { block in + guard case .toolUse(var call) = block.kind, case .pending = call.approvalState else { return block } + call.approvalState = .cancelled + return ChatContentBlockWire(id: block.id, kind: .toolUse(call)) + } + return settled + } + } + static func interruptedResults(for blocks: [ToolUseBlock]) -> [ToolResultBlock] { blocks.map { block in switch block.approvalState { @@ -59,11 +72,23 @@ enum ChatToolCallRepair { for block in turn.blocks { guard case .toolUse(let call) = block.kind, !answered.contains(call.id) else { continue } answered.insert(call.id) - owed.append(ToolResultBlock(toolUseId: call.id, content: interruptedContent, isError: true)) + owed.append(ToolResultBlock(toolUseId: call.id, content: unansweredContent(for: call), isError: true)) } return owed } + // An approved call with no result may have run; a cancelled or denied one never did. + private static func unansweredContent(for call: ToolUseBlock) -> String { + switch call.approvalState { + case .approved, .pending: + return interruptedContent + case .cancelled: + return stoppedBeforeRunContent + case .denied(let reason): + return reason + } + } + private static func toolResultIds(in turn: ChatTurnWire) -> Set { Set(turn.blocks.compactMap { block -> String? in guard case .toolResult(let result) = block.kind else { return nil } diff --git a/apple/TablePro/Core/AI/Chat/ChatTransport.swift b/apple/TablePro/Core/AI/Chat/ChatTransport.swift index 03f685965a..2b0b566c0c 100644 --- a/apple/TablePro/Core/AI/Chat/ChatTransport.swift +++ b/apple/TablePro/Core/AI/Chat/ChatTransport.swift @@ -24,17 +24,14 @@ struct ChatTransportOptions: Sendable { var temperature: Double? var tools: [ChatToolSpec] var reasoningEffort: ReasoningEffort? - /// Which agent session this turn belongs to. - /// - /// A stateful transport keeps conversation state per session rather than per provider - /// configuration. `AIProviderFactory` caches one provider per configuration, so two sessions on - /// one Copilot configuration appended their turns to a single server-side conversation and each - /// was answered with the other's context, across connections included, while their local - /// transcripts stayed correctly separate. + // A stateful transport keys its conversation by session: providers are cached per configuration, and two + // sessions on one Copilot configuration once shared a single server-side conversation. var sessionId: UUID // The connection the conversation is attached to. A transport that hands the model its own // database tools scopes them to this one, and hands out none without it. var connectionId: UUID? + // A transport that grants its own database tools narrows them by mode; a caller that leaves it out gets Ask. + var chatMode: AIChatMode init( model: String, @@ -44,7 +41,8 @@ struct ChatTransportOptions: Sendable { tools: [ChatToolSpec] = [], reasoningEffort: ReasoningEffort? = nil, sessionId: UUID = UUID(), - connectionId: UUID? = nil + connectionId: UUID? = nil, + chatMode: AIChatMode = .ask ) { self.model = model self.systemPrompt = systemPrompt @@ -54,6 +52,7 @@ struct ChatTransportOptions: Sendable { self.reasoningEffort = reasoningEffort self.sessionId = sessionId self.connectionId = connectionId + self.chatMode = chatMode } } diff --git a/apple/TablePro/Core/AI/Chat/ToolApprovalCenter.swift b/apple/TablePro/Core/AI/Chat/ToolApprovalCenter.swift index 6e0677cc21..eee5e72245 100644 --- a/apple/TablePro/Core/AI/Chat/ToolApprovalCenter.swift +++ b/apple/TablePro/Core/AI/Chat/ToolApprovalCenter.swift @@ -124,6 +124,13 @@ final class ToolApprovalCenter { } } + // Not observable: safe for a view to read only because every change here lands in the same + // main-actor turn as the transcript change that draws or settles the card. + func isAwaiting(sessionId: UUID, toolUseId: String) -> Bool { + let key = ApprovalRequestID(sessionId: sessionId, toolUseId: toolUseId) + return expected.contains(key) || pending[key] != nil + } + func hasPending(sessionId: UUID) -> Bool { pending.keys.contains { $0.sessionId == sessionId } || expected.contains { key in diff --git a/apple/TablePro/Core/AI/Chat/Tools/ConfirmDestructiveOperationChatTool.swift b/apple/TablePro/Core/AI/Chat/Tools/ConfirmDestructiveOperationChatTool.swift index 417bb6b8ba..44c10fac30 100644 --- a/apple/TablePro/Core/AI/Chat/Tools/ConfirmDestructiveOperationChatTool.swift +++ b/apple/TablePro/Core/AI/Chat/Tools/ConfirmDestructiveOperationChatTool.swift @@ -98,7 +98,8 @@ struct ConfirmDestructiveOperationChatTool: ChatTool { operation: operation, maxRows: maxRows, timeoutSeconds: MCPLimitResolver.resolveTimeoutSeconds(requested: nil, settings: mcpSettings), - principal: .inAppAssistant + principal: .inAppAssistant, + budget: .assistantAnswer ) return ChatToolResult(content: payload.jsonString(prettyPrinted: true)) } diff --git a/apple/TablePro/Core/AI/Chat/Tools/ExecuteQueryChatTool.swift b/apple/TablePro/Core/AI/Chat/Tools/ExecuteQueryChatTool.swift index 66f7fa5da1..a80934bf33 100644 --- a/apple/TablePro/Core/AI/Chat/Tools/ExecuteQueryChatTool.swift +++ b/apple/TablePro/Core/AI/Chat/Tools/ExecuteQueryChatTool.swift @@ -118,7 +118,8 @@ struct ExecuteQueryChatTool: ChatTool { operation: operation, maxRows: maxRows, timeoutSeconds: timeoutSeconds, - principal: .inAppAssistant + principal: .inAppAssistant, + budget: .assistantAnswer ) return ChatToolResult(content: payload.jsonString(prettyPrinted: true)) } diff --git a/apple/TablePro/Core/AI/Chat/Tools/ListConnectionsChatTool.swift b/apple/TablePro/Core/AI/Chat/Tools/ListConnectionsChatTool.swift index 92c74bda4d..2245a87974 100644 --- a/apple/TablePro/Core/AI/Chat/Tools/ListConnectionsChatTool.swift +++ b/apple/TablePro/Core/AI/Chat/Tools/ListConnectionsChatTool.swift @@ -8,12 +8,13 @@ import TableProMCP struct ListConnectionsChatTool: ChatTool { let name = "list_connections" - let description = String(localized: "List all saved database connections with their current status.") + let description = String(localized: "Show the connection this conversation works on, with its status.") let inputSchema: JsonValue = ChatToolSchemaBuilder.object(properties: [:]) let mode: ChatToolMode = .readOnly func execute(input: JsonValue, context: ChatToolContext) async throws -> ChatToolResult { - let payload = await context.bridge.listConnections() + let connectionId = try await ChatToolTarget.authorized(context: context, input: input, tool: name) + let payload = await context.bridge.listConnection(id: connectionId) return ChatToolResult(content: payload.jsonString(prettyPrinted: true)) } } diff --git a/apple/TablePro/Core/AI/ClaudeAgent/ClaudeAgentMCPBridge.swift b/apple/TablePro/Core/AI/ClaudeAgent/ClaudeAgentMCPBridge.swift index 3450ce5425..fa87cef153 100644 --- a/apple/TablePro/Core/AI/ClaudeAgent/ClaudeAgentMCPBridge.swift +++ b/apple/TablePro/Core/AI/ClaudeAgent/ClaudeAgentMCPBridge.swift @@ -10,10 +10,12 @@ import TableProMCP struct ClaudeAgentToolGrant: Sendable, Equatable { let configPath: String let tokenId: UUID + // Nil leaves every tool the token's scopes allow. + let allowedToolNames: [String]? } protocol ClaudeAgentMCPBridging: Sendable { - func makeGrant(connectionId: UUID?) async -> ClaudeAgentToolGrant? + func makeGrant(connectionId: UUID?, mode: AIChatMode) async -> ClaudeAgentToolGrant? func release(_ grant: ClaudeAgentToolGrant?) async } @@ -33,7 +35,15 @@ struct ClaudeAgentMCPBridge: ClaudeAgentMCPBridging { return .limited([connectionId]) } - func makeGrant(connectionId: UUID?) async -> ClaudeAgentToolGrant? { + // Ask promises schema lookups only, so the server holds the token to them; Edit and Agent keep the read-only token. + static func toolProfile(for mode: AIChatMode) -> MCPToolProfile? { + switch mode { + case .ask: .schemaLookup + case .edit, .agent: nil + } + } + + func makeGrant(connectionId: UUID?, mode: AIChatMode) async -> ClaudeAgentToolGrant? { guard let connectionAccess = Self.connectionAccess(for: connectionId) else { Self.logger.info("No connection attached; Claude Agent runs without database tools") return nil @@ -41,6 +51,7 @@ struct ClaudeAgentMCPBridge: ClaudeAgentMCPBridging { guard let endpoint = await Self.resolveEndpoint() else { return nil } guard let tokenStore = await MCPServerManager.shared.tokenStore else { return nil } + let toolProfile = Self.toolProfile(for: mode) guard let generated = try? await tokenStore.generate( name: Self.tokenName, permissions: permissions, @@ -48,7 +59,8 @@ struct ClaudeAgentMCPBridge: ClaudeAgentMCPBridging { expiresAt: Date.now.addingTimeInterval(Self.tokenLifetime), isBridgeCredential: false, isClaudeAgentCredential: true, - extraScopes: [] + extraScopes: [], + toolProfile: toolProfile ) else { return nil } guard let configPath = Self.writeConfig(endpoint: endpoint, token: generated.plaintext) else { @@ -57,7 +69,11 @@ struct ClaudeAgentMCPBridge: ClaudeAgentMCPBridging { } Self.logger.debug("Issued Claude Agent MCP grant") - return ClaudeAgentToolGrant(configPath: configPath, tokenId: generated.token.id) + return ClaudeAgentToolGrant( + configPath: configPath, + tokenId: generated.token.id, + allowedToolNames: toolProfile.map { $0.toolNames.sorted() } + ) } func release(_ grant: ClaudeAgentToolGrant?) async { diff --git a/apple/TablePro/Core/AI/ClaudeAgent/ClaudeAgentProvider.swift b/apple/TablePro/Core/AI/ClaudeAgent/ClaudeAgentProvider.swift index cdf25b831a..bb00bc479a 100644 --- a/apple/TablePro/Core/AI/ClaudeAgent/ClaudeAgentProvider.swift +++ b/apple/TablePro/Core/AI/ClaudeAgent/ClaudeAgentProvider.swift @@ -31,14 +31,15 @@ final class ClaudeAgentProvider: ChatTransport { let task = Task { var grant: ClaudeAgentToolGrant? do { - grant = await toolBridge.makeGrant(connectionId: options.connectionId) + grant = await toolBridge.makeGrant(connectionId: options.connectionId, mode: options.chatMode) let prompt = AgentPromptRenderer.renderPrompt(turns: turns, includingSystemPrompt: nil) let lines = try cli.streamLines( arguments: Self.inferenceArguments( prompt: prompt, model: model, systemPrompt: options.systemPrompt ?? ClaudeAgent.systemPrompt, - mcpConfigPath: grant?.configPath + mcpConfigPath: grant?.configPath, + allowedToolNames: grant?.allowedToolNames ), currentDirectory: FileManager.default.temporaryDirectory ) @@ -100,7 +101,8 @@ final class ClaudeAgentProvider: ChatTransport { prompt: String, model: String, systemPrompt: String?, - mcpConfigPath: String? + mcpConfigPath: String?, + allowedToolNames: [String]? ) -> [String] { var arguments = [ "-p", @@ -114,7 +116,7 @@ final class ClaudeAgentProvider: ChatTransport { ] if let mcpConfigPath { - arguments += ["--mcp-config", mcpConfigPath, "--allowedTools", "mcp__\(ClaudeAgent.mcpServerName)__*"] + arguments += ["--mcp-config", mcpConfigPath, "--allowedTools", allowedToolsArgument(allowedToolNames)] } arguments.append("--strict-mcp-config") @@ -128,6 +130,13 @@ final class ClaudeAgentProvider: ChatTransport { return arguments } + // The server's token profile is the gate; naming the tools here keeps the CLI from offering any other. + static func allowedToolsArgument(_ toolNames: [String]?) -> String { + let prefix = "mcp__\(ClaudeAgent.mcpServerName)__" + guard let toolNames else { return prefix + "*" } + return toolNames.map { prefix + $0 }.joined(separator: ",") + } + static func decodeEvent(_ line: String) -> DecodedEvent? { guard let json = decodeJSON(line), let type = json["type"] as? String else { return nil } switch type { diff --git a/apple/TablePro/Core/Coordinators/ExactRowCounter.swift b/apple/TablePro/Core/Coordinators/ExactRowCounter.swift index cae7d98f63..b262e649dc 100644 --- a/apple/TablePro/Core/Coordinators/ExactRowCounter.swift +++ b/apple/TablePro/Core/Coordinators/ExactRowCounter.swift @@ -42,12 +42,15 @@ internal enum ExactRowCounter { return try await driver.fetchExactRowCount(table: table, browseFilters: search.pluginQueryFilters) } + // `safeModeIsReadOnly` reaches the host's COUNT(*), which carries the user's raw filter. A driver's own count builds + // its query from the filters behind a call that takes no context. internal static func count( on driver: DatabaseDriver, table: String, filters: [TableFilter], logicMode: FilterLogicMode, - countSQL: String? + countSQL: String?, + safeModeIsReadOnly: Bool = false ) async throws -> Int? { let chosen = route( countSQL: countSQL, @@ -58,14 +61,14 @@ internal enum ExactRowCounter { case .driverCount: return try await driver.fetchExactRowCount(table: table, filters: filters, logicMode: logicMode) case .hostCountSQL(let sql): - return try await hostCount(sql, on: driver) + return try await hostCount(sql, on: driver, safeModeIsReadOnly: safeModeIsReadOnly) case .driverCountThenHostSQL(let sql): if let counted = try await driverCountAllowingFallback( on: driver, table: table, filters: filters, logicMode: logicMode ) { return counted } - return try await hostCount(sql, on: driver) + return try await hostCount(sql, on: driver, safeModeIsReadOnly: safeModeIsReadOnly) } } @@ -84,8 +87,13 @@ internal enum ExactRowCounter { } } - private static func hostCount(_ sql: String, on driver: DatabaseDriver) async throws -> Int? { - let result = try await driver.execute(query: sql) + private static func hostCount(_ sql: String, on driver: DatabaseDriver, safeModeIsReadOnly: Bool) async throws -> Int? { + let result: QueryResult + if let context = driver.readOnlyRunContext(safeModeIsReadOnly: safeModeIsReadOnly) { + result = try await driver.execute(query: sql, context: context) + } else { + result = try await driver.execute(query: sql) + } guard let countText = result.rows.first?.first?.asText else { return nil } return Int(countText) } diff --git a/apple/TablePro/Core/Coordinators/PaginationCoordinator.swift b/apple/TablePro/Core/Coordinators/PaginationCoordinator.swift index b0361ec6db..d510cdd1b8 100644 --- a/apple/TablePro/Core/Coordinators/PaginationCoordinator.swift +++ b/apple/TablePro/Core/Coordinators/PaginationCoordinator.swift @@ -172,6 +172,10 @@ final class PaginationCoordinator: ObservableObject { tableName: tableName, schemaName: schemaName, filters: filters, logicMode: logicMode, columns: queryColumns.columns, columnTypes: queryColumns.columnTypes ) + let safeModeIsReadOnly = parent.runsAtReadOnly + let gate = browseFilters == nil && parent.rawFilterNeedsReadOnlyGate(filters, tabId: tabId) + ? countSQL.map { parent.rawFilterRequest(sql: $0, canPrompt: true) } + : nil // Claims the superseded automatic count's flag too: its task finds the token changed and won't clear a successor's state. parent.tabManager.mutate(at: index) { tab in @@ -183,14 +187,27 @@ final class PaginationCoordinator: ObservableObject { let token = UUID() parent.claimExactCount(for: tabId, token: token) let task = Task(priority: .userInitiated) { [parent] in - let outcome = await Self.exactRowCount( - scope: scope, - tableName: tableName, - filters: filters, - logicMode: logicMode, - browseFilters: browseFilters, - countSQL: countSQL - ) + var refusal: ExecutionGateError? + if let gate { + refusal = await parent.executionGate.authorize(gate).denialError + if refusal == nil { parent.approveReadOnlyRawFilter(filters, tabId: tabId) } + } + let outcome: Result + if case .cancelledByUser? = refusal { + outcome = .success(nil) + } else if let refusal { + outcome = .failure(refusal) + } else { + outcome = await Self.exactRowCount( + scope: scope, + tableName: tableName, + filters: filters, + logicMode: logicMode, + browseFilters: browseFilters, + countSQL: countSQL, + safeModeIsReadOnly: safeModeIsReadOnly + ) + } // Clears on every exit: returning early on cancel left a page turn during COUNT(*) with an endless spinner and no Count Exactly. let isCurrent = !Task.isCancelled && parent.tabExecution.isSameContent(contentEpoch, for: tabId) @@ -233,7 +250,8 @@ final class PaginationCoordinator: ObservableObject { filters: [TableFilter], logicMode: FilterLogicMode, browseFilters: [PluginQueryFilter]?, - countSQL: String? + countSQL: String?, + safeModeIsReadOnly: Bool ) async -> Result { do { let count = try await DatabaseManager.shared.withRowCountDriver(scope: scope, filtered: !filters.isEmpty) { driver in @@ -241,7 +259,12 @@ final class PaginationCoordinator: ObservableObject { return try await driver.fetchExactRowCount(table: tableName, browseFilters: browseFilters) } return try await ExactRowCounter.count( - on: driver, table: tableName, filters: filters, logicMode: logicMode, countSQL: countSQL + on: driver, + table: tableName, + filters: filters, + logicMode: logicMode, + countSQL: countSQL, + safeModeIsReadOnly: safeModeIsReadOnly ) } return .success(count) @@ -310,6 +333,7 @@ final class PaginationCoordinator: ObservableObject { let isTableTab = parent.tabManager.tabs[idx].tabType == .table let lengthColumns = isTableTab ? parent.unloadedValueLengthColumns(for: parent.tabManager.tabs[idx]) : [] let failureOutput = ServerOutputBox() + let safeModeIsReadOnly = parent.runsAtReadOnly let startedAt = ContinuousClock.Instant.now // Both releases cover the whole task: a cancelled bare return left a finished fetch installed, which `installQueryTask` then ended. @@ -328,12 +352,15 @@ final class PaginationCoordinator: ObservableObject { isTableTab: isTableTab, lease: lease ) { driver in - let rerun = { - try await driver.executeUserQuery( - query: baseQuery, - rowCap: nil, - parameters: storedParamValues.map { $0.map { $0 as Any? } } - ) + let parameters = storedParamValues.map { $0.map { $0 as Any? } } + let context = driver.readOnlyRunContext(safeModeIsReadOnly: safeModeIsReadOnly) + let rerun: () async throws -> QueryResult = { + if let context { + return try await driver.executeUserQuery( + query: baseQuery, rowCap: nil, parameters: parameters, context: context + ) + } + return try await driver.executeUserQuery(query: baseQuery, rowCap: nil, parameters: parameters) } let result: QueryResult if isTableTab { diff --git a/apple/TablePro/Core/Coordinators/QueryExecutionCoordinator+Helpers.swift b/apple/TablePro/Core/Coordinators/QueryExecutionCoordinator+Helpers.swift index ead6c8e0e9..84846b4c8b 100644 --- a/apple/TablePro/Core/Coordinators/QueryExecutionCoordinator+Helpers.swift +++ b/apple/TablePro/Core/Coordinators/QueryExecutionCoordinator+Helpers.swift @@ -744,11 +744,30 @@ extension QueryExecutionCoordinator { columns: queryColumns.columns, columnTypes: queryColumns.columnTypes ) - return (plan, ExactCountInput(sql: sql, filters: filters, logicMode: logicMode), scope) + let input = ExactCountInput( + sql: sql, + filters: filters, + logicMode: logicMode, + safeModeIsReadOnly: parent.runsAtReadOnly, + gate: parent.rawFilterNeedsReadOnlyGate(filters, tabId: tabId) + ? sql.map { parent.rawFilterRequest(sql: $0, canPrompt: false) } + : nil + ) + return (plan, input, scope) + } + + // Nobody asked for this count, so nothing may ask the user about it: a refusal leaves the total unknown. + var plan = prepared.plan + if let count = prepared.count, let gate = count.gate { + if await parent.executionGate.authorize(gate).isAuthorized { + parent.approveReadOnlyRawFilter(count.filters, tabId: tabId) + } else { + plan = .clear + } } let outcome = await Self.rowCountOutcome( - plan: prepared.plan, + plan: plan, count: prepared.count, scope: prepared.scope, tableName: tableName @@ -815,7 +834,8 @@ extension QueryExecutionCoordinator { table: tableName, filters: exactCount.filters, logicMode: exactCount.logicMode, - countSQL: sql + countSQL: sql, + safeModeIsReadOnly: exactCount.safeModeIsReadOnly ) } return count.map { RowCountOutcome.count($0, isApproximate: false) } @@ -921,6 +941,9 @@ internal struct ExactCountInput: Sendable { internal let sql: String? internal let filters: [TableFilter] internal let logicMode: FilterLogicMode + internal var safeModeIsReadOnly = false + // What the gate is asked before a raw filter that calls a function runs at Read-Only. + internal var gate: OperationRequest? } enum RowCountPlan: Equatable { diff --git a/apple/TablePro/Core/Coordinators/QueryExecutionCoordinator+MultiStatement.swift b/apple/TablePro/Core/Coordinators/QueryExecutionCoordinator+MultiStatement.swift index 7006e2f157..ec32cef392 100644 --- a/apple/TablePro/Core/Coordinators/QueryExecutionCoordinator+MultiStatement.swift +++ b/apple/TablePro/Core/Coordinators/QueryExecutionCoordinator+MultiStatement.swift @@ -58,12 +58,19 @@ extension QueryExecutionCoordinator { } } + // A context is Read-Only's, and every call below carries it. func executeStatement( rowCap: Int?, originalSQL: String, driver: DatabaseDriver, - parameters: [Any?]? = nil + parameters: [Any?]? = nil, + context: PluginStatementContext? = nil ) async throws -> QueryResult { + if let context { + return try await executeStatement( + rowCap: rowCap, originalSQL: originalSQL, driver: driver, parameters: parameters, carrying: context + ) + } if rowCap != nil { if parameters == nil, let cap = rowCap, cap > 0, let bounded = try await driver.executeBoundedQuery(query: originalSQL, rowCap: cap) { @@ -77,6 +84,28 @@ extension QueryExecutionCoordinator { return try await driver.execute(query: originalSQL) } + private func executeStatement( + rowCap: Int?, + originalSQL: String, + driver: DatabaseDriver, + parameters: [Any?]?, + carrying context: PluginStatementContext + ) async throws -> QueryResult { + if rowCap != nil { + if parameters == nil, let cap = rowCap, cap > 0, + let bounded = try await driver.executeBoundedQuery(query: originalSQL, rowCap: cap, context: context) { + return bounded + } + return try await driver.executeUserQuery( + query: originalSQL, rowCap: rowCap, parameters: parameters, context: context + ) + } + if let parameters { + return try await driver.executeParameterized(query: originalSQL, parameters: parameters, context: context) + } + return try await driver.execute(query: originalSQL, context: context) + } + func makeStatementResultSet( result: QueryResult, sql: String, diff --git a/apple/TablePro/Core/Coordinators/QueryExecutionCoordinator+Parameters.swift b/apple/TablePro/Core/Coordinators/QueryExecutionCoordinator+Parameters.swift index 0520265783..cbf256db84 100644 --- a/apple/TablePro/Core/Coordinators/QueryExecutionCoordinator+Parameters.swift +++ b/apple/TablePro/Core/Coordinators/QueryExecutionCoordinator+Parameters.swift @@ -202,6 +202,7 @@ extension QueryExecutionCoordinator { let boundValues = BoundParameterValues(values: parameters) let failureOutput = ServerOutputBox() let grammar = parent.lexicalGrammar + let safeModeIsReadOnly = parent.runsAtReadOnly let parameterizedTask = Task { [weak self, parent] in guard let self else { return } @@ -222,6 +223,7 @@ extension QueryExecutionCoordinator { sql: statement.sql, parameters: boundValues.values, rowCap: rowCap, + safeModeIsReadOnly: safeModeIsReadOnly, capturingOutputInto: failureOutput ) let edits = await SucceededStatements.single( @@ -364,7 +366,7 @@ extension QueryExecutionCoordinator { let statementTexts = statements.map(\.sql) let transactionKind = OperationKind.worst(of: statementTexts, databaseType: conn.type) let grammar = parent.lexicalGrammar - let plan = BatchTransactionPolicy.plan(for: statementTexts, databaseType: conn.type, grammar: grammar) + let plan = BatchTransactionPolicy.plan(for: statementTexts, databaseType: conn.type, grammar: grammar, safeModeIsReadOnly: parent.runsAtReadOnly) let prepared = statements.map { statement in prepareStatement( statement: statement, @@ -539,12 +541,14 @@ extension QueryExecutionCoordinator { let failureOutput = ServerOutputBox() let commitOutput = ServerOutputBox() let databaseType = parent.connection.type + let safeModeIsReadOnly = parent.runsAtReadOnly do { var run = try await DatabaseManager.shared.withScopedDriver( scope: scope, route: DatabaseManager.shared.executionRoute(for: scope), cancellation: .cancellableRead(lease) ) { driver in + let context = driver.readOnlyRunContext(safeModeIsReadOnly: safeModeIsReadOnly) let startState = await driver.heldSessionTransactionState() let sessionPlan = plan.joining(startState) let outcome = await BatchStatementRun.run( @@ -571,7 +575,8 @@ extension QueryExecutionCoordinator { rowCap: statement.rowCap, originalSQL: statement.sentSQL, driver: driver, - parameters: statement.parameterValues + parameters: statement.parameterValues, + context: context ) } } diff --git a/apple/TablePro/Core/Database/Access/DatabaseAccessBridge+Scripts.swift b/apple/TablePro/Core/Database/Access/DatabaseAccessBridge+Scripts.swift index dc9e221372..0ef565a6b7 100644 --- a/apple/TablePro/Core/Database/Access/DatabaseAccessBridge+Scripts.swift +++ b/apple/TablePro/Core/Database/Access/DatabaseAccessBridge+Scripts.swift @@ -21,26 +21,36 @@ extension DatabaseAccessBridge { internal let executionTimeMs: Double + // What the answer spent, nil when it ran with none. + internal var budget: ExternalAnswerBudget? + internal var rowsReturned: Int { resultSets.reduce(0) { $0 + $1.rows.count } } - internal static func statement(_ result: QueryResult, executionTimeMs: Double) -> ScriptOutcome { - ScriptOutcome(resultSets: [result], primary: result, executionTimeMs: executionTimeMs) + internal static func statement( + _ result: QueryResult, + executionTimeMs: Double, + budget: ExternalAnswerBudget? = nil + ) -> ScriptOutcome { + ScriptOutcome(resultSets: [result], primary: result, executionTimeMs: executionTimeMs, budget: budget) } } - // An approved statement as a statement; an approved SQL Server script batch by batch at its GO lines. + // An approved statement as a statement; an approved SQL Server script batch by batch at its GO lines. One + // `budget` holds every result set; AppleScript and Shortcuts pass none. internal func runScript( _ operation: ApprovedOperation, maxRows: Int, timeoutSeconds: Int, - cancellation: (any StatementCancellationSignal)? + cancellation: (any StatementCancellationSignal)?, + budget: ExternalAnswerBudget? = nil ) async throws -> ScriptOutcome { let reach = DriverReach() do { return try await runApprovedScript( - operation, maxRows: maxRows, timeoutSeconds: timeoutSeconds, cancellation: cancellation, reach: reach + operation, maxRows: maxRows, timeoutSeconds: timeoutSeconds, cancellation: cancellation, budget: budget, + reach: reach ) } catch { throw await externalError(error, on: operation.scope.connectionId, reach: reach) @@ -52,6 +62,7 @@ extension DatabaseAccessBridge { maxRows: Int, timeoutSeconds: Int, cancellation: (any StatementCancellationSignal)?, + budget: ExternalAnswerBudget?, reach: DriverReach ) async throws -> ScriptOutcome { guard operation.unit == .script else { @@ -59,9 +70,10 @@ extension DatabaseAccessBridge { operation, maxRows: maxRows, timeoutSeconds: timeoutSeconds, - cancellation: cancellation + cancellation: cancellation, + budget: budget ) - return .statement(outcome.result, executionTimeMs: outcome.executionTimeMs) + return .statement(outcome.result, executionTimeMs: outcome.executionTimeMs, budget: outcome.budget) } guard operation.statements.count == 1, let script = operation.statements.first, script.parameters.isEmpty else { throw Self.notApproved } @@ -80,14 +92,34 @@ extension DatabaseAccessBridge { rowCap: maxRows, timeoutSeconds: timeoutSeconds, cancellation: cancellation, - context: operation.context, + context: operation.context(readingAtMost: budget?.remainingBytes), + budget: budget, reach: reach ) - let policy = await maskingPolicy(for: scope.connectionId) + guard var spent = outcome.budget else { + let policy = await maskingPolicy(for: scope.connectionId) + return ScriptOutcome( + resultSets: outcome.resultSets.map { $0.maskedForExternalCaller(by: policy) }, + primary: outcome.primary.maskedForExternalCaller(by: policy), + executionTimeMs: outcome.executionTimeMs + ) + } + // Masked and held to the budget batch by batch. The payload writes the first of several sets twice, so the sets + // are counted again with it doubled, their values as already cut. + guard outcome.resultSets.count > 1 else { return outcome } + var recount = ExternalAnswerBudget(bytes: spent.limitBytes, longValues: .whole) + var kept: [QueryResult] = [] + for (index, set) in outcome.resultSets.enumerated() { + kept.append(recount.keep(set, weight: index == 0 ? 2 : 1)) + } + if recount.sizeLimited { + spent.markSizeLimited() + } return ScriptOutcome( - resultSets: outcome.resultSets.map { $0.maskedForExternalCaller(by: policy) }, - primary: outcome.primary.maskedForExternalCaller(by: policy), - executionTimeMs: outcome.executionTimeMs + resultSets: kept, + primary: outcome.primary.keepingLeadingRows(kept[0].rows, truncated: kept[0].isTruncated), + executionTimeMs: outcome.executionTimeMs, + budget: spent ) } @@ -102,6 +134,7 @@ extension DatabaseAccessBridge { timeoutSeconds: Int, cancellation: (any StatementCancellationSignal)?, context: PluginStatementContext, + budget: ExternalAnswerBudget?, reach: DriverReach ) async throws -> ScriptOutcome { let classification = QueryClassifier.classify(text, databaseType: databaseType) @@ -122,6 +155,13 @@ extension DatabaseAccessBridge { databaseType: databaseType ) + // With a budget each answer is masked as it arrives, so it can be held to the budget before the next batch runs. + var masking = MaskingPolicy.none + if budget != nil { + masking = await maskingPolicy(for: connectionId) + } + let answerMasking = masking + let progress = ScriptBatchProgress() let run: ScriptBatchRun do { @@ -136,7 +176,8 @@ extension DatabaseAccessBridge { reach.mark() do { let answered = try await ScriptBatchRun.run( - batches, startLines: startLines, rowCap: rowCap, driver: driver, progress: progress, context: context + batches, startLines: startLines, rowCap: rowCap, driver: driver, progress: progress, context: context, + budget: budget, masking: answerMasking ) await manager.noteSessionStatements(Self.ranStatements(of: batches, progress), ranOn: driver, scope: scope) return answered @@ -234,6 +275,8 @@ final class ScriptBatchProgress: Sendable { struct ScriptBatchRun: Sendable { let answers: [QueryBatchResult] let sessionState: PluginSessionTransactionState + // With a budget, every answer was masked and held to it as it came in; this is what they spent. + var budget: ExternalAnswerBudget? /// The session is asked what it holds before and after, as the editor asks: the first answer decides how a /// failure reads, and the second whether the script left a transaction open. A batch that raised an error ends the @@ -244,15 +287,20 @@ struct ScriptBatchRun: Sendable { rowCap: Int, driver: DatabaseDriver, progress: ScriptBatchProgress, - context: PluginStatementContext? = nil + context: PluginStatementContext? = nil, + budget: ExternalAnswerBudget? = nil, + masking: MaskingPolicy = .none ) async throws -> ScriptBatchRun { let startState = await driver.heldSessionTransactionState() progress.begin(in: startState) let plan = BatchTransactionPlan.autocommit.joining(startState) var answers: [QueryBatchResult] = [] + var held = budget for (batch, startLine) in zip(batches, startLines) { try Task.checkCancellation() - let answer = try await repeatedAnswer(to: batch, rowCap: rowCap, driver: driver, context: context) + let answer = try await repeatedAnswer( + to: batch, rowCap: rowCap, driver: driver, context: context, held: &held, masking: masking + ) answers.append(answer) guard answer.errors.isEmpty else { let endState = await driver.heldSessionTransactionState() @@ -272,29 +320,61 @@ struct ScriptBatchRun: Sendable { } let endState = await driver.heldSessionTransactionState() progress.end(in: endState) - return ScriptBatchRun(answers: answers, sessionState: endState) + return ScriptBatchRun(answers: answers, sessionState: endState, budget: held) } /// `GO 5` sends the batch five times and keeps every answer. A repetition that raised an error ends the - /// repeating, as it ends the run. + /// repeating, as it ends the run. With a budget, each answer is cut down to it before the next is read, so rows + /// past it are not held between batches. Once it is spent a batch still runs, since it may write and what was + /// approved runs to its end, but it reads one row per result set and keeps none. private static func repeatedAnswer( to batch: ExecutableBatch, rowCap: Int, driver: DatabaseDriver, - context: PluginStatementContext? + context: PluginStatementContext?, + held: inout ExternalAnswerBudget?, + masking: MaskingPolicy ) async throws -> QueryBatchResult { var combined = QueryBatchResult.empty for _ in 0.. QueryBatchResult { + var sets: [QueryResult] = [] + sets.reserveCapacity(answer.resultSets.count) + for (index, set) in answer.resultSets.enumerated() { + sets.append(index < room ? budget.keep(set.maskedForExternalCaller(by: masking)) : set) + } + return QueryBatchResult( + resultSets: sets, + rowsAffected: answer.rowsAffected, + errors: answer.errors, + discardedResultSetCount: answer.discardedResultSetCount, + executionTime: answer.executionTime + ) + } + func outcome(executionTimeMs: Double) -> DatabaseAccessBridge.ScriptOutcome { let resultSets = answers.flatMap(\.resultSets) let first = resultSets.first @@ -319,7 +399,8 @@ struct ScriptBatchRun: Sendable { return DatabaseAccessBridge.ScriptOutcome( resultSets: resultSets, primary: primary, - executionTimeMs: executionTimeMs + executionTimeMs: executionTimeMs, + budget: budget ) } } diff --git a/apple/TablePro/Core/Database/Access/DatabaseAccessBridge.swift b/apple/TablePro/Core/Database/Access/DatabaseAccessBridge.swift index 58b00eb116..e848d577e3 100644 --- a/apple/TablePro/Core/Database/Access/DatabaseAccessBridge.swift +++ b/apple/TablePro/Core/Database/Access/DatabaseAccessBridge.swift @@ -340,21 +340,25 @@ internal actor DatabaseAccessBridge { internal struct StatementOutcome: Sendable { internal let result: QueryResult internal let executionTimeMs: Double + // What the answer spent, nil when it ran with none. + internal let budget: ExternalAnswerBudget? } - // Runs the one approved statement as it was approved, its rows masked as `masking` says. + // Runs the one approved statement as it was approved, its rows masked as `masking` says, then held to `budget`. + // AppleScript and Shortcuts pass none. internal func runStatement( _ operation: ApprovedOperation, maxRows: Int, timeoutSeconds: Int, cancellation: (any StatementCancellationSignal)?, - masking: ExternalRowMasking = .byOrigin + masking: ExternalRowMasking = .byOrigin, + budget: ExternalAnswerBudget? = nil ) async throws -> StatementOutcome { let reach = DriverReach() do { return try await runApprovedStatement( operation, maxRows: maxRows, timeoutSeconds: timeoutSeconds, cancellation: cancellation, masking: masking, - reach: reach + budget: budget, reach: reach ) } catch { throw await externalError(error, on: operation.scope.connectionId, reach: reach) @@ -367,6 +371,7 @@ internal actor DatabaseAccessBridge { timeoutSeconds: Int, cancellation: (any StatementCancellationSignal)?, masking: ExternalRowMasking, + budget: ExternalAnswerBudget?, reach: DriverReach ) async throws -> StatementOutcome { guard operation.unit == .statement, operation.statements.count == 1, let statement = operation.statements.first @@ -384,7 +389,7 @@ internal actor DatabaseAccessBridge { let ceiling = await MainActor.run { PluginManager.shared.paginationCapability(for: databaseType).maximumRows } rowCap = ceiling.map { min(maxRows, $0) } ?? maxRows } - let context = operation.context + let context = operation.context(readingAtMost: budget?.remainingBytes) let fetchCap = rowCap let connectionId = scope.connectionId // One owner per statement, so a cancel or a timeout reaches this statement's lease only. @@ -442,7 +447,17 @@ internal actor DatabaseAccessBridge { let executionTimeMs = (CFAbsoluteTimeGetCurrent() - startTime) * 1_000 let rules = await maskingPolicy(for: connectionId) let applied = await MainActor.run { masking.resolved(in: scope, databaseType: databaseType) } - return StatementOutcome(result: applied.apply(rules, to: result), executionTimeMs: executionTimeMs) + // Masked first, so a partial mask reads the whole value before any cut. + let masked = applied.apply(rules, to: result) + guard var spent = budget else { + return StatementOutcome(result: masked, executionTimeMs: executionTimeMs, budget: nil) + } + let kept = spent.keep(masked) + // Short of the row cap and truncated anyway: the driver stopped at the byte limit. + if context.resultByteLimit != nil, let fetchCap, masked.isTruncated, masked.rows.count < fetchCap { + spent.markSizeLimited() + } + return StatementOutcome(result: kept, executionTimeMs: executionTimeMs, budget: spent) } // A plan the driver builds for the approved statement, masked as `masking` says. @@ -459,7 +474,9 @@ internal actor DatabaseAccessBridge { operation, step: step, timeoutSeconds: timeoutSeconds, cancellation: cancellation, reach: reach ) let rules = await maskingPolicy(for: operation.scope.connectionId) - return StatementOutcome(result: masking.apply(rules, to: outcome.result), executionTimeMs: outcome.executionTimeMs) + return StatementOutcome( + result: masking.apply(rules, to: outcome.result), executionTimeMs: outcome.executionTimeMs, budget: nil + ) } catch { throw await externalError(error, on: operation.scope.connectionId, reach: reach) } @@ -524,7 +541,9 @@ internal actor DatabaseAccessBridge { if writes { CatalogChangeService.post(statementRan) } - return StatementOutcome(result: result, executionTimeMs: (CFAbsoluteTimeGetCurrent() - startTime) * 1_000) + return StatementOutcome( + result: result, executionTimeMs: (CFAbsoluteTimeGetCurrent() - startTime) * 1_000, budget: nil + ) } // One approved INSERT per row, on one write lease, each with the values it was approved with. @@ -822,6 +841,7 @@ internal actor DatabaseAccessBridge { _ work: @Sendable @escaping (_ enterDriver: @escaping @Sendable () throws -> Void) async throws -> Output ) async throws -> Output { let phase = OSAllocatedUnfairLock(initialState: ProtectedWritePhase.queued) + let callerLeft = OSAllocatedUnfairLock(initialState: false) let abandonIfQueued: @Sendable () -> Bool = { phase.withLock { current -> Bool in guard current == .queued else { return current == .abandoned } @@ -855,8 +875,9 @@ internal actor DatabaseAccessBridge { continuation.finish(throwing: error) } deadline.cancel() - let ranInDriver = phase.withLock { $0 == .inDriver } - if await deadline.value, ranInDriver { + guard phase.withLock({ $0 == .inDriver }) else { return } + let deadlinePassed = await deadline.value + if deadlinePassed || callerLeft.withLock({ $0 }) { onLateCompletion?() } } @@ -878,16 +899,31 @@ internal actor DatabaseAccessBridge { context: ["outcome": "unknown"] ) } + if phase.withLock({ $0 == .inDriver }) { + throw stoppedWhileRunning + } try Task.checkCancellation() throw DatabaseAccessError.dataSourceError("No result from query execution") } onCancel: { if abandonIfQueued() { workTask.cancel() + } else { + callerLeft.withLock { $0 = true } } deadline.cancel() } } + // Not "timed out": the caller stopped it, and a retry would apply it twice. + static var stoppedWhileRunning: DatabaseAccessError { + .stoppedWhileRunning(String( + localized: """ + The statement was still running in the database when it was stopped, and it runs to the end. \ + It may have been applied; check before running it again. + """ + )) + } + /// The text an external client's statement reaches the driver as. /// /// Trailing separators come off and a terminator that belongs to the statement stays, as the editor decides it: diff --git a/apple/TablePro/Core/Database/Access/DatabaseAccessError.swift b/apple/TablePro/Core/Database/Access/DatabaseAccessError.swift index b4c3fedfcd..f072fc1f6e 100644 --- a/apple/TablePro/Core/Database/Access/DatabaseAccessError.swift +++ b/apple/TablePro/Core/Database/Access/DatabaseAccessError.swift @@ -9,6 +9,8 @@ public enum DatabaseAccessError: Error, Sendable { case expired(String) case userCancelled case dataSourceError(String) + // The caller stopped waiting while a write it sent was in the driver, so the write may have been applied. + case stoppedWhileRunning(String) public var message: String { switch self { @@ -28,6 +30,8 @@ public enum DatabaseAccessError: Error, Sendable { "User cancelled" case .dataSourceError(let detail): "Data source error: \(detail)" + case .stoppedWhileRunning(let detail): + detail } } diff --git a/apple/TablePro/Core/Database/Access/ExternalAnswerBudget.swift b/apple/TablePro/Core/Database/Access/ExternalAnswerBudget.swift new file mode 100644 index 0000000000..e950945d17 --- /dev/null +++ b/apple/TablePro/Core/Database/Access/ExternalAnswerBudget.swift @@ -0,0 +1,210 @@ +// +// ExternalAnswerBudget.swift +// TablePro +// + +import Foundation +import TableProPluginKit + +// The bytes one answer to an MCP client or the assistant may hold, measured after masking. The limits, the +// measure and the mark are the core's (`core/crates/mcp/src/collected_results.rs`), so both apps answer alike. +internal struct ExternalAnswerBudget: Sendable, Equatable { + internal enum LongValues: Sendable, Equatable { + case cut + // An export is data to keep exactly, so a row that does not fit is left out instead. + case whole + } + + internal static let answerBytes = 4 << 20 + internal static let valueBytes = 64 << 10 + internal static let assistantAnswerBytes = 256 << 10 + + // A cell's quotes, colon and comma, and a row's brackets and comma. + private static let cellBytes = 6 + private static let rowBytes = 3 + private static let nullBytes = 4 + + internal let limitBytes: Int + internal let longValues: LongValues + internal private(set) var spentBytes = 0 + internal private(set) var valuesCut = false + internal private(set) var sizeLimited = false + + internal init(bytes: Int, longValues: LongValues = .cut) { + limitBytes = max(bytes, 0) + self.longValues = longValues + } + + internal static var mcpAnswer: ExternalAnswerBudget { + ExternalAnswerBudget(bytes: answerBytes) + } + + internal static var assistantAnswer: ExternalAnswerBudget { + ExternalAnswerBudget(bytes: assistantAnswerBytes) + } + + internal static var export: ExternalAnswerBudget { + ExternalAnswerBudget(bytes: answerBytes, longValues: .whole) + } + + internal var remainingBytes: Int { + max(limitBytes - spentBytes, 0) + } + + // The leading rows of `result` that fit, each long value cut when values are cut. Once one row does not fit, + // no later row of this or any later result is kept. `weight` counts a result the answer writes more than once. + internal mutating func keep(_ result: QueryResult, weight: Int = 1) -> QueryResult { + guard !result.rows.isEmpty else { return result } + let nameBytes = result.columns.map { $0.utf8.count + Self.cellBytes } + var kept: [[PluginCellValue]] = [] + kept.reserveCapacity(result.rows.count) + var changed = false + for row in result.rows { + guard !sizeLimited else { + changed = true + break + } + var cells = row + var cut = false + var size = Self.rowBytes + for index in cells.indices { + if longValues == .cut, let shortened = Self.cut(cells[index]) { + cells[index] = shortened + cut = true + } + size += (index < nameBytes.count ? nameBytes[index] : Self.cellBytes) + Self.encodedBytes(cells[index]) + } + let cost = size.multipliedReportingOverflow(by: max(weight, 1)) + guard !cost.overflow, cost.partialValue <= remainingBytes else { + sizeLimited = true + changed = true + break + } + spentBytes += cost.partialValue + valuesCut = valuesCut || cut + changed = changed || cut + kept.append(cells) + } + guard changed else { return result } + return result.keepingLeadingRows(kept, truncated: result.isTruncated || kept.count < result.rows.count) + } + + internal mutating func keep(text: String) -> String { + let size = text.utf8.count + guard size > remainingBytes else { + spentBytes += size + return text + } + sizeLimited = true + let kept = Self.cut(text, toBytes: remainingBytes) + spentBytes = limitBytes + return kept + } + + // A part its caller measured, kept only whole. Leaving it out does not stop the parts after it, which may fit. + internal mutating func keepWhole(bytes: Int) -> Bool { + guard !sizeLimited, bytes <= remainingBytes else { return false } + spentBytes += bytes + return true + } + + // A source that stopped reading at its own byte limit left rows out too. + internal mutating func markSizeLimited() { + sizeLimited = true + } + + internal var notes: [String] { + var notes: [String] = [] + if valuesCut { + notes.append(String(localized: """ + Some values were longer than 64 KB and were cut there; each one cut ends in a mark with its full \ + length. Read such a value in parts with a narrower query. + """)) + } + if sizeLimited { + let limit = ByteCountFormatter.string(fromByteCount: Int64(limitBytes), countStyle: .memory) + let text = longValues == .whole + ? String(localized: """ + The rows reached TablePro's %@ limit for one answer, so an export marked truncated holds only the \ + rows that fit, and the exports after it may hold none. Export fewer tables, columns or rows per call. + """) + : String(localized: """ + The rows reached TablePro's %@ limit for one answer, so a result marked truncated holds only the \ + rows that fit. Ask for fewer columns or rows. + """) + notes.append(String(format: text, limit)) + } + return notes + } + + internal static func cut(_ text: String) -> String { + cut(text, toBytes: valueBytes) + } + + private static func cut(_ text: String, toBytes limit: Int) -> String { + let utf8 = text.utf8 + let total = utf8.count + guard total > limit else { return text } + var end = utf8.index(utf8.startIndex, offsetBy: max(limit, 0)) + while end > utf8.startIndex, utf8[end] & 0xC0 == 0x80 { + end = utf8.index(before: end) + } + return String(String.UnicodeScalarView(text.unicodeScalars[.. PluginCellValue? { + switch cell { + case .null: + return nil + case .text(let text): + guard text.utf8.count > valueBytes else { return nil } + return .text(cut(text)) + case .bytes(let data): + guard base64Bytes(data.count) > valueBytes else { return nil } + return .text(data.prefix(valueBytes / 4 * 3).base64EncodedString() + mark(data.count)) + } + } + + private static func mark(_ total: Int) -> String { + " [TablePro cut this value: \(total) bytes in all]" + } + + // Measured without rendering, since rendering is what the limit is there to avoid. + private static func encodedBytes(_ cell: PluginCellValue) -> Int { + switch cell { + case .null: return nullBytes + case .text(let text): return text.utf8.count + case .bytes(let data): return base64Bytes(data.count) + } + } + + private static func base64Bytes(_ count: Int) -> Int { + 4 * ((count + 2) / 3) + } +} + +extension QueryResult { + // `rows` is a prefix of this result's rows, so what the driver keeps per row keeps its place. + func keepingLeadingRows(_ rows: [[PluginCellValue]], truncated: Bool) -> QueryResult { + var kept = QueryResult( + columns: columns, + columnTypes: columnTypes, + rows: rows, + rowsAffected: rowsAffected, + executionTime: executionTime, + error: error + ) + kept.timing = timing + kept.isTruncated = truncated + kept.statusMessage = statusMessage + kept.columnMeta = columnMeta + kept.columnOrigins = columnOrigins + kept.rowLocators = rowLocators.map { Array($0.prefix(rows.count)) } + kept.serverOutput = serverOutput + kept.exportDocuments = exportDocuments.map { Array($0.prefix(rows.count)) } + kept.absentCells = absentCells.filter { $0.key < rows.count } + kept.unloadedCells = unloadedCells.filter { $0.key < rows.count } + return kept + } +} diff --git a/apple/TablePro/Core/Database/DatabaseDriver.swift b/apple/TablePro/Core/Database/DatabaseDriver.swift index 5bae9e93a9..83a445b173 100644 --- a/apple/TablePro/Core/Database/DatabaseDriver.swift +++ b/apple/TablePro/Core/Database/DatabaseDriver.swift @@ -116,6 +116,10 @@ protocol DatabaseDriver: AnyObject, Sendable { // Whether this driver runs a statement marked read-only in the engine's read-only mode. var runsReadOnlyStatements: Bool { get } + // Whether this driver runs a statement whose context `enforcesReadOnly` in the engine's read-only mode, or refuses + // it, from every call that takes a context. + var enforcesReadOnlyMode: Bool { get } + /// Run a read that stops once `rowCap` rows are known to be exceeded, rather than fetching the /// whole result and discarding the tail. Returns nil when the driver cannot bound its own fetch. /// @@ -123,6 +127,10 @@ protocol DatabaseDriver: AnyObject, Sendable { /// rest of the fetch, which for some drivers cancels the statement on the server. func executeBoundedQuery(query: String, rowCap: Int) async throws -> QueryResult? + // Nil also when the driver cannot honor `context` on a bounded read: the caller then runs `executeUserQuery` with the + // same context and cap. + func executeBoundedQuery(query: String, rowCap: Int, context: PluginStatementContext) async throws -> QueryResult? + /// Runs a statement the app wrote to read, telling the driver it may only read. A driver that /// evaluates statements in a scripting engine with state refuses any write the script reaches, /// whatever an earlier statement redefined. @@ -550,6 +558,24 @@ extension DatabaseDriver { func executeBoundedQuery(query: String, rowCap: Int) async throws -> QueryResult? { nil } + // A driver without its own overload ignores every field, so a read-only mode, a statement policy or a catalog pin + // it was asked for would silently not happen. + func executeBoundedQuery(query: String, rowCap: Int, context: PluginStatementContext) async throws -> QueryResult? { + guard !context.enforcesReadOnly, !context.readOnly, !context.readsCatalog, + context.allowsDestructive, context.allowsServerCode + else { return nil } + return try await executeBoundedQuery(query: query, rowCap: rowCap) + } + + var enforcesReadOnlyMode: Bool { false } + + // What a run carries when Safe Mode is Read-Only and this driver can hold the engine to it. Nil otherwise, so the run + // keeps the calls it always made. + func readOnlyRunContext(safeModeIsReadOnly: Bool) -> PluginStatementContext? { + guard safeModeIsReadOnly, enforcesReadOnlyMode else { return nil } + return PluginStatementContext(enforcesReadOnly: true) + } + func executeRead(query: String) async throws -> QueryResult { try await execute(query: query) } diff --git a/apple/TablePro/Core/LSP/LSPTransport.swift b/apple/TablePro/Core/LSP/LSPTransport.swift index 6677bb3748..16942e5285 100644 --- a/apple/TablePro/Core/LSP/LSPTransport.swift +++ b/apple/TablePro/Core/LSP/LSPTransport.swift @@ -82,6 +82,7 @@ actor LSPTransport { private var stdoutPipe: Pipe? private var stderrPipe: Pipe? private var stderrReader: PipeReader? + private var stdoutReader: PipeReader? private var nextRequestID: Int = 1 private var pendingRequests: [Int: CheckedContinuation] = [:] private var notificationHandlers: [String: @Sendable (Data) -> Void] = [:] @@ -128,9 +129,14 @@ actor LSPTransport { try proc.run() - let handle = stdout.fileHandleForReading + // Not FileHandle.bytes: its reads share one serial queue, so this idle server would hold every + // other AsyncBytes reader in the app, the Claude Agent chat among them. + let (chunks, chunkSink) = AsyncStream.makeStream() + let stdoutReader = PipeReader(stdout.fileHandleForReading) + stdoutReader.start(delivering: { chunkSink.yield($0) }, onEnd: { chunkSink.finish() }) + self.stdoutReader = stdoutReader readerTask = Task { [weak self] in - await self?.runReadLoop(handle: handle) + await self?.runReadLoop(chunks) } Self.logger.info("LSP transport started: \(executablePath)") @@ -148,6 +154,8 @@ actor LSPTransport { writer?.close() writer = nil + stdoutReader?.stop() + stdoutReader = nil if let stdoutHandle = stdoutPipe?.fileHandleForReading { try? stdoutHandle.close() } @@ -270,25 +278,20 @@ actor LSPTransport { continuation.resume(throwing: error) } - private func runReadLoop(handle: FileHandle) async { + private func runReadLoop(_ chunks: AsyncStream) async { var buffer = Data() - do { - for try await byte in handle.bytes { - if Task.isCancelled { return } - buffer.append(byte) - while let (messageData, _) = Self.parseMessageFromBuffer(&buffer) { - dispatchMessage(messageData) - } - } - } catch { - if !Task.isCancelled { - Self.logger.debug("LSP read loop ended: \(error.localizedDescription)") + for await chunk in chunks { + if Task.isCancelled { return } + buffer.append(chunk) + while let (messageData, _) = Self.parseMessageFromBuffer(&buffer) { + dispatchMessage(messageData) } } + if !Task.isCancelled { + Self.logger.debug("LSP read loop ended at end of output") + } } - /// Parse a single LSP message from the buffer. - /// Returns (messageBody, totalBytesConsumed) or nil if buffer is incomplete. private static func parseMessageFromBuffer(_ buffer: inout Data) -> (Data, Int)? { let separator: [UInt8] = [0x0D, 0x0A, 0x0D, 0x0A] guard let separatorRange = buffer.range(of: Data(separator)) else { diff --git a/apple/TablePro/Core/MCP/Auth/MCPBearerTokenAuthenticator.swift b/apple/TablePro/Core/MCP/Auth/MCPBearerTokenAuthenticator.swift index c93f2427a7..c6a30934fb 100644 --- a/apple/TablePro/Core/MCP/Auth/MCPBearerTokenAuthenticator.swift +++ b/apple/TablePro/Core/MCP/Auth/MCPBearerTokenAuthenticator.swift @@ -13,6 +13,7 @@ public struct MCPValidatedToken: Sendable, Equatable { public let expiresAt: Date? public let isBridgeCredential: Bool public let isClaudeAgentCredential: Bool + public let toolAllowlist: Set? public init( tokenId: UUID, @@ -22,7 +23,8 @@ public struct MCPValidatedToken: Sendable, Equatable { issuedAt: Date, expiresAt: Date?, isBridgeCredential: Bool = false, - isClaudeAgentCredential: Bool = false + isClaudeAgentCredential: Bool = false, + toolAllowlist: Set? = nil ) { self.tokenId = tokenId self.label = label @@ -32,6 +34,7 @@ public struct MCPValidatedToken: Sendable, Equatable { self.isClaudeAgentCredential = isClaudeAgentCredential self.issuedAt = issuedAt self.expiresAt = expiresAt + self.toolAllowlist = toolAllowlist } } @@ -58,7 +61,8 @@ internal extension MCPTokenStore { issuedAt: authToken.createdAt, expiresAt: authToken.expiresAt, isBridgeCredential: authToken.isBridgeCredential, - isClaudeAgentCredential: authToken.isClaudeAgentCredential + isClaudeAgentCredential: authToken.isClaudeAgentCredential, + toolAllowlist: authToken.toolAllowlist ) } } @@ -133,7 +137,8 @@ public actor MCPBearerTokenAuthenticator: MCPAuthenticator { expiresAt: validated.expiresAt, isBridgeCredential: validated.isBridgeCredential, isClaudeAgentCredential: validated.isClaudeAgentCredential - ) + ), + toolAllowlist: validated.toolAllowlist ) MCPAuditLogger.logAuthSuccess( tokenId: validated.tokenId, diff --git a/apple/TablePro/Core/MCP/Legacy/Handlers/InitializeHandler.swift b/apple/TablePro/Core/MCP/Legacy/Handlers/InitializeHandler.swift index 8317f1f683..94757be96a 100644 --- a/apple/TablePro/Core/MCP/Legacy/Handlers/InitializeHandler.swift +++ b/apple/TablePro/Core/MCP/Legacy/Handlers/InitializeHandler.swift @@ -11,7 +11,7 @@ public struct InitializeHandler: MCPMethodHandler { public func handle(params: JsonValue?, context: MCPRequestContext) async throws -> MCPResult { let negotiated = try Self.negotiate(requestedVersion: params?["protocolVersion"]?.stringValue) - return Self.result(protocolVersion: negotiated) + return Self.result(protocolVersion: negotiated, scopes: context.principal.scopes) } public static func negotiate(requestedVersion: String?) throws -> MCPProtocolVersion { @@ -25,12 +25,25 @@ public struct InitializeHandler: MCPMethodHandler { return requested } - public static func result(protocolVersion: MCPProtocolVersion) -> MCPResult { + public static func result(protocolVersion: MCPProtocolVersion, scopes: Set) -> MCPResult { MCPResult.complete([ "protocolVersion": .string(protocolVersion.rawValue), - "capabilities": MCPMethodRegistry.capabilities().asJsonValue(era: .legacy), + "capabilities": capabilities(for: scopes).asJsonValue(era: .legacy), "serverInfo": MCPMethodRegistry.serverInfo.asJsonValue, "instructions": .string(MCPMethodRegistry.instructions) ]) } + + // A client told it has resources or prompts lists them at connect, and a 403 there can end the session. + static func capabilities(for scopes: Set) -> MCPServerCapabilities { + let all = MCPMethodRegistry.capabilities() + let readsResources = scopes.contains(.resourcesRead) + return MCPServerCapabilities( + tools: scopes.contains(.toolsRead) ? all.tools : nil, + resources: readsResources ? all.resources : nil, + prompts: readsResources ? all.prompts : nil, + completions: readsResources && all.completions, + extensions: all.extensions + ) + } } diff --git a/apple/TablePro/Core/MCP/Legacy/MCPLegacyEraAdapter.swift b/apple/TablePro/Core/MCP/Legacy/MCPLegacyEraAdapter.swift index 2ded825edf..9af96c8329 100644 --- a/apple/TablePro/Core/MCP/Legacy/MCPLegacyEraAdapter.swift +++ b/apple/TablePro/Core/MCP/Legacy/MCPLegacyEraAdapter.swift @@ -96,7 +96,7 @@ public actor MCPLegacyEraAdapter { Self.logger.info( "Legacy initialize: client=\(clientInfo?.name ?? "unknown", privacy: .private(mask: .hash)) version=\(negotiated.rawValue, privacy: .public)" ) - return (InitializeHandler.result(protocolVersion: negotiated), sessionId) + return (InitializeHandler.result(protocolVersion: negotiated, scopes: principal.scopes), sessionId) } public func terminate(sessionId: MCPLegacySessionId) async { diff --git a/apple/TablePro/Core/MCP/MCPAuthPolicy.swift b/apple/TablePro/Core/MCP/MCPAuthPolicy.swift index 8a8e15b6b9..13743bf717 100644 --- a/apple/TablePro/Core/MCP/MCPAuthPolicy.swift +++ b/apple/TablePro/Core/MCP/MCPAuthPolicy.swift @@ -143,10 +143,7 @@ public actor MCPAuthPolicy { ) } - /// An aggregate read spans connections, so it cannot prompt for one that asks each time. It - /// answers the narrower question every per-connection check already asks: may this principal - /// see this connection at all. Anything it excludes is a connection the client could not have - /// named directly either. + // May this principal see the connection at all; a call that names one is asked about it on top of this. func readableConnectionIds(principal: MCPPrincipal) async -> Set { let candidates = await connectionIdsProvider() return await readableConnectionIds(principal: principal, from: candidates) @@ -172,6 +169,16 @@ public actor MCPAuthPolicy { return true } + // A read spanning connections cannot ask about each one, so it keeps only those already allowed. + func connectionIdsReadableWithoutPrompt(principal: MCPPrincipal) async -> Set { + var allowed: Set = [] + for candidate in await readableConnectionIds(principal: principal) { + guard await isAllowedWithoutPrompt(principal: principal, connectionId: candidate) else { continue } + allowed.insert(candidate) + } + return allowed + } + func resolveAndAuthorize( principal: MCPPrincipal, tool: MCPToolName, diff --git a/apple/TablePro/Core/MCP/MCPConnectionBridge+Data.swift b/apple/TablePro/Core/MCP/MCPConnectionBridge+Data.swift index 6912d91cf7..b463c0ef6f 100644 --- a/apple/TablePro/Core/MCP/MCPConnectionBridge+Data.swift +++ b/apple/TablePro/Core/MCP/MCPConnectionBridge+Data.swift @@ -290,11 +290,13 @@ struct MCPExplainRequest: Equatable { } extension MCPConnectionBridge { + // The router reads the whole plan, so the budget holds only what goes out: the plan text, the parsed plan, the rows. func explainQuery( _ operation: ApprovedOperation, request: MCPExplainRequest, timeoutSeconds: Int, - cancellation: MCPCancellationToken? + cancellation: MCPCancellationToken?, + budget: ExternalAnswerBudget = .mcpAnswer ) async throws -> JsonValue { let databaseType = operation.databaseType let masking = ExternalRowMasking.plan(in: request.format) @@ -316,13 +318,15 @@ extension MCPConnectionBridge { declaredVariants: databaseType.explainVariants ) } else { - outcome = try await runStatement( + let statement = try await runStatement( operation, maxRows: Self.explainRowLimit, timeoutSeconds: timeoutSeconds, cancellation: cancellation, - masking: masking + masking: masking, + budget: nil ) + outcome = (statement.result, statement.executionTimeMs) routed = ExplainResultRouter.route( sql: request.statement, columns: outcome.result.columns, @@ -332,24 +336,56 @@ extension MCPConnectionBridge { ) } + var spent = budget + let planText = routed.map { spent.keep(text: $0.rawText) } + // The tree repeats every node's text, so it goes out only when it fits whole; plan_text already carries it. + let encodedPlan = routed?.plan.map(Self.encode(plan:)) + let plan = encodedPlan.flatMap { spent.keepWhole(bytes: Self.encodedBytes(of: $0)) ? $0 : nil } + let planLeftOut = encodedPlan != nil && plan == nil + let kept = spent.keep(outcome.result) var payload: [String: JsonValue] = [ "statement": .string(request.statement), "execution_time_ms": .double(outcome.executionTimeMs), "columns": .array(outcome.result.columns.map { .string($0) }), - "rows": .array(outcome.result.rows.map { row in .array(row.map(Self.cellValue)) }) + "rows": .array(kept.rows.map { row in .array(row.map(Self.cellValue)) }) ] if let statusMessage = outcome.result.statusMessage { payload["status_message"] = .string(statusMessage) } - if let routed { - payload["plan_text"] = .string(routed.rawText) - if let plan = routed.plan { - payload["plan"] = Self.encode(plan: plan) - } + if let planText { + payload["plan_text"] = .string(planText) + } + if let plan { + payload["plan"] = plan + } + if spent.sizeLimited || planLeftOut { + payload["is_truncated"] = .bool(true) + } + var notes = spent.notes + if planLeftOut { + notes.append(String( + format: String(localized: "The parsed plan did not fit in TablePro's %@ limit for one answer, so it is left out. Read plan_text instead."), + ByteCountFormatter.string(fromByteCount: Int64(spent.limitBytes), countStyle: .memory) + )) + } + if !notes.isEmpty { + payload["notes"] = .array(notes.map(JsonValue.string)) } return .object(payload) } + // What `value` encodes to, measured without rendering it; a number counts as the core counts a scalar. + static func encodedBytes(of value: JsonValue) -> Int { + switch value { + case .null: return 4 + case .bool: return 5 + case .int, .double: return 40 + case .string(let text): return text.utf8.count + 2 + case .array(let items): return items.reduce(2) { $0 + encodedBytes(of: $1) + 1 } + case .object(let fields): return fields.reduce(2) { $0 + $1.key.utf8.count + 3 + encodedBytes(of: $1.value) + 1 } + } + } + static let explainRowLimit = 5_000 static func explainStatement( diff --git a/apple/TablePro/Core/MCP/MCPConnectionBridge+Server.swift b/apple/TablePro/Core/MCP/MCPConnectionBridge+Server.swift index ed5436db9e..75d4538ac6 100644 --- a/apple/TablePro/Core/MCP/MCPConnectionBridge+Server.swift +++ b/apple/TablePro/Core/MCP/MCPConnectionBridge+Server.swift @@ -481,7 +481,35 @@ extension MCPConnectionBridge { ) let entries = page.entries.filter { Self.isSearchable($0, by: search, masksValues: masksValues) } - return .object(["history": .array(entries.map { Self.encode(historyEntry: $0, masksValues: masksValues) })]) + let answer = Self.withinAnswerBudget(entries.map { Self.encode(historyEntry: $0, masksValues: masksValues) }) + return .object([ + "history": .array(answer.entries), + "is_truncated": .bool(answer.isTruncated) + ]) + } + + // Entries keep their order; the first one that would pass the budget ends the answer. + static func withinAnswerBudget(_ entries: [JsonValue]) -> (entries: [JsonValue], isTruncated: Bool) { + var kept: [JsonValue] = [] + var usedBytes = 0 + for entry in entries { + usedBytes += entry.jsonString().utf8.count + guard usedBytes <= ExternalAnswerBudget.answerBytes else { + return (kept, true) + } + kept.append(entry) + } + return (kept, false) + } + + // The chat reaches only its own connection, so it lists only that one. + func listConnection(id connectionId: UUID) async -> JsonValue { + let listings = await MainActor.run { ExternalConnectionDirectory.listings() } + return Self.connectionListing(listings, id: connectionId) + } + + static func connectionListing(_ listings: [ExternalConnectionListing], id connectionId: UUID) -> JsonValue { + MCPConnectionListEncoder.encode(listings, access: .limited([connectionId]), purpose: .agent) } static func scopePath(_ scope: PluginPrivilegeScope) -> String { @@ -528,7 +556,9 @@ extension MCPConnectionBridge { // A statement the app wrote from the data itself carries stored values: a document edit's `replaceOne` holds every // field of the original and the edit, an import the file's values. What a person or a client typed is theirs. static func externalQueryText(of entry: QueryHistoryEntry, masksValues: Bool) -> String { - withholdsQuery(of: entry, masksValues: masksValues) ? ServerErrorText.withheldStatementText : entry.query + withholdsQuery(of: entry, masksValues: masksValues) + ? ServerErrorText.withheldStatementText + : ExternalAnswerBudget.cut(entry.query) } // The search matches the stored statement, so an entry whose statement is withheld would answer for its text. @@ -550,7 +580,7 @@ extension MCPConnectionBridge { // A failure the editor or a grid save recorded keeps the server's words, which can repeat a value it read. static func externalErrorText(_ error: String, masksValues: Bool) -> String { - masksValues ? ServerErrorText.withheldMessage : MCPErrorRedactor.redact(error) + masksValues ? ServerErrorText.withheldMessage : ExternalAnswerBudget.cut(MCPErrorRedactor.redact(error)) } } diff --git a/apple/TablePro/Core/MCP/MCPConnectionBridge.swift b/apple/TablePro/Core/MCP/MCPConnectionBridge.swift index 8a3f3dcbd2..9d10a04494 100644 --- a/apple/TablePro/Core/MCP/MCPConnectionBridge.swift +++ b/apple/TablePro/Core/MCP/MCPConnectionBridge.swift @@ -19,10 +19,6 @@ public actor MCPConnectionBridge { await listConnections(access: principal.connectionAccess, purpose: purpose) } - func listConnections() async -> JsonValue { - await listConnections(access: .all, purpose: .agent) - } - private func listConnections(access: ConnectionAccess, purpose: MCPConnectionListPurpose) async -> JsonValue { let listings = await MainActor.run { ExternalConnectionDirectory.listings() } return MCPConnectionListEncoder.encode(listings, access: access, purpose: purpose) @@ -125,19 +121,22 @@ public actor MCPConnectionBridge { maxRows: Int, timeoutSeconds: Int, cancellation: MCPCancellationToken?, - masking: ExternalRowMasking = .byOrigin + masking: ExternalRowMasking = .byOrigin, + budget: ExternalAnswerBudget = .mcpAnswer ) async throws -> JsonValue { let outcome = try await runStatement( operation, maxRows: maxRows, timeoutSeconds: timeoutSeconds, cancellation: cancellation, - masking: masking + masking: masking, + budget: budget ) return Self.encode( result: outcome.result, scope: operation.scope, - executionTimeMs: outcome.executionTimeMs + executionTimeMs: outcome.executionTimeMs, + notes: outcome.budget?.notes ?? [] ) } @@ -145,32 +144,36 @@ public actor MCPConnectionBridge { _ operation: ApprovedOperation, maxRows: Int, timeoutSeconds: Int, - cancellation: MCPCancellationToken? + cancellation: MCPCancellationToken?, + budget: ExternalAnswerBudget = .mcpAnswer ) async throws -> (payload: JsonValue, rowsReturned: Int) { let outcome = try await access.runScript( operation, maxRows: maxRows, timeoutSeconds: timeoutSeconds, - cancellation: cancellation + cancellation: cancellation, + budget: budget ) return (Self.encode(script: outcome, scope: operation.scope), outcome.rowsReturned) } + // A nil budget sends the rows whole, for a caller that holds them to a budget of its own. func runStatement( _ operation: ApprovedOperation, maxRows: Int, timeoutSeconds: Int, cancellation: MCPCancellationToken?, - masking: ExternalRowMasking = .byOrigin - ) async throws -> (result: QueryResult, executionTimeMs: Double) { - let outcome = try await access.runStatement( + masking: ExternalRowMasking = .byOrigin, + budget: ExternalAnswerBudget? = .mcpAnswer + ) async throws -> DatabaseAccessBridge.StatementOutcome { + try await access.runStatement( operation, maxRows: maxRows, timeoutSeconds: timeoutSeconds, cancellation: cancellation, - masking: masking + masking: masking, + budget: budget ) - return (outcome.result, outcome.executionTimeMs) } func runExplainPlan( @@ -245,14 +248,24 @@ public actor MCPConnectionBridge { return .object(["status": .string("dropped"), "schema": .string(name)]) } - static func encode(result: QueryResult, scope: DatabaseScope, executionTimeMs: Double) -> JsonValue { - .object(fields(of: result, scope: scope, executionTimeMs: executionTimeMs)) + static func encode( + result: QueryResult, + scope: DatabaseScope, + executionTimeMs: Double, + notes: [String] = [] + ) -> JsonValue { + .object(fields(of: result, scope: scope, executionTimeMs: executionTimeMs, notes: notes)) } /// A script's answer in the shape a statement's has, so a client that reads one result reads the first, plus /// `result_sets` with every one of them once there is more than one to read. static func encode(script: DatabaseAccessBridge.ScriptOutcome, scope: DatabaseScope) -> JsonValue { - var response = fields(of: script.primary, scope: scope, executionTimeMs: script.executionTimeMs) + var response = fields( + of: script.primary, + scope: scope, + executionTimeMs: script.executionTimeMs, + notes: script.budget?.notes ?? [] + ) if script.resultSets.count > 1 { response["result_sets"] = .array(script.resultSets.map(encode(resultSet:))) } @@ -272,7 +285,12 @@ public actor MCPConnectionBridge { return .object(entry) } - private static func fields(of result: QueryResult, scope: DatabaseScope, executionTimeMs: Double) -> [String: JsonValue] { + private static func fields( + of result: QueryResult, + scope: DatabaseScope, + executionTimeMs: Double, + notes: [String] + ) -> [String: JsonValue] { var response: [String: JsonValue] = [ "columns": .array(result.columns.map { .string($0) }), "rows": .array(result.rows.map { row in .array(row.map(cellValue)) }), @@ -288,6 +306,9 @@ public actor MCPConnectionBridge { if let statusMessage = result.statusMessage { response["status_message"] = .string(statusMessage) } + if !notes.isEmpty { + response["notes"] = .array(notes.map(JsonValue.string)) + } return response } diff --git a/apple/TablePro/Core/MCP/MCPTokenStore.swift b/apple/TablePro/Core/MCP/MCPTokenStore.swift index d75cd00d7f..2df8be7fcd 100644 --- a/apple/TablePro/Core/MCP/MCPTokenStore.swift +++ b/apple/TablePro/Core/MCP/MCPTokenStore.swift @@ -39,9 +39,17 @@ struct MCPAuthToken: Codable, Identifiable, Sendable { // Set only by the Claude Agent chat's own mint, on the same terms. let isClaudeAgentCredential: Bool let extraScopes: Set + // Set only by the Claude Agent chat's own mint; no pairing input reaches it. + let toolProfile: MCPToolProfile? var scopes: Set { - permissions.scopes.union(extraScopes) + let granted = permissions.scopes.union(extraScopes) + guard let toolProfile else { return granted } + return granted.intersection(toolProfile.scopes) + } + + var toolAllowlist: Set? { + toolProfile?.toolNames } var isExpired: Bool { @@ -74,7 +82,8 @@ struct MCPAuthToken: Codable, Identifiable, Sendable { isActive: Bool, isBridgeCredential: Bool = false, isClaudeAgentCredential: Bool = false, - extraScopes: Set = [] + extraScopes: Set = [], + toolProfile: MCPToolProfile? = nil ) { self.id = id self.name = name @@ -90,6 +99,7 @@ struct MCPAuthToken: Codable, Identifiable, Sendable { self.isBridgeCredential = isBridgeCredential self.isClaudeAgentCredential = isClaudeAgentCredential self.extraScopes = extraScopes.intersection(MCPScope.optionalGrants) + self.toolProfile = toolProfile } private enum CodingKeys: String, CodingKey { @@ -107,6 +117,7 @@ struct MCPAuthToken: Codable, Identifiable, Sendable { case isBridgeCredential case isClaudeAgentCredential case extraScopes + case toolProfile } init(from decoder: Decoder) throws { @@ -128,6 +139,12 @@ struct MCPAuthToken: Codable, Identifiable, Sendable { let storedExtraScopes = (try? container.decodeIfPresent([String].self, forKey: .extraScopes)) ?? [] self.extraScopes = Set(storedExtraScopes.compactMap(MCPScope.init(rawValue:))) .intersection(MCPScope.optionalGrants) + // Strict, unlike extraScopes: a profile only narrows, so a damaged value read as absent would widen the token. + if container.contains(.toolProfile) { + self.toolProfile = try container.decode(MCPToolProfile.self, forKey: .toolProfile) + } else { + self.toolProfile = nil + } } func encode(to encoder: Encoder) throws { @@ -150,6 +167,34 @@ struct MCPAuthToken: Codable, Identifiable, Sendable { if !extraScopes.isEmpty { try container.encode(extraScopes.map(\.rawValue).sorted(), forKey: .extraScopes) } + try container.encodeIfPresent(toolProfile, forKey: .toolProfile) + } +} + +enum MCPToolProfile: String, Codable, Sendable { + // The chat's Ask mode: the schema lookups, no rows, no history. + case schemaLookup + + var scopes: Set { + switch self { + case .schemaLookup: + [.toolsRead] + } + } + + var toolNames: Set { + switch self { + case .schemaLookup: + [ + ListConnectionsTool.name, + GetConnectionStatusTool.name, + ListDatabasesTool.name, + ListSchemasTool.name, + ListTablesTool.name, + DescribeTableTool.name, + GetTableDdlTool.name + ] + } } } @@ -220,7 +265,7 @@ actor MCPTokenStore { revocationObservers.removeValue(forKey: id) } - // Only the bridge's own mint may take its name: a pairing client could otherwise claim the bridge's grants. + // Pairing and Settings mint without a profile; only the Claude Agent mint passes one. func generate( name: String, permissions: TokenPermissions, @@ -229,6 +274,29 @@ actor MCPTokenStore { isBridgeCredential: Bool, isClaudeAgentCredential: Bool, extraScopes: Set + ) throws -> (token: MCPAuthToken, plaintext: String) { + try generate( + name: name, + permissions: permissions, + connectionAccess: connectionAccess, + expiresAt: expiresAt, + isBridgeCredential: isBridgeCredential, + isClaudeAgentCredential: isClaudeAgentCredential, + extraScopes: extraScopes, + toolProfile: nil + ) + } + + // Only the bridge's own mint may take its name: a pairing client could otherwise claim the bridge's grants. + func generate( + name: String, + permissions: TokenPermissions, + connectionAccess: ConnectionAccess, + expiresAt: Date?, + isBridgeCredential: Bool, + isClaudeAgentCredential: Bool, + extraScopes: Set, + toolProfile: MCPToolProfile? ) throws -> (token: MCPAuthToken, plaintext: String) { let name = (!isBridgeCredential && name == Self.stdioBridgeTokenName) ? name + " (client)" @@ -256,7 +324,8 @@ actor MCPTokenStore { isActive: true, isBridgeCredential: isBridgeCredential, isClaudeAgentCredential: isClaudeAgentCredential && !isBridgeCredential, - extraScopes: isBridgeCredential ? [] : extraScopes + extraScopes: isBridgeCredential ? [] : extraScopes, + toolProfile: toolProfile ) tokens.append(token) diff --git a/apple/TablePro/Core/MCP/Prompts/MCPPromptSchemaReader.swift b/apple/TablePro/Core/MCP/Prompts/MCPPromptSchemaReader.swift index 663a939832..90ab1da09b 100644 --- a/apple/TablePro/Core/MCP/Prompts/MCPPromptSchemaReader.swift +++ b/apple/TablePro/Core/MCP/Prompts/MCPPromptSchemaReader.swift @@ -316,7 +316,7 @@ struct MCPPromptSchemaReader: Sendable { return MCPProtocolError(code: JsonRpcErrorCode.expired, message: detail, httpStatus: .ok) case .timeout(let detail, _): return MCPProtocolError.requestTimeout(detail: detail) - case .userCancelled: + case .userCancelled, .stoppedWhileRunning: return MCPProtocolError.requestCancelled() case .dataSourceError(let detail): return MCPProtocolError.internalError(detail: detail) diff --git a/apple/TablePro/Core/MCP/Protocol/Handlers/ResourcesReadHandler.swift b/apple/TablePro/Core/MCP/Protocol/Handlers/ResourcesReadHandler.swift index 73ebc728f1..ba44066f75 100644 --- a/apple/TablePro/Core/MCP/Protocol/Handlers/ResourcesReadHandler.swift +++ b/apple/TablePro/Core/MCP/Protocol/Handlers/ResourcesReadHandler.swift @@ -130,7 +130,7 @@ public struct ResourcesReadHandler: MCPMethodHandler { return MCPProtocolError(code: JsonRpcErrorCode.expired, message: detail, httpStatus: .ok) case .timeout(let detail, _): return .requestTimeout(detail: detail) - case .userCancelled: + case .userCancelled, .stoppedWhileRunning: return .requestCancelled() case .dataSourceError(let detail): return .internalError(detail: detail) diff --git a/apple/TablePro/Core/MCP/Protocol/Handlers/ToolsCallHandler.swift b/apple/TablePro/Core/MCP/Protocol/Handlers/ToolsCallHandler.swift index df4650c1b0..a1e29c32ff 100644 --- a/apple/TablePro/Core/MCP/Protocol/Handlers/ToolsCallHandler.swift +++ b/apple/TablePro/Core/MCP/Protocol/Handlers/ToolsCallHandler.swift @@ -28,6 +28,18 @@ public struct ToolsCallHandler: MCPMethodHandler { } let connectionId = Self.connectionId(in: arguments) + // Before the scope check: no scope step-up opens a tool the token's profile leaves out. + guard context.principal.allowsTool(toolName) else { + Self.audit( + context: context, + tool: toolName, + connectionId: connectionId, + outcome: .denied, + error: "tool_not_in_profile" + ) + throw MCPProtocolError.forbidden(reason: "Tool '\(toolName)' is not available to this token") + } + let requiredScopes = type(of: tool).requiredScopes guard requiredScopes.isSubset(of: context.principal.scopes) else { Self.audit( diff --git a/apple/TablePro/Core/MCP/Protocol/Handlers/ToolsListHandler.swift b/apple/TablePro/Core/MCP/Protocol/Handlers/ToolsListHandler.swift index 68eae9bf53..96995a16b6 100644 --- a/apple/TablePro/Core/MCP/Protocol/Handlers/ToolsListHandler.swift +++ b/apple/TablePro/Core/MCP/Protocol/Handlers/ToolsListHandler.swift @@ -34,7 +34,10 @@ public struct ToolsListHandler: MCPMethodHandler { private static func visibleTools(for principal: MCPPrincipal) -> [any MCPToolImplementation] { MCPToolRegistry.allTools - .filter { type(of: $0).requiredScopes.isSubset(of: principal.scopes) } + .filter { tool in + let toolType = type(of: tool) + return toolType.requiredScopes.isSubset(of: principal.scopes) && principal.allowsTool(toolType.name) + } .sorted { type(of: $0).name < type(of: $1).name } } diff --git a/apple/TablePro/Core/MCP/Protocol/Tools/ExplainQueryTool.swift b/apple/TablePro/Core/MCP/Protocol/Tools/ExplainQueryTool.swift index 057a66292c..69d3515294 100644 --- a/apple/TablePro/Core/MCP/Protocol/Tools/ExplainQueryTool.swift +++ b/apple/TablePro/Core/MCP/Protocol/Tools/ExplainQueryTool.swift @@ -54,6 +54,13 @@ public struct ExplainQueryTool: MCPToolImplementation { ), "plan_text": MCPToolSchema.string(String(localized: "Flattened plan text")), "status_message": MCPToolSchema.string(String(localized: "Why values are masked, when they are")), + "is_truncated": MCPToolSchema.boolean( + String(localized: "Present and true when the answer's size limit cut plan_text or left the parsed plan or rows out") + ), + "notes": MCPToolSchema.array( + String(localized: "Why rows were left out or values cut, when they were"), + of: MCPToolSchema.stringItem + ), "plan": MCPToolSchema.object( properties: [ "root": MCPToolSchema.anyValue, @@ -170,20 +177,18 @@ public struct ExplainQueryTool: MCPToolImplementation { ) } - do { - var payload = try await services.connectionBridge.explainQuery( - operation, - request: request, - timeoutSeconds: timeoutSeconds, - cancellation: context.cancellation - ) - if case .object(var fields) = payload { - fields["available_variants"] = MCPConnectionBridge.explainVariants(for: meta.databaseType) - payload = .object(fields) - } - return .structured(payload) - } catch { - throw ToolQueryExecutor.translate(error, secrets: meta.redactionSecrets) + var payload = try await ToolQueryExecutor.explainAndLog( + services: services, + operation: operation, + request: request, + timeoutSeconds: timeoutSeconds, + context: context, + secrets: meta.redactionSecrets + ) + if case .object(var fields) = payload { + fields["available_variants"] = MCPConnectionBridge.explainVariants(for: meta.databaseType) + payload = .object(fields) } + return .structured(payload) } } diff --git a/apple/TablePro/Core/MCP/Protocol/Tools/ExportDataTool.swift b/apple/TablePro/Core/MCP/Protocol/Tools/ExportDataTool.swift index 14e1f492ab..5f7f4dec82 100644 --- a/apple/TablePro/Core/MCP/Protocol/Tools/ExportDataTool.swift +++ b/apple/TablePro/Core/MCP/Protocol/Tools/ExportDataTool.swift @@ -56,7 +56,13 @@ public struct ExportDataTool: MCPToolImplementation { properties: [ "format": MCPToolSchema.string(String(localized: "Format that was produced")), "rows_exported": MCPToolSchema.integer(String(localized: "Total rows written")), - "is_truncated": MCPToolSchema.boolean(String(localized: "Whether the row limit clipped any export")), + "is_truncated": MCPToolSchema.boolean( + String(localized: "Whether the row limit or the answer's size limit left rows out of any export") + ), + "notes": MCPToolSchema.array( + String(localized: "Why rows were left out, when the size limit left them out"), + of: MCPToolSchema.stringItem + ), "path": MCPToolSchema.string(String(localized: "File that was written, when output_path was given")), "exports": MCPToolSchema.array( String(localized: "One entry per query or table"), @@ -64,7 +70,9 @@ public struct ExportDataTool: MCPToolImplementation { properties: [ "label": MCPToolSchema.string(String(localized: "Table name, or 'query'")), "row_count": MCPToolSchema.integer(String(localized: "Rows in this export")), - "is_truncated": MCPToolSchema.boolean(String(localized: "Whether this export was clipped")), + "is_truncated": MCPToolSchema.boolean( + String(localized: "Whether the row limit or the answer's size limit left rows out") + ), "data": MCPToolSchema.string( String(localized: "Exported text. Omitted when the export was written to a file.") ) @@ -168,6 +176,8 @@ public struct ExportDataTool: MCPToolImplementation { var documents: [String] = [] var totalRows = 0 var anyTruncated = false + // Every table of the call shares one answer, so each gets what the ones before it left. + var budget = ExternalAnswerBudget.export for (statement, classification) in zip(statements, classifications) { let planned: OperationStatement @@ -191,15 +201,18 @@ public struct ExportDataTool: MCPToolImplementation { services: services ) - let result = try await ToolQueryExecutor.executeResultAndLog( + let read = try await ToolQueryExecutor.executeResultAndLog( services: services, operation: operation, maxRows: maxRows + 1, timeoutSeconds: timeoutSeconds, context: context, secrets: meta.redactionSecrets, - masking: statement.masking + masking: statement.masking, + budget: budget ) + let result = read.result + budget = read.budget let rowCount = min(result.rows.count, maxRows) let isTruncated = result.rows.count > maxRows || result.isTruncated @@ -233,6 +246,10 @@ public struct ExportDataTool: MCPToolImplementation { "is_truncated": .bool(anyTruncated), "exports": .array(exports) ] + let notes = budget.notes + if !notes.isEmpty { + payload["notes"] = .array(notes.map(JsonValue.string)) + } guard let destination else { return .structured(.object(payload)) diff --git a/apple/TablePro/Core/MCP/Protocol/Tools/FocusQueryTabTool.swift b/apple/TablePro/Core/MCP/Protocol/Tools/FocusQueryTabTool.swift index c964c2f0f8..11b534ac74 100644 --- a/apple/TablePro/Core/MCP/Protocol/Tools/FocusQueryTabTool.swift +++ b/apple/TablePro/Core/MCP/Protocol/Tools/FocusQueryTabTool.swift @@ -6,7 +6,10 @@ public struct FocusQueryTabTool: MCPToolImplementation { public static let name = "focus_query_tab" public static let title: String? = String(localized: "Focus Tab") public static let description = String( - localized: "Bring an open tab to the front, using a tab id from list_recent_tabs." + localized: """ + Bring an open tab to the front, using a tab id from list_recent_tabs. TablePro asks the user about \ + the tab's connection first, as it does for any call that names a connection. + """ ) public static let requiredScopes: Set = [.toolsWrite] public static let annotations = MCPToolAnnotations( @@ -32,7 +35,15 @@ public struct FocusQueryTabTool: MCPToolImplementation { required: ["status", "tab_id", "connection_id"] ) - public init() {} + private let collectSnapshots: MCPTabSnapshotCollector + + public init() { + self.init(collectSnapshots: MCPTabSnapshotProvider.liveSnapshots) + } + + init(collectSnapshots: @escaping MCPTabSnapshotCollector) { + self.collectSnapshots = collectSnapshots + } public func perform( arguments: JsonValue, @@ -44,7 +55,8 @@ public struct FocusQueryTabTool: MCPToolImplementation { let snapshots = await MCPTabSnapshotProvider.readableSnapshots( context: context, - services: services + services: services, + collect: collectSnapshots ) guard let snapshot = snapshots.first(where: { $0.tabId == tabId }) else { throw MCPToolExecutionError.notFound( @@ -52,6 +64,13 @@ public struct FocusQueryTabTool: MCPToolImplementation { ) } + // The call names a tab, not a connection, so ToolsCallHandler had nothing to ask about. + try await services.authPolicy.resolveAndAuthorize( + principal: context.principal, + tool: Self.name, + connectionId: snapshot.connectionId + ) + let raised = await MainActor.run { () -> Bool in guard Self.focus(tabId: tabId, among: WindowManager.shared.coordinators(for: snapshot.connectionId)) else { return false diff --git a/apple/TablePro/Core/MCP/Protocol/Tools/ListRecentTabsTool.swift b/apple/TablePro/Core/MCP/Protocol/Tools/ListRecentTabsTool.swift index 7125a08ed9..5ed6de9d97 100644 --- a/apple/TablePro/Core/MCP/Protocol/Tools/ListRecentTabsTool.swift +++ b/apple/TablePro/Core/MCP/Protocol/Tools/ListRecentTabsTool.swift @@ -6,8 +6,9 @@ public struct ListRecentTabsTool: MCPToolImplementation { public static let title: String? = String(localized: "List Open Tabs") public static let description = String( localized: """ - List the tabs open in TablePro for connections this client may reach. Each entry carries the tab \ - id that focus_query_tab takes and the window id that open_table_tab returns. + List the tabs open in TablePro. With connection_id, TablePro asks the user about that connection first. \ + Without it, the list covers only connections this client may already reach without asking. Each entry \ + carries the tab id that focus_query_tab takes and the window id that open_table_tab returns. """ ) public static let requiredScopes: Set = [.toolsRead] @@ -22,7 +23,7 @@ public struct ListRecentTabsTool: MCPToolImplementation { public static let inputSchema = MCPToolSchema.object( properties: [ "connection_id": MCPToolSchema.string( - String(localized: "Restrict to one connection. Omit for every readable connection.") + String(localized: "Restrict to one connection. Omit for every connection already approved.") ), "limit": MCPToolSchema.integer( String(localized: "Maximum tabs to return (1-500, default 20)"), @@ -42,7 +43,15 @@ public struct ListRecentTabsTool: MCPToolImplementation { required: ["tabs"] ) - public init() {} + private let collectSnapshots: MCPTabSnapshotCollector + + public init() { + self.init(collectSnapshots: MCPTabSnapshotProvider.liveSnapshots) + } + + init(collectSnapshots: @escaping MCPTabSnapshotCollector) { + self.collectSnapshots = collectSnapshots + } public func perform( arguments: JsonValue, @@ -53,17 +62,25 @@ public struct ListRecentTabsTool: MCPToolImplementation { let connectionId = try MCPArgumentDecoder.optionalUuid(arguments, key: "connection_id") let limit = try MCPArgumentDecoder.optionalInt(arguments, key: "limit", range: 1...500) ?? 20 - var snapshots = await MCPTabSnapshotProvider.readableSnapshots( - context: context, - services: services - ) + let snapshots: [MCPTabSnapshot] if let connectionId { + // ToolsCallHandler already asked about the named connection. try await MCPToolAuthorization.requireReadAccess( connectionId, context: context, services: services ) - snapshots = snapshots.filter { $0.connectionId == connectionId } + snapshots = await MCPTabSnapshotProvider.readableSnapshots( + context: context, + services: services, + collect: collectSnapshots + ).filter { $0.connectionId == connectionId } + } else { + snapshots = await MCPTabSnapshotProvider.snapshotsReadableWithoutPrompt( + context: context, + services: services, + collect: collectSnapshots + ) } let payload = snapshots.prefix(limit).map(MCPTabSnapshotProvider.encode) diff --git a/apple/TablePro/Core/MCP/Protocol/Tools/MCPTabSnapshotProvider.swift b/apple/TablePro/Core/MCP/Protocol/Tools/MCPTabSnapshotProvider.swift index 4415afdd91..381395f52b 100644 --- a/apple/TablePro/Core/MCP/Protocol/Tools/MCPTabSnapshotProvider.swift +++ b/apple/TablePro/Core/MCP/Protocol/Tools/MCPTabSnapshotProvider.swift @@ -16,6 +16,8 @@ struct MCPTabSnapshot { weak var window: NSWindow? } +typealias MCPTabSnapshotCollector = @MainActor @Sendable () -> [MCPTabSnapshot] + enum MCPTabSnapshotProvider { static let openPollInterval: Duration = .milliseconds(50) static let openTimeout: Duration = .seconds(8) @@ -58,13 +60,31 @@ enum MCPTabSnapshotProvider { } } + static let liveSnapshots: MCPTabSnapshotCollector = { MCPTabSnapshotProvider.collectTabSnapshots() } + + // Tabs of every connection the token may see; the caller asks about one before acting on it. static func readableSnapshots( context: MCPRequestContext, - services: MCPToolServices + services: MCPToolServices, + collect: MCPTabSnapshotCollector = MCPTabSnapshotProvider.liveSnapshots ) async -> [MCPTabSnapshot] { let readable = await MCPToolAuthorization.readableConnectionIds(context: context, services: services) - let snapshots = await MainActor.run { collectTabSnapshots() } - return snapshots.filter { readable.contains($0.connectionId) } + let collected = await collect() + return snapshots(collected, in: readable) + } + + static func snapshotsReadableWithoutPrompt( + context: MCPRequestContext, + services: MCPToolServices, + collect: MCPTabSnapshotCollector = MCPTabSnapshotProvider.liveSnapshots + ) async -> [MCPTabSnapshot] { + let allowed = await services.authPolicy.connectionIdsReadableWithoutPrompt(principal: context.principal) + let collected = await collect() + return snapshots(collected, in: allowed) + } + + static func snapshots(_ snapshots: [MCPTabSnapshot], in connectionIds: Set) -> [MCPTabSnapshot] { + snapshots.filter { connectionIds.contains($0.connectionId) } } /// A database or schema the caller left out means the one being browsed, which is where the diff --git a/apple/TablePro/Core/MCP/Protocol/Tools/MCPToolExecutionError.swift b/apple/TablePro/Core/MCP/Protocol/Tools/MCPToolExecutionError.swift index 30fdfebc5d..8b63947854 100644 --- a/apple/TablePro/Core/MCP/Protocol/Tools/MCPToolExecutionError.swift +++ b/apple/TablePro/Core/MCP/Protocol/Tools/MCPToolExecutionError.swift @@ -12,7 +12,8 @@ public enum MCPToolErrorCode: String, Sendable, Equatable { case internalFailure = "internal_failure" } -public struct MCPToolExecutionError: Error, Sendable, Equatable { +// LocalizedError: the assistant's transcript shows `localizedDescription`, which was Foundation's generic text. +public struct MCPToolExecutionError: LocalizedError, Sendable, Equatable { public let code: MCPToolErrorCode public let message: String @@ -21,6 +22,10 @@ public struct MCPToolExecutionError: Error, Sendable, Equatable { self.message = message } + public var errorDescription: String? { + "\(code.rawValue): \(message)" + } + public var asToolResult: MCPToolCallResult { MCPToolCallResult(content: [.text("\(code.rawValue): \(message)")], isError: true) } @@ -73,6 +78,9 @@ public struct MCPToolExecutionError: Error, Sendable, Equatable { ) case .dataSourceError(let detail): return MCPToolExecutionError(code: .queryFailed, message: MCPErrorRedactor.redact(detail, secrets: secrets)) + case .stoppedWhileRunning(let detail): + // The code a write past its deadline gets, the other answer that means "may have been applied". + return MCPToolExecutionError(code: .timeout, message: detail) } } } diff --git a/apple/TablePro/Core/MCP/Protocol/Tools/MCPToolImplementation.swift b/apple/TablePro/Core/MCP/Protocol/Tools/MCPToolImplementation.swift index 6c409723c5..e282074c06 100644 --- a/apple/TablePro/Core/MCP/Protocol/Tools/MCPToolImplementation.swift +++ b/apple/TablePro/Core/MCP/Protocol/Tools/MCPToolImplementation.swift @@ -50,6 +50,16 @@ public extension MCPToolImplementation { } catch let error as MCPToolExecutionError { return error.asToolResult } catch let error as DatabaseAccessError { + // A cancelled request keeps its documented -33002, and says the write it sent may have been applied. + if case .stoppedWhileRunning = error, Task.isCancelled { + let cancelled = MCPProtocolError.requestCancelled() + throw MCPProtocolError( + code: cancelled.code, + message: cancelled.message, + httpStatus: cancelled.httpStatus, + data: .object(["outcome": .string("unknown")]) + ) + } return MCPToolExecutionError.from(error).asToolResult } catch is CancellationError { throw MCPProtocolError.requestCancelled() diff --git a/apple/TablePro/Core/MCP/Protocol/Tools/SearchQueryHistoryTool.swift b/apple/TablePro/Core/MCP/Protocol/Tools/SearchQueryHistoryTool.swift index 46bac19014..7d8903f65d 100644 --- a/apple/TablePro/Core/MCP/Protocol/Tools/SearchQueryHistoryTool.swift +++ b/apple/TablePro/Core/MCP/Protocol/Tools/SearchQueryHistoryTool.swift @@ -6,8 +6,9 @@ public struct SearchQueryHistoryTool: MCPToolImplementation { public static let title: String? = String(localized: "Search Query History") public static let description = String( localized: """ - Search the query history TablePro keeps on this Mac. Results cover only connections this client \ - may reach, whether or not connection_id is given. + Search the query history TablePro keeps on this Mac. With connection_id, TablePro asks the user about \ + that connection first. Without it, results cover only connections this client may already reach \ + without asking. """ ) public static let requiredScopes: Set = [.toolsRead] @@ -22,7 +23,9 @@ public struct SearchQueryHistoryTool: MCPToolImplementation { public static let inputSchema = MCPToolSchema.object( properties: [ "query": MCPToolSchema.string(String(localized: "Text matched against the recorded statement")), - "connection_id": MCPToolSchema.string(String(localized: "Restrict to one connection")), + "connection_id": MCPToolSchema.string( + String(localized: "Restrict to one connection. Omit for every connection already approved.") + ), "limit": MCPToolSchema.integer( String(localized: "Maximum entries to return (1-500, default 50)"), minimum: 1, @@ -60,9 +63,12 @@ public struct SearchQueryHistoryTool: MCPToolImplementation { "row_count", "was_successful" ] ) + ), + "is_truncated": MCPToolSchema.boolean( + String(localized: "Whether the size limit left out matching entries") ) ], - required: ["entries"] + required: ["entries", "is_truncated"] ) public init() {} @@ -90,21 +96,16 @@ public struct SearchQueryHistoryTool: MCPToolImplementation { ) } + let allowlist: Set if let connectionId { - try await services.authPolicy.resolveAndAuthorize( - principal: context.principal, - tool: Self.name, - connectionId: connectionId, - sql: nil - ) - } - - var allowlist = await services.authPolicy.readableConnectionIds(principal: context.principal) - if let connectionId { - allowlist = allowlist.intersection([connectionId]) + // ToolsCallHandler already asked about this connection; Ask Every Time records nothing, so asking here asked twice. + try await MCPToolAuthorization.requireReadAccess(connectionId, context: context, services: services) + allowlist = [connectionId] + } else { + allowlist = await services.authPolicy.connectionIdsReadableWithoutPrompt(principal: context.principal) } guard !allowlist.isEmpty else { - return .structured(.object(["entries": .array([])])) + return .structured(.object(["entries": .array([]), "is_truncated": .bool(false)])) } var anyMasksValues = false @@ -161,6 +162,10 @@ public struct SearchQueryHistoryTool: MCPToolImplementation { return .object(fields) } - return .structured(.object(["entries": .array(payload)])) + let answer = MCPConnectionBridge.withinAnswerBudget(payload) + return .structured(.object([ + "entries": .array(answer.entries), + "is_truncated": .bool(answer.isTruncated) + ])) } } diff --git a/apple/TablePro/Core/MCP/Protocol/Tools/ToolQueryExecutor.swift b/apple/TablePro/Core/MCP/Protocol/Tools/ToolQueryExecutor.swift index 81d490792e..a97727d5d8 100644 --- a/apple/TablePro/Core/MCP/Protocol/Tools/ToolQueryExecutor.swift +++ b/apple/TablePro/Core/MCP/Protocol/Tools/ToolQueryExecutor.swift @@ -21,7 +21,8 @@ enum ToolQueryExecutor { cancellation: context.cancellation, secrets: secrets, source: historySource(for: context), - masking: masking + masking: masking, + budget: .mcpAnswer ) } @@ -39,7 +40,8 @@ enum ToolQueryExecutor { cancellation: MCPCancellationToken? = nil, secrets: [String] = [], source: QueryHistorySource = .mcp, - masking: ExternalRowMasking = .byOrigin + masking: ExternalRowMasking = .byOrigin, + budget: ExternalAnswerBudget = .mcpAnswer ) async throws -> JsonValue { try await logged( services: services, @@ -54,11 +56,13 @@ enum ToolQueryExecutor { maxRows: maxRows, timeoutSeconds: timeoutSeconds, cancellation: cancellation, - masking: masking + masking: masking, + budget: budget ) } } + // The rows held to `budget`, and what is left of it for the next table of the same export. static func executeResultAndLog( services: MCPToolServices, operation: ApprovedOperation, @@ -66,8 +70,9 @@ enum ToolQueryExecutor { timeoutSeconds: Int, context: MCPRequestContext, secrets: [String], - masking: ExternalRowMasking = .byOrigin - ) async throws -> QueryResult { + masking: ExternalRowMasking = .byOrigin, + budget: ExternalAnswerBudget + ) async throws -> (result: QueryResult, budget: ExternalAnswerBudget) { try await context.cancellation.throwIfCancelled() return try await logged( services: services, @@ -81,9 +86,43 @@ enum ToolQueryExecutor { maxRows: maxRows, timeoutSeconds: timeoutSeconds, cancellation: context.cancellation, - masking: masking + masking: masking, + budget: budget ) - return (outcome.result, outcome.result.rows.count) + return ((result: outcome.result, budget: outcome.budget ?? budget), outcome.result.rows.count) + } + } + + // An analyzed write ran, so it is recorded the way execute_query records one; a plan of a read is not. + static func explainAndLog( + services: MCPToolServices, + operation: ApprovedOperation, + request: MCPExplainRequest, + timeoutSeconds: Int, + context: MCPRequestContext, + secrets: [String] + ) async throws -> JsonValue { + guard operation.kind.declaresWrite else { + do { + return try await services.connectionBridge.explainQuery( + operation, request: request, timeoutSeconds: timeoutSeconds, cancellation: context.cancellation + ) + } catch { + throw translate(error, secrets: secrets) + } + } + try await context.cancellation.throwIfCancelled() + return try await logged( + services: services, + approved: operation, + principal: context.principal, + secrets: secrets, + source: historySource(for: context) + ) { + let payload = try await services.connectionBridge.explainQuery( + operation, request: request, timeoutSeconds: timeoutSeconds, cancellation: context.cancellation + ) + return (payload, payload["rows"]?.arrayValue?.count ?? 0) } } @@ -170,7 +209,8 @@ enum ToolQueryExecutor { maxRows: Int, timeoutSeconds: Int, cancellation: MCPCancellationToken?, - masking: ExternalRowMasking + masking: ExternalRowMasking, + budget: ExternalAnswerBudget ) async throws -> (payload: JsonValue, rowsReturned: Int) { switch operation.unit { case .statement: @@ -179,7 +219,8 @@ enum ToolQueryExecutor { maxRows: maxRows, timeoutSeconds: timeoutSeconds, cancellation: cancellation, - masking: masking + masking: masking, + budget: budget ) return (payload, payload["row_count"]?.intValue ?? 0) case .script: @@ -187,13 +228,16 @@ enum ToolQueryExecutor { operation, maxRows: maxRows, timeoutSeconds: timeoutSeconds, - cancellation: cancellation + cancellation: cancellation, + budget: budget ) } } static func translate(_ error: Error, secrets: [String]) -> Error { if let dataError = error as? DatabaseAccessError { + // Kept as it is, so an MCP call answers it as the cancel it was and the assistant shows its own words. + if case .stoppedWhileRunning = dataError { return dataError } return MCPToolExecutionError.from(dataError, secrets: secrets) } if let toolError = error as? MCPToolExecutionError { diff --git a/apple/TablePro/Core/Plugins/ExportDataSourceAdapter.swift b/apple/TablePro/Core/Plugins/ExportDataSourceAdapter.swift index 5878dcda37..bfb2c5a5a1 100644 --- a/apple/TablePro/Core/Plugins/ExportDataSourceAdapter.swift +++ b/apple/TablePro/Core/Plugins/ExportDataSourceAdapter.swift @@ -27,8 +27,11 @@ final class ExportDataSourceAdapter: PluginExportDataSource, @unchecked Sendable private let appliesRowScope: Bool private let commentableKinds: Set private let cappedTables = OSAllocatedUnfairLock<[(table: String, limit: Int)]>(initialState: []) + // The caller reads the connection's level on the main actor, where the gate reads it. + private let safeModeIsReadOnly: Bool - init(driver: DatabaseDriver, databaseType: DatabaseType) { + init(driver: DatabaseDriver, databaseType: DatabaseType, safeModeIsReadOnly: Bool = false) { + self.safeModeIsReadOnly = safeModeIsReadOnly let snapshot = PluginMetadataRegistry.shared.snapshot(for: databaseType) self.supportsCascadeDrop = snapshot?.capabilities.supportsCascadeDrop ?? false self.lexicalFeatures = SQLLexicalResolver.executionFeatures(for: databaseType, session: driver.sessionLexicalState) @@ -80,6 +83,7 @@ final class ExportDataSourceAdapter: PluginExportDataSource, @unchecked Sendable // A table export only reads, whatever the statement the driver chose classifies as. var context = PluginStatementContext() context.readOnly = true + context.enforcesReadOnly = driver.readOnlyRunContext(safeModeIsReadOnly: safeModeIsReadOnly) != nil // The driver's own export query is not the app's capped SELECT, so only a limit it declares applies. return reportingCut( of: pluginDriver.streamExportRows(query: customQuery, context: context), @@ -129,7 +133,8 @@ final class ExportDataSourceAdapter: PluginExportDataSource, @unchecked Sendable guard let adapter = driver as? PluginDriverAdapter else { return AsyncThrowingStream { $0.finish(throwing: PluginExportError.exportFailed("No plugin driver available")) } } - return reportingCut(of: adapter.streamExportRows(query: query), table: table, engineCeiling: pagination.maximumRows) + let rows = adapter.streamExportRows(query: query, safeModeIsReadOnly: safeModeIsReadOnly) + return reportingCut(of: rows, table: table, engineCeiling: pagination.maximumRows) } private func reportingCut( @@ -178,11 +183,21 @@ final class ExportDataSourceAdapter: PluginExportDataSource, @unchecked Sendable return try await pluginDriver.fetchTableDDL(table: table, schema: exportSchema(for: databaseName)) } + // At Read-Only each read runs in a read-only transaction of its own, which an open one refuses, so a format's + // snapshot (`BEGIN`, `START TRANSACTION`, `COMMIT`) is not opened and the export reads table by table. func execute(query: String) async throws -> PluginQueryResult { - let result = try await driver.execute(query: query) - return mapToPluginResult(result) + guard let context = driver.readOnlyRunContext(safeModeIsReadOnly: safeModeIsReadOnly) else { + return mapToPluginResult(try await driver.execute(query: query)) + } + let keyword = QueryClassifier.leadingKeyword(of: query, grammar: dbType.lexicalGrammar) + guard !Self.transactionKeywords.contains(keyword) else { + return PluginQueryResult(columns: [], columnTypeNames: [], rows: [], rowsAffected: 0, executionTime: 0) + } + return mapToPluginResult(try await driver.execute(query: query, context: context)) } + private static let transactionKeywords: Set = ["BEGIN", "START", "COMMIT", "END", "ROLLBACK"] + func quoteIdentifier(_ identifier: String) -> String { driver.quoteIdentifier(identifier) } diff --git a/apple/TablePro/Core/Plugins/PluginDriverAdapter+Export.swift b/apple/TablePro/Core/Plugins/PluginDriverAdapter+Export.swift index 4e4313d3dd..bbab92efda 100644 --- a/apple/TablePro/Core/Plugins/PluginDriverAdapter+Export.swift +++ b/apple/TablePro/Core/Plugins/PluginDriverAdapter+Export.swift @@ -2,17 +2,26 @@ import Foundation import TableProPluginKit extension PluginDriverAdapter { - func streamRows(query: String) -> AsyncThrowingStream { + func streamRows(query: String, safeModeIsReadOnly: Bool = false) -> AsyncThrowingStream { if let error = StatementTextValidator.error(for: query) { return AsyncThrowingStream { $0.finish(throwing: error) } } - return schemaPluginDriver.streamRows(query: query, context: statementContext(query)) + return schemaPluginDriver.streamRows(query: query, context: streamContext(query, safeModeIsReadOnly)) } - func streamExportRows(query: String) -> AsyncThrowingStream { + func streamExportRows( + query: String, + safeModeIsReadOnly: Bool = false + ) -> AsyncThrowingStream { if let error = StatementTextValidator.error(for: query) { return AsyncThrowingStream { $0.finish(throwing: error) } } - return schemaPluginDriver.streamExportRows(query: query, context: statementContext(query)) + return schemaPluginDriver.streamExportRows(query: query, context: streamContext(query, safeModeIsReadOnly)) + } + + private func streamContext(_ query: String, _ safeModeIsReadOnly: Bool) -> PluginStatementContext { + var context = statementContext(query) + context.enforcesReadOnly = readOnlyRunContext(safeModeIsReadOnly: safeModeIsReadOnly) != nil + return context } } diff --git a/apple/TablePro/Core/Plugins/PluginDriverAdapter+ReadOnlyMode.swift b/apple/TablePro/Core/Plugins/PluginDriverAdapter+ReadOnlyMode.swift new file mode 100644 index 0000000000..dc8b03615b --- /dev/null +++ b/apple/TablePro/Core/Plugins/PluginDriverAdapter+ReadOnlyMode.swift @@ -0,0 +1,26 @@ +// +// PluginDriverAdapter+ReadOnlyMode.swift +// TablePro +// + +import Foundation +import TableProPluginKit + +extension PluginDriverAdapter { + var enforcesReadOnlyMode: Bool { + schemaPluginDriver.capabilities.contains(.enforcesReadOnlyMode) + } + + func executeBoundedQuery(query: String, rowCap: Int, context: PluginStatementContext) async throws -> QueryResult? { + try StatementTextValidator.validate(query) + try refuseUnenforcedPolicy(context) + guard let pluginResult = try await schemaPluginDriver.executeBoundedQuery( + query: query, + rowCap: rowCap, + context: statementContext(query, approvedAs: context) + ) else { + return nil + } + return mapQueryResult(pluginResult) + } +} diff --git a/apple/TablePro/Core/Plugins/PluginDriverAdapter.swift b/apple/TablePro/Core/Plugins/PluginDriverAdapter.swift index 4b4ec3ab6d..78d0cbe44d 100644 --- a/apple/TablePro/Core/Plugins/PluginDriverAdapter.swift +++ b/apple/TablePro/Core/Plugins/PluginDriverAdapter.swift @@ -1318,7 +1318,7 @@ final class PluginDriverAdapter: DatabaseDriver, SchemaSwitchable, DatabaseRepor // MARK: - Result Mapping - private func mapQueryResult(_ pluginResult: PluginQueryResult) -> QueryResult { + func mapQueryResult(_ pluginResult: PluginQueryResult) -> QueryResult { let columnTypes = mapColumnTypes( rawTypeNames: pluginResult.columnTypeNames, classificationHints: PluginResultColumnHints.hints( @@ -1462,7 +1462,7 @@ extension PluginDriverAdapter { return mapQueryResult(pluginResult) } - fileprivate func statementContext(_ query: String, approvedAs caller: PluginStatementContext, parameters: [Any?]? = nil) -> PluginStatementContext { + func statementContext(_ query: String, approvedAs caller: PluginStatementContext, parameters: [Any?]? = nil) -> PluginStatementContext { var context = PluginStatementContext.statement(query, databaseType: connection.type, approvedAs: caller) context.lexicalFeatures = sessionLexicalFeatures context.parameterKinds = TypedParameterValue.kinds(of: parameters) ?? context.parameterKinds @@ -1470,7 +1470,7 @@ extension PluginDriverAdapter { } // A driver built before `.statementPolicy` would run an external write the approval did not cover. - fileprivate func refuseUnenforcedPolicy(_ context: PluginStatementContext) throws { + func refuseUnenforcedPolicy(_ context: PluginStatementContext) throws { guard context.origin == .external, !context.readOnly, !context.allowsDestructive || !context.allowsServerCode, PluginMetadataRegistry.shared.snapshot(for: connection.type)?.capabilities.statementPolicyInDriver == true, diff --git a/apple/TablePro/Core/Plugins/PluginMetadataRegistry+CuratedDefaults.swift b/apple/TablePro/Core/Plugins/PluginMetadataRegistry+CuratedDefaults.swift index 410a02df9b..bad9d1bcba 100644 --- a/apple/TablePro/Core/Plugins/PluginMetadataRegistry+CuratedDefaults.swift +++ b/apple/TablePro/Core/Plugins/PluginMetadataRegistry+CuratedDefaults.swift @@ -20,6 +20,18 @@ extension PluginMetadataRegistry { // their driver creates is a SQLite database. static let sqliteFileExtensions = ["db", "db3", "s3db", "sl3", "sqlite", "sqlite3", "sqlitedb"] + // Engines where a function a SELECT calls can change data: an autonomous-transaction function on Oracle and Dameng, + // a stored function on MySQL, MariaDB and OceanBase, PL/pgSQL on the PostgreSQL family. Read-Only asks before such a + // call only when the live driver cannot run the statement in the engine's read-only mode. MySQL and MariaDB are + // listed by type because an OceanBase server answers as MySQL, so only the driver's own bit tells them apart. TiDB, + // StarRocks and Databend have no stored function or UDF that writes; SQL Server, Snowflake, BigQuery, Trino and + // Redshift refuse a write from a function a query calls; ClickHouse, the SQLite engines and Cassandra have none. + static let queryFunctionsCanWrite: Set = [ + .oracle, .dameng, + .mysql, .mariadb, .oceanbase, + .postgresql, .cockroachdb, .pglite, .kingbasees, + ] + // swiftlint:disable:next function_body_length static func curatedDefaults() -> [(typeId: String, snapshot: PluginMetadataSnapshot)] { let mysqlDialect = SQLDialectDescriptor( diff --git a/apple/TablePro/Core/Plugins/StreamingQueryExportDataSource.swift b/apple/TablePro/Core/Plugins/StreamingQueryExportDataSource.swift index eb5520437a..fa822d1bba 100644 --- a/apple/TablePro/Core/Plugins/StreamingQueryExportDataSource.swift +++ b/apple/TablePro/Core/Plugins/StreamingQueryExportDataSource.swift @@ -18,6 +18,7 @@ final class StreamingQueryExportDataSource: PluginExportDataSource, @unchecked S private let query: String private let driver: DatabaseDriver private let dbType: DatabaseType + private let safeModeIsReadOnly: Bool private let rowCut = OSAllocatedUnfairLock(initialState: nil) private static let logger = Logger(subsystem: "com.TablePro", category: "StreamingQueryExport") @@ -27,8 +28,9 @@ final class StreamingQueryExportDataSource: PluginExportDataSource, @unchecked S rowCut.withLock { $0 } } - init(query: String, driver: DatabaseDriver, databaseType: DatabaseType) { + init(query: String, driver: DatabaseDriver, databaseType: DatabaseType, safeModeIsReadOnly: Bool = false) { self.query = query + self.safeModeIsReadOnly = safeModeIsReadOnly self.driver = driver self.dbType = databaseType self.databaseTypeId = databaseType.rawValue @@ -40,7 +42,8 @@ final class StreamingQueryExportDataSource: PluginExportDataSource, @unchecked S return AsyncThrowingStream { $0.finish(throwing: PluginExportError.exportFailed("No plugin driver available")) } } let rowCut = rowCut - return ExportRowCut.reporting(adapter.streamRows(query: query), engineCeiling: nil) { limit in + let rows = adapter.streamRows(query: query, safeModeIsReadOnly: safeModeIsReadOnly) + return ExportRowCut.reporting(rows, engineCeiling: nil) { limit in rowCut.withLock { $0 = limit } } } @@ -54,7 +57,8 @@ final class StreamingQueryExportDataSource: PluginExportDataSource, @unchecked S return AsyncThrowingStream { $0.finish(throwing: PluginExportError.exportFailed("No plugin driver available")) } } let rowCut = rowCut - return ExportRowCut.reporting(adapter.streamExportRows(query: query), engineCeiling: nil) { limit in + let rows = adapter.streamExportRows(query: query, safeModeIsReadOnly: safeModeIsReadOnly) + return ExportRowCut.reporting(rows, engineCeiling: nil) { limit in rowCut.withLock { $0 = limit } } } diff --git a/apple/TablePro/Core/Process/PipeLineSplitter.swift b/apple/TablePro/Core/Process/PipeLineSplitter.swift new file mode 100644 index 0000000000..67c66175a0 --- /dev/null +++ b/apple/TablePro/Core/Process/PipeLineSplitter.swift @@ -0,0 +1,34 @@ +// +// PipeLineSplitter.swift +// TablePro +// + +import Foundation + +// Splits on LF only and decodes whole lines, so a chunk that ends inside a UTF-8 sequence loses nothing +// and a U+2028 inside a JSON string does not cut the line. +struct PipeLineSplitter: Sendable { + private var pending = Data() + + mutating func lines(appending chunk: Data) -> [String] { + pending.append(chunk) + var lines: [String] = [] + var lineStart = pending.startIndex + while let newline = pending[lineStart...].firstIndex(of: 0x0A) { + lines.append(Self.decode(pending[lineStart ..< newline])) + lineStart = pending.index(after: newline) + } + pending.removeSubrange(pending.startIndex ..< lineStart) + return lines + } + + mutating func finish() -> String? { + defer { pending = Data() } + return pending.isEmpty ? nil : Self.decode(pending) + } + + private static func decode(_ bytes: Data) -> String { + let line = bytes.last == 0x0D ? bytes.dropLast() : bytes + return String(decoding: line, as: UTF8.self) // swiftlint:disable:this optional_data_string_conversion + } +} diff --git a/apple/TablePro/Core/Process/PipeReader.swift b/apple/TablePro/Core/Process/PipeReader.swift index 916db0a0e0..6ba0292584 100644 --- a/apple/TablePro/Core/Process/PipeReader.swift +++ b/apple/TablePro/Core/Process/PipeReader.swift @@ -14,19 +14,47 @@ internal final class PipeReader: @unchecked Sendable { private let descriptor: Int32 private let lock = NSLock() private var consumer: (@Sendable (Data) -> Void)? + private var onEnd: (@Sendable () -> Void)? init(_ handle: FileHandle) { self.handle = handle descriptor = handle.fileDescriptor } - func start(delivering consumer: @escaping @Sendable (Data) -> Void) { - lock.withLock { self.consumer = consumer } + // onEnd runs once when the pipe reaches end of file or fails, never after a stop. + func start( + delivering consumer: @escaping @Sendable (Data) -> Void, + onEnd: (@Sendable () -> Void)? = nil + ) { + lock.withLock { + self.consumer = consumer + self.onEnd = onEnd + } handle.readabilityHandler = { _ in self.readArrivedBytes() } } + func startDeliveringLines( + _ deliver: @escaping @Sendable (String) -> Void, + onEnd: @escaping @Sendable () -> Void = {} + ) { + let splitter = OSAllocatedUnfairLock(initialState: PipeLineSplitter()) + start( + delivering: { chunk in + for line in splitter.withLock({ $0.lines(appending: chunk) }) { + deliver(line) + } + }, + onEnd: { + if let last = splitter.withLock({ $0.finish() }) { + deliver(last) + } + onEnd() + } + ) + } + func stop(drainingUpTo limit: Int = 0) { stopReading { consumer in let buffered = DescriptorRead.bufferedBytes(from: descriptor, upTo: limit) @@ -49,30 +77,37 @@ internal final class PipeReader: @unchecked Sendable { defer { lock.unlock() } guard let consumer else { return } self.consumer = nil + onEnd = nil drain(consumer) } private func readArrivedBytes() { lock.lock() - defer { lock.unlock() } - guard let consumer else { return } + let ended = readLocked() + lock.unlock() + // Outside the lock: the callback may stop this reader. + ended?() + } + + private func readLocked() -> (@Sendable () -> Void)? { + guard let consumer else { return nil } do { let chunk = try DescriptorRead.availableBytes(from: descriptor) - guard !chunk.isEmpty else { - endReading() - return - } + guard !chunk.isEmpty else { return endReading() } consumer(chunk) + return nil } catch POSIXError.EAGAIN { - return + return nil } catch { Self.logger.error("Reading a pipe failed: \(error.publicLogShape, privacy: .public)") - endReading() + return endReading() } } - private func endReading() { + private func endReading() -> (@Sendable () -> Void)? { consumer = nil handle.readabilityHandler = nil + defer { onEnd = nil } + return onEnd } } diff --git a/apple/TablePro/Core/Process/SupervisedProcessRunner.swift b/apple/TablePro/Core/Process/SupervisedProcessRunner.swift index c22c0d2d41..8858b20775 100644 --- a/apple/TablePro/Core/Process/SupervisedProcessRunner.swift +++ b/apple/TablePro/Core/Process/SupervisedProcessRunner.swift @@ -30,7 +30,7 @@ final class ProcessSupervisedRunner: SupervisedProcessRunner, @unchecked Sendabl private static let forcedTerminationGrace = Duration.seconds(2) - private var partialLine = "" + private var stderrSplitter = PipeLineSplitter() private var wasRequested = false private var terminationResult: SubprocessTermination? private var terminationContinuations: [CheckedContinuation] = [] @@ -121,14 +121,8 @@ final class ProcessSupervisedRunner: SupervisedProcessRunner, @unchecked Sendabl } private func ingestStderr(_ chunk: Data) { - guard let text = String(data: chunk, encoding: .utf8) else { return } stateLock.lock() - partialLine += text - var lines: [String] = [] - while let newlineIndex = partialLine.firstIndex(of: "\n") { - lines.append(String(partialLine[.. BatchTransactionPlan { let family = TransactionEngineFamily.of(databaseType) guard family.wrapsBatchInTransaction else { return queuedBlockPlan(for: statements, grammar: grammar) } - var runsInAutocommit = false + var runsInAutocommit = safeModeIsReadOnly var holdsASavepoint = false for statement in statements { let text = statement as NSString diff --git a/apple/TablePro/Core/Services/Execution/DefaultExecutionGate.swift b/apple/TablePro/Core/Services/Execution/DefaultExecutionGate.swift index 3f41c4f7ad..4ce9545812 100644 --- a/apple/TablePro/Core/Services/Execution/DefaultExecutionGate.swift +++ b/apple/TablePro/Core/Services/Execution/DefaultExecutionGate.swift @@ -12,6 +12,7 @@ internal actor DefaultExecutionGate: ExecutionGate { private let safeModeLevelResolver: @Sendable (UUID) async -> SafeModeLevel private let forcesWriteResolver: @Sendable (DatabaseType) async -> Bool private let connectionNameResolver: @Sendable (UUID) async -> String? + private let readOnlyFunctionPolicyResolver: @Sendable (UUID, DatabaseType) async -> ReadOnlyFunctionPolicy private let auditLog: any ExecutionAuditLogging /// The end of a sentence macOS writes, not a sentence of its own. `LAContext` shows the reason @@ -29,6 +30,9 @@ internal actor DefaultExecutionGate: ExecutionGate { safeModeLevelResolver: @escaping @Sendable (UUID) async -> SafeModeLevel, forcesWriteResolver: @escaping @Sendable (DatabaseType) async -> Bool, connectionNameResolver: @escaping @Sendable (UUID) async -> String? = { _ in nil }, + readOnlyFunctionPolicyResolver: @escaping @Sendable (UUID, DatabaseType) async -> ReadOnlyFunctionPolicy = { _, _ in + ReadOnlyFunctionPolicy() + }, auditLog: any ExecutionAuditLogging = ExecutionAuditLog.shared ) { self.confirming = confirming @@ -36,6 +40,7 @@ internal actor DefaultExecutionGate: ExecutionGate { self.safeModeLevelResolver = safeModeLevelResolver self.forcesWriteResolver = forcesWriteResolver self.connectionNameResolver = connectionNameResolver + self.readOnlyFunctionPolicyResolver = readOnlyFunctionPolicyResolver self.auditLog = auditLog } @@ -74,6 +79,11 @@ internal actor DefaultExecutionGate: ExecutionGate { )) } + if level.blocksAllWrites, request.kind != .metadataRead, let sql = request.sql, + let refusal = await readOnlyFunctionRefusal(request, sql: sql) { + return refusal + } + /// Narrower than `effectiveWrite`, which is true for every statement on a driver that /// cannot be opened read-only. A caller asking to confirm its writes means the ones that /// actually write, not every `GET` sent to Redis. @@ -129,6 +139,43 @@ internal actor DefaultExecutionGate: ExecutionGate { ) } + // The classifier tiers `SELECT purge_orders()` as a read. Where the driver cannot run it in the engine's read-only + // mode and a function on the engine can write, a call off the built-in list is asked about, or refused when a + // profile or the engine floor holds the level, since an answer there would let a write past them. Nil runs it. + private func readOnlyFunctionRefusal(_ request: OperationRequest, sql: String) async -> OperationDecision? { + let policy = await readOnlyFunctionPolicyResolver(request.connectionId, request.databaseType) + guard policy.functionsCanWrite, + ReadOnlyFunctionCalls.callsUnlistedFunction(sql, databaseType: request.databaseType) + else { return nil } + if policy.levelIsHeld { + return .denied(reason: String( + localized: "Cannot run this query: it calls a function that can change data, and Safe Mode is held at Read-Only for this connection." + )) + } + let caps = request.capabilities + guard !caps.contains(.preCleared), !caps.contains(.confirmationPreCleared) else { return nil } + if caps.contains(.cannotPrompt) { + return .denied(reason: String(localized: "Confirmation is required for this operation")) + } + let confirmed = await confirming.confirm( + OperationConfirmationRequest( + sql: request.sql, + operationDescription: String(localized: "This query calls a function that can change data"), + connectionId: request.connectionId, + connectionName: await connectionNameResolver(request.connectionId), + databaseType: request.databaseType, + caller: request.caller, + isDestructive: false, + scope: request.scope, + statements: request.statements + ) + ) + guard confirmed else { + return .denied(reason: String(localized: "Operation cancelled by user"), cause: .cancelledByUser) + } + return nil + } + private func resolveEffectiveWrite(_ request: OperationRequest, tier: QueryTier?) async -> Bool { if request.kind == .metadataRead { return false diff --git a/apple/TablePro/Core/Services/Execution/ExecutionGateProvider.swift b/apple/TablePro/Core/Services/Execution/ExecutionGateProvider.swift index 733f1eb502..d2afb9b51e 100644 --- a/apple/TablePro/Core/Services/Execution/ExecutionGateProvider.swift +++ b/apple/TablePro/Core/Services/Execution/ExecutionGateProvider.swift @@ -13,24 +13,55 @@ internal enum ExecutionGateProvider { !supportsReadOnlyMode && !QueryClassifier.enginesWithProvenReads.contains(databaseType) } + // The level the gate decides with. A run reads it here too, so the statement carries the level it was approved under. + @MainActor + static func effectiveSafeModeLevel(for connectionId: UUID, in manager: DatabaseManager? = nil) -> SafeModeLevel { + let connectionLevel: SafeModeLevel + switch (manager ?? .shared).connectionState(connectionId) { + case .live(_, let session): + connectionLevel = session.safeModeLevel + case .stored(let connection): + connectionLevel = connection.safeModeLevel + case .unknown: + connectionLevel = .silent + } + return ManagedPolicyResolver.effectiveSafeModeLevel( + connectionLevel: connectionLevel, + policy: ManagedPolicyReader.shared + ) + } + + @MainActor + static func runsAtReadOnly(_ connectionId: UUID, in manager: DatabaseManager? = nil) -> Bool { + effectiveSafeModeLevel(for: connectionId, in: manager).blocksAllWrites + } + + @MainActor + static func readOnlyFunctionPolicy( + connectionId: UUID, + databaseType: DatabaseType, + in manager: DatabaseManager? = nil + ) -> ReadOnlyFunctionPolicy { + let state = (manager ?? .shared).connectionState(connectionId) + let (liveDriver, connection): (DatabaseDriver?, DatabaseConnection?) = switch state { + case .live(let driver, let session): (driver, session.connection) + case .stored(let stored): (nil, stored) + case .unknown: (nil, nil) + } + let managedMinimum = ManagedPolicyResolver.minimumSafeModeLevel(policy: ManagedPolicyReader.shared) + return ReadOnlyFunctionPolicy( + functionsCanWrite: liveDriver?.enforcesReadOnlyMode != true + && PluginMetadataRegistry.queryFunctionsCanWrite.contains(databaseType), + levelIsHeld: managedMinimum?.blocksAllWrites == true + || connection?.safeModeFloor?.level.blocksAllWrites == true + ) + } + static let shared: ExecutionGate = DefaultExecutionGate( confirming: AlertOperationConfirming(), authenticating: BiometricOperationAuthenticating(), safeModeLevelResolver: { connectionId in - let connectionLevel: SafeModeLevel = await MainActor.run { - switch DatabaseManager.shared.connectionState(connectionId) { - case .live(_, let session): - return session.safeModeLevel - case .stored(let connection): - return connection.safeModeLevel - case .unknown: - return .silent - } - } - return ManagedPolicyResolver.effectiveSafeModeLevel( - connectionLevel: connectionLevel, - policy: ManagedPolicyReader.shared - ) + await effectiveSafeModeLevel(for: connectionId) }, forcesWriteResolver: { databaseType in let supportsReadOnlyMode = await MainActor.run { @@ -49,6 +80,9 @@ internal enum ExecutionGateProvider { return nil } } + }, + readOnlyFunctionPolicyResolver: { connectionId, databaseType in + await readOnlyFunctionPolicy(connectionId: connectionId, databaseType: databaseType) } ) } diff --git a/apple/TablePro/Core/Services/Execution/ExternalStatementGate.swift b/apple/TablePro/Core/Services/Execution/ExternalStatementGate.swift index 493ef67dde..04b4747b0c 100644 --- a/apple/TablePro/Core/Services/Execution/ExternalStatementGate.swift +++ b/apple/TablePro/Core/Services/Execution/ExternalStatementGate.swift @@ -350,6 +350,15 @@ internal struct ApprovedOperation: Sendable { context = Self.context(for: pending) } + // A read stops once its rows pass the caller's answer budget. A write runs to its end whatever it returns. + internal func context(readingAtMost byteLimit: Int?) -> PluginStatementContext { + var bounded = context + if !kind.declaresWrite { + bounded.resultByteLimit = byteLimit + } + return bounded + } + private static func context(for pending: PendingOperation) -> PluginStatementContext { let graded = pending.gradedSQL.map { [$0] } ?? pending.statements.map(\.sql) let approvedAsDestructive = pending.kind.declaresDestructive diff --git a/apple/TablePro/Core/Services/Execution/ReadOnlyFunctionCalls.swift b/apple/TablePro/Core/Services/Execution/ReadOnlyFunctionCalls.swift new file mode 100644 index 0000000000..7563abf951 --- /dev/null +++ b/apple/TablePro/Core/Services/Execution/ReadOnlyFunctionCalls.swift @@ -0,0 +1,374 @@ +// +// ReadOnlyFunctionCalls.swift +// TablePro +// + +import Foundation +import TableProPluginKit +import TableProSQLGrammar + +// What Read-Only needs to know about a connection before it lets a read call a function. +internal struct ReadOnlyFunctionPolicy: Sendable, Equatable { + // The live driver cannot run a statement in the engine's read-only mode, and a function on this engine can write. + var functionsCanWrite = false + // A configuration profile or the connection's floor holds it at Read-Only, so no answer may let a write through. + var levelIsHeld = false +} + +// The one place that says which calls Read-Only lets through without asking, on an engine whose functions can write and +// a driver that cannot hold the statement to the engine's read-only mode. A built-in here reads only: nothing that +// writes, locks, sleeps, waits or reaches a file (GET_LOCK, SLEEP, LOAD_FILE, NEXTVAL, SETVAL, a DBMS_ package) is listed. +// A qualified or quoted name, a name the scanner cannot read, and anything else may be a function the user wrote. A +// call written without parentheses, which Oracle and Dameng accept for a function with no arguments, cannot be told +// from a column and is not seen. +internal enum ReadOnlyFunctionCalls { + static let commonBuiltins: Set = [ + "COUNT", "SUM", "AVG", "MIN", "MAX", "STDDEV", "STDDEV_POP", "STDDEV_SAMP", "VARIANCE", "VAR_POP", "VAR_SAMP", + "CORR", "COVAR_POP", "COVAR_SAMP", "GROUPING", + "ROW_NUMBER", "RANK", "DENSE_RANK", "PERCENT_RANK", "CUME_DIST", "NTILE", "LAG", "LEAD", "FIRST_VALUE", + "LAST_VALUE", "NTH_VALUE", + "COALESCE", "NULLIF", "GREATEST", "LEAST", "CAST", "EXTRACT", + "LOWER", "UPPER", "LENGTH", "TRIM", "LTRIM", "RTRIM", "SUBSTR", "SUBSTRING", "REPLACE", "CONCAT", "LPAD", "RPAD", + "REVERSE", "ASCII", "REGEXP_REPLACE", "REGEXP_SUBSTR", "REGEXP_INSTR", "REGEXP_LIKE", "POSITION", "TRANSLATE", + "ABS", "CEIL", "CEILING", "FLOOR", "ROUND", "MOD", "POWER", "SQRT", "EXP", "LN", "LOG", "SIGN", "SIN", "COS", + "TAN", "ASIN", "ACOS", "ATAN", "ATAN2", + "CURRENT_DATE", "CURRENT_TIME", "CURRENT_TIMESTAMP", "LOCALTIMESTAMP", + "JSON_VALUE", "JSON_QUERY", "JSON_OBJECT", "JSON_ARRAY", "JSON_TABLE", + ] + + // Oracle and Dameng. + static let oracleBuiltins: Set = [ + "NVL", "NVL2", "DECODE", "LNNVL", "NANVL", "LISTAGG", "MEDIAN", "RATIO_TO_REPORT", + "TO_CHAR", "TO_DATE", "TO_NUMBER", "TO_TIMESTAMP", "TO_TIMESTAMP_TZ", "TO_DSINTERVAL", "TO_YMINTERVAL", + "NUMTODSINTERVAL", "NUMTOYMINTERVAL", "TO_NCHAR", "HEXTORAW", "RAWTOHEX", "ROWIDTOCHAR", "CONVERT", + "INSTR", "INSTRB", "LENGTHB", "SUBSTRB", "INITCAP", "CHR", "SOUNDEX", "REGEXP_COUNT", + "TRUNC", "BITAND", "WIDTH_BUCKET", "REMAINDER", + "ADD_MONTHS", "MONTHS_BETWEEN", "LAST_DAY", "NEXT_DAY", "NEW_TIME", "FROM_TZ", "TZ_OFFSET", "SYS_EXTRACT_UTC", + "SYS_GUID", "SYS_CONTEXT", "USERENV", "ORA_HASH", "STANDARD_HASH", "XMLTABLE", + ] + + // MySQL, MariaDB and the servers that answer as MySQL. + static let mysqlBuiltins: Set = [ + "IF", "IFNULL", "ISNULL", "GROUP_CONCAT", "JSON_ARRAYAGG", "JSON_OBJECTAGG", "BIT_AND", "BIT_OR", "BIT_XOR", + "ANY_VALUE", "CONVERT", + "CONCAT_WS", "CHAR_LENGTH", "CHARACTER_LENGTH", "OCTET_LENGTH", "BIT_LENGTH", "LCASE", "UCASE", "LEFT", "RIGHT", + "MID", "LOCATE", "INSTR", "FIND_IN_SET", "FIELD", "ELT", "STRCMP", "SUBSTRING_INDEX", "REPEAT", "SPACE", "CHAR", + "FORMAT", "QUOTE", "SOUNDEX", "HEX", "UNHEX", "BIN", "OCT", "CONV", + "TRUNCATE", "POW", "LOG2", "LOG10", "COT", "DEGREES", "RADIANS", "PI", "RAND", "CRC32", + "NOW", "SYSDATE", "CURDATE", "CURTIME", "LOCALTIME", "UTC_DATE", "UTC_TIME", "UTC_TIMESTAMP", "DATE", "TIME", + "TIMESTAMP", "YEAR", "QUARTER", "MONTH", "WEEK", "WEEKDAY", "WEEKOFYEAR", "YEARWEEK", "DAY", "DAYOFMONTH", + "DAYOFWEEK", "DAYOFYEAR", "DAYNAME", "MONTHNAME", "HOUR", "MINUTE", "SECOND", "MICROSECOND", "DATE_FORMAT", + "TIME_FORMAT", "STR_TO_DATE", "DATE_ADD", "DATE_SUB", "ADDDATE", "SUBDATE", "ADDTIME", "SUBTIME", "DATEDIFF", + "TIMEDIFF", "TIMESTAMPDIFF", "TIMESTAMPADD", "UNIX_TIMESTAMP", "FROM_UNIXTIME", "FROM_DAYS", "TO_DAYS", + "TO_SECONDS", "SEC_TO_TIME", "TIME_TO_SEC", "MAKEDATE", "MAKETIME", "PERIOD_ADD", "PERIOD_DIFF", "LAST_DAY", + "CONVERT_TZ", + "JSON_EXTRACT", "JSON_UNQUOTE", "JSON_CONTAINS", "JSON_CONTAINS_PATH", "JSON_KEYS", "JSON_LENGTH", "JSON_TYPE", + "JSON_VALID", "JSON_SEARCH", "JSON_DEPTH", + "MD5", "SHA", "SHA1", "SHA2", "UUID", "UUID_SHORT", + "DATABASE", "SCHEMA", "USER", "CURRENT_USER", "SESSION_USER", "SYSTEM_USER", "VERSION", "CONNECTION_ID", + ] + + // An older PostgreSQL-family driver without the mode. + static let postgresBuiltins: Set = [ + "NOW", "TO_CHAR", "TO_DATE", "TO_NUMBER", "TO_TIMESTAMP", "DATE_TRUNC", "DATE_PART", "AGE", "TRUNC", "LEFT", + "RIGHT", "CHAR_LENGTH", "CHARACTER_LENGTH", "OCTET_LENGTH", "SPLIT_PART", "STRING_AGG", "ARRAY_AGG", + "ARRAY_LENGTH", "INITCAP", "CHR", "MD5", "FORMAT", "CONCAT_WS", "TO_JSON", "TO_JSONB", "JSON_AGG", "JSONB_AGG", + "JSON_BUILD_OBJECT", "JSONB_BUILD_OBJECT", "JSON_BUILD_ARRAY", "JSONB_BUILD_ARRAY", "GENERATE_SERIES", + "BOOL_AND", "BOOL_OR", "EVERY", "POW", "LOG10", "PI", "RANDOM", + ] + + private static let oracleFamily: Set = [.oracle, .dameng] + private static let mysqlFamily: Set = [.mysql, .mariadb, .oceanbase, .tidb, .starrocks, .databend] + private static let postgresFamily: Set = [.postgresql, .cockroachdb, .pglite, .kingbasees, .redshift] + + private static let oracleCalls = commonBuiltins.union(oracleBuiltins) + private static let mysqlCalls = commonBuiltins.union(mysqlBuiltins) + private static let postgresCalls = commonBuiltins.union(postgresBuiltins) + private static let anyCalls = commonBuiltins.union(oracleBuiltins).union(mysqlBuiltins).union(postgresBuiltins) + + static func builtins(for databaseType: DatabaseType) -> Set { + if oracleFamily.contains(databaseType) { return oracleCalls } + if mysqlFamily.contains(databaseType) { return mysqlCalls } + if postgresFamily.contains(databaseType) { return postgresCalls } + return anyCalls + } + + // Reserved in Oracle, MySQL and PostgreSQL alike, so no function a query calls can carry the bare name. + static let reservedWords: Set = [ + "SELECT", "FROM", "WHERE", "AND", "OR", "NOT", "IN", "EXISTS", "AS", "ON", "WITH", "GROUP", "ORDER", "HAVING", + "UNION", "DISTINCT", "ELSE", "THEN", "ALL", "VALUES", "BETWEEN", "TABLE", + ] + + // A word another engine lets a user function carry stands before a parenthesis only in this syntax. + private enum Syntax { + // Right after a call's closing parenthesis: `SUM(x) OVER (`. + case afterCall + // After a call, opening with this word: `COUNT(*) FILTER (WHERE`, `MAX(x) KEEP (DENSE_RANK`. + case afterCallOpening(String) + // After one of these words: `GROUP BY (`, `GROUPING SETS (`, `USE INDEX (`. + case afterWord(Set) + // After a name, a literal or a closing parenthesis: `t PARTITION (`, `x LIKE (`, `) EXCEPT (`. + case afterOperand + // Opening with one of these words: `ARRAY(SELECT`, `LATERAL (SELECT`. + case opening(Set) + // After a comparison: `= ANY (`. + case afterComparison + // Its parenthesized part followed by one of these words: `MATCH (a) AGAINST`, `INTERVAL (n) DAY`. + case followedBy(Set) + } + + private static let intervalUnits: Set = [ + "MICROSECOND", "SECOND", "MINUTE", "HOUR", "DAY", "WEEK", "MONTH", "QUARTER", "YEAR", + ] + + private static let syntax: [String: [Syntax]] = [ + "BY": [.afterWord(["GROUP", "ORDER", "PARTITION", "CONNECT"])], + "OVER": [.afterCall], + "FILTER": [.afterCallOpening("WHERE")], + "KEEP": [.afterCallOpening("DENSE_RANK")], + "AGAINST": [.afterCall], + "MATCH": [.followedBy(["AGAINST"])], + "INTERVAL": [.followedBy(intervalUnits)], + "CASE": [.followedBy(["WHEN"])], + "WHEN": [.afterOperand, .afterWord(["CASE"])], + "PARTITION": [.afterOperand], + "PIVOT": [.afterOperand], + "UNPIVOT": [.afterOperand], + "CUBE": [.afterWord(["BY"])], + "ROLLUP": [.afterWord(["BY"])], + "SETS": [.afterWord(["GROUPING"])], + "ARRAY": [.opening(["SELECT", "WITH"])], + "LATERAL": [.opening(["SELECT", "WITH"])], + "ANY": [.afterComparison], + "SOME": [.afterComparison], + "JOIN": [.afterOperand, .afterWord(["INNER", "LEFT", "RIGHT", "FULL", "OUTER", "CROSS", "NATURAL", "FOR"])], + "USING": [.afterOperand], + "INDEX": [.afterWord(["USE", "FORCE", "IGNORE"])], + "KEY": [.afterWord(["USE", "FORCE", "IGNORE"])], + "LIKE": [.afterOperand], + "IS": [.afterOperand], + "INTERSECT": [.afterOperand], + "EXCEPT": [.afterOperand], + "MINUS": [.afterOperand], + "LIMIT": [.afterOperand], + "OFFSET": [.afterOperand], + ] + + // After one of these a call can start, so a word that follows it is not standing after an operand. + private static let introducers: Set = reservedWords.union(syntax.keys).union([ + "SET", "INTO", "RETURN", "RETURNING", "PRIOR", "CALL", "DO", "EXECUTE", "FOR", "CONNECT", + ]) + + private static let openParenthesis = UInt16(UnicodeScalar("(").value) + private static let closeParenthesis = UInt16(UnicodeScalar(")").value) + private static let period = UInt16(UnicodeScalar(".").value) + private static let colon = UInt16(UnicodeScalar(":").value) + private static let quoteOpeners: Set = [0x22, 0x60, 0x5B] + private static let quoteUnits: Set = [0x22, 0x27, 0x60, 0x5B, 0x5D] + private static let comparisons: Set = [0x3D, 0x3C, 0x3E, 0x21] + + // Under every way the engine could lex the text, as the classifier reads it. A type with a length, such as + // `DECIMAL(10, 2)` in a cast, is the engine's own type, not a call. + static func callsUnlistedFunction(_ sql: String, databaseType: DatabaseType) -> Bool { + let typeNames = Set( + (PluginMetadataRegistry.shared.snapshot(for: databaseType)?.editor.sqlDialect?.dataTypes ?? []) + .map { $0.uppercased() } + ) + let known = builtins(for: databaseType) + return databaseType.lexicalReadings.all.contains { grammar in + SQLStatementClassifier.statements(of: sql, grammar: grammar).contains { statement in + statementCallsUnlistedFunction(statement, grammar: grammar, builtins: known, typeNames: typeNames) + } + } + } + + private static func statementCallsUnlistedFunction( + _ statement: String, + grammar: SQLLexicalGrammar, + builtins: Set, + typeNames: Set + ) -> Bool { + // A plain EXPLAIN plans its statement without running it. + if let explain = SQLExplainStatement.read(statement, grammar: grammar) { + switch explain.reading { + case .describesObject, .explains(_, false): + return false + case .explains(_, true), .unreadable: + break + } + } + let source = statement as NSString + let code = SQLCodeProjection.code(of: statement, grammar: grammar) as NSString + return SQLFunctionCallScanner.calls(in: source, code: code).contains { call in + guard case .named(let name, let start) = call.callee, + !quoteOpeners.contains(source.character(at: start)) + else { return true } + let word = name.uppercased() + switch qualifier(before: start, in: code) { + case .member: + return true + case .cast: + return !typeNames.contains(word) + case .unqualified: + break + } + if builtins.contains(word) || typeNames.contains(word) || reservedWords.contains(word) { return false } + guard let rules = syntax[word] else { return true } + let site = CallSite(source: source, code: code, nameStart: start, parenthesis: call.parenthesis) + return !rules.contains { site.matches($0) } + } + } + + private enum Qualifier { + case unqualified + // `schema.name(`, `package.name(`. + case member + // PostgreSQL's `value::numeric(10, 2)`. + case cast + } + + private static func qualifier(before start: Int, in code: NSString) -> Qualifier { + var cursor = start - 1 + while cursor >= 0, SQLFunctionCallScanner.isBlank(code.character(at: cursor)) { + cursor -= 1 + } + guard cursor >= 0 else { return .unqualified } + let unit = code.character(at: cursor) + if unit == period { return .member } + guard unit == colon else { return .unqualified } + return cursor > 0 && code.character(at: cursor - 1) == colon ? .cast : .member + } + + private enum Neighbor: Equatable { + case start + case word(String) + case symbol(UInt16) + // A literal or a quoted name, which the projection blanked. + case quoted + } + + private struct CallSite { + let source: NSString + let code: NSString + let nameStart: Int + let parenthesis: Int + + func matches(_ rule: Syntax) -> Bool { + switch rule { + case .afterCall: + return before == .symbol(ReadOnlyFunctionCalls.closeParenthesis) + case .afterCallOpening(let word): + return before == .symbol(ReadOnlyFunctionCalls.closeParenthesis) && firstWord(after: parenthesis) == word + case .afterWord(let words): + guard case .word(let word) = before else { return false } + return words.contains(word) + case .afterOperand: + switch before { + case .quoted: + return true + case .symbol(let unit): + return unit == ReadOnlyFunctionCalls.closeParenthesis + case .word(let word): + return !ReadOnlyFunctionCalls.introducers.contains(word) + case .start: + return false + } + case .opening(let words): + return firstWord(after: parenthesis).map { words.contains($0) } ?? false + case .afterComparison: + guard case .symbol(let unit) = before else { return false } + return ReadOnlyFunctionCalls.comparisons.contains(unit) + case .followedBy(let words): + return wordAfterGroup().map { words.contains($0) } ?? false + } + } + + private var before: Neighbor { + var cursor = nameStart - 1 + while cursor >= 0, SQLFunctionCallScanner.isBlank(code.character(at: cursor)) { + if ReadOnlyFunctionCalls.quoteUnits.contains(source.character(at: cursor)) { return .quoted } + cursor -= 1 + } + guard cursor >= 0 else { return .start } + let unit = code.character(at: cursor) + guard SQLFunctionCallScanner.isIdentifierUnit(unit) else { return .symbol(unit) } + var start = cursor + while start > 0, SQLFunctionCallScanner.isIdentifierUnit(code.character(at: start - 1)) { + start -= 1 + } + return .word(code.substring(with: NSRange(location: start, length: cursor - start + 1)).uppercased()) + } + + private func firstWord(after index: Int) -> String? { + var cursor = index + 1 + while cursor < code.length, SQLFunctionCallScanner.isBlank(code.character(at: cursor)) { + cursor += 1 + } + var end = cursor + while end < code.length, SQLFunctionCallScanner.isIdentifierUnit(code.character(at: end)) { + end += 1 + } + guard end > cursor else { return nil } + return code.substring(with: NSRange(location: cursor, length: end - cursor)).uppercased() + } + + private func wordAfterGroup() -> String? { + var depth = 0 + for index in parenthesis.. String + ) -> [String] { + objects.compactMap { object in + let filter = object.rowScope.sanitizedFilter + guard !filter.isEmpty else { return nil } + let table = SchemaQualifiedName.render( + name: object.name, schema: object.databaseName, databaseType: databaseType, quote: quote + ) + return "SELECT * FROM \(table) WHERE \(filter)" + } + } +} diff --git a/apple/TablePro/Core/Services/Export/ExportService.swift b/apple/TablePro/Core/Services/Export/ExportService.swift index 5276597a82..4f65b45341 100644 --- a/apple/TablePro/Core/Services/Export/ExportService.swift +++ b/apple/TablePro/Core/Services/Export/ExportService.swift @@ -77,6 +77,7 @@ final class ExportService: ObservableObject { private let databaseType: DatabaseType? private let effectiveQueryTimeoutSeconds: Int private let exportPlugin: @MainActor (String) -> (any ExportFormatPlugin)? + var executionGate: any ExecutionGate = ExecutionGateProvider.shared init( driver: DatabaseDriver, @@ -207,15 +208,29 @@ final class ExportService: ObservableObject { } try throwIfStopped() - let dataSource = ExportDataSourceAdapter(driver: driver, databaseType: databaseType) - let rowObjects = objects.filter { plugin.readsRows(optionValues: $0.optionValues, kind: $0.kind) } + try await ReadOnlyExportGate.authorize( + ReadOnlyExportGate.rowScopeStatements(rowObjects, databaseType: databaseType) { driver.quoteIdentifier($0) }, + connectionId: driver.connection.id, + databaseType: databaseType, + gate: executionGate + ) + try throwIfStopped() + + let safeModeIsReadOnly = ExecutionGateProvider.runsAtReadOnly(driver.connection.id) + let dataSource = ExportDataSourceAdapter( + driver: driver, + databaseType: databaseType, + safeModeIsReadOnly: safeModeIsReadOnly + ) + state.readsRows = !rowObjects.isEmpty state.totalRows = await fetchTotalRowCount( for: rowObjects.filter { $0.kind.carriesRows }, driver: driver, databaseType: databaseType, - dataSource: dataSource + dataSource: dataSource, + context: driver.readOnlyRunContext(safeModeIsReadOnly: safeModeIsReadOnly) ) try throwIfStopped() @@ -429,11 +444,16 @@ final class ExportService: ObservableObject { currentProgress = nil } try throwIfStopped() + try await ReadOnlyExportGate.authorize( + [query], connectionId: driver.connection.id, databaseType: databaseType, gate: executionGate + ) + try throwIfStopped() let dataSource = StreamingQueryExportDataSource( query: LeadingRowsStatement.resolve(query, rowCap: nil, databaseType: databaseType).sql, driver: driver, - databaseType: databaseType + databaseType: databaseType, + safeModeIsReadOnly: ExecutionGateProvider.runsAtReadOnly(driver.connection.id) ) let nsProgress = Progress(totalUnitCount: Int64(max(estimatedRows, 1))) @@ -516,9 +536,15 @@ final class ExportService: ObservableObject { for tables: [ExportObjectItem], driver: DatabaseDriver, databaseType: DatabaseType, - dataSource: ExportDataSourceAdapter + dataSource: ExportDataSourceAdapter, + context: PluginStatementContext? ) async -> Int { guard !tables.isEmpty else { return 0 } + // At Read-Only a count runs read-only too: a view's COUNT(*) runs the view. + let runCount: (String) async throws -> QueryResult = { query in + guard let context else { return try await driver.execute(query: query) } + return try await driver.execute(query: query, context: context) + } var total = 0 var failedCount = 0 @@ -560,7 +586,7 @@ final class ExportService: ObservableObject { let batchQuery = unionParts.joined(separator: " UNION ALL ") do { - let result = try await driver.execute(query: batchQuery) + let result = try await runCount(batchQuery) for row in result.rows { if let cell = row.first, let count = Int(cell.asText ?? "0") { total = Self.saturatingSum(total, count) @@ -571,7 +597,7 @@ final class ExportService: ObservableObject { guard !isStopRequested else { return total } do { let tableRef = qualifiedTableRef(for: table, driver: driver, databaseType: databaseType) - let result = try await driver.execute(query: "SELECT COUNT(*) FROM \(tableRef)") + let result = try await runCount("SELECT COUNT(*) FROM \(tableRef)") if let cell = result.rows.first?.first, let count = Int(cell.asText ?? "0") { total = Self.saturatingSum(total, count) } diff --git a/apple/TablePro/Core/Services/Export/TableTransferService.swift b/apple/TablePro/Core/Services/Export/TableTransferService.swift index 8806cac233..443cbd9b2c 100644 --- a/apple/TablePro/Core/Services/Export/TableTransferService.swift +++ b/apple/TablePro/Core/Services/Export/TableTransferService.swift @@ -138,7 +138,8 @@ final class TableTransferService: ObservableObject { /// drivers, so every statement lands on the database the sheet named rather than wherever /// either shared driver was last parked. The gate asks before either lease is taken, so a /// prompt nobody has answered yet holds no connection. `confirmedByCaller` is a delete of the - /// destination's rows the sheet already confirmed in its own words; Touch ID still applies. + /// destination's rows the sheet already confirmed in its own words; Touch ID still applies. At + /// Read-Only on the source, a row filter meets the gate first, as a Run of it would. func run( request: Request, source: DatabaseScope, @@ -148,6 +149,17 @@ final class TableTransferService: ObservableObject { manager: DatabaseManager = .shared, gate: ExecutionGate = ExecutionGateProvider.shared ) async throws { + let sourceSession = manager.driver(for: source.connectionId) + try await ReadOnlyExportGate.authorize( + ReadOnlyExportGate.rowScopeStatements(request.objects, databaseType: request.sourceType) { name in + sourceSession?.quoteIdentifier(name) ?? name + }, + connectionId: source.connectionId, + databaseType: request.sourceType, + gate: gate, + manager: manager + ) + var capabilities = CallerCapabilities.interactiveUser if confirmedByCaller { capabilities.insert(.confirmationPreCleared) @@ -204,7 +216,11 @@ final class TableTransferService: ObservableObject { isCancelled = false } - let source = ExportDataSourceAdapter(driver: sourceDriver, databaseType: request.sourceType) + let source = ExportDataSourceAdapter( + driver: sourceDriver, + databaseType: request.sourceType, + safeModeIsReadOnly: ExecutionGateProvider.runsAtReadOnly(sourceDriver.connection.id) + ) /// The destination can be the user's own editor session, so the scopes close on every exit. let scopes = await openSessionScopes( for: request, objects: rowObjects, sourceDriver: sourceDriver, destinationDriver: destinationDriver) diff --git a/apple/TablePro/Core/Services/Query/QueryExecutor.swift b/apple/TablePro/Core/Services/Query/QueryExecutor.swift index f67b7650aa..5233d4dc07 100644 --- a/apple/TablePro/Core/Services/Query/QueryExecutor.swift +++ b/apple/TablePro/Core/Services/Query/QueryExecutor.swift @@ -110,13 +110,18 @@ final class QueryExecutor { sql: String, parameters: [Any?]? = nil, rowCap: Int?, + safeModeIsReadOnly: Bool = false, capturingOutputInto failureOutput: ServerOutputBox? ) async throws -> QueryFetchResult { guard let failureOutput else { - return try await executeQuery(driver: driver, sql: sql, parameters: parameters, rowCap: rowCap) + return try await executeQuery( + driver: driver, sql: sql, parameters: parameters, rowCap: rowCap, safeModeIsReadOnly: safeModeIsReadOnly + ) } return try await ServerOutputCapture.running(on: driver, failureOutput: failureOutput) { - try await executeQuery(driver: driver, sql: sql, parameters: parameters, rowCap: rowCap) + try await executeQuery( + driver: driver, sql: sql, parameters: parameters, rowCap: rowCap, safeModeIsReadOnly: safeModeIsReadOnly + ) } } @@ -124,20 +129,24 @@ final class QueryExecutor { driver: DatabaseDriver, sql: String, parameters: [Any?]? = nil, - rowCap: Int? + rowCap: Int?, + safeModeIsReadOnly: Bool = false ) async throws -> QueryFetchResult { + let context = driver.readOnlyRunContext(safeModeIsReadOnly: safeModeIsReadOnly) if let parameters { return try await Self.fetchQueryDataParameterized( driver: driver, sql: sql, parameters: parameters, - rowCap: rowCap + rowCap: rowCap, + context: context ) } return try await Self.fetchQueryData( driver: driver, sql: sql, - rowCap: rowCap + rowCap: rowCap, + context: context ) } @@ -150,11 +159,18 @@ final class QueryExecutor { nonisolated static func fetchBoundedQueryData( driver: DatabaseDriver, sql: String, - rowCap: Int? + rowCap: Int?, + context: PluginStatementContext? = nil ) async throws -> QueryFetchResult? { guard let rowCap, rowCap > 0 else { return nil } let start = CFAbsoluteTimeGetCurrent() - guard let result = try await driver.executeBoundedQuery(query: sql, rowCap: rowCap) else { + let bounded: QueryResult? + if let context { + bounded = try await driver.executeBoundedQuery(query: sql, rowCap: rowCap, context: context) + } else { + bounded = try await driver.executeBoundedQuery(query: sql, rowCap: rowCap) + } + guard let result = bounded else { return nil } let elapsed = CFAbsoluteTimeGetCurrent() - start @@ -197,17 +213,24 @@ final class QueryExecutor { ) } + // A non-nil context is Read-Only's, which the bounded read and its fallback both carry. nonisolated static func fetchQueryData( driver: DatabaseDriver, sql: String, - rowCap: Int? + rowCap: Int?, + context: PluginStatementContext? = nil ) async throws -> QueryFetchResult { - if let bounded = try await fetchBoundedQueryData(driver: driver, sql: sql, rowCap: rowCap) { + if let bounded = try await fetchBoundedQueryData(driver: driver, sql: sql, rowCap: rowCap, context: context) { return bounded } let start = CFAbsoluteTimeGetCurrent() queryExecutorLog.info("[executeUserQuery] sql=\(sql.prefix(100), privacy: .private) rowCap=\(rowCap?.description ?? "nil")") - let result = try await driver.executeUserQuery(query: sql, rowCap: rowCap, parameters: nil) + let result: QueryResult + if let context { + result = try await driver.executeUserQuery(query: sql, rowCap: rowCap, parameters: nil, context: context) + } else { + result = try await driver.executeUserQuery(query: sql, rowCap: rowCap, parameters: nil) + } let elapsed = CFAbsoluteTimeGetCurrent() - start queryExecutorLog.info("[executeUserQuery] rows=\(result.rows.count) truncated=\(result.isTruncated) driverTime=\(String(format: "%.3f", result.executionTime))s totalTime=\(String(format: "%.3f", elapsed))s") return QueryFetchResult( @@ -231,11 +254,17 @@ final class QueryExecutor { driver: DatabaseDriver, sql: String, parameters: [Any?], - rowCap: Int? + rowCap: Int?, + context: PluginStatementContext? = nil ) async throws -> QueryFetchResult { let start = CFAbsoluteTimeGetCurrent() queryExecutorLog.info("[executeUserQueryParameterized] sql=\(sql.prefix(100), privacy: .private) rowCap=\(rowCap?.description ?? "nil") params=\(parameters.count)") - let result = try await driver.executeUserQuery(query: sql, rowCap: rowCap, parameters: parameters) + let result: QueryResult + if let context { + result = try await driver.executeUserQuery(query: sql, rowCap: rowCap, parameters: parameters, context: context) + } else { + result = try await driver.executeUserQuery(query: sql, rowCap: rowCap, parameters: parameters) + } let elapsed = CFAbsoluteTimeGetCurrent() - start queryExecutorLog.info("[executeUserQueryParameterized] rows=\(result.rows.count) truncated=\(result.isTruncated) driverTime=\(String(format: "%.3f", result.executionTime))s totalTime=\(String(format: "%.3f", elapsed))s") return QueryFetchResult( diff --git a/apple/TablePro/Models/AI/AIModels.swift b/apple/TablePro/Models/AI/AIModels.swift index 778b3a5af8..3c82df312a 100644 --- a/apple/TablePro/Models/AI/AIModels.swift +++ b/apple/TablePro/Models/AI/AIModels.swift @@ -270,6 +270,7 @@ enum AIChatMode: String, Codable, CaseIterable, Identifiable, Sendable { case .edit: return """ You are in Edit mode. You can read schema and run SELECT/INSERT/UPDATE/DELETE via execute_query. \ + Leave schema, privilege and session changes, such as CREATE, ALTER, GRANT or SET, to Agent mode. \ Destructive statements, such as DROP, TRUNCATE or a DELETE with no WHERE, are blocked. """ case .agent: diff --git a/apple/TablePro/Models/AI/AgentSession.swift b/apple/TablePro/Models/AI/AgentSession.swift index a2b68570b2..79cf7466fe 100644 --- a/apple/TablePro/Models/AI/AgentSession.swift +++ b/apple/TablePro/Models/AI/AgentSession.swift @@ -196,7 +196,6 @@ internal final class AgentSession: ObservableObject, Identifiable { /// throwing a conversation away, so the transcript is written first and kept. internal func stop() { viewModel.cancelStream() - settlePendingApprovals() viewModel.persistCurrentConversation() mark(.stopped) } @@ -212,23 +211,6 @@ internal final class AgentSession: ObservableObject, Identifiable { mark(.ready) } - /// Settles a card that is still waiting before the transcript is written. - /// - /// `cancelStream()` resumes the continuation, but the suspended task only marks the block - /// `.cancelled` once it is back on the main actor, and the snapshot was taken before that. The - /// restored transcript then held a `.pending` card with no continuation behind it, whose Run - /// button could never work. - private func settlePendingApprovals() { - for turn in viewModel.messages { - for block in turn.blocks { - guard case .toolUse(var use) = block.kind, - case .pending = use.approvalState else { continue } - use.approvalState = .cancelled - block.setKind(.toolUse(use)) - } - } - } - internal var record: AgentSessionRecord { AgentSessionRecord( id: id, diff --git a/apple/TablePro/ViewModels/AIChatViewModel+Persistence.swift b/apple/TablePro/ViewModels/AIChatViewModel+Persistence.swift index 108e8c9e2e..5e6187706a 100644 --- a/apple/TablePro/ViewModels/AIChatViewModel+Persistence.swift +++ b/apple/TablePro/ViewModels/AIChatViewModel+Persistence.swift @@ -26,7 +26,7 @@ extension AIChatViewModel { guard self.messages.isEmpty else { return } guard let wanted, let stored = loaded.first(where: { $0.id == wanted }) else { return } self.activeConversationID = stored.id - self.messages = stored.messages.map { ChatTurn(wire: $0) } + self.messages = ChatToolCallRepair.settlingPendingApprovals(stored.messages).map { ChatTurn(wire: $0) } } } } @@ -73,7 +73,7 @@ extension AIChatViewModel { private func snapshotCurrentConversation() -> AIConversation? { guard !messages.isEmpty else { return nil } - let wireMessages = messages.map { $0.wireSnapshot } + let wireMessages = ChatToolCallRepair.settlingPendingApprovals(messages.map { $0.wireSnapshot }) if let existingID = activeConversationID { var conversation = conversations.first(where: { $0.id == existingID }) diff --git a/apple/TablePro/ViewModels/AIChatViewModel+Streaming.swift b/apple/TablePro/ViewModels/AIChatViewModel+Streaming.swift index b24a0a9a6a..cdb7924280 100644 --- a/apple/TablePro/ViewModels/AIChatViewModel+Streaming.swift +++ b/apple/TablePro/ViewModels/AIChatViewModel+Streaming.swift @@ -383,7 +383,8 @@ extension AIChatViewModel { model: resolved.model ), sessionId: sessionId, - connectionId: connection?.id + connectionId: connection?.id, + chatMode: chatMode ) ) diff --git a/apple/TablePro/ViewModels/AIChatViewModel+ToolApproval.swift b/apple/TablePro/ViewModels/AIChatViewModel+ToolApproval.swift index 3ff38336b6..45a1770230 100644 --- a/apple/TablePro/ViewModels/AIChatViewModel+ToolApproval.swift +++ b/apple/TablePro/ViewModels/AIChatViewModel+ToolApproval.swift @@ -87,22 +87,24 @@ extension AIChatViewModel { sessionId: sessionId, toolUseId: block.id ) - let finalState: ToolApprovalState + let decidedState: ToolApprovalState switch decision { case .run: - finalState = .approved - explicitlyApproved.insert(block.id) + decidedState = .approved case .alwaysAllow: await MainActor.run { [weak self] in self?.persistAlwaysAllowed(toolName: block.name) } - finalState = .approved - explicitlyApproved.insert(block.id) + decidedState = .approved case .cancel: - finalState = .cancelled + decidedState = .cancelled } - await MainActor.run { [weak self] in - self?.updateApprovalState(blockID: block.id, newState: finalState, assistantID: assistantID) + let finalState = await MainActor.run { [weak self] in + self?.updateApprovalState(blockID: block.id, newState: decidedState, assistantID: assistantID) + ?? .cancelled + } + if case .approved = finalState { + explicitlyApproved.insert(block.id) } resolved.append(ToolUseBlock( id: block.id, @@ -149,7 +151,7 @@ extension AIChatViewModel { return .pending } - if let connection, connection.aiAlwaysAllowedTools.contains(toolName) { + if let connection, alwaysAllowedTools(for: connection).contains(toolName) { return .approved } if let connection { @@ -171,6 +173,14 @@ extension AIChatViewModel { DatabaseManager.shared.session(for: connection.id)?.safeModeLevel ?? connection.safeModeLevel } + // `connection` is read once per pane, so a grant forgotten in the form or changed by sync after + // that is only in storage. + @MainActor + private func alwaysAllowedTools(for connection: DatabaseConnection) -> Set { + services.connectionStorage.loadConnection(id: connection.id)?.aiAlwaysAllowedTools + ?? connection.aiAlwaysAllowedTools + } + @MainActor func appendPendingToolUseBlocks(_ blocks: [ToolUseBlock], assistantID: UUID) { guard let turn = turn(withID: assistantID) else { return } @@ -179,27 +189,38 @@ extension AIChatViewModel { } } + // Returns the state the card holds afterwards. Stop settles a card before a Run click already + // in flight lands, and New Conversation or a switch drops its turn: either way nothing runs. @MainActor - func updateApprovalState(blockID: String, newState: ToolApprovalState, assistantID: UUID) { - guard let turn = turn(withID: assistantID) else { return } + @discardableResult + func updateApprovalState(blockID: String, newState: ToolApprovalState, assistantID: UUID) -> ToolApprovalState { + guard let turn = turn(withID: assistantID) else { return .cancelled } for chatBlock in turn.blocks { if case .toolUse(var block) = chatBlock.kind, block.id == blockID { + if case .cancelled = block.approvalState { return .cancelled } block.approvalState = newState chatBlock.setKind(.toolUse(block)) - return + return newState } } + return .cancelled } - /// Records an Always Allow grant as a read-modify-write of the one field it changes. - /// - /// It used to save the whole `DatabaseConnection` the view model was holding. That record is - /// read once, in `AIChatPanelView`'s `onAppear` and again only when the connection's id changes, - /// so a Safe Mode level raised in the toolbar while the pane stayed mounted was still the old - /// one here, and the grant wrote it back and silently undid the change. - /// - /// A destructive operation is never granted: each DROP, TRUNCATE and ALTER...DROP is confirmed - /// on its own. + // The task marks a stopped card only after a hop back to the main actor, by which time the + // transcript has been saved or replaced. + @MainActor + func settlePendingToolApprovals() { + for turn in messages { + for block in turn.blocks { + guard case .toolUse(var use) = block.kind, case .pending = use.approvalState else { continue } + use.approvalState = .cancelled + block.setKind(.toolUse(use)) + } + } + } + + // Writes only this field: the view model's copy of the connection can be stale. A destructive + // or remote tool is never granted. @MainActor func persistAlwaysAllowed(toolName: String) { if ChatToolRegistry.shared.tool(named: toolName)?.mode == .agentOnly { @@ -209,7 +230,6 @@ extension AIChatViewModel { return } guard let connectionId = connection?.id else { return } - guard connection?.aiAlwaysAllowedTools.contains(toolName) == false else { return } guard services.connectionStorage.mutateConnections(ids: [connectionId], { stored in stored.aiAlwaysAllowedTools.insert(toolName) }) else { @@ -265,18 +285,20 @@ extension AIChatViewModel { sessionId: sessionId, toolUseId: block.id ) + let decidedState: ToolApprovalState switch decision { case .run: - finalState = .approved - approvalWasExplicit = true + decidedState = .approved case .alwaysAllow: persistAlwaysAllowed(toolName: block.name) - finalState = .approved - approvalWasExplicit = true + decidedState = .approved case .cancel: - finalState = .cancelled + decidedState = .cancelled + } + finalState = updateApprovalState(blockID: block.id, newState: decidedState, assistantID: assistantID) + if case .approved = finalState { + approvalWasExplicit = true } - updateApprovalState(blockID: block.id, newState: finalState, assistantID: assistantID) } else { finalState = initialState } diff --git a/apple/TablePro/ViewModels/AIChatViewModel.swift b/apple/TablePro/ViewModels/AIChatViewModel.swift index 04b481e4ab..6883b67a75 100644 --- a/apple/TablePro/ViewModels/AIChatViewModel.swift +++ b/apple/TablePro/ViewModels/AIChatViewModel.swift @@ -285,6 +285,7 @@ final class AIChatViewModel: ObservableObject { streamingTask?.cancel() streamingTask = nil ToolApprovalCenter.shared.cancelAll(sessionId: sessionId) + settlePendingToolApprovals() if case .streaming(let assistantID) = streamingState, let idx = messages.firstIndex(where: { $0.id == assistantID }) { @@ -352,7 +353,7 @@ final class AIChatViewModel: ObservableObject { AIProviderFactory.resetCopilotConversation(sessionId: sessionId) cancelStream() persistCurrentConversation() - messages = conversation.messages.map { ChatTurn(wire: $0) } + messages = ChatToolCallRepair.settlingPendingApprovals(conversation.messages).map { ChatTurn(wire: $0) } activeConversationID = conversation.id clearError() } diff --git a/apple/TablePro/ViewModels/DashboardTileBoard.swift b/apple/TablePro/ViewModels/DashboardTileBoard.swift index 780137070e..a4af9501be 100644 --- a/apple/TablePro/ViewModels/DashboardTileBoard.swift +++ b/apple/TablePro/ViewModels/DashboardTileBoard.swift @@ -51,16 +51,7 @@ final class DashboardTileBoard: ObservableObject { ) }, safeModeLevel: { connectionId in - let connectionLevel: SafeModeLevel - switch services.databaseManager.connectionState(connectionId) { - case .live(_, let session): connectionLevel = session.safeModeLevel - case .stored(let connection): connectionLevel = connection.safeModeLevel - case .unknown: connectionLevel = .silent - } - return ManagedPolicyResolver.effectiveSafeModeLevel( - connectionLevel: connectionLevel, - policy: ManagedPolicyReader.shared - ) + ExecutionGateProvider.effectiveSafeModeLevel(for: connectionId, in: services.databaseManager) }, chartsAvailable: { services.featureAccess.isAvailable(.resultCharts) }, nullDisplay: { services.appSettings.dataGrid.nullDisplay }, diff --git a/apple/TablePro/ViewModels/ServerDashboardViewModel.swift b/apple/TablePro/ViewModels/ServerDashboardViewModel.swift index 2ad773a298..d95dcfe9d8 100644 --- a/apple/TablePro/ViewModels/ServerDashboardViewModel.swift +++ b/apple/TablePro/ViewModels/ServerDashboardViewModel.swift @@ -1,6 +1,7 @@ import Combine import Foundation import os +import TableProPluginKit @MainActor final class ServerDashboardViewModel: ObservableObject { @@ -180,15 +181,25 @@ final class ServerDashboardViewModel: ObservableObject { } } - /// A saved query can return a whole table, so a tile stops at the rows it can draw. A driver - /// with no bounded fetch gets `executeRead`, which is also where a scripting engine refuses writes. + // A saved query can return a whole table, so a tile stops at the rows it can draw. A driver with no bounded fetch + // gets `executeRead`, which is also where a scripting engine refuses writes. At Read-Only, a driver that can runs + // the saved query in the engine's read-only mode. private static func tileExecutor(connectionId: UUID, services: AppServices) -> DashboardTileBoard.Execute { { [connectionId, services] query in let driver = try liveDriver(connectionId: connectionId, services: services) let policy = await MainActor.run { services.databaseManager.session(for: connectionId)?.connection.maskingPolicy ?? .none } - if let bounded = try await driver.executeBoundedQuery(query: query, rowCap: DashboardTileTable.rowCap) { + let rowCap = DashboardTileTable.rowCap + let isReadOnly = ExecutionGateProvider.runsAtReadOnly(connectionId, in: services.databaseManager) + if let context = driver.readOnlyRunContext(safeModeIsReadOnly: isReadOnly) { + if let bounded = try await driver.executeBoundedQuery(query: query, rowCap: rowCap, context: context) { + return bounded.masked(by: policy) + } + return try await driver.executeUserQuery(query: query, rowCap: rowCap, parameters: nil, context: context) + .masked(by: policy) + } + if let bounded = try await driver.executeBoundedQuery(query: query, rowCap: rowCap) { return bounded.masked(by: policy) } return try await driver.executeRead(query: query).masked(by: policy) diff --git a/apple/TablePro/Views/AIChat/AIChatPanelView.swift b/apple/TablePro/Views/AIChat/AIChatPanelView.swift index 4f4a0c1ee9..4f2cfef58c 100644 --- a/apple/TablePro/Views/AIChat/AIChatPanelView.swift +++ b/apple/TablePro/Views/AIChat/AIChatPanelView.swift @@ -29,12 +29,14 @@ struct AIChatPanelView: View { settingsManager.ai.hasActiveProvider } - /// The first call still waiting, in transcript order, which is the only one Return may answer. + // Only a card a turn still waits on may take Return and Escape. private var primaryPendingToolUseId: String? { for turn in viewModel.messages { for block in turn.blocks { guard case .toolUse(let useBlock) = block.kind, - case .pending = useBlock.approvalState else { continue } + case .pending = useBlock.approvalState, + ToolApprovalCenter.shared.isAwaiting(sessionId: viewModel.sessionId, toolUseId: useBlock.id) + else { continue } return useBlock.id } } diff --git a/apple/TablePro/Views/AIChat/AIChatToolUseBlockView.swift b/apple/TablePro/Views/AIChat/AIChatToolUseBlockView.swift index 948fd87f6c..c49084b81c 100644 --- a/apple/TablePro/Views/AIChat/AIChatToolUseBlockView.swift +++ b/apple/TablePro/Views/AIChat/AIChatToolUseBlockView.swift @@ -13,6 +13,7 @@ struct AIChatToolUseBlockView: View { @State private var isExpanded: Bool = false @Environment(\.chatApprovalConnectionName) private var connectionName + @Environment(\.chatApprovalSessionId) private var sessionId internal static let inputFont = Font.system(.caption, design: .monospaced) @@ -21,6 +22,12 @@ struct AIChatToolUseBlockView: View { return false } + // A click on a card no turn waits on is dropped, so its buttons are not drawn. + private var isAwaitingAnswer: Bool { + guard isPending, let sessionId else { return false } + return ToolApprovalCenter.shared.isAwaiting(sessionId: sessionId, toolUseId: block.id) + } + private var shouldShowInput: Bool { hasInput && (isExpanded || (isPending && proposedStatement == nil)) } @@ -79,7 +86,7 @@ struct AIChatToolUseBlockView: View { CodeWell(prettyInput, font: Self.inputFont) } - if case .pending = block.approvalState { + if isAwaitingAnswer { ToolApprovalActionsRow( toolUseId: block.id, toolName: block.name, diff --git a/apple/TablePro/Views/ConnectionForm/ConnectionFormCoordinator.swift b/apple/TablePro/Views/ConnectionForm/ConnectionFormCoordinator.swift index 963a6e019b..ae7ffd9afc 100644 --- a/apple/TablePro/Views/ConnectionForm/ConnectionFormCoordinator.swift +++ b/apple/TablePro/Views/ConnectionForm/ConnectionFormCoordinator.swift @@ -384,7 +384,8 @@ final class ConnectionFormCoordinator: ObservableObject { localOnly: advanced.localOnly, additionalFields: fields, ownedAdditionalFieldIDs: ownedAdditionalFieldIDs(), - maskingRules: maskingRules.savedRules + maskingRules: maskingRules.savedRules, + revokedAIAlwaysAllowedTools: advanced.revokedAIAlwaysAllowedTools ) } diff --git a/apple/TablePro/Views/ConnectionForm/Panes/OptionsPaneView.swift b/apple/TablePro/Views/ConnectionForm/Panes/OptionsPaneView.swift index c188a6dffb..211f50348d 100644 --- a/apple/TablePro/Views/ConnectionForm/Panes/OptionsPaneView.swift +++ b/apple/TablePro/Views/ConnectionForm/Panes/OptionsPaneView.swift @@ -364,6 +364,9 @@ struct OptionsPaneView: View { .tag(AIConnectionPolicy?.some(policy) as AIConnectionPolicy?) } } + ForEach(coordinator.advanced.aiAlwaysAllowedTools, id: \.self) { tool in + alwaysAllowedRow(tool) + } } Picker(String(localized: "External Clients"), selection: $coordinator.advanced.externalAccess) { ForEach(ExternalAccessLevel.allCases) { level in @@ -378,6 +381,18 @@ struct OptionsPaneView: View { } } + private func alwaysAllowedRow(_ tool: String) -> some View { + LabeledContent { + Button(String(localized: "Forget")) { + coordinator.advanced.forgetAlwaysAllowed(tool) + } + .accessibilityLabel(String(format: String(localized: "Forget %@"), tool)) + } label: { + Text(verbatim: tool) + Text(String(localized: "Always allowed")) + } + } + @ViewBuilder private var safeModeRow: some View { let levels = SafeModeFloor.levels(allowedBy: coordinator.safeModeFloor) @@ -401,6 +416,9 @@ struct OptionsPaneView: View { if aiIsEnabled { // swiftlint:disable:next line_length Text(String(localized: "AI Policy controls in-app AI agents. External Clients controls Raycast, Cursor, Claude Desktop, other MCP clients, AppleScript, and Shortcuts. Effective scope is the minimum of the requesting token's scope and the External Clients level.")) + if !coordinator.advanced.aiAlwaysAllowedTools.isEmpty { + Text(String(localized: "Always Allow on an assistant tool card adds the tool here. Forget makes its next call ask again.")) + } } else { // swiftlint:disable:next line_length Text(String(localized: "Controls how external clients (Raycast, Cursor, Claude Desktop, AppleScript, Shortcuts) access this connection. Tokens cannot exceed this level even with full-access scope.")) diff --git a/apple/TablePro/Views/ConnectionForm/Support/ConnectionFormEdits.swift b/apple/TablePro/Views/ConnectionForm/Support/ConnectionFormEdits.swift index 21fcb4ee5f..5dc067ce1c 100644 --- a/apple/TablePro/Views/ConnectionForm/Support/ConnectionFormEdits.swift +++ b/apple/TablePro/Views/ConnectionForm/Support/ConnectionFormEdits.swift @@ -41,6 +41,7 @@ struct ConnectionFormEdits: Equatable { var additionalFields: [String: String] var ownedAdditionalFieldIDs: Set var maskingRules: [MaskingRule] = [] + var revokedAIAlwaysAllowedTools: Set = [] static let appManagedAdditionalFieldIDs: Set = [ "preConnectScript", @@ -90,6 +91,8 @@ struct ConnectionFormEdits: Equatable { result.binaryInlineLimitKilobytes = binaryInlineLimitKilobytes result.completionIdentifierCase = completionIdentifierCase result.maskingRules = maskingRules + // Subtract, never assign: a grant recorded while the form was open stays. + result.aiAlwaysAllowedTools.subtract(revokedAIAlwaysAllowedTools) return result } diff --git a/apple/TablePro/Views/ConnectionForm/ViewModels/AdvancedPaneViewModel.swift b/apple/TablePro/Views/ConnectionForm/ViewModels/AdvancedPaneViewModel.swift index 1b85ee7181..28de5c3540 100644 --- a/apple/TablePro/Views/ConnectionForm/ViewModels/AdvancedPaneViewModel.swift +++ b/apple/TablePro/Views/ConnectionForm/ViewModels/AdvancedPaneViewModel.swift @@ -18,6 +18,8 @@ final class AdvancedPaneViewModel: ObservableObject { @Published var externalAccess: ExternalAccessLevel = .readOnly @Published var localOnly: Bool = false @Published var aiPolicy: AIConnectionPolicy? + @Published private(set) var aiAlwaysAllowedTools: [String] = [] + @Published private(set) var revokedAIAlwaysAllowedTools: Set = [] @Published var coordinator: WeakCoordinatorRef? @@ -139,10 +141,17 @@ final class AdvancedPaneViewModel: ObservableObject { postDisconnectScript = connection.postDisconnectScript ?? "" scriptTimeoutSeconds = connection.scriptTimeoutSeconds aiPolicy = connection.aiPolicy + aiAlwaysAllowedTools = connection.aiAlwaysAllowedTools.sorted() + revokedAIAlwaysAllowedTools = [] externalAccess = connection.externalAccess localOnly = connection.localOnly } + func forgetAlwaysAllowed(_ tool: String) { + aiAlwaysAllowedTools.removeAll { $0 == tool } + revokedAIAlwaysAllowedTools.insert(tool) + } + func write(into fields: inout [String: String]) { for (key, value) in additionalFieldValues { fields[key] = value diff --git a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Explain.swift b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Explain.swift index 067f3496e6..30bacd1990 100644 --- a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Explain.swift +++ b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Explain.swift @@ -115,6 +115,7 @@ extension MainContentCoordinator { let (claim, lease) = beginTabExecution(for: tabId) let conn = connection + let safeModeIsReadOnly = runsAtReadOnly tabManager.mutate(tabId: tabId) { $0.execution.errorMessage = nil } @@ -136,7 +137,7 @@ extension MainContentCoordinator { ) } return try await queryExecutor.executeQuery( - driver: driver, sql: request.sql, parameters: nil, rowCap: nil + driver: driver, sql: request.sql, parameters: nil, rowCap: nil, safeModeIsReadOnly: safeModeIsReadOnly ) } } diff --git a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+ReadOnlySafeMode.swift b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+ReadOnlySafeMode.swift new file mode 100644 index 0000000000..65911a3449 --- /dev/null +++ b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+ReadOnlySafeMode.swift @@ -0,0 +1,53 @@ +// +// MainContentCoordinator+ReadOnlySafeMode.swift +// TablePro +// + +import Foundation + +extension MainContentCoordinator { + // The gate's own answer, read as a run starts, so every read the app sends for the user carries the level its + // approval was given under: a view or a row security policy can call a writing function with no call in the text. + var runsAtReadOnly: Bool { + ExecutionGateProvider.runsAtReadOnly(connectionId, in: services.databaseManager) + } + + // What the tab's query was built from, and what its next page or count runs. + static func browseFilters(_ filterState: TabFilterState) -> [TableFilter] { + filterState.executedFilters + filterState.rerunFilters.filters + } + + static func rawFilterExpressions(_ filters: [TableFilter]) -> [String] { + filters.filter { $0.isEnabled && $0.isRawSQL }.map(\.value) + } + + private static func rawFilterKey(_ filters: [TableFilter]) -> String { + Set(rawFilterExpressions(filters)).sorted().joined(separator: "\n") + } + + // A raw filter is text the user typed, inside a query no gate reads below the Full levels. At Read-Only one that + // calls a function goes to the gate before the browse or a count sends it, and the gate asks where the driver cannot + // hold the engine to read-only. Once per filter text and tab: its page turns, sorts and counts run what was approved. + func rawFilterNeedsReadOnlyGate(_ filters: [TableFilter], tabId: UUID) -> Bool { + let expressions = Self.rawFilterExpressions(filters) + guard !expressions.isEmpty, runsAtReadOnly, approvedReadOnlyRawFilters[tabId] != Self.rawFilterKey(filters) + else { return false } + return expressions.contains { ReadOnlyFunctionCalls.callsUnlistedFunction($0, databaseType: connection.type) } + } + + func approveReadOnlyRawFilter(_ filters: [TableFilter], tabId: UUID) { + approvedReadOnlyRawFilters[tabId] = Self.rawFilterKey(filters) + } + + func rawFilterRequest(sql: String, canPrompt: Bool) -> OperationRequest { + OperationRequest( + connectionId: connectionId, + databaseType: connection.type, + sql: sql, + kind: .readQuery, + caller: .userInterface, + capabilities: canPrompt ? .interactiveUser : CallerCapabilities.interactiveUser.union(.cannotPrompt), + operationDescription: String(localized: "Execute Query") + ) + } +} diff --git a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+UnloadedValues.swift b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+UnloadedValues.swift index 90b35b5799..1448717a5a 100644 --- a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+UnloadedValues.swift +++ b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+UnloadedValues.swift @@ -17,6 +17,8 @@ extension MainContentCoordinator { !tab.tableContext.primaryKeyColumns.isEmpty, let scope = scope(for: tab) else { return nil } let builder = queryBuilder + // A view can call a function that writes, so the value is read the way the browse was. + let safeModeIsReadOnly = runsAtReadOnly return UnloadedValueReader( tableName: tableName, schemaName: tab.tableContext.schemaName, @@ -31,10 +33,18 @@ extension MainContentCoordinator { isTableTab: true, lease: lease ) { driver in - try await driver.executeUserQuery( + guard let context = driver.readOnlyRunContext(safeModeIsReadOnly: safeModeIsReadOnly) else { + return try await driver.executeUserQuery( + query: statement.sql, + rowCap: nil, + parameters: statement.parameters + ).rows + } + return try await driver.executeUserQuery( query: statement.sql, rowCap: nil, - parameters: statement.parameters + parameters: statement.parameters, + context: context ).rows } } diff --git a/apple/TablePro/Views/Main/MainContentCoordinator.swift b/apple/TablePro/Views/Main/MainContentCoordinator.swift index d603b2f471..e4c0863ab2 100644 --- a/apple/TablePro/Views/Main/MainContentCoordinator.swift +++ b/apple/TablePro/Views/Main/MainContentCoordinator.swift @@ -173,6 +173,8 @@ final class MainContentCoordinator: ObservableObject { let tabSessionRegistry: TabSessionRegistry let queryExecutor: QueryExecutor internal var executionGate: any ExecutionGate = ExecutionGateProvider.shared + // The raw filter text each tab had approved at Read-Only. + var approvedReadOnlyRawFilters: [UUID: String] = [:] let windowSidebarState: WindowSidebarState // MARK: - Services @@ -1111,9 +1113,10 @@ final class MainContentCoordinator: ObservableObject { } let level = safeModeLevel - if level.appliesToAllQueries && level.requiresConfirmation, - tab.execution.lastExecutedAt == nil - { + let asksBeforeFirstLoad = level.appliesToAllQueries && level.requiresConfirmation + && tab.execution.lastExecutedAt == nil + let asksAboutRawFilter = rawFilterNeedsReadOnlyGate(Self.browseFilters(tab.filterState), tabId: tab.id) + if asksBeforeFirstLoad || asksAboutRawFilter { guard !isShowingSafeModePrompt else { turnAwayTableLoad(tabId: tab.id, trigger: trigger) return nil @@ -1150,6 +1153,7 @@ final class MainContentCoordinator: ObservableObject { } switch decision { case .authorized: + if asksAboutRawFilter { approveReadOnlyRawFilter(Self.browseFilters(tab.filterState), tabId: tabId) } executeQueryInternal( sql, isAutoLoad: true, trigger: trigger, viewport: viewport, source: .sameSource, onTab: tabId ) @@ -1275,6 +1279,7 @@ final class MainContentCoordinator: ObservableObject { let isTableTab = tab.tabType == .table let readsView = tab.tableContext.knowsView(named: tableName) let (grammar, lengthColumns) = (lexicalGrammar, unloadedValueLengthColumns(for: tab)) + let safeModeIsReadOnly = runsAtReadOnly let failureOutput = ServerOutputBox() let queryTask = Task { [weak self] in @@ -1300,6 +1305,7 @@ final class MainContentCoordinator: ObservableObject { sql: statement.sql, parameters: nil, rowCap: rowCap, + safeModeIsReadOnly: safeModeIsReadOnly, capturingOutputInto: isTableTab ? nil : failureOutput ) let edits = isAutoLoad ? nil : await SucceededStatements.single( diff --git a/apple/TableProMobile/TableProMobileTests/Drivers/DuckDBSafeModeReadOnlyTests.swift b/apple/TableProMobile/TableProMobileTests/Drivers/DuckDBSafeModeReadOnlyTests.swift new file mode 100644 index 0000000000..17535c2a15 --- /dev/null +++ b/apple/TableProMobile/TableProMobileTests/Drivers/DuckDBSafeModeReadOnlyTests.swift @@ -0,0 +1,117 @@ +// +// DuckDBSafeModeReadOnlyTests.swift +// TableProMobileTests +// +// Plugins/DuckDBDriverPlugin/DuckDBReadOnlyStatement.swift, compiled into this target: the Mac plugin is a registry +// plugin with no test bundle of its own. +// + +import CDuckDB +import Foundation +import TableProPluginKit +import Testing + +struct DuckDBSafeModeReadOnlyTests { + private struct Refused: Error {} + + private final class Session { + private var database: duckdb_database? + private var connection: duckdb_connection? + + init() throws { + guard duckdb_open_ext(nil, &database, nil, nil) != DuckDBError, + duckdb_connect(database, &connection) != DuckDBError + else { throw Refused() } + try run("CREATE TABLE kept (a INTEGER)") + try run("INSERT INTO kept VALUES (1)") + } + + deinit { + duckdb_disconnect(&connection) + duckdb_close(&database) + } + + var handle: duckdb_connection { + get throws { + guard let connection else { throw Refused() } + return connection + } + } + + func run(_ sql: String) throws { + var result = duckdb_result() + defer { duckdb_destroy_result(&result) } + guard duckdb_query(try handle, sql, &result) != DuckDBError else { throw Refused() } + } + + // The CAST fails unless the count matches, so the check needs no value reading. + func holds(_ rows: Int) -> Bool { + (try? run("SELECT CAST(CASE WHEN count(*) = \(rows) THEN '1' ELSE 'x' END AS INTEGER) FROM kept")) != nil + } + } + + @Test("An external read names external clients, Read-Only from any origin names Safe Mode, an app read alone neither") + func reasonFollowsTheContext() { + var externalRead = PluginStatementContext() + externalRead.readOnly = true + externalRead.origin = .external + var externalSafeModeRead = externalRead + externalSafeModeRead.enforcesReadOnly = true + var externalWrite = PluginStatementContext() + externalWrite.origin = .external + externalWrite.enforcesReadOnly = true + var catalogRead = PluginStatementContext() + catalogRead.readOnly = true + catalogRead.readsCatalog = true + + #expect(DuckDBReadOnlyStatement.reason(for: externalRead) == .externalClients) + #expect(DuckDBReadOnlyStatement.reason(for: externalSafeModeRead) == .externalClients) + #expect(DuckDBReadOnlyStatement.reason(for: PluginStatementContext(enforcesReadOnly: true)) == .safeMode) + #expect(DuckDBReadOnlyStatement.reason(for: externalWrite) == .safeMode) + #expect(DuckDBReadOnlyStatement.reason(for: catalogRead) == nil) + #expect(DuckDBReadOnlyStatement.reason(for: PluginStatementContext()) == nil) + } + + @Test("At Read-Only a write is refused inside the read-only transaction, and the table keeps its rows") + func safeModeRefusesWrites() throws { + let session = try Session() + + for sql in ["INSERT INTO kept VALUES (2)", "UPDATE kept SET a = 5", "CREATE TABLE added (a INTEGER)"] { + #expect(throws: Refused.self, "\(sql)") { + try DuckDBReadOnlyStatement.run(on: try session.handle, session: .idle, reason: .safeMode) { + try session.run(sql) + } + } + } + let read = try DuckDBReadOnlyStatement.run(on: try session.handle, session: .idle, reason: .safeMode) { + session.holds(1) + } + + #expect(read) + try session.run("INSERT INTO kept VALUES (3)") + #expect(session.holds(2)) + } + + @Test( + "At Read-Only a session not known to be idle is refused before anything runs, naming Safe Mode", + arguments: [PluginSessionTransactionState.inTransaction, .abortedTransaction, .unknown] + ) + func safeModeRefusesUnlessIdle(state: PluginSessionTransactionState) throws { + let session = try Session() + try session.run("BEGIN TRANSACTION") + try session.run("INSERT INTO kept VALUES (500)") + + var ran = false + #expect(throws: DuckDBReadOnlyStatement.Refusal.safeModeTransactionOpen) { + try DuckDBReadOnlyStatement.run(on: try session.handle, session: state, reason: .safeMode) { ran = true } + } + + #expect(!ran) + #expect( + DuckDBReadOnlyStatement.Refusal.safeModeTransactionOpen.pluginErrorMessage + != DuckDBReadOnlyStatement.Refusal.transactionOpen.pluginErrorMessage + ) + try session.run("COMMIT") + #expect(session.holds(2)) + } +} diff --git a/apple/TableProTests/Core/AI/AgentCLIProcessTests.swift b/apple/TableProTests/Core/AI/AgentCLIProcessTests.swift index a2480ec0d5..2f6dbe4a47 100644 --- a/apple/TableProTests/Core/AI/AgentCLIProcessTests.swift +++ b/apple/TableProTests/Core/AI/AgentCLIProcessTests.swift @@ -9,6 +9,29 @@ struct AgentCLIProcessTests { environment: [:] ) + static let perl = "/usr/bin/perl" + + static var hasPerl: Bool { + FileManager.default.isExecutableFile(atPath: perl) + } + + private static func perlLaunch(_ script: String) -> AgentCLILaunch { + AgentCLILaunch(executable: URL(fileURLWithPath: perl), arguments: ["-e", script], environment: [:]) + } + + // Two lines half a second apart from a tool that then keeps running with stderr open and quiet. + private static let twoLinesThenWait = + #"$| = 1; print "one\n"; select(undef, undef, undef, 0.5); print "two\n"; sleep 120;"# + + private static func firstLines(_ count: Int, of script: String) async throws -> [String] { + var received: [String] = [] + for try await line in AgentCLIProcess(label: "test").streamLines(perlLaunch(script)) { + received.append(line) + if received.count == count { break } + } + return received + } + @Test("A launch the launcher refuses fails as a launch failure instead of running") func refusedLaunchFailsTheRun() async { await #expect(throws: AgentCLIError.self) { @@ -22,4 +45,61 @@ struct AgentCLIProcessTests { for try await _ in AgentCLIProcess(label: "claude").streamLines(shellLaunch) {} } } + + @Test( + "Each line arrives while the tool still runs", + .enabled(if: AgentCLIProcessTests.hasPerl), + .timeLimit(.minutes(1)) + ) + func linesStreamBeforeExit() async throws { + #expect(try await Self.firstLines(2, of: Self.twoLinesThenWait) == ["one", "two"]) + } + + @Test( + "Output larger than the pipe buffer is read through while the tool still runs", + .enabled(if: AgentCLIProcessTests.hasPerl), + .timeLimit(.minutes(1)) + ) + func outputPastThePipeBuffer() async throws { + let script = #"$| = 1; print(("x" x 99) . "\n") for 1 .. 3000; sleep 120;"# + var count = 0 + for try await line in AgentCLIProcess(label: "test").streamLines(Self.perlLaunch(script)) { + #expect(line.count == 99) + count += 1 + if count == 3000 { break } + } + #expect(count == 3000) + } + + @Test( + "The stream ends at end of output and keeps a last line with no newline", + .enabled(if: AgentCLIProcessTests.hasPerl), + .timeLimit(.minutes(1)) + ) + func lastLineWithoutNewline() async throws { + var received: [String] = [] + for try await line in AgentCLIProcess(label: "test").streamLines(Self.perlLaunch(#"print "a\r\n\nb";"#)) { + received.append(line) + } + #expect(received == ["a", "b"]) + } + + @Test( + "An idle language server does not hold the chat's output", + .enabled(if: AgentCLIProcessTests.hasPerl), + .timeLimit(.minutes(1)) + ) + func idleLanguageServerDoesNotHoldTheStream() async throws { + let transport = LSPTransport() + try await transport.start(executablePath: Self.perl, arguments: ["-e", "sleep 120"]) + let received: [String] + do { + received = try await Self.firstLines(2, of: Self.twoLinesThenWait) + } catch { + await transport.stop() + throw error + } + await transport.stop() + #expect(received == ["one", "two"]) + } } diff --git a/apple/TableProTests/Core/AI/Chat/ListConnectionsChatToolTests.swift b/apple/TableProTests/Core/AI/Chat/ListConnectionsChatToolTests.swift new file mode 100644 index 0000000000..dde0ff317a --- /dev/null +++ b/apple/TableProTests/Core/AI/Chat/ListConnectionsChatToolTests.swift @@ -0,0 +1,97 @@ +// +// ListConnectionsChatToolTests.swift +// TableProTests +// + +import Foundation +@testable import TablePro +import TableProMCP +import TableProPluginKit +import Testing + +struct ListConnectionsChatToolTests { + private let session = UUID() + private let other = UUID() + private let third = UUID() + + private func context( + connectionId: UUID?, + policy: AIConnectionPolicy = .askEachTime + ) -> ChatToolContext { + ChatToolContext( + connectionId: connectionId, + bridge: MCPConnectionBridge(), + authPolicy: MCPAuthPolicy(), + policyLookup: { _ in policy } + ) + } + + private func connection(id: UUID, name: String, host: String) -> DatabaseConnection { + var connection = TestFixtures.makeConnection(id: id, name: name, database: "\(name)_db", type: .postgresql) + connection.host = host + return connection + } + + @Test("A conversation with no connection attached lists nothing") + func noSessionConnectionIsRefused() async { + await #expect(throws: ChatToolTargetError.noConnection) { + _ = try await ListConnectionsChatTool().execute(input: .object([:]), context: context(connectionId: nil)) + } + } + + @Test("A connection whose AI policy is Never is refused") + func neverPolicyIsRefused() async { + await #expect(throws: ChatToolAuthorizationError.self) { + _ = try await ListConnectionsChatTool().execute( + input: .object([:]), + context: context(connectionId: session, policy: .never) + ) + } + } + + @Test("The tool lists no connection other than the conversation's own") + func toolListsOnlyTheSessionConnection() async throws { + let result = try await ListConnectionsChatTool().execute( + input: .object([:]), + context: context(connectionId: session) + ) + let data = try #require(result.content.data(using: .utf8)) + let payload = try JSONDecoder().decode(JsonValue.self, from: data) + let listed = try #require(payload["connections"]?.arrayValue) + + for entry in listed { + #expect(entry["id"]?.stringValue == session.uuidString) + } + } + + @Test("The listing carries the session's connection and no other host or name") + func listingHoldsOnlyTheSessionEntry() throws { + let listings = ExternalConnectionDirectory.listings( + connections: [ + connection(id: session, name: "dev", host: "localhost"), + connection(id: other, name: "prod_payments", host: "db1.internal.corp"), + connection(id: third, name: "ledger", host: "10.0.0.5") + ], + groups: [], + tags: [], + live: [:], + defaultPolicy: .askEachTime + ) + + let payload = MCPConnectionBridge.connectionListing(listings, id: session) + let listed = try #require(payload["connections"]?.arrayValue) + let text = payload.jsonString() + + #expect(listed.count == 1) + #expect(listed.first?["id"]?.stringValue == session.uuidString) + #expect(!text.contains("prod_payments")) + #expect(!text.contains("db1.internal.corp")) + #expect(!text.contains("ledger")) + #expect(!text.contains("10.0.0.5")) + } + + @Test("The tool's description no longer promises every saved connection") + func descriptionNamesTheSessionConnection() { + #expect(!ListConnectionsChatTool().description.localizedCaseInsensitiveContains("all saved")) + } +} diff --git a/apple/TableProTests/Core/AI/ClaudeAgentAskProfileTests.swift b/apple/TableProTests/Core/AI/ClaudeAgentAskProfileTests.swift new file mode 100644 index 0000000000..0198e9a29e --- /dev/null +++ b/apple/TableProTests/Core/AI/ClaudeAgentAskProfileTests.swift @@ -0,0 +1,225 @@ +// +// ClaudeAgentAskProfileTests.swift +// TableProTests +// + +import Foundation +import TableProMCP +import TableProMCPTestSupport +import Testing + +@testable import TablePro + +private final class AskProfileCredentials: MCPTokenCredentialStoring, @unchecked Sendable { + private let lock = NSLock() + private var stored: Data? + + init(stored: Data? = Data("[]".utf8)) { + self.stored = stored + } + + func read() -> MCPTokenBlob { + lock.withLock { stored.map(MCPTokenBlob.found) ?? .missing } + } + + @discardableResult + func write(_ data: Data) -> Bool { + lock.withLock { stored = data } + return true + } + + func delete() { + lock.withLock { stored = nil } + } +} + +struct ClaudeAgentAskProfileTests { + private static let askToolNames: Set = [ + "list_connections", + "get_connection_status", + "list_databases", + "list_schemas", + "list_tables", + "describe_table", + "get_table_ddl" + ] + + private let connectionId = UUID() + + private func mint( + in store: MCPTokenStore, + toolProfile: MCPToolProfile? + ) async throws -> (token: MCPAuthToken, plaintext: String) { + try await store.generate( + name: "__claude_agent__", + permissions: .readOnly, + connectionAccess: .limited([connectionId]), + expiresAt: Date.now.addingTimeInterval(900), + isBridgeCredential: false, + isClaudeAgentCredential: true, + extraScopes: [], + toolProfile: toolProfile + ) + } + + private func authenticate(_ plaintext: String, against store: MCPTokenStore) async -> MCPAuthDecision { + let clock = MCPTestClock() + let authenticator = MCPBearerTokenAuthenticator( + tokenStore: store, + rateLimiter: MCPRateLimiter(clock: clock), + clock: clock + ) + return await authenticator.authenticate(authorizationHeader: "Bearer \(plaintext)", clientAddress: .loopback) + } + + private func principal(presenting plaintext: String, to store: MCPTokenStore) async throws -> MCPPrincipal { + let decision = await authenticate(plaintext, against: store) + guard case .allow(let principal) = decision else { + Issue.record("Expected allow, got \(decision)") + throw CancellationError() + } + return principal + } + + private func storedEntries(_ tokens: [MCPAuthToken]) throws -> [[String: Any]] { + let data = try MCPTokenStore.encodeStored(tokens: tokens, undecoded: []) + guard let entries = try JSONSerialization.jsonObject(with: data) as? [[String: Any]] else { + Issue.record("The stored tokens are not a JSON list") + throw CancellationError() + } + return entries + } + + private func reloaded(entries: [[String: Any]]) throws -> MCPTokenStore { + let data = try JSONSerialization.data(withJSONObject: entries, options: [.sortedKeys]) + return MCPTokenStore(credentialStore: AskProfileCredentials(stored: data)) + } + + @Test("Ask mode gets the schema lookup profile, Edit and Agent keep today's token") + func profileFollowsTheChatMode() { + #expect(ClaudeAgentMCPBridge.toolProfile(for: .ask) == .schemaLookup) + #expect(ClaudeAgentMCPBridge.toolProfile(for: .edit) == nil) + #expect(ClaudeAgentMCPBridge.toolProfile(for: .agent) == nil) + } + + @Test("The profile's tools are the seven documented Ask tools") + @MainActor + func profileMatchesTheDocumentedAskSet() throws { + let chatAskTools = Set(ChatToolBootstrap.makeTools().filter { $0.mode.isAllowed(in: .ask) }.map { $0.name }) + + #expect(MCPToolProfile.schemaLookup.toolNames == Self.askToolNames) + #expect(MCPToolProfile.schemaLookup.toolNames == chatAskTools) + #expect(MCPToolProfile.schemaLookup.scopes == [.toolsRead]) + for name in MCPToolProfile.schemaLookup.toolNames { + let tool = try #require(MCPToolRegistry.tool(named: name), "\(name) is not an MCP tool") + #expect(type(of: tool).requiredScopes.isSubset(of: MCPToolProfile.schemaLookup.scopes)) + } + } + + @Test("An Ask token authenticates as tools:read only, held to the seven tools") + func askTokenIsNarrowed() async throws { + let store = MCPTokenStore(credentialStore: AskProfileCredentials()) + let minted = try await mint(in: store, toolProfile: .schemaLookup) + + let principal = try await principal(presenting: minted.plaintext, to: store) + + #expect(principal.scopes == [.toolsRead]) + #expect(!principal.has(.resourcesRead)) + #expect(principal.toolAllowlist == Self.askToolNames) + #expect(principal.allowsTool("describe_table")) + #expect(!principal.allowsTool("execute_query")) + #expect(!principal.allowsTool("browse_table")) + #expect(!principal.allowsTool("search_query_history")) + } + + @Test("A token minted without a profile, as pairing and Settings mint, keeps its tier and every tool") + func mintWithoutProfileIsUnchanged() async throws { + let store = MCPTokenStore(credentialStore: AskProfileCredentials()) + let minted = try await store.generate( + name: "Raycast", + permissions: .readOnly, + connectionAccess: .all, + expiresAt: nil, + isBridgeCredential: false, + isClaudeAgentCredential: false, + extraScopes: [] + ) + + let principal = try await principal(presenting: minted.plaintext, to: store) + + #expect(minted.token.toolProfile == nil) + #expect(principal.scopes == MCPScope.readOnlySet) + #expect(principal.toolAllowlist == nil) + } + + @Test("The profile survives a save and reload, and a token without one writes no key") + func profileRoundTrips() async throws { + let store = MCPTokenStore(credentialStore: AskProfileCredentials()) + let ask = try await mint(in: store, toolProfile: .schemaLookup).token + let edit = try await mint(in: store, toolProfile: nil).token + + let data = try MCPTokenStore.encodeStored(tokens: [ask, edit], undecoded: []) + let decoded = MCPTokenStore.decodeStored(data) + let profiles = decoded.tokens.map { $0.toolProfile } + let mentions = String(bytes: data, encoding: .utf8)?.components(separatedBy: "toolProfile").count + + #expect(profiles == [.schemaLookup, nil]) + #expect(decoded.undecoded.isEmpty) + #expect(mentions == 2) + #expect(decoded.tokens.first?.scopes == [.toolsRead]) + } + + @Test("A stored profile this build cannot read keeps the entry undecoded and its token stops working") + func unknownProfileFailsClosed() async throws { + let original = MCPTokenStore(credentialStore: AskProfileCredentials()) + let minted = try await mint(in: original, toolProfile: .schemaLookup) + + let damagedValues: [Any] = ["everything", "", NSNull(), 7] + for damaged in damagedValues { + var entries = try storedEntries([minted.token]) + entries[0]["toolProfile"] = damaged + let data = try JSONSerialization.data(withJSONObject: entries, options: [.sortedKeys]) + + let decoded = MCPTokenStore.decodeStored(data) + #expect(decoded.tokens.isEmpty, "a damaged profile \(damaged) must not decode") + #expect(decoded.undecoded.count == 1) + + let store = try reloaded(entries: entries) + await store.loadFromDisk() + let decision = await authenticate(minted.plaintext, against: store) + guard case .deny = decision else { + Issue.record("A token with a damaged profile \(damaged) authenticated: \(decision)") + continue + } + } + } + + @Test("An Ask token is refused resources/read and prompts/get at the dispatcher") + func askTokenIsRefusedResourcesAndPrompts() async throws { + let store = MCPTokenStore(credentialStore: AskProfileCredentials()) + let minted = try await mint(in: store, toolProfile: .schemaLookup) + let principal = try await principal(presenting: minted.plaintext, to: store) + let services = MCPProtocolHandlerTestSupport.makeToolServices() + + for method in [ResourcesReadHandler.method, PromptsGetHandler.method, ResourcesListHandler.method] { + let dispatcher = MCPProtocolTestSupport.makeDispatcher(handlers: [ + ResourcesReadHandler(services: services), + ResourcesListHandler(services: services), + PromptsGetHandler(services: services) + ]) + let (exchange, sink) = MCPProtocolTestSupport.makeExchange( + message: MCPProtocolTestSupport.makeModernRequest( + method: method, + payload: ["uri": .string("tablepro://connections/\(connectionId.uuidString)/history")] + ), + principal: principal + ) + await dispatcher.dispatch(exchange) + await sink.waitForCompletion() + + let errorValue = try await sink.errorEnvelope() + let error = try #require(errorValue, "\(method) answered an Ask token") + #expect(error.code == JsonRpcErrorCode.forbidden) + } + } +} diff --git a/apple/TableProTests/Core/AI/ClaudeAgentConnectionScopeTests.swift b/apple/TableProTests/Core/AI/ClaudeAgentConnectionScopeTests.swift index c589970a09..ae9e05a04a 100644 --- a/apple/TableProTests/Core/AI/ClaudeAgentConnectionScopeTests.swift +++ b/apple/TableProTests/Core/AI/ClaudeAgentConnectionScopeTests.swift @@ -10,9 +10,11 @@ import Testing private actor RecordingToolBridge: ClaudeAgentMCPBridging { private(set) var requested: [UUID?] = [] + private(set) var modes: [AIChatMode] = [] - func makeGrant(connectionId: UUID?) async -> ClaudeAgentToolGrant? { + func makeGrant(connectionId: UUID?, mode: AIChatMode) async -> ClaudeAgentToolGrant? { requested.append(connectionId) + modes.append(mode) return nil } @@ -33,7 +35,7 @@ struct ClaudeAgentConnectionScopeTests { @Test("A conversation with no connection gets no token") func noConnectionMeansNoToken() async { #expect(ClaudeAgentMCPBridge.connectionAccess(for: nil) == nil) - #expect(await ClaudeAgentMCPBridge().makeGrant(connectionId: nil) == nil) + #expect(await ClaudeAgentMCPBridge().makeGrant(connectionId: nil, mode: .agent) == nil) } @Test("Transport options carry no connection unless the caller names one") @@ -41,6 +43,31 @@ struct ClaudeAgentConnectionScopeTests { #expect(ChatTransportOptions(model: "sonnet").connectionId == nil) } + @Test("Transport options that name no mode are Ask, the narrowest") + func optionsDefaultToAsk() { + #expect(ChatTransportOptions(model: "sonnet").chatMode == .ask) + } + + @Test("The provider asks for a grant in the chat mode its options carry") + func providerPassesTheModeToTheBridge() async throws { + let bridge = RecordingToolBridge() + let provider = ClaudeAgentProvider( + model: "sonnet", + cli: ClaudeAgentCLI(executableOverride: "/usr/bin/true"), + toolBridge: bridge + ) + + for mode in AIChatMode.allCases { + let stream = provider.streamChat( + turns: [ChatTurnWire(role: .user, blocks: [.text("hi")])], + options: ChatTransportOptions(model: "sonnet", connectionId: UUID(), chatMode: mode) + ) + for try await _ in stream {} + } + + #expect(await bridge.modes == AIChatMode.allCases) + } + @Test("The provider asks for a grant on the connection in its options") func providerPassesTheConnectionToTheBridge() async throws { let bridge = RecordingToolBridge() diff --git a/apple/TableProTests/Core/AI/ClaudeAgentProviderTests.swift b/apple/TableProTests/Core/AI/ClaudeAgentProviderTests.swift index 0df630ac06..90bd6bde51 100644 --- a/apple/TableProTests/Core/AI/ClaudeAgentProviderTests.swift +++ b/apple/TableProTests/Core/AI/ClaudeAgentProviderTests.swift @@ -14,7 +14,8 @@ struct ClaudeAgentProviderTests { prompt: "count the users", model: "sonnet", systemPrompt: "be terse", - mcpConfigPath: nil + mcpConfigPath: nil, + allowedToolNames: nil ) #expect(args.contains("-p")) #expect(args.contains("stream-json")) @@ -35,7 +36,8 @@ struct ClaudeAgentProviderTests { prompt: "hi", model: "", systemPrompt: nil, - mcpConfigPath: nil + mcpConfigPath: nil, + allowedToolNames: nil ) guard let index = args.firstIndex(of: "--tools") else { Issue.record("--tools flag missing") @@ -52,7 +54,8 @@ struct ClaudeAgentProviderTests { prompt: "--dangerously-skip-permissions", model: "", systemPrompt: nil, - mcpConfigPath: nil + mcpConfigPath: nil, + allowedToolNames: nil ) #expect(args[args.count - 2] == "--") #expect(args.last == "--dangerously-skip-permissions") @@ -64,7 +67,8 @@ struct ClaudeAgentProviderTests { prompt: "hi", model: "", systemPrompt: nil, - mcpConfigPath: "/tmp/config.json" + mcpConfigPath: "/tmp/config.json", + allowedToolNames: nil ) #expect(args.contains("--mcp-config")) #expect(args.contains("/tmp/config.json")) @@ -75,6 +79,31 @@ struct ClaudeAgentProviderTests { #expect(args[index + 1] == "mcp__tablepro__*") } + @Test("An Ask grant names the seven schema tools to the CLI instead of the whole server") + func askGrantNamesItsTools() { + let args = ClaudeAgentProvider.inferenceArguments( + prompt: "hi", + model: "", + systemPrompt: nil, + mcpConfigPath: "/tmp/config.json", + allowedToolNames: MCPToolProfile.schemaLookup.toolNames.sorted() + ) + guard let index = args.firstIndex(of: "--allowedTools") else { + Issue.record("--allowedTools flag missing") + return + } + #expect(args[index + 1] == [ + "mcp__tablepro__describe_table", + "mcp__tablepro__get_connection_status", + "mcp__tablepro__get_table_ddl", + "mcp__tablepro__list_connections", + "mcp__tablepro__list_databases", + "mcp__tablepro__list_schemas", + "mcp__tablepro__list_tables" + ].joined(separator: ",")) + #expect(!args.contains("mcp__tablepro__*")) + } + @Test("Partial message deltas decode as incremental text") func partialMessageDeltasDecodeAsText() { let line = """ diff --git a/apple/TableProTests/Core/AI/ToolApprovalCenterTests.swift b/apple/TableProTests/Core/AI/ToolApprovalCenterTests.swift index 1e309c3db8..84ea1808dd 100644 --- a/apple/TableProTests/Core/AI/ToolApprovalCenterTests.swift +++ b/apple/TableProTests/Core/AI/ToolApprovalCenterTests.swift @@ -125,6 +125,25 @@ struct ToolApprovalCenterTests { #expect(center.hasPending == false) } + @Test("A call is awaited from its announcement until its turn lets it go, and never in another session") + func isAwaitingFollowsTheTurn() async { + let center = ToolApprovalCenter() + #expect(!center.isAwaiting(sessionId: session, toolUseId: "call_0")) + + center.expect(sessionId: session, toolUseIds: ["call_0"]) + #expect(center.isAwaiting(sessionId: session, toolUseId: "call_0")) + #expect(!center.isAwaiting(sessionId: UUID(), toolUseId: "call_0")) + + let waiter = Task { await center.awaitDecision(sessionId: session, toolUseId: "call_0") } + await runQueuedMainActorWork() + center.forget(sessionId: session, toolUseIds: ["call_0"]) + #expect(center.isAwaiting(sessionId: session, toolUseId: "call_0")) + + center.resolve(sessionId: session, toolUseId: "call_0", decision: .run) + _ = await waiter.value + #expect(!center.isAwaiting(sessionId: session, toolUseId: "call_0")) + } + /// The main queue runs its jobs in order, so a job queued now runs after every waiter the case /// started has reached its await. private func runQueuedMainActorWork() async { diff --git a/apple/TableProTests/Core/Database/DatabaseAccessBridgeAnswerBudgetTests.swift b/apple/TableProTests/Core/Database/DatabaseAccessBridgeAnswerBudgetTests.swift new file mode 100644 index 0000000000..1975a0f28e --- /dev/null +++ b/apple/TableProTests/Core/Database/DatabaseAccessBridgeAnswerBudgetTests.swift @@ -0,0 +1,257 @@ +// +// DatabaseAccessBridgeAnswerBudgetTests.swift +// TableProTests +// +// MCP and the assistant read rows through DatabaseAccessBridge, which held them to a row count and never to a size. +// + +import Foundation +@testable import TablePro +import TableProPluginKit +import Testing + +@MainActor +struct DatabaseAccessBridgeAnswerBudgetTests { + private func approved( + _ sql: String, + scope: DatabaseScope, + databaseType: DatabaseType, + maxRows: Int = 500 + ) async throws -> ApprovedOperation { + let statement = try await DatabaseAccessBridge().plannedStatement(scope: scope, query: sql, maxRows: maxRows) + return try await ApprovedOperationFixture.approve([statement], scope: scope, databaseType: databaseType) + } + + @Test("A read of 10,000 rows of 2 KB answers at most 4 MiB, marked truncated") + func largeReadIsHeldToTheBudget() async throws { + let driver = AnsweringPluginDriver { _ in + AnsweringPluginDriver.textRows(column: "body", count: 10_000, bytes: 2_048) + } + let scope = AnsweringPluginDriver.install(driver, connection: TestFixtures.makeConnection(database: "shop", type: .postgresql)) + defer { DatabaseManager.shared.removeSession(for: scope.connectionId) } + let operation = try await approved("SELECT body FROM documents", scope: scope, databaseType: .postgresql, maxRows: 10_000) + + let outcome = try await DatabaseAccessBridge().runStatement( + operation, maxRows: 10_000, timeoutSeconds: 30, cancellation: nil, budget: .mcpAnswer + ) + + let budget = try #require(outcome.budget) + #expect(!outcome.result.rows.isEmpty) + #expect(outcome.result.rows.count * 2_048 <= ExternalAnswerBudget.answerBytes) + #expect(outcome.result.rows.count < 10_000) + #expect(outcome.result.isTruncated) + #expect(budget.sizeLimited) + #expect(budget.spentBytes <= ExternalAnswerBudget.answerBytes) + } + + @Test("Without a budget, as AppleScript and Shortcuts run, every row comes back") + func noBudgetKeepsEveryRow() async throws { + let driver = AnsweringPluginDriver { _ in + AnsweringPluginDriver.textRows(column: "body", count: 3_000, bytes: 2_048) + } + let scope = AnsweringPluginDriver.install(driver, connection: TestFixtures.makeConnection(database: "shop", type: .postgresql)) + defer { DatabaseManager.shared.removeSession(for: scope.connectionId) } + let operation = try await approved("SELECT body FROM documents", scope: scope, databaseType: .postgresql, maxRows: 3_000) + + let outcome = try await DatabaseAccessBridge().runStatement( + operation, maxRows: 3_000, timeoutSeconds: 30, cancellation: nil + ) + + #expect(outcome.result.rows.count == 3_000) + #expect(outcome.budget == nil) + #expect(driver.sent.last?.context.resultByteLimit == nil) + } + + @Test("A read reaches the driver with the answer's byte limit, and a write reaches it with none") + func onlyReadsCarryTheByteLimit() async throws { + let driver = AnsweringPluginDriver() + let scope = AnsweringPluginDriver.install(driver, connection: TestFixtures.makeConnection(database: "shop", type: .postgresql)) + defer { DatabaseManager.shared.removeSession(for: scope.connectionId) } + let bridge = DatabaseAccessBridge() + + _ = try await bridge.runStatement( + try await approved("SELECT 1", scope: scope, databaseType: .postgresql), + maxRows: 500, timeoutSeconds: 30, cancellation: nil, budget: .assistantAnswer + ) + _ = try await bridge.runStatement( + try await approved("UPDATE orders SET paid = true WHERE id = 1", scope: scope, databaseType: .postgresql), + maxRows: 500, timeoutSeconds: 30, cancellation: nil, budget: .assistantAnswer + ) + + #expect(driver.sent.map { $0.context.resultByteLimit } == [ExternalAnswerBudget.assistantAnswerBytes, nil]) + #expect(driver.sent.map { $0.context.readOnly } == [true, false]) + } + + @Test("A read the driver stopped at its byte limit says the size limit left rows out") + func driverStopIsReported() async throws { + let driver = AnsweringPluginDriver { _ in + AnsweringPluginDriver.textRows(column: "body", count: 10, bytes: 10, isTruncated: true) + } + let scope = AnsweringPluginDriver.install(driver, connection: TestFixtures.makeConnection(database: "shop", type: .postgresql)) + defer { DatabaseManager.shared.removeSession(for: scope.connectionId) } + + let outcome = try await DatabaseAccessBridge().runStatement( + try await approved("SELECT body FROM documents", scope: scope, databaseType: .postgresql), + maxRows: 500, timeoutSeconds: 30, cancellation: nil, budget: .mcpAnswer + ) + + #expect(outcome.result.rows.count == 10) + #expect(outcome.result.isTruncated) + #expect(outcome.budget?.sizeLimited == true) + #expect(outcome.budget?.notes.count == 1) + } + + // If the cut ran first, the last four characters shown would be the end of the cut mark. + @Test("A partial mask reads the whole value before any cut") + func maskRunsBeforeTheCut() async throws { + var connection = TestFixtures.makeConnection(database: "shop", type: .mysql) + connection.maskingRules = [MaskingRule(pattern: "*.card", style: .lastFour)] + let long = String(repeating: "1", count: 200_000) + "WXYZ" + let driver = ScriptAnsweringDriver(connection: connection, statementAnswer: { _ in + var result = ScriptAnsweringDriver.resultSet(columns: ["card"], rows: [[long]]) + result.columnOrigins = [PluginColumnOrigin(schema: "shop", table: "payments", column: "card")] + return result + }) + var session = ConnectionSession(connection: connection) + session.driver = driver + DatabaseManager.shared.injectSession(session, for: connection.id) + defer { DatabaseManager.shared.removeSession(for: connection.id) } + let scope = DatabaseScope(connectionId: connection.id, database: "shop", schema: nil) + + let outcome = try await DatabaseAccessBridge().runStatement( + try await approved("SELECT card FROM payments", scope: scope, databaseType: .mysql), + maxRows: 500, timeoutSeconds: 30, cancellation: nil, budget: .mcpAnswer + ) + + let cell = try #require(outcome.result.rows.first?.first?.asText) + #expect(cell.hasSuffix("WXYZ")) + #expect(!cell.contains("TablePro cut")) + #expect(outcome.budget?.valuesCut == false) + } +} + +@Suite("SQL Server scripts held to one answer budget", .serialized) +@MainActor +struct DatabaseAccessBridgeScriptBudgetTests { + private func run( + _ text: String, + on driver: ScriptAnsweringDriver, + budget: ExternalAnswerBudget? + ) async throws -> DatabaseAccessBridge.ScriptOutcome { + var session = ConnectionSession(connection: driver.connection) + session.driver = driver + DatabaseManager.shared.injectSession(session, for: driver.connection.id) + defer { DatabaseManager.shared.removeSession(for: driver.connection.id) } + let scope = DatabaseScope(connectionId: driver.connection.id, database: driver.connection.database, schema: nil) + let bridge = DatabaseAccessBridge() + let planned = try await bridge.plannedScript(scope: scope, query: text, maxRows: 500) + let operation = try await ApprovedOperationFixture.approve( + [planned.statement], scope: scope, databaseType: .mssql, unit: planned.unit + ) + return try await bridge.runScript(operation, maxRows: 500, timeoutSeconds: 30, cancellation: nil, budget: budget) + } + + nonisolated private static func rows(_ column: String, count: Int, bytes: Int) -> QueryResult { + ScriptAnsweringDriver.resultSet( + columns: [column], + rows: Array(repeating: [String(repeating: "x", count: bytes)], count: count) + ) + } + + private func makeDriver(answer: @escaping @Sendable (String) -> QueryBatchResult) -> ScriptAnsweringDriver { + ScriptAnsweringDriver( + connection: TestFixtures.makeConnection(database: "warehouse", type: .mssql), + answer: answer + ) + } + + nonisolated private static let threeBatches = "SELECT 1 AS a\nGO\nSELECT 2 AS b\nGO\nSELECT 3 AS c" + + nonisolated private static func firstBatchFillsTheBudget(_ batch: String) -> QueryBatchResult { + batch.contains("AS a") + ? ScriptAnsweringDriver.batch([rows("a", count: 50, bytes: 1_000)]) + : ScriptAnsweringDriver.batch([rows("b", count: 5, bytes: 10)]) + } + + @Test("Once the budget is spent, later batches still run, reading one row and keeping none") + func laterBatchesRunWithoutRows() async throws { + let driver = makeDriver(answer: Self.firstBatchFillsTheBudget) + + let outcome = try await run(Self.threeBatches, on: driver, budget: ExternalAnswerBudget(bytes: 10_000)) + + #expect(driver.sentBatches.map { $0.rowCap } == [500, 1, 1]) + #expect(outcome.resultSets.count == 3) + let first = try #require(outcome.resultSets.first) + #expect(!first.rows.isEmpty && first.rows.count < 50) + #expect(first.isTruncated) + #expect(outcome.resultSets.dropFirst().allSatisfy { $0.rows.isEmpty && $0.isTruncated }) + #expect(outcome.primary.rows.count == first.rows.count) + #expect(outcome.primary.isTruncated) + #expect(outcome.budget?.sizeLimited == true) + } + + @Test("Without a budget, as AppleScript and Shortcuts run, every batch keeps its rows") + func noBudgetKeepsEveryBatch() async throws { + let driver = makeDriver(answer: Self.firstBatchFillsTheBudget) + + let outcome = try await run(Self.threeBatches, on: driver, budget: nil) + + #expect(driver.sentBatches.map { $0.rowCap } == [500, 500, 500]) + #expect(outcome.resultSets.map { $0.rows.count } == [50, 5, 5]) + #expect(outcome.budget == nil) + } + + @Test("Each batch's answer is cut to the budget before the next batch runs, so rows past it are not held") + func answersAreHeldToTheBudgetBatchByBatch() async throws { + let driver = makeDriver(answer: Self.firstBatchFillsTheBudget) + let batches = QueryBatchPlanner.batches( + in: Self.threeBatches, + model: QueryStatementModel.forDatabaseType(.mssql), + grammar: DatabaseType.mssql.lexicalGrammar + ) + + let answered = try await ScriptBatchRun.run( + batches, startLines: [1, 3, 5], rowCap: 500, driver: driver, progress: ScriptBatchProgress(), + budget: ExternalAnswerBudget(bytes: 10_000) + ) + + let heldRows = answered.answers.map { answer in answer.resultSets.map { set in set.rows.count } } + #expect(heldRows == [[9], [0], [0]]) + #expect(driver.sentBatches.map { $0.rowCap } == [500, 1, 1]) + #expect(answered.budget?.sizeLimited == true) + #expect((answered.budget?.spentBytes ?? .max) <= 10_000) + } + + // If a value were cut before the mask, the last four characters shown would be the end of the cut mark. + @Test("A script's partial mask reads the whole value before any cut") + func scriptMaskRunsBeforeTheCut() async throws { + var connection = TestFixtures.makeConnection(database: "warehouse", type: .mssql) + connection.maskingRules = [MaskingRule(pattern: "*.card", style: .lastFour)] + let long = String(repeating: "1", count: 200_000) + "WXYZ" + let driver = ScriptAnsweringDriver(connection: connection, answer: { _ in + var set = ScriptAnsweringDriver.resultSet(columns: ["card"], rows: [[long]]) + set.columnOrigins = [PluginColumnOrigin(schema: "dbo", table: "payments", column: "card")] + return ScriptAnsweringDriver.batch([set]) + }) + + let outcome = try await run("SELECT card FROM payments\nGO\nSELECT 2 AS b", on: driver, budget: .mcpAnswer) + + let cell = try #require(outcome.resultSets.first?.rows.first?.first?.asText) + #expect(cell.hasSuffix("WXYZ")) + #expect(!cell.contains("TablePro cut")) + } + + @Test("With more than one result set the first costs twice, since the answer writes it twice") + func firstSetCostsTwice() async throws { + let twoSets = makeDriver { _ in + ScriptAnsweringDriver.batch([Self.rows("a", count: 6, bytes: 1_000), Self.rows("b", count: 1, bytes: 1)]) + } + let oneSet = makeDriver { _ in ScriptAnsweringDriver.batch([Self.rows("a", count: 6, bytes: 1_000)]) } + + let both = try await run("EXEC report", on: twoSets, budget: ExternalAnswerBudget(bytes: 10_000)) + let single = try await run("EXEC report", on: oneSet, budget: ExternalAnswerBudget(bytes: 10_000)) + + #expect(both.resultSets.first?.rows.count == 4) + #expect(single.resultSets.first?.rows.count == 6) + } +} diff --git a/apple/TableProTests/Core/Database/ExternalAnswerBudgetTests.swift b/apple/TableProTests/Core/Database/ExternalAnswerBudgetTests.swift new file mode 100644 index 0000000000..58115371c0 --- /dev/null +++ b/apple/TableProTests/Core/Database/ExternalAnswerBudgetTests.swift @@ -0,0 +1,174 @@ +// +// ExternalAnswerBudgetTests.swift +// TableProTests +// + +import Foundation +@testable import TablePro +import TableProPluginKit +import Testing + +struct ExternalAnswerBudgetTests { + private func result(_ column: String, _ rows: [[PluginCellValue]], isTruncated: Bool = false) -> QueryResult { + var result = QueryResult( + columns: [column], + columnTypes: [.text(rawType: nil)], + rows: rows, + rowsAffected: 0, + executionTime: 0, + error: nil + ) + result.isTruncated = isTruncated + return result + } + + private func textRows(_ column: String, count: Int, bytes: Int) -> QueryResult { + result(column, Array(repeating: [.text(String(repeating: "x", count: bytes))], count: count)) + } + + // What one row of a single column costs in the core's measure: the row's brackets and comma, then the name, the + // cell's quotes, colon and comma, and the value. + private func rowCost(column: String, valueBytes: Int) -> Int { + 3 + column.utf8.count + 6 + valueBytes + } + + @Test("A value past 64 KiB keeps its first 64 KiB and a mark with its full length") + func longTextIsCut() throws { + var budget = ExternalAnswerBudget.mcpAnswer + + let kept = budget.keep(result("body", [[.text(String(repeating: "a", count: 200_000))]])) + + let cell = try #require(kept.rows.first?.first) + #expect(cell == .text(String(repeating: "a", count: 65_536) + " [TablePro cut this value: 200000 bytes in all]")) + #expect(budget.valuesCut) + #expect(!budget.sizeLimited) + #expect(!kept.isTruncated) + #expect(budget.notes.count == 1) + } + + @Test("A cut never splits a character, and a value inside the limit is left as it is") + func cutKeepsWholeCharacters() { + let euros = String(repeating: "€", count: 30_000) + + let cut = ExternalAnswerBudget.cut(euros) + + let mark = " [TablePro cut this value: 90000 bytes in all]" + #expect(cut.hasSuffix(mark)) + #expect(String(cut.dropLast(mark.count)) == String(repeating: "€", count: 21_845)) + #expect(ExternalAnswerBudget.cut("short") == "short") + } + + @Test("Binary is measured as the base64 the answer carries, and a long one is cut to base64 text") + func binaryIsMeasuredAsBase64() throws { + let row = result("b", [[.bytes(Data(repeating: 7, count: 3_000))]]) + let cost = rowCost(column: "b", valueBytes: 4_000) + + var fits = ExternalAnswerBudget(bytes: cost) + var short = ExternalAnswerBudget(bytes: cost - 1) + let fitted = fits.keep(row) + let shortened = short.keep(row) + + #expect(fitted.rows.count == 1) + #expect(shortened.rows.isEmpty) + #expect(short.sizeLimited) + + let data = Data(repeating: 1, count: 100_000) + var budget = ExternalAnswerBudget.mcpAnswer + let kept = budget.keep(result("b", [[.bytes(data)]])) + let cell = try #require(kept.rows.first?.first) + #expect(cell == .text(data.prefix(49_152).base64EncodedString() + " [TablePro cut this value: 100000 bytes in all]")) + } + + @Test("Rows past the answer limit are left out, and the result says it was truncated") + func rowsPastTheLimitAreLeftOut() { + var budget = ExternalAnswerBudget.mcpAnswer + + let kept = budget.keep(textRows("body", count: 3_000, bytes: 2_000)) + + #expect(kept.rows.count == ExternalAnswerBudget.answerBytes / rowCost(column: "body", valueBytes: 2_000)) + #expect(kept.isTruncated) + #expect(budget.sizeLimited) + #expect(!budget.valuesCut) + #expect(budget.spentBytes <= ExternalAnswerBudget.answerBytes) + #expect(budget.notes.count == 1) + } + + @Test("An export keeps a long value whole, and leaves out a row that does not fit instead of cutting it") + func exportKeepsValuesWhole() { + let long = String(repeating: "y", count: 100_000) + var export = ExternalAnswerBudget.export + + let kept = export.keep(result("doc", [[.text(long)]])) + + #expect(kept.rows == [[.text(long)]]) + #expect(!export.valuesCut) + + var small = ExternalAnswerBudget(bytes: 50_000, longValues: .whole) + let leftOut = small.keep(result("doc", [[.text(long)]])) + #expect(leftOut.rows.isEmpty) + #expect(leftOut.isTruncated) + #expect(small.sizeLimited) + #expect(!small.valuesCut) + } + + @Test("A result after the limit was reached keeps no rows") + func laterResultKeepsNoRows() { + var budget = ExternalAnswerBudget(bytes: 100) + + let first = budget.keep(textRows("a", count: 1, bytes: 200)) + let second = budget.keep(textRows("b", count: 1, bytes: 1)) + + #expect(first.rows.isEmpty) + #expect(second.rows.isEmpty) + #expect(second.isTruncated) + } + + @Test("A result the answer writes twice costs twice") + func weightCountsEveryCopy() { + let rows = textRows("b", count: 6, bytes: 1_000) + var once = ExternalAnswerBudget(bytes: 10_000) + var twice = ExternalAnswerBudget(bytes: 10_000) + let keptOnce = once.keep(rows) + let keptTwice = twice.keep(rows, weight: 2) + + #expect(keptOnce.rows.count == 6) + #expect(keptTwice.rows.count == 4) + } + + @Test("A text beside the rows is cut to what the answer has left") + func textIsCutToTheRest() { + var budget = ExternalAnswerBudget(bytes: 100) + + let kept = budget.keep(text: String(repeating: "p", count: 300)) + + #expect(kept == String(repeating: "p", count: 100) + " [TablePro cut this value: 300 bytes in all]") + #expect(budget.sizeLimited) + #expect(budget.remainingBytes == 0) + } + + @Test("A part kept only whole is left out when it does not fit, and a smaller part after it still fits") + func wholePartIsLeftOut() { + var budget = ExternalAnswerBudget(bytes: 1_000) + + let large = budget.keepWhole(bytes: 1_200) + let small = budget.keepWhole(bytes: 800) + + #expect(!large) + #expect(small) + #expect(budget.spentBytes == 800) + #expect(!budget.sizeLimited) + } + + @Test("An answer that fits comes back as it was, with no notes") + func fittingAnswerIsUntouched() { + var budget = ExternalAnswerBudget.mcpAnswer + let rows = textRows("n", count: 10, bytes: 10) + + let kept = budget.keep(rows) + + #expect(kept.rows == rows.rows) + #expect(!kept.isTruncated) + #expect(budget.notes.isEmpty) + #expect(budget.spentBytes == 10 * rowCost(column: "n", valueBytes: 10)) + } +} diff --git a/apple/TableProTests/Core/Database/ProtectedWriteDeadlineTests.swift b/apple/TableProTests/Core/Database/ProtectedWriteDeadlineTests.swift index f18bfc632a..1e60929931 100644 --- a/apple/TableProTests/Core/Database/ProtectedWriteDeadlineTests.swift +++ b/apple/TableProTests/Core/Database/ProtectedWriteDeadlineTests.swift @@ -149,15 +149,17 @@ struct ProtectedWriteDeadlineTests { #expect(await !ran.isSet) } - @Test("A write already in the driver when the caller cancels runs to the end") + @Test("A write already in the driver when the caller cancels runs to the end, and the caller hears it may have been applied") func inDriverAtCancelFinishes() async throws { let gate = AsyncGate() let finished = Flag() + let lateCompletion = Flag() let caller = Task { try await DatabaseAccessBridge.awaitProtectedWrite( timeout: .seconds(60), timeoutSeconds: 60, + onLateCompletion: { Task { await lateCompletion.set() } }, { enterDriver -> Int in try enterDriver() await gate.wait() @@ -168,9 +170,19 @@ struct ProtectedWriteDeadlineTests { } await gate.arrived(1) caller.cancel() - await #expect(throws: CancellationError.self) { _ = try await caller.value } + let error = await #expect(throws: DatabaseAccessError.self) { _ = try await caller.value } + + guard case .stoppedWhileRunning(let detail) = error else { + Issue.record("expected the outcome to be unknown, got \(String(describing: error))") + return + } + #expect(detail == DatabaseAccessBridge.stoppedWhileRunning.message) + #expect(!detail.contains("timed out")) + #expect(await !finished.isSet) + #expect(await !lateCompletion.isSet) await gate.open() try await waitUntil { await finished.isSet } + try await waitUntil { await lateCompletion.isSet } } } diff --git a/apple/TableProTests/Core/MCP/Legacy/InitializeHandlerTests.swift b/apple/TableProTests/Core/MCP/Legacy/InitializeHandlerTests.swift index 61cd61f061..45e12e63a9 100644 --- a/apple/TableProTests/Core/MCP/Legacy/InitializeHandlerTests.swift +++ b/apple/TableProTests/Core/MCP/Legacy/InitializeHandlerTests.swift @@ -105,7 +105,7 @@ struct LegacyInitializeHandlerTests { @Test("The legacy initialize result keeps its legacy wire shape") func theLegacyResultKeepsItsLegacyShape() { - let result = InitializeHandler.result(protocolVersion: .v20251125) + let result = InitializeHandler.result(protocolVersion: .v20251125, scopes: MCPScope.readOnlySet) let encoded = result.asJsonValue(era: .legacy, serverInfo: MCPMethodRegistry.serverInfo) @@ -120,7 +120,7 @@ struct LegacyInitializeHandlerTests { @Test("The legacy capabilities object carries no extensions field") func legacyCapabilitiesCarryNoExtensions() { - let encoded = InitializeHandler.result(protocolVersion: .v20251125) + let encoded = InitializeHandler.result(protocolVersion: .v20251125, scopes: MCPScope.readOnlySet) .asJsonValue(era: .legacy, serverInfo: MCPMethodRegistry.serverInfo) #expect(encoded["capabilities"]?["extensions"] == nil) @@ -155,14 +155,49 @@ struct LegacyInitializeHandlerTests { #expect(modern["resources"]?["subscribe"]?.boolValue == true) } + @Test("A token without resources:read is not offered resources, prompts or completions") + func toolsOnlyTokenIsOfferedToolsOnly() { + let narrow = InitializeHandler.capabilities(for: [.toolsRead]).asJsonValue(era: .legacy) + let full = InitializeHandler.capabilities(for: MCPScope.readOnlySet).asJsonValue(era: .legacy) + + #expect(narrow["tools"] != nil) + #expect(narrow["resources"] == nil) + #expect(narrow["prompts"] == nil) + #expect(narrow["completions"] == nil) + #expect(full == MCPMethodRegistry.capabilities().asJsonValue(era: .legacy)) + } + + @Test("The initialize a client receives follows the scopes of the token that sent it") + func initializeFollowsThePrincipalScopes() async throws { + let adapter = MCPLegacyTestSupport.makeAdapter() + let params = MCPLegacyTestSupport.initializeParams() + + let (narrowResult, _) = try await adapter.handleInitialize( + params: params, + principal: MCPLegacyTestSupport.principal(scopes: [.toolsRead]) + ) + let (fullResult, _) = try await adapter.handleInitialize( + params: params, + principal: MCPLegacyTestSupport.principal(scopes: MCPScope.readOnlySet) + ) + let narrow = narrowResult.asJsonValue(era: .legacy, serverInfo: MCPMethodRegistry.serverInfo)["capabilities"] + let full = fullResult.asJsonValue(era: .legacy, serverInfo: MCPMethodRegistry.serverInfo)["capabilities"] + + #expect(narrow?["tools"] != nil) + #expect(narrow?["resources"] == nil) + #expect(narrow?["prompts"] == nil) + #expect(full?["resources"] != nil) + #expect(full?["prompts"] != nil) + } + @Test("The initialize result carries no cache hint") func theResultIsNotCacheable() { - #expect(InitializeHandler.result(protocolVersion: .v20251125).cacheHint == nil) + #expect(InitializeHandler.result(protocolVersion: .v20251125, scopes: MCPScope.readOnlySet).cacheHint == nil) } @Test("The modern envelope fields the legacy shape omits are added only for the modern era") func modernEnvelopeIsAddedOnlyForModernClients() { - let encoded = InitializeHandler.result(protocolVersion: .v20251125) + let encoded = InitializeHandler.result(protocolVersion: .v20251125, scopes: MCPScope.readOnlySet) .asJsonValue(era: .modern, serverInfo: MCPMethodRegistry.serverInfo) #expect(encoded["resultType"]?.stringValue == "complete") diff --git a/apple/TableProTests/Core/MCP/MCPAggregateReadApprovalTests.swift b/apple/TableProTests/Core/MCP/MCPAggregateReadApprovalTests.swift new file mode 100644 index 0000000000..9b31282f0f --- /dev/null +++ b/apple/TableProTests/Core/MCP/MCPAggregateReadApprovalTests.swift @@ -0,0 +1,315 @@ +// +// MCPAggregateReadApprovalTests.swift +// TableProTests +// +// A read that spans connections cannot ask about each one, so it covers only the connections the +// approval gate already lets through. A call that names one connection is asked about it once. +// + +import Foundation +@testable import TablePro +import TableProMCP +import TableProMCPTestSupport +import TableProPluginKit +import Testing + +struct MCPAggregateReadApprovalTests { + private let granted = UUID() + private let withheld = UUID() + + private func ledger() -> MCPApprovalLedger { + MCPApprovalLedger(clock: MCPTestClock(), store: MCPInMemoryApprovalStore()) + } + + private func makePolicy( + approval: MCPConnectionApproval, + presenter: RecordingApprovalPresenter, + connections: [UUID: MCPConnectionAuthSnapshot]? = nil + ) -> MCPAuthPolicy { + MCPToolTestHarness.authPolicy( + connections: connections ?? [ + granted: MCPToolTestHarness.snapshot(policy: .askEachTime, name: "Granted"), + withheld: MCPToolTestHarness.snapshot(policy: .askEachTime, name: "Withheld") + ], + approval: approval, + presenter: presenter, + ledger: ledger() + ) + } + + private func entry(connectionId: UUID, query: String) -> QueryHistoryEntry { + QueryHistoryEntry( + query: query, + connectionId: connectionId, + databaseName: "shop", + databaseType: .postgresql, + source: .editor, + executionTime: 0.01, + rowCount: 1, + wasSuccessful: true + ) + } + + private func historyStore() -> ToolTestQueryHistoryStore { + ToolTestQueryHistoryStore(entries: [ + entry(connectionId: granted, query: "SELECT 1"), + entry(connectionId: withheld, query: "UPDATE employees SET salary = 250000") + ]) + } + + private func tab(on connectionId: UUID, title: String) -> MCPTabSnapshot { + MCPTabSnapshot( + tabId: UUID(), + connectionId: connectionId, + connectionName: title, + tabType: "table", + tableName: title, + databaseName: "shop", + schemaName: "public", + displayTitle: title, + windowId: UUID(), + isActive: true, + window: nil + ) + } + + private func context() -> MCPRequestContext { + MCPToolTestHarness.context(principal: MCPToolTestHarness.principal(access: .all)) + } + + private func approve(_ connectionId: UUID, in policy: MCPAuthPolicy) async throws { + try await policy.resolveAndAuthorize( + principal: MCPToolTestHarness.principal(access: .all), + tool: "list_tables", + connectionId: connectionId + ) + } + + @Test("Under Ask Once the silent set holds only the connections already granted") + func askOnceKeepsOnlyGrantedConnections() async throws { + let presenter = RecordingApprovalPresenter(answer: true) + let policy = makePolicy(approval: .oncePerConnection, presenter: presenter) + try await approve(granted, in: policy) + + let allowed = await policy.connectionIdsReadableWithoutPrompt(principal: MCPToolTestHarness.principal()) + + #expect(allowed == [granted]) + #expect(await presenter.askedCount == 1) + } + + @Test("A connection the user just denied stays out of the silent set") + func deniedConnectionStaysOut() async throws { + let presenter = RecordingApprovalPresenter(answer: false) + let policy = makePolicy(approval: .oncePerConnection, presenter: presenter, connections: [ + granted: MCPToolTestHarness.snapshot(policy: .alwaysAllow, name: "Granted"), + withheld: MCPToolTestHarness.snapshot(policy: .askEachTime, name: "Withheld") + ]) + await #expect(throws: DatabaseAccessError.self) { + try await approve(withheld, in: policy) + } + + let allowed = await policy.connectionIdsReadableWithoutPrompt(principal: MCPToolTestHarness.principal()) + + #expect(allowed == [granted]) + #expect(await presenter.askedCount == 1) + } + + @Test("Under Ask Every Time the silent set holds only connections set to Always Allow") + func askEveryTimeKeepsOnlyAlwaysAllow() async throws { + let presenter = RecordingApprovalPresenter(answer: true) + let policy = makePolicy(approval: .everyTime, presenter: presenter, connections: [ + granted: MCPToolTestHarness.snapshot(policy: .alwaysAllow, name: "Granted"), + withheld: MCPToolTestHarness.snapshot(policy: .askEachTime, name: "Withheld") + ]) + try await approve(withheld, in: policy) + + let allowed = await policy.connectionIdsReadableWithoutPrompt(principal: MCPToolTestHarness.principal()) + + #expect(allowed == [granted]) + } + + @Test("Under Never Ask the silent set is every connection the token may see") + func neverAskKeepsEveryReadableConnection() async { + let hidden = UUID() + let blocked = UUID() + let outsideToken = UUID() + let presenter = RecordingApprovalPresenter(answer: false) + let policy = makePolicy(approval: .alwaysApprove, presenter: presenter, connections: [ + granted: MCPToolTestHarness.snapshot(policy: .askEachTime, name: "Granted"), + hidden: MCPToolTestHarness.snapshot(policy: .never, name: "Hidden"), + blocked: MCPToolTestHarness.snapshot(policy: .askEachTime, externalAccess: .blocked, name: "Blocked"), + outsideToken: MCPToolTestHarness.snapshot(policy: .askEachTime, name: "Outside") + ]) + + let allowed = await policy.connectionIdsReadableWithoutPrompt( + principal: MCPToolTestHarness.principal(access: .limited([granted, hidden, blocked])) + ) + + #expect(allowed == [granted]) + #expect(await presenter.askedCount == 0) + } + + @Test("History without a connection id leaves out a connection nobody approved, and asks nothing") + func historyWithoutConnectionCoversOnlyApprovedConnections() async throws { + let presenter = RecordingApprovalPresenter(answer: true) + let policy = makePolicy(approval: .oncePerConnection, presenter: presenter) + try await approve(granted, in: policy) + let store = historyStore() + + let result = try await SearchQueryHistoryTool().call( + arguments: .object(["query": .string("")]), + context: context(), + services: MCPToolTestHarness.services(authPolicy: policy, history: store) + ) + + let filters = await store.receivedFilters + #expect(filters.first?.allowedConnectionIds == [granted]) + let entries = try #require(result.structuredContent?["entries"]?.arrayValue) + #expect(entries.allSatisfy { $0["connection_id"]?.stringValue == granted.uuidString }) + #expect(await presenter.askedCount == 1) + } + + @Test("History without a connection id still leaves out a connection the user denied") + func historyAfterADenialLeavesTheConnectionOut() async throws { + let presenter = RecordingApprovalPresenter(answer: false) + let policy = makePolicy(approval: .oncePerConnection, presenter: presenter, connections: [ + granted: MCPToolTestHarness.snapshot(policy: .alwaysAllow, name: "Granted"), + withheld: MCPToolTestHarness.snapshot(policy: .askEachTime, name: "Withheld") + ]) + await #expect(throws: DatabaseAccessError.self) { + try await approve(withheld, in: policy) + } + let store = historyStore() + + let result = try await SearchQueryHistoryTool().call( + arguments: .object(["query": .string("")]), + context: context(), + services: MCPToolTestHarness.services(authPolicy: policy, history: store) + ) + + let filters = await store.receivedFilters + #expect(filters.first?.allowedConnectionIds == [granted]) + let entries = try #require(result.structuredContent?["entries"]?.arrayValue) + for entry in entries { + #expect(entry["query"]?.stringValue?.contains("salary") != true) + } + } + + @Test("Under Ask Every Time history without a connection id reads nothing and asks nothing") + func askEveryTimeAggregateHistoryIsEmpty() async throws { + let presenter = RecordingApprovalPresenter(answer: true) + let policy = makePolicy(approval: .everyTime, presenter: presenter) + let store = historyStore() + + let result = try await SearchQueryHistoryTool().call( + arguments: .object(["query": .string("")]), + context: context(), + services: MCPToolTestHarness.services(authPolicy: policy, history: store) + ) + + #expect(result.structuredContent?["entries"]?.arrayValue?.isEmpty == true) + #expect(await store.receivedFilters.isEmpty) + #expect(await presenter.askedCount == 0) + } + + @Test("Under Ask Every Time a history search that names a connection returns it, without asking again") + func askEveryTimeNamedHistoryIsReturned() async throws { + let presenter = RecordingApprovalPresenter(answer: true) + let policy = makePolicy(approval: .everyTime, presenter: presenter) + let store = historyStore() + + let result = try await SearchQueryHistoryTool().call( + arguments: .object(["query": .string(""), "connection_id": .string(withheld.uuidString)]), + context: context(), + services: MCPToolTestHarness.services(authPolicy: policy, history: store) + ) + + let entries = try #require(result.structuredContent?["entries"]?.arrayValue) + #expect(entries.count == 1) + #expect(entries.first?["connection_id"]?.stringValue == withheld.uuidString) + #expect(await presenter.askedCount == 0) + } + + @Test("Tabs without a connection id leave out a connection nobody approved") + func tabsWithoutConnectionCoverOnlyApprovedConnections() async throws { + let presenter = RecordingApprovalPresenter(answer: true) + let policy = makePolicy(approval: .oncePerConnection, presenter: presenter) + try await approve(granted, in: policy) + let tabs = [tab(on: granted, title: "orders"), tab(on: withheld, title: "payroll")] + + let result = try await ListRecentTabsTool(collectSnapshots: { tabs }).call( + arguments: .object([:]), + context: context(), + services: MCPToolTestHarness.services(authPolicy: policy) + ) + + let listed = try #require(result.structuredContent?["tabs"]?.arrayValue) + #expect(listed.count == 1) + #expect(listed.first?["connection_id"]?.stringValue == granted.uuidString) + #expect(await presenter.askedCount == 1) + } + + @Test("Under Ask Every Time tabs of a named connection are listed without asking again") + func askEveryTimeNamedTabsAreListed() async throws { + let presenter = RecordingApprovalPresenter(answer: true) + let policy = makePolicy(approval: .everyTime, presenter: presenter) + let tabs = [tab(on: granted, title: "orders"), tab(on: withheld, title: "payroll")] + + let result = try await ListRecentTabsTool(collectSnapshots: { tabs }).call( + arguments: .object(["connection_id": .string(withheld.uuidString)]), + context: context(), + services: MCPToolTestHarness.services(authPolicy: policy) + ) + + let listed = try #require(result.structuredContent?["tabs"]?.arrayValue) + #expect(listed.count == 1) + #expect(listed.first?["connection_id"]?.stringValue == withheld.uuidString) + #expect(await presenter.askedCount == 0) + } + + @Test("The snapshot filter keeps only the given connections") + func snapshotFilterKeepsTheGivenConnections() { + let tabs = [tab(on: granted, title: "orders"), tab(on: withheld, title: "payroll")] + + let kept = MCPTabSnapshotProvider.snapshots(tabs, in: [granted]) + + #expect(kept.count == 1) + #expect(kept.first?.connectionId == granted) + #expect(MCPTabSnapshotProvider.snapshots(tabs, in: []).isEmpty) + } + + @Test("Focusing a tab asks about its connection, and a denial raises nothing") + func focusHonoursADenial() async throws { + let presenter = RecordingApprovalPresenter(answer: false) + let policy = makePolicy(approval: .oncePerConnection, presenter: presenter) + let payroll = tab(on: withheld, title: "payroll") + + let result = try await FocusQueryTabTool(collectSnapshots: { [payroll] }).call( + arguments: .object(["tab_id": .string(payroll.tabId.uuidString)]), + context: context(), + services: MCPToolTestHarness.services(authPolicy: policy) + ) + + #expect(result.isError) + let text = MCPToolTestHarness.errorText(result) ?? "" + #expect(text.hasPrefix("denied:")) + #expect(!text.contains(withheld.uuidString)) + #expect(await presenter.askedCount == 1) + } + + @Test("Focusing a tab the user allows goes on to look for its window") + func focusAfterAnAllowLooksForTheWindow() async throws { + let presenter = RecordingApprovalPresenter(answer: true) + let policy = makePolicy(approval: .everyTime, presenter: presenter) + let payroll = tab(on: withheld, title: "payroll") + + let result = try await FocusQueryTabTool(collectSnapshots: { [payroll] }).call( + arguments: .object(["tab_id": .string(payroll.tabId.uuidString)]), + context: context(), + services: MCPToolTestHarness.services(authPolicy: policy) + ) + + #expect(await presenter.askedCount == 1) + #expect(MCPToolTestHarness.errorText(result)?.hasPrefix("not_found:") == true) + } +} diff --git a/apple/TableProTests/Core/MCP/MCPAnswerBudgetPayloadTests.swift b/apple/TableProTests/Core/MCP/MCPAnswerBudgetPayloadTests.swift new file mode 100644 index 0000000000..1fae86a8f1 --- /dev/null +++ b/apple/TableProTests/Core/MCP/MCPAnswerBudgetPayloadTests.swift @@ -0,0 +1,159 @@ +// +// MCPAnswerBudgetPayloadTests.swift +// TableProTests +// +// An answer the size limit cut tells the client so: is_truncated stays the flag it reads, and notes say why. +// + +import Foundation +@testable import TablePro +import TableProMCP +import TableProPluginKit +import Testing + +@MainActor +struct MCPAnswerBudgetPayloadTests { + private let scope = DatabaseScope(connectionId: UUID(), database: "shop", schema: nil) + + private func textResult(_ values: [String]) -> QueryResult { + ScriptAnsweringDriver.resultSet(columns: ["body"], rows: values.map { [$0] }) + } + + @Test("An answer with a cut value and rows left out carries both notes and is_truncated") + func cutAnswerCarriesNotes() throws { + var budget = ExternalAnswerBudget(bytes: 70_000) + let kept = budget.keep(textResult([String(repeating: "a", count: 100_000), String(repeating: "b", count: 10_000)])) + + let payload = MCPConnectionBridge.encode( + script: .statement(kept, executionTimeMs: 1, budget: budget), + scope: scope + ) + + #expect(payload["row_count"]?.intValue == 1) + #expect(payload["is_truncated"]?.boolValue == true) + #expect(payload["notes"]?.arrayValue?.compactMap { $0.stringValue } == budget.notes) + #expect(budget.notes.count == 2) + } + + @Test("An answer that fit carries no notes") + func fittingAnswerHasNoNotes() { + var budget = ExternalAnswerBudget.mcpAnswer + let kept = budget.keep(textResult(["1", "2"])) + + let payload = MCPConnectionBridge.encode(script: .statement(kept, executionTimeMs: 1, budget: budget), scope: scope) + + #expect(payload["notes"] == nil) + #expect(payload["is_truncated"]?.boolValue == false) + } + + @Test("execute_query through the bridge holds a large read to 4 MiB and says so") + func executeQueryIsHeldToTheBudget() async throws { + let driver = AnsweringPluginDriver { _ in + AnsweringPluginDriver.textRows(column: "body", count: 3_000, bytes: 2_048) + } + let scope = AnsweringPluginDriver.install(driver, connection: TestFixtures.makeConnection(database: "shop", type: .postgresql)) + defer { DatabaseManager.shared.removeSession(for: scope.connectionId) } + + let payload = try await MCPConnectionBridge().executeQuery( + scope: scope, query: "SELECT body FROM documents", maxRows: 3_000, timeoutSeconds: 30, cancellation: nil + ) + + let rowCount = try #require(payload["row_count"]?.intValue) + #expect(rowCount < 3_000) + #expect(rowCount * 2_048 <= ExternalAnswerBudget.answerBytes) + #expect(payload["rows"]?.arrayValue?.count == rowCount) + #expect(payload["is_truncated"]?.boolValue == true) + #expect(payload["notes"]?.arrayValue?.count == 1) + } + + @Test("The assistant's execute_query holds its answer to 256 KiB") + func assistantAnswerIsSmaller() async throws { + let driver = AnsweringPluginDriver { _ in + AnsweringPluginDriver.textRows(column: "body", count: 500, bytes: 2_048) + } + let scope = AnsweringPluginDriver.install(driver, connection: TestFixtures.makeConnection(database: "shop", type: .postgresql)) + defer { DatabaseManager.shared.removeSession(for: scope.connectionId) } + let context = ChatToolContext( + connectionId: scope.connectionId, + bridge: MCPConnectionBridge(), + authPolicy: MCPAuthPolicy( + connectionResolver: { _ in nil }, + connectionIdsProvider: { [] }, + historyRecorder: RecordingQueryHistory(), + executionGate: AlwaysAllowGate() + ), + policyLookup: { _ in .alwaysAllow } + ) + + let result = try await ExecuteQueryChatTool().execute( + input: .object(["query": .string("SELECT body FROM documents"), "max_rows": .int(500)]), + context: context + ) + + #expect(!result.isError) + let payload = try JSONDecoder().decode(JsonValue.self, from: Data(result.content.utf8)) + let rowCount = try #require(payload["row_count"]?.intValue) + #expect(rowCount > 0) + #expect(rowCount * 2_048 <= ExternalAnswerBudget.assistantAnswerBytes) + #expect(payload["is_truncated"]?.boolValue == true) + #expect(driver.sent.first?.context.resultByteLimit == ExternalAnswerBudget.assistantAnswerBytes) + } + + private func explain(planFilter filter: String) async throws -> JsonValue { + let plan = #"[{"Plan": {"Node Type": "Seq Scan", "Relation Name": "orders", "Filter": "\#(filter)"}}]"# + let connection = TestFixtures.makeConnection(database: "shop", type: .postgresql) + let driver = ScriptAnsweringDriver(connection: connection, statementAnswer: { _ in + ScriptAnsweringDriver.resultSet(columns: ["QUERY PLAN"], rows: [[plan]]) + }) + var session = ConnectionSession(connection: connection) + session.driver = driver + DatabaseManager.shared.injectSession(session, for: connection.id) + defer { DatabaseManager.shared.removeSession(for: connection.id) } + let scope = DatabaseScope(connectionId: connection.id, database: "shop", schema: nil) + let request = try MCPConnectionBridge.explainRequest( + for: "SELECT * FROM orders", + databaseType: .postgresql, + variants: [ExplainVariant(id: "json", label: "JSON", sqlPrefix: "EXPLAIN (FORMAT JSON)", format: .postgresJson)], + variantId: nil, + analyze: false + ) + let operation = try await ApprovedOperationFixture.approve( + [OperationStatement(sql: request.statement)], scope: scope, databaseType: .postgresql + ) + return try await MCPConnectionBridge().explainQuery( + operation, request: request, timeoutSeconds: 30, cancellation: nil + ) + } + + @Test("explain_query leaves out a parsed plan past the budget, so the answer stays under 4 MiB") + func explainPlanIsHeldToTheBudget() async throws { + let payload = try await explain(planFilter: String(repeating: "f", count: 3_000_000)) + + #expect(payload["plan"] == nil) + #expect((payload["plan_text"]?.stringValue?.utf8.count ?? 0) >= 3_000_000) + #expect(payload["is_truncated"]?.boolValue == true) + #expect(payload["notes"]?.arrayValue?.isEmpty == false) + #expect(payload.jsonString(prettyPrinted: false).utf8.count <= ExternalAnswerBudget.answerBytes) + } + + @Test("explain_query keeps a parsed plan that fits") + func explainPlanThatFitsIsKept() async throws { + let payload = try await explain(planFilter: "(id > 1)") + + #expect(payload["plan"]?["root"]?["operation"]?.stringValue == "Seq Scan") + #expect(payload["is_truncated"] == nil) + #expect(payload["notes"] == nil) + } + + @Test("The result schemas declare notes and keep the fields they required") + func schemasDeclareNotes() { + let executeQuery = ExecuteQueryTool.outputSchema + #expect(executeQuery?["properties"]?["notes"]?["type"]?.stringValue == "array") + #expect(executeQuery?["required"] == MCPToolSchema.resultSet["required"]) + #expect(executeQuery?["properties"]?["result_sets"]?["items"]?["properties"]?["notes"] == nil) + #expect(MCPToolSchema.resultSet["properties"]?["notes"] != nil) + #expect(ExportDataTool.outputSchema?["properties"]?["notes"] != nil) + #expect(ExplainQueryTool.outputSchema?["properties"]?["notes"] != nil) + #expect(ExplainQueryTool.outputSchema?["properties"]?["is_truncated"] != nil) + } +} diff --git a/apple/TableProTests/Core/MCP/MCPHistoryAnswerBudgetTests.swift b/apple/TableProTests/Core/MCP/MCPHistoryAnswerBudgetTests.swift new file mode 100644 index 0000000000..2f688dfd9e --- /dev/null +++ b/apple/TableProTests/Core/MCP/MCPHistoryAnswerBudgetTests.swift @@ -0,0 +1,97 @@ +// +// MCPHistoryAnswerBudgetTests.swift +// TableProTests +// + +import Foundation +@testable import TablePro +import TableProMCP +import TableProPluginKit +import Testing + +struct MCPHistoryAnswerBudgetTests { + private let connectionId = UUID() + + private func entry(query: String, errorMessage: String? = nil) -> QueryHistoryEntry { + QueryHistoryEntry( + query: query, + connectionId: connectionId, + databaseName: "shop", + databaseType: .postgresql, + source: .editor, + executionTime: 0.01, + rowCount: 1, + wasSuccessful: errorMessage == nil, + errorMessage: errorMessage + ) + } + + @Test("A long statement or error in history is cut before it leaves the Mac") + func longTextIsCut() { + let statement = "INSERT INTO t VALUES " + String(repeating: "(1),", count: 50_000) + let failure = "syntax error near " + String(repeating: "y", count: 200_000) + + let query = MCPConnectionBridge.externalQueryText(of: entry(query: statement), masksValues: false) + let error = MCPConnectionBridge.externalErrorText(failure, masksValues: false) + + #expect(query.utf8.count < statement.utf8.count) + #expect(query.hasPrefix(String(statement.prefix(1_000)))) + #expect(error.utf8.count < failure.utf8.count) + #expect(error.hasPrefix("syntax error near ")) + } + + @Test("Short history text passes through whole") + func shortTextIsWhole() { + #expect(MCPConnectionBridge.externalQueryText(of: entry(query: "SELECT 1"), masksValues: false) == "SELECT 1") + } + + @Test("Entries stop at the answer budget, in order, and the answer says so") + func entriesStopAtTheBudget() { + let megabyte = String(repeating: "x", count: 1 << 20) + let entries = (0..<6).map { index in JsonValue.object(["id": .int(index), "query": .string(megabyte)]) } + + let answer = MCPConnectionBridge.withinAnswerBudget(entries) + let used = answer.entries.reduce(0) { $0 + $1.jsonString().utf8.count } + + #expect(answer.isTruncated) + #expect(!answer.entries.isEmpty) + #expect(answer.entries.count < entries.count) + #expect(used <= ExternalAnswerBudget.answerBytes) + #expect(answer.entries == Array(entries.prefix(answer.entries.count))) + } + + @Test("An answer under the budget keeps every entry") + func smallAnswerIsWhole() { + let entries = (0..<3).map { index in JsonValue.object(["id": .int(index)]) } + + let answer = MCPConnectionBridge.withinAnswerBudget(entries) + + #expect(!answer.isTruncated) + #expect(answer.entries == entries) + } + + @Test("search_query_history reports is_truncated and cuts each statement") + func searchReportsTruncation() async throws { + let statement = String(repeating: "z", count: 200_000) + let store = ToolTestQueryHistoryStore(entries: (0..<80).map { _ in entry(query: statement) }) + + let result = try await SearchQueryHistoryTool().call( + arguments: .object(["query": .string(""), "limit": .int(500)]), + context: MCPToolTestHarness.context(principal: MCPToolTestHarness.principal(access: .all)), + services: MCPToolTestHarness.services( + authPolicy: MCPToolTestHarness.authPolicy(connections: [ + connectionId: MCPToolTestHarness.snapshot(policy: .alwaysAllow) + ]), + history: store + ) + ) + + let payload = try #require(result.structuredContent) + let entries = try #require(payload["entries"]?.arrayValue) + #expect(payload["is_truncated"]?.boolValue == true) + #expect(entries.count < 80) + for entry in entries { + #expect((entry["query"]?.stringValue?.utf8.count ?? 0) < statement.utf8.count) + } + } +} diff --git a/apple/TableProTests/Core/MCP/Protocol/Handlers/ToolsCallHandlerTests.swift b/apple/TableProTests/Core/MCP/Protocol/Handlers/ToolsCallHandlerTests.swift index 019de074d0..5b01d6e98b 100644 --- a/apple/TableProTests/Core/MCP/Protocol/Handlers/ToolsCallHandlerTests.swift +++ b/apple/TableProTests/Core/MCP/Protocol/Handlers/ToolsCallHandlerTests.swift @@ -1,5 +1,6 @@ import Foundation import TableProMCP +import TableProMCPTestSupport import TableProPluginKit @testable import TablePro import Testing @@ -124,6 +125,103 @@ struct ToolsCallHandlerTests { #expect(result.payload["content"] != nil) } + @Test("A tool outside the token's allowlist is refused before it runs, with no scope to step up to") + func toolOutsideTheAllowlistIsRefused() async throws { + let history = ToolTestQueryHistoryStore() + let handler = ToolsCallHandler(services: MCPToolTestHarness.services(history: history)) + let principal = askPrincipal() + + for name in ["execute_query", "browse_table", "export_data", "search_query_history", "explain_query"] { + let params = JsonValue.object([ + "name": .string(name), + "arguments": .object(["query": .string("SELECT 1")]) + ]) + let context = await MCPProtocolHandlerTestSupport.makeContext( + method: ToolsCallHandler.method, + params: params, + principal: principal + ) + do { + _ = try await handler.handle(params: params, context: context) + Issue.record("\(name) ran for a token whose allowlist leaves it out") + } catch let error as MCPProtocolError { + #expect(error.code == JsonRpcErrorCode.forbidden) + #expect(header(named: "WWW-Authenticate", in: error) == nil) + #expect(error.data?["requiredScopes"] == nil) + } + } + #expect(await history.receivedFilters.isEmpty) + } + + @Test("A tool inside the token's allowlist still runs") + func toolInsideTheAllowlistRuns() async throws { + let params = JsonValue.object(["name": .string("list_connections"), "arguments": .object([:])]) + let context = await MCPProtocolHandlerTestSupport.makeContext( + method: ToolsCallHandler.method, + params: params, + principal: askPrincipal() + ) + + let result = try await makeHandler().handle(params: params, context: context) + + #expect(result.payload["isError"]?.boolValue == false) + } + + @Test("Under Ask Every Time, one search_query_history call that names a connection asks once") + func namedHistorySearchAsksOnce() async throws { + let connectionId = UUID() + let presenter = RecordingApprovalPresenter(answer: true) + let history = ToolTestQueryHistoryStore(entries: [ + QueryHistoryEntry( + query: "SELECT 1", + connectionId: connectionId, + databaseName: "shop", + databaseType: .postgresql, + source: .editor, + executionTime: 0.01, + rowCount: 1, + wasSuccessful: true + ) + ]) + let services = MCPToolTestHarness.services( + authPolicy: MCPToolTestHarness.authPolicy( + connections: [connectionId: MCPToolTestHarness.snapshot(policy: .askEachTime, name: "Payroll")], + approval: .everyTime, + presenter: presenter, + ledger: MCPApprovalLedger(clock: MCPTestClock(), store: MCPInMemoryApprovalStore()) + ), + history: history + ) + let params = JsonValue.object([ + "name": .string("search_query_history"), + "arguments": .object(["query": .string(""), "connection_id": .string(connectionId.uuidString)]) + ]) + let context = await MCPProtocolHandlerTestSupport.makeContext( + method: ToolsCallHandler.method, + params: params, + principalScopes: [.toolsRead] + ) + + let result = try await ToolsCallHandler(services: services).handle(params: params, context: context) + + #expect(await presenter.askedCount == 1) + #expect(result.payload["isError"]?.boolValue == false) + let entries = result.payload["structuredContent"]?["entries"]?.arrayValue ?? [] + #expect(entries.count == 1) + #expect(entries.first?["connection_id"]?.stringValue == connectionId.uuidString) + } + + private func askPrincipal() -> MCPPrincipal { + MCPPrincipal( + tokenFingerprint: "ask", + tokenId: UUID(), + scopes: [.toolsRead], + connectionAccess: .limited([]), + metadata: MCPPrincipalMetadata(label: "ask", issuedAt: .distantPast, expiresAt: nil), + toolAllowlist: MCPToolProfile.schemaLookup.toolNames + ) + } + private func makeHandler() -> ToolsCallHandler { ToolsCallHandler(services: MCPProtocolHandlerTestSupport.makeToolServices()) } diff --git a/apple/TableProTests/Core/MCP/Protocol/Handlers/ToolsListHandlerTests.swift b/apple/TableProTests/Core/MCP/Protocol/Handlers/ToolsListHandlerTests.swift index 94ccf79144..dd515d5320 100644 --- a/apple/TableProTests/Core/MCP/Protocol/Handlers/ToolsListHandlerTests.swift +++ b/apple/TableProTests/Core/MCP/Protocol/Handlers/ToolsListHandlerTests.swift @@ -35,6 +35,30 @@ struct ToolsListHandlerTests { #expect(Set(readOnly).isSubset(of: Set(readWrite))) } + @Test("A principal held to a tool allowlist sees only those tools, even with wider scopes") + func allowlistNarrowsTheListing() async throws { + let allowlist = MCPToolProfile.schemaLookup.toolNames + let principal = MCPPrincipal( + tokenFingerprint: "ask", + tokenId: UUID(), + scopes: [.toolsRead, .toolsWrite], + metadata: MCPPrincipalMetadata(label: "ask", issuedAt: .distantPast, expiresAt: nil), + toolAllowlist: allowlist + ) + let context = await MCPProtocolHandlerTestSupport.makeContext( + method: ToolsListHandler.method, + principal: principal + ) + + let result = try await ToolsListHandler().handle(params: nil, context: context) + let names = try #require(result.payload["tools"]?.arrayValue).compactMap { $0["name"]?.stringValue } + + #expect(Set(names) == allowlist) + #expect(names.count == allowlist.count) + #expect(!names.contains("execute_query")) + #expect(!names.contains("connect")) + } + @Test("A principal with no scopes sees no tools at all") func noScopesListsNothing() async throws { let payload = try await runToolsList(scopes: []) diff --git a/apple/TableProTests/Core/MCP/Protocol/Tools/ExportDataBudgetTests.swift b/apple/TableProTests/Core/MCP/Protocol/Tools/ExportDataBudgetTests.swift new file mode 100644 index 0000000000..4538de797b --- /dev/null +++ b/apple/TableProTests/Core/MCP/Protocol/Tools/ExportDataBudgetTests.swift @@ -0,0 +1,70 @@ +// +// ExportDataBudgetTests.swift +// TableProTests +// + +import Foundation +@testable import TablePro +import TableProMCP +import TableProPluginKit +import Testing + +@MainActor +struct ExportDataBudgetTests { + nonisolated private static let wideValue = String(repeating: "w", count: 100_000) + + // `wide` holds values past 64 KiB, `big` alone passes 4 MiB, and `after` comes once the answer is spent. + nonisolated private static func answer(_ query: String) -> QueryResult { + if query.contains("\"wide\"") { + return ScriptAnsweringDriver.resultSet(columns: ["doc"], rows: Array(repeating: [wideValue], count: 3)) + } + if query.contains("\"big\"") { + return ScriptAnsweringDriver.resultSet( + columns: ["body"], + rows: Array(repeating: [String(repeating: "x", count: 1_000)], count: 4_500) + ) + } + return ScriptAnsweringDriver.resultSet(columns: ["n"], rows: Array(repeating: ["1"], count: 5)) + } + + @Test("The tables of one export share one budget, and every value stays whole") + func tablesShareOneBudget() async throws { + let connection = TestFixtures.makeConnection(database: "shop", type: .postgresql) + let driver = ScriptAnsweringDriver(connection: connection, statementAnswer: Self.answer) + var session = ConnectionSession(connection: connection) + session.driver = driver + DatabaseManager.shared.injectSession(session, for: connection.id) + defer { DatabaseManager.shared.removeSession(for: connection.id) } + + let result = try await ExportDataTool().perform( + arguments: .object([ + "connection_id": .string(connection.id.uuidString), + "format": .string("csv"), + "tables": .array([.string("wide"), .string("big"), .string("after")]), + "max_rows": .int(5_000) + ]), + context: MCPToolTestHarness.context(), + services: MCPToolTestHarness.services( + authPolicy: MCPToolTestHarness.authPolicy(executionGate: AlwaysAllowGate()), + settings: MCPSettings(defaultRowLimit: 500, maxRowLimit: 10_000) + ) + ) + + let payload = try #require(result.structuredContent) + let exports = try #require(payload["exports"]?.arrayValue) + try #require(exports.count == 3) + #expect(exports[0]["row_count"]?.intValue == 3) + #expect(exports[0]["is_truncated"]?.boolValue == false) + let wide = try #require(exports[0]["data"]?.stringValue) + #expect(wide.contains(Self.wideValue)) + #expect(!wide.contains("TablePro cut")) + let bigRows = try #require(exports[1]["row_count"]?.intValue) + #expect(bigRows > 0 && bigRows < 4_500) + #expect((bigRows * 1_000) + (3 * 100_000) <= ExternalAnswerBudget.answerBytes) + #expect(exports[1]["is_truncated"]?.boolValue == true) + #expect(exports[2]["row_count"]?.intValue == 0) + #expect(exports[2]["is_truncated"]?.boolValue == true) + #expect(payload["is_truncated"]?.boolValue == true) + #expect(payload["notes"]?.arrayValue?.count == 1) + } +} diff --git a/apple/TableProTests/Core/MCP/Protocol/Tools/MCPToolErrorSurfaceTests.swift b/apple/TableProTests/Core/MCP/Protocol/Tools/MCPToolErrorSurfaceTests.swift index e07fa7792d..ece2d3f664 100644 --- a/apple/TableProTests/Core/MCP/Protocol/Tools/MCPToolErrorSurfaceTests.swift +++ b/apple/TableProTests/Core/MCP/Protocol/Tools/MCPToolErrorSurfaceTests.swift @@ -48,6 +48,12 @@ struct MCPToolErrorSurfaceTests { #expect(MCPToolTestHarness.errorText(result) == "query_failed: syntax error at or near \"SELEC\"") } + @Test("The assistant's transcript reads a tool failure's own message, not Foundation's generic text") + func localizedDescriptionCarriesTheMessage() { + let error: Error = MCPToolExecutionError.queryFailed("relation \"ordrs\" does not exist") + #expect(error.localizedDescription == "query_failed: relation \"ordrs\" does not exist") + } + @Test("A timeout and a denied write also come back as results, not protocol errors") func timeoutAndDenialAreToolResults() async throws { let timedOut = try await result(for: MCPToolExecutionError.timedOut("Query timed out after 30 seconds")) diff --git a/apple/TableProTests/Core/MCP/Protocol/Tools/MCPToolTestHarness.swift b/apple/TableProTests/Core/MCP/Protocol/Tools/MCPToolTestHarness.swift index 9c988fb871..2b926da0b7 100644 --- a/apple/TableProTests/Core/MCP/Protocol/Tools/MCPToolTestHarness.swift +++ b/apple/TableProTests/Core/MCP/Protocol/Tools/MCPToolTestHarness.swift @@ -166,13 +166,26 @@ enum MCPToolTestHarness { } // A test that reaches a confirmation passes a stub gate: the default is the real alert gate. + // A test whose connection asks passes an approval and a presenter: the defaults read Settings and raise an alert. static func authPolicy( connections: [UUID: MCPConnectionAuthSnapshot] = [:], - executionGate: any ExecutionGate = ExecutionGateProvider.shared + executionGate: any ExecutionGate = ExecutionGateProvider.shared, + approval: MCPConnectionApproval? = nil, + presenter: (any MCPApprovalPresenting)? = nil, + ledger: MCPApprovalLedger? = nil ) -> MCPAuthPolicy { - MCPAuthPolicy( + let approvalSetting: any MCPConnectionApprovalReading + if let approval { + approvalSetting = MCPFixedApprovalReader(approval) + } else { + approvalSetting = MCPSettingsApprovalReader() + } + return MCPAuthPolicy( connectionResolver: { id in connections[id] }, connectionIdsProvider: { Set(connections.keys) }, + approvalLedger: ledger ?? MCPApprovalLedger(), + approvalSetting: approvalSetting, + presenter: presenter ?? MCPApprovalAlertPresenter(), executionGate: executionGate ) } diff --git a/apple/TableProTests/Core/MCP/Protocol/Tools/StoppedWriteOutcomeTests.swift b/apple/TableProTests/Core/MCP/Protocol/Tools/StoppedWriteOutcomeTests.swift new file mode 100644 index 0000000000..ca1b8802a0 --- /dev/null +++ b/apple/TableProTests/Core/MCP/Protocol/Tools/StoppedWriteOutcomeTests.swift @@ -0,0 +1,218 @@ +// +// StoppedWriteOutcomeTests.swift +// TableProTests +// +// A write already in the driver runs to its end when the caller stops waiting. The caller used to hear a plain +// CancellationError, history kept no row, and the write committed anyway. +// + +import Foundation +@testable import TablePro +import TableProMCP +import TableProPluginKit +import TableProTestSupport +import Testing + +private struct StoppedTool: MCPToolImplementation { + static let name = "stopped_test_tool" + static let description = "A tool whose write was stopped in the driver." + static let requiredScopes: Set = [.toolsRead] + static let inputSchema = MCPToolSchema.empty + + let cancelsItsTask: Bool + + func perform( + arguments: JsonValue, + context: MCPRequestContext, + services: MCPToolServices + ) async throws -> MCPToolCallResult { + if cancelsItsTask { + withUnsafeCurrentTask { $0?.cancel() } + } + throw DatabaseAccessBridge.stoppedWhileRunning + } +} + +@MainActor +struct StoppedWriteOutcomeTests { + private static let write = "UPDATE orders SET n = n + 1 WHERE id = 1" + + private func makeServices(history: RecordingQueryHistory) -> MCPToolServices { + MCPToolServices( + connectionBridge: MCPConnectionBridge(), + authPolicy: MCPAuthPolicy( + connectionResolver: { _ in nil }, + connectionIdsProvider: { [] }, + historyRecorder: history, + executionGate: AlwaysAllowGate() + ) + ) + } + + @Test("A write stopped in the driver is recorded in history as failed, with the outcome unknown") + func stoppedWriteIsRecorded() async throws { + let gate = AsyncGate() + let driver = AnsweringPluginDriver(hold: gate) + let scope = AnsweringPluginDriver.install(driver, connection: TestFixtures.makeConnection(database: "shop", type: .postgresql)) + defer { DatabaseManager.shared.removeSession(for: scope.connectionId) } + let history = RecordingQueryHistory() + let services = makeServices(history: history) + let operation = try await ApprovedOperationFixture.approve( + [OperationStatement(sql: Self.write)], scope: scope, databaseType: .postgresql + ) + + let caller = Task { + try await ToolQueryExecutor.executeAndLog( + services: services, + operation: operation, + maxRows: 500, + timeoutSeconds: 30, + principal: MCPToolTestHarness.principal() + ) + } + await gate.arrived(1) + caller.cancel() + let error = await #expect(throws: DatabaseAccessError.self) { _ = try await caller.value } + + guard case .stoppedWhileRunning? = error else { + Issue.record("expected the outcome to be unknown, got \(String(describing: error))") + return + } + let rows = history.requests + #expect(rows.count == 1) + #expect(rows.first?.query == Self.write) + #expect(rows.first?.wasSuccessful == false) + #expect(rows.first?.errorMessage == MCPErrorRedactor.message(for: DatabaseAccessBridge.stoppedWhileRunning)) + #expect(driver.finishedCount == 0) + + await gate.open() + try await waitUntil { driver.finishedCount == 1 } + } + + @Test("A cancelled MCP request whose write was in the driver keeps -33002 and says the outcome is unknown") + func cancelledRequestKeepsItsCode() async throws { + do { + _ = try await Task { + try await StoppedTool(cancelsItsTask: true).call( + arguments: .object([:]), + context: MCPToolTestHarness.context(), + services: MCPToolTestHarness.services() + ) + }.value + Issue.record("Expected a cancellation protocol error") + } catch let error as MCPProtocolError { + #expect(error.code == JsonRpcErrorCode.requestCancelled) + #expect(error.data == .object(["outcome": .string("unknown")])) + } + } + + @Test("Outside a cancelled request the stopped write is a timeout result that says it may have been applied") + func uncancelledStopIsATimeoutResult() async throws { + let result = try await StoppedTool(cancelsItsTask: false).call( + arguments: .object([:]), + context: MCPToolTestHarness.context(), + services: MCPToolTestHarness.services() + ) + + #expect(result.isError) + #expect(MCPToolTestHarness.errorText(result) == "timeout: \(DatabaseAccessBridge.stoppedWhileRunning.message)") + } + + @Test("An analyzed write explain_query runs is recorded in history, and a plan of a read is not") + func analyzedWriteIsRecorded() async throws { + let connection = TestFixtures.makeConnection(database: "shop", type: .postgresql) + let driver = ScriptAnsweringDriver(connection: connection, statementAnswer: { _ in + ScriptAnsweringDriver.resultSet(columns: ["QUERY PLAN"], rows: [["Update on orders"]]) + }) + var session = ConnectionSession(connection: connection) + session.driver = driver + DatabaseManager.shared.injectSession(session, for: connection.id) + defer { DatabaseManager.shared.removeSession(for: connection.id) } + let scope = DatabaseScope(connectionId: connection.id, database: "shop", schema: nil) + let variants = [ + ExplainVariant(id: "plan", label: "Plan", sqlPrefix: "EXPLAIN"), + ExplainVariant(id: "analyze", label: "Analyze", sqlPrefix: "EXPLAIN ANALYZE") + ] + let analyzedWrite = try MCPConnectionBridge.explainRequest( + for: Self.write, databaseType: .postgresql, variants: variants, variantId: nil, analyze: true + ) + let plannedRead = try MCPConnectionBridge.explainRequest( + for: "SELECT 1", databaseType: .postgresql, variants: variants, variantId: nil, analyze: false + ) + let history = RecordingQueryHistory() + let services = makeServices(history: history) + + for (request, kind) in [(analyzedWrite, OperationKind.writeQuery), (plannedRead, .readQuery)] { + let operation = try await ApprovedOperationFixture.approve( + [OperationStatement(sql: request.statement)], scope: scope, databaseType: .postgresql, kind: kind + ) + _ = try await ToolQueryExecutor.explainAndLog( + services: services, + operation: operation, + request: request, + timeoutSeconds: 30, + context: MCPToolTestHarness.context(), + secrets: [] + ) + } + + #expect(driver.sentStatements == [analyzedWrite.statement, plannedRead.statement]) + #expect(history.requests.map { $0.query } == [analyzedWrite.statement]) + #expect(history.requests.first?.wasSuccessful == true) + } +} + +// A request that reaches a write needs a saved connection open to external writes. +extension GlobalStateSuites { + @MainActor + struct StoppedWriteRequestTests { + @Test("execute_query cancelled during a write in the driver answers -33002, and history keeps the write") + func executeQueryCancelledInTheDriver() async throws { + let gate = AsyncGate() + let driver = AnsweringPluginDriver(hold: gate) + var connection = TestFixtures.makeConnection(database: "shop", type: .postgresql) + connection.externalAccess = .readWrite + let savedBefore = ConnectionStorage.shared.loadConnections() + ConnectionStorage.shared.saveConnections(savedBefore + [connection]) + let scope = AnsweringPluginDriver.install(driver, connection: connection) + defer { + DatabaseManager.shared.removeSession(for: scope.connectionId) + ConnectionStorage.shared.saveConnections(savedBefore) + } + let history = RecordingQueryHistory() + let services = MCPToolServices( + connectionBridge: MCPConnectionBridge(), + authPolicy: MCPAuthPolicy( + connectionResolver: { _ in nil }, + connectionIdsProvider: { [] }, + historyRecorder: history, + executionGate: AlwaysAllowGate() + ) + ) + + let caller = Task { + try await ExecuteQueryTool().call( + arguments: .object([ + "connection_id": .string(connection.id.uuidString), + "query": .string("UPDATE orders SET n = n + 1 WHERE id = 1") + ]), + context: MCPToolTestHarness.context(), + services: services + ) + } + await gate.arrived(1) + caller.cancel() + do { + _ = try await caller.value + Issue.record("Expected the cancelled request to end as a cancellation") + } catch let error as MCPProtocolError { + #expect(error.code == JsonRpcErrorCode.requestCancelled) + #expect(error.data == .object(["outcome": .string("unknown")])) + } + #expect(history.requests.map { $0.wasSuccessful } == [false]) + + await gate.open() + try await waitUntil { driver.finishedCount == 1 } + } + } +} diff --git a/apple/TableProTests/Core/Plugins/PluginDriverAdapterStatementContextTests.swift b/apple/TableProTests/Core/Plugins/PluginDriverAdapterStatementContextTests.swift index 8844e795ba..61889d05b5 100644 --- a/apple/TableProTests/Core/Plugins/PluginDriverAdapterStatementContextTests.swift +++ b/apple/TableProTests/Core/Plugins/PluginDriverAdapterStatementContextTests.swift @@ -24,13 +24,21 @@ private final class ContextRecordingDriver: PluginDriverStub, @unchecked Sendabl private let lock = NSLock() private var recorded: [PluginStatementContext] = [] + private var caps: [Int?] = [] var contexts: [PluginStatementContext] { lock.withLock { recorded } } - private func record(_ context: PluginStatementContext) { - lock.withLock { recorded.append(context) } + var rowCaps: [Int?] { + lock.withLock { caps } + } + + private func record(_ context: PluginStatementContext, rowCap: Int? = nil) { + lock.withLock { + recorded.append(context) + caps.append(rowCap) + } } private static let empty = PluginQueryResult( @@ -59,7 +67,7 @@ private final class ContextRecordingDriver: PluginDriverStub, @unchecked Sendabl parameters: [PluginCellValue]?, context: PluginStatementContext ) async throws -> PluginQueryResult { - record(context) + record(context, rowCap: rowCap) return Self.empty } @@ -69,6 +77,32 @@ private final class ContextRecordingDriver: PluginDriverStub, @unchecked Sendabl } } +// A driver that bounds its read under a context, as one declaring `.enforcesReadOnlyMode` does. +private final class BoundingContextDriver: PluginDriverStub, @unchecked Sendable { + var capabilities: PluginCapabilities { [.readOnlyStatements, .enforcesReadOnlyMode] } + + private let lock = NSLock() + private var recorded: [PluginStatementContext?] = [] + + var boundedContexts: [PluginStatementContext?] { + lock.withLock { recorded } + } + + func executeBoundedQuery(query: String, rowCap: Int) async throws -> PluginQueryResult? { + lock.withLock { recorded.append(nil) } + return .empty + } + + func executeBoundedQuery( + query: String, + rowCap: Int, + context: PluginStatementContext + ) async throws -> PluginQueryResult? { + lock.withLock { recorded.append(context) } + return .empty + } +} + struct PluginDriverAdapterStatementContextTests { private func makeAdapter(_ type: DatabaseType, driver: ContextRecordingDriver) -> PluginDriverAdapter { PluginDriverAdapter(connection: DatabaseConnection(name: "Test", type: type), pluginDriver: driver) @@ -359,3 +393,102 @@ struct PluginDriverAdapterParameterKindTests { #expect(PluginDriverAdapter.cellValue(for: TypedParameterValue(text: "1E5", kind: .text)) == .text("1E5")) } } + +// Read-Only Safe Mode reaches the driver as `enforcesReadOnly`, only where the driver declares it can hold the engine to +// it, and never on the app's own catalog reads. +struct PluginDriverAdapterReadOnlyModeTests { + private static let readOnly = PluginStatementContext(enforcesReadOnly: true) + + private func adapter(_ driver: any PluginDatabaseDriver, type: DatabaseType = .postgresql) -> PluginDriverAdapter { + PluginDriverAdapter(connection: DatabaseConnection(name: "Test", type: type), pluginDriver: driver) + } + + @Test("Only a driver that declares the mode reports that it enforces Read-Only") + func adapterReportsTheMode() { + #expect(!adapter(ContextRecordingDriver(capabilities: .readOnlyStatements)).enforcesReadOnlyMode) + #expect(adapter(ContextRecordingDriver(capabilities: .enforcesReadOnlyMode)).enforcesReadOnlyMode) + } + + @Test("A run carries the field only at Read-Only and only on a driver that declares the mode") + func runContextNeedsTheLevelAndTheMode() { + let enforcing = adapter(ContextRecordingDriver(capabilities: .enforcesReadOnlyMode)) + let older = adapter(ContextRecordingDriver(capabilities: .readOnlyStatements)) + + #expect(enforcing.readOnlyRunContext(safeModeIsReadOnly: true)?.enforcesReadOnly == true) + #expect(enforcing.readOnlyRunContext(safeModeIsReadOnly: false) == nil) + #expect(older.readOnlyRunContext(safeModeIsReadOnly: true) == nil) + } + + @Test("Every entry point that takes a context forwards the field to the driver") + func everyEntryPointForwardsTheField() async throws { + let driver = ContextRecordingDriver(capabilities: .enforcesReadOnlyMode) + let adapter = adapter(driver) + let read = "SELECT purge_orders()" + + _ = try await adapter.executeUserQuery(query: read, rowCap: 100, parameters: nil, context: Self.readOnly) + _ = try await adapter.executeParameterized(query: "SELECT $1", parameters: [1], context: Self.readOnly) + _ = try await adapter.execute(query: read, context: Self.readOnly) + for try await _ in adapter.streamExportRows(query: read, safeModeIsReadOnly: true) {} + for try await _ in adapter.streamRows(query: read, safeModeIsReadOnly: true) {} + + #expect(driver.contexts.count == 5) + #expect(driver.contexts.allSatisfy { $0.enforcesReadOnly }) + #expect(driver.contexts.allSatisfy { $0.origin == .app && !$0.readOnly }) + } + + @Test("The bounded read takes the context") + func boundedReadForwardsTheField() async throws { + let driver = BoundingContextDriver() + let adapter = adapter(driver) + + _ = try await adapter.executeBoundedQuery(query: "SELECT 1", rowCap: 10, context: Self.readOnly) + _ = try await adapter.executeBoundedQuery(query: "SELECT 1", rowCap: 10) + + #expect(driver.boundedContexts.count == 2) + #expect(driver.boundedContexts.first??.enforcesReadOnly == true) + #expect(driver.boundedContexts.last == .some(nil)) + } + + @Test("A driver that cannot bound under the context falls back to the user query with the same context and cap") + func boundedFallbackKeepsTheContext() async throws { + let driver = ContextRecordingDriver(capabilities: .enforcesReadOnlyMode) + let adapter = adapter(driver) + + #expect(try await adapter.executeBoundedQuery(query: "SELECT 1", rowCap: 10, context: Self.readOnly) == nil) + _ = try await QueryExecutor.fetchQueryData(driver: adapter, sql: "SELECT 1", rowCap: 10, context: Self.readOnly) + + #expect(driver.contexts.map { $0.enforcesReadOnly } == [true]) + #expect(driver.rowCaps == [10]) + } + + @Test("At Silent nothing changes: the export stream keeps the default context") + func silentKeepsTheDefault() async throws { + let driver = ContextRecordingDriver(capabilities: .enforcesReadOnlyMode) + let adapter = adapter(driver) + + for try await _ in adapter.streamExportRows(query: "SELECT 1") {} + _ = try await adapter.executeUserQuery(query: "SELECT 1", rowCap: nil, parameters: nil) + + #expect(driver.contexts.count == 2) + #expect(driver.contexts.allSatisfy { !$0.enforcesReadOnly }) + } + + @Test("A driver without the mode is never told to enforce it, even at Read-Only") + func olderDriverIsNotTold() async throws { + let driver = ContextRecordingDriver(capabilities: .readOnlyStatements) + + for try await _ in adapter(driver).streamExportRows(query: "SELECT 1", safeModeIsReadOnly: true) {} + + #expect(driver.contexts.map { $0.enforcesReadOnly } == [false]) + } + + @Test("A catalog read never carries the field") + func catalogReadIsNotMarked() async throws { + let driver = ContextRecordingDriver(capabilities: .enforcesReadOnlyMode) + + _ = try await adapter(driver).executeCatalogRead(query: "SELECT 1 FROM pg_catalog.pg_class") + + #expect(driver.contexts.map { $0.enforcesReadOnly } == [false]) + #expect(driver.contexts.first?.readsCatalog == true) + } +} diff --git a/apple/TableProTests/Core/Plugins/PluginMetadataRegistryKingbaseESTests.swift b/apple/TableProTests/Core/Plugins/PluginMetadataRegistryKingbaseESTests.swift index 88182458fc..6cf10aa5c6 100644 --- a/apple/TableProTests/Core/Plugins/PluginMetadataRegistryKingbaseESTests.swift +++ b/apple/TableProTests/Core/Plugins/PluginMetadataRegistryKingbaseESTests.swift @@ -24,6 +24,15 @@ struct PluginMetadataRegistryKingbaseESTests { #expect(snapshot.connection.additionalConnectionFields.map(\.id) == ["usePgpass", "connectionOptions"]) } + // Its driver is PostgreSQL's, which runs an external read in START TRANSACTION READ ONLY, so the Options pane says so. + @Test("KingbaseES describes Read Only for external clients as PostgreSQL does") + func readOnlyNoteMatchesItsDriver() { + #expect(ReadOnlyEnforcement(for: .kingbasees) == .sessionDefault) + #expect(ReadOnlyEnforcement(for: .kingbasees) == ReadOnlyEnforcement(for: .postgresql)) + #expect(ReadOnlyEnforcement(for: .kingbasees, liveDriverRunsReadOnly: true) == .sessionDefault) + #expect(ReadOnlyEnforcement(for: .kingbasees, liveDriverRunsReadOnly: false) == .loginPrivileges) + } + @Test("Both KingbaseES URL schemes keep credentials and route to the dedicated engine", arguments: ["kingbase", "kingbase8"]) func connectionURLs(scheme: String) throws { let parsed = try ConnectionURLParser.parse("\(scheme)://system:pass@localhost:55432/test").get() diff --git a/apple/TableProTests/Core/Process/PipeLineSplitterTests.swift b/apple/TableProTests/Core/Process/PipeLineSplitterTests.swift new file mode 100644 index 0000000000..9505dee992 --- /dev/null +++ b/apple/TableProTests/Core/Process/PipeLineSplitterTests.swift @@ -0,0 +1,30 @@ +import Foundation +@testable import TablePro +import Testing + +struct PipeLineSplitterTests { + @Test("A character split across two chunks arrives whole") + func multibyteCharacterAcrossChunks() { + var splitter = PipeLineSplitter() + let bytes = Data("café\n".utf8) + let cut = bytes.firstIndex(of: 0xC3).map { bytes.index(after: $0) } ?? bytes.endIndex + + #expect(splitter.lines(appending: bytes[.. DefaultExecutionGate { + DefaultExecutionGate( + confirming: confirm, + authenticating: StubAuthenticating(answer: true), + safeModeLevelResolver: { _ in level }, + forcesWriteResolver: { _ in false }, + readOnlyFunctionPolicyResolver: { _, _ in policy } + ) + } + + private static let writableFunctions = ReadOnlyFunctionPolicy(functionsCanWrite: true, levelIsHeld: false) + + @Test("Read-Only asks before a read that calls a function the engine could let write") + func readOnlyAsksBeforeAFunctionCall() async { + let confirm = StubConfirming(answer: true) + let gate = makeReadOnlyGate(policy: Self.writableFunctions, confirm: confirm) + + let decision = await gate.authorize(makeRequest(sql: "SELECT purge_orders()", kind: .readQuery)) + + #expect(decision.isAuthorized) + #expect(confirm.callCount == 1) + #expect(confirm.lastRequest?.sql == "SELECT purge_orders()") + #expect(confirm.lastDestructive == false) + } + + @Test("A Cancel at that question is a Cancel, and nothing runs") + func readOnlyFunctionCallCancelled() async { + let confirm = StubConfirming(answer: false) + let gate = makeReadOnlyGate(policy: Self.writableFunctions, confirm: confirm) + + let decision = await gate.authorize(makeRequest(sql: "SELECT purge_orders()", kind: .readQuery)) + + guard case .denied(_, let cause) = decision else { + Issue.record("A Cancel must deny") + return + } + #expect(cause == .cancelledByUser) + } + + @Test("A managed policy or an engine floor holding Read-Only refuses the call without asking") + func heldReadOnlyRefusesAFunctionCall() async { + let confirm = StubConfirming(answer: true) + let gate = makeReadOnlyGate( + policy: ReadOnlyFunctionPolicy(functionsCanWrite: true, levelIsHeld: true), + confirm: confirm + ) + + let decision = await gate.authorize(makeRequest(sql: "SELECT purge_orders()", kind: .readQuery)) + + guard case .denied(let reason, let cause) = decision else { + Issue.record("A held Read-Only must refuse") + return + } + #expect(reason.contains("Read-Only")) + #expect(cause == .policy) + #expect(confirm.callCount == 0) + } + + @Test("A caller that cannot prompt is denied the call") + func readOnlyFunctionCallDeniedWithoutPrompt() async { + let confirm = StubConfirming(answer: true) + let gate = makeReadOnlyGate(policy: Self.writableFunctions, confirm: confirm) + + let decision = await gate.authorize( + makeRequest(sql: "SELECT purge_orders()", kind: .readQuery, capabilities: [.cannotPrompt]) + ) + + #expect(decision.deniedReason?.contains("Confirmation") == true) + #expect(confirm.callCount == 0) + } + + @Test( + "Read-Only runs built-ins, keywords before a parenthesis and a plain EXPLAIN without asking", + arguments: [ + "SELECT COUNT(*), MAX(total), LOWER(name) FROM orders WHERE id IN (1, 2) AND EXISTS (SELECT 1)", + "SELECT ROW_NUMBER() OVER (PARTITION BY region ORDER BY total) FROM orders", + "SELECT 'purge_orders()' FROM orders -- purge_orders()", + "EXPLAIN SELECT purge_orders()", + ] + ) + func readOnlyRunsBuiltins(sql: String) async { + let confirm = StubConfirming(answer: false) + let gate = makeReadOnlyGate(policy: Self.writableFunctions, confirm: confirm) + + let decision = await gate.authorize(makeRequest(sql: sql, kind: .readQuery)) + + #expect(decision.isAuthorized) + #expect(confirm.callCount == 0) + } + + @Test( + "A qualified name, a quoted name and a call in a later statement are asked about", + arguments: [ + "SELECT app.count(id) FROM orders", + "SELECT `purge_orders`() FROM orders", + "SELECT 1;\nSELECT purge_orders()", + ] + ) + func readOnlyAsksAboutAnyOtherCall(sql: String) async { + let confirm = StubConfirming(answer: false) + let gate = makeReadOnlyGate(policy: Self.writableFunctions, confirm: confirm) + + let decision = await gate.authorize( + makeRequest(sql: sql, kind: .readQuery, capabilities: CallerCapabilities.interactiveUser) + ) + + #expect(!decision.isAuthorized) + #expect(confirm.callCount == 1) + } + + @Test( + "A user function named like a keyword another engine leaves unreserved is asked about", + arguments: [ + "SELECT filter(x) FROM orders", + "SELECT `within`(1) FROM orders", + "SELECT keep(1), sets(2) FROM orders", + "SELECT pivot(total) FROM orders", + "SELECT array(1) FROM orders", + "SELECT id FROM orders WHERE total = offset(1)", + "SELECT join(id) FROM orders", + "SELECT id FROM orders WHERE status IN (purge_orders())", + ] + ) + func keywordNamedFunctionIsAsked(sql: String) async { + let confirm = StubConfirming(answer: false) + let gate = makeReadOnlyGate(policy: Self.writableFunctions, confirm: confirm) + + let decision = await gate.authorize(makeRequest(sql: sql, kind: .readQuery)) + + #expect(!decision.isAuthorized) + #expect(confirm.callCount == 1) + } + + @Test( + "A keyword in the syntax that uses it before a parenthesis runs without asking", + arguments: [ + "SELECT MAX(sal) KEEP (DENSE_RANK FIRST ORDER BY hired) FROM emp", + "SELECT LISTAGG(name, ',') WITHIN GROUP (ORDER BY name) FROM emp", + "SELECT * FROM sales PIVOT (SUM(amount) FOR quarter IN ('Q1', 'Q2'))", + "SELECT region, SUM(total) FROM orders GROUP BY ROLLUP (region)", + "SELECT region, SUM(total) FROM orders GROUP BY GROUPING SETS ((region), ())", + "SELECT * FROM orders PARTITION (p1)", + "SELECT * FROM orders WHERE total = ANY (SELECT total FROM refunds)", + "SELECT * FROM orders o JOIN (SELECT id FROM refunds) r ON o.id = r.id", + "SELECT CASE WHEN (total > 0) THEN 1 ELSE 0 END FROM orders", + ] + ) + func keywordSyntaxRuns(sql: String) async { + let confirm = StubConfirming(answer: false) + let gate = makeReadOnlyGate(policy: Self.writableFunctions, confirm: confirm) + + let decision = await gate.authorize(makeRequest(sql: sql, kind: .readQuery, databaseType: .oracle)) + + #expect(decision.isAuthorized) + #expect(confirm.callCount == 0) + } + + @Test( + "The fallback engines' everyday built-ins run without asking", + arguments: [ + (DatabaseType.oracle, "SELECT DECODE(status, 1, 'open', 'closed'), NVL2(a, b, c), INSTR(name, 'x') FROM orders"), + (.oracle, "SELECT ADD_MONTHS(created, 1), MONTHS_BETWEEN(a, b), TO_TIMESTAMP(t), SYS_GUID() FROM orders"), + (.oracle, "SELECT * FROM orders WHERE REGEXP_LIKE(name, '^a') AND TRUNC(created) = LAST_DAY(created)"), + (.dameng, "SELECT INITCAP(name), LPAD(code, 5, '0'), CHR(65) FROM orders"), + (.mysql, "SELECT DATE_FORMAT(created_at, '%Y-%m') AS m, COUNT(*) FROM orders GROUP BY m"), + (.mysql, "SELECT IF(paid, 1, 0), GROUP_CONCAT(name), JSON_EXTRACT(doc, '$.a') FROM orders USE INDEX (idx)"), + (.mariadb, "SELECT * FROM orders WHERE MATCH (title) AGAINST ('x') AND DATEDIFF(NOW(), created) < 7"), + (.oceanbase, "SELECT DATE_ADD(created, INTERVAL (1) DAY), UUID(), MD5(name) FROM orders"), + ] + ) + func everydayBuiltinsRun(type: DatabaseType, sql: String) async { + let confirm = StubConfirming(answer: false) + let gate = makeReadOnlyGate(policy: Self.writableFunctions, confirm: confirm) + + let decision = await gate.authorize(makeRequest(sql: sql, kind: .readQuery, databaseType: type)) + + #expect(decision.isAuthorized) + #expect(confirm.callCount == 0) + } + + // GET_LOCK is refused as a write before the question; the others are asked about, and the test answers Cancel. + @Test( + "A built-in that locks, sleeps or waits never runs on its own", + arguments: ["SELECT GET_LOCK('a', 10)", "SELECT SLEEP(5)", "SELECT BENCHMARK(1000, MD5('a'))"] + ) + func lockingOrSleepingBuiltinDoesNotRun(sql: String) async { + let confirm = StubConfirming(answer: false) + let gate = makeReadOnlyGate(policy: Self.writableFunctions, confirm: confirm) + + let decision = await gate.authorize(makeRequest(sql: sql, kind: .readQuery)) + + #expect(!decision.isAuthorized) + } + + @Test("The built-in lists hold nothing that writes, locks, sleeps or reaches a file") + func builtinListsHoldOnlyReads() { + let forbidden: Set = [ + "GET_LOCK", "RELEASE_LOCK", "IS_FREE_LOCK", "SLEEP", "PG_SLEEP", "BENCHMARK", "LOAD_FILE", "NEXTVAL", + "SETVAL", "LAST_INSERT_ID", "SET_CONFIG", "MASTER_POS_WAIT", "SOURCE_POS_WAIT", "TXID_CURRENT", + ] + for type in [DatabaseType.oracle, .dameng, .mysql, .mariadb, .oceanbase, .postgresql, .sqlite] { + let builtins = ReadOnlyFunctionCalls.builtins(for: type) + #expect(builtins.isDisjoint(with: forbidden)) + #expect(!builtins.contains { $0.hasPrefix("DBMS_") || $0.hasPrefix("UTL_") || $0.hasPrefix("PG_") }) + } + } + + @Test("A driver that runs the statement in the engine's read-only mode is not asked about") + func enforcingDriverRunsTheCall() async { + let confirm = StubConfirming(answer: false) + let gate = makeReadOnlyGate(policy: ReadOnlyFunctionPolicy(), confirm: confirm) + + let decision = await gate.authorize(makeRequest(sql: "SELECT purge_orders()", kind: .readQuery)) + + #expect(decision.isAuthorized) + #expect(confirm.callCount == 0) + } + + @Test("Only Read-Only asks: Silent runs the same call", arguments: [SafeModeLevel.silent, .alert]) + func otherLevelsDoNotAsk(level: SafeModeLevel) async { + let confirm = StubConfirming(answer: false) + let gate = makeReadOnlyGate(level: level, policy: Self.writableFunctions, confirm: confirm) + + let decision = await gate.authorize(makeRequest(sql: "SELECT purge_orders()", kind: .readQuery)) + + #expect(decision.isAuthorized) + #expect(confirm.callCount == 0) + } + + @Test("A metadata read is never asked about") + func metadataReadSkipsTheFunctionCheck() async { + let confirm = StubConfirming(answer: false) + let gate = makeReadOnlyGate(policy: Self.writableFunctions, confirm: confirm) + + let decision = await gate.authorize(makeRequest(sql: "SELECT pg_get_viewdef(1)", kind: .metadataRead)) + + #expect(decision.isAuthorized) + #expect(confirm.callCount == 0) + } +} + +// Which engines let a function a query calls write, and what the live driver adds to that. +@MainActor +struct ReadOnlyFunctionPolicyTests { + @Test( + "Oracle, Dameng, MySQL, MariaDB, OceanBase and the PostgreSQL family count as engines whose functions can write", + arguments: [DatabaseType.oracle, .dameng, .mysql, .mariadb, .oceanbase, .postgresql, .kingbasees] + ) + func enginesWhoseFunctionsWrite(type: DatabaseType) { + #expect(PluginMetadataRegistry.queryFunctionsCanWrite.contains(type)) + } + + @Test( + "Engines that refuse a write from a function in a query, or have no such function, do not", + arguments: [ + DatabaseType.mssql, .snowflake, .bigQuery, .trino, .redshift, .clickhouse, .sqlite, .libsql, .duckdb, + .cassandra, .mongodb, .redis, .tidb, .starrocks, .databend, + ] + ) + func enginesWhoseFunctionsDoNotWrite(type: DatabaseType) { + #expect(!PluginMetadataRegistry.queryFunctionsCanWrite.contains(type)) + } + + @Test("A live MySQL driver without the mode needs the question, and one with it does not") + func liveDriverDecides() { + let connection = DatabaseConnection(name: "Shop", database: "shop", type: .mysql, safeModeLevel: .readOnly) + defer { DatabaseManager.shared.removeSession(for: connection.id) } + + install(connection, capabilities: [.readOnlyStatements]) + let older = ExecutionGateProvider.readOnlyFunctionPolicy(connectionId: connection.id, databaseType: .mysql) + install(connection, capabilities: [.readOnlyStatements, .enforcesReadOnlyMode]) + let enforcing = ExecutionGateProvider.readOnlyFunctionPolicy(connectionId: connection.id, databaseType: .mysql) + + #expect(older.functionsCanWrite) + #expect(!older.levelIsHeld) + #expect(!enforcing.functionsCanWrite) + #expect(ExecutionGateProvider.runsAtReadOnly(connection.id)) + } + + private func install(_ connection: DatabaseConnection, capabilities: PluginCapabilities) { + var session = ConnectionSession( + connection: connection, + driver: PluginDriverAdapter(connection: connection, pluginDriver: CapabilityOnlyDriver(capabilities)) + ) + session.status = .connected + DatabaseManager.shared.injectSession(session, for: connection.id) + } +} + +private final class CapabilityOnlyDriver: PluginDriverStub, @unchecked Sendable { + private let declared: PluginCapabilities + + init(_ capabilities: PluginCapabilities) { + declared = capabilities + } + + var capabilities: PluginCapabilities { declared } } // The editor's request for a text the gate must tier by what the server runs, behind each executable comment form. diff --git a/apple/TableProTests/Helpers/AnsweringPluginDriver.swift b/apple/TableProTests/Helpers/AnsweringPluginDriver.swift new file mode 100644 index 0000000000..35fe116b38 --- /dev/null +++ b/apple/TableProTests/Helpers/AnsweringPluginDriver.swift @@ -0,0 +1,92 @@ +// +// AnsweringPluginDriver.swift +// TableProTests +// + +import Foundation +@testable import TablePro +import TableProPluginKit +import TableProTestSupport + +// A plugin driver that answers each external statement from a closure, records what reached it, and with a `hold` +// keeps the statement in the driver until the test opens the gate. +final class AnsweringPluginDriver: PluginDriverStub, @unchecked Sendable { + struct Sent { + let query: String + let rowCap: Int? + let context: PluginStatementContext + } + + var capabilities: PluginCapabilities { [.statementPolicy] } + + private let hold: AsyncGate? + private let answer: @Sendable (String) -> PluginQueryResult + private let lock = NSLock() + private var received: [Sent] = [] + private var finished = 0 + + init(hold: AsyncGate? = nil, answer: @escaping @Sendable (String) -> PluginQueryResult = { _ in .empty }) { + self.hold = hold + self.answer = answer + } + + var sent: [Sent] { + lock.withLock { received } + } + + var finishedCount: Int { + lock.withLock { finished } + } + + func executeUserQuery( + query: String, + rowCap: Int?, + parameters: [PluginCellValue]?, + context: PluginStatementContext + ) async throws -> PluginQueryResult { + lock.withLock { received.append(Sent(query: query, rowCap: rowCap, context: context)) } + if let hold { + await hold.wait() + } + lock.withLock { finished += 1 } + return answer(query) + } + + static func textRows(column: String, count: Int, bytes: Int, isTruncated: Bool = false) -> PluginQueryResult { + PluginQueryResult( + columns: [column], + columnTypeNames: ["text"], + rows: Array(repeating: [.text(String(repeating: "x", count: bytes))], count: count), + rowsAffected: 0, + executionTime: 0, + isTruncated: isTruncated + ) + } + + // Installed as the live session of a new connection, which the test removes. + @MainActor + static func install(_ driver: AnsweringPluginDriver, connection: DatabaseConnection) -> DatabaseScope { + var session = ConnectionSession( + connection: connection, + driver: PluginDriverAdapter(connection: connection, pluginDriver: driver) + ) + session.browseDatabase = connection.database + session.status = .connected + DatabaseManager.shared.injectSession(session, for: connection.id) + return DatabaseScope(connectionId: connection.id, database: connection.database, schema: nil) + } +} + +final class RecordingQueryHistory: QueryHistoryRecording, @unchecked Sendable { + private let lock = NSLock() + private var recorded: [QueryHistoryRecordRequest] = [] + + var requests: [QueryHistoryRecordRequest] { + lock.withLock { recorded } + } + + func record(_ request: QueryHistoryRecordRequest) async -> Bool { + lock.withLock { recorded.append(request) } + return true + } +} diff --git a/apple/TableProTests/ViewModels/AIChatAlwaysAllowTests.swift b/apple/TableProTests/ViewModels/AIChatAlwaysAllowTests.swift new file mode 100644 index 0000000000..792da18a78 --- /dev/null +++ b/apple/TableProTests/ViewModels/AIChatAlwaysAllowTests.swift @@ -0,0 +1,136 @@ +// +// AIChatAlwaysAllowTests.swift +// TableProTests +// + +import Foundation +import TableProMCP +import TableProSyncTransport +@testable import TablePro +import Testing + +@MainActor +struct AIChatAlwaysAllowTests { + private struct WriterTool: ChatTool { + let name = "writer" + let description = "" + let inputSchema: JsonValue = .object(["type": .string("object")]) + let mode: ChatToolMode = .write + + func execute(input: JsonValue, context: ChatToolContext) async throws -> ChatToolResult { + ChatToolResult(content: "1 row changed", isError: false) + } + } + + private let pool = EphemeralDefaultsPool() + private let storage: ConnectionStorage + private let registry: ChatToolRegistry + + init() throws { + let fileURL = FileManager.default.temporaryDirectory + .appendingPathComponent("tablepro-tests") + .appendingPathComponent("always-allow-\(UUID().uuidString).json") + try FileManager.default.createDirectory( + at: fileURL.deletingLastPathComponent(), + withIntermediateDirectories: true + ) + let tracker = SyncChangeTracker(metadataStorage: SyncMetadataStorage(userDefaults: try pool.make().defaults)) + storage = ConnectionStorage( + fileURL: fileURL, + userDefaults: try pool.make().defaults, + syncTracker: tracker, + appEvents: AppEvents() + ) + registry = ChatToolRegistry() + registry.register(WriterTool()) + } + + private func viewModel(holding connection: DatabaseConnection) -> AIChatViewModel { + let viewModel = AIChatViewModel(services: services()) + viewModel.connection = connection + return viewModel + } + + private func stored(alwaysAllowed tools: Set) throws -> DatabaseConnection { + let connection = DatabaseConnection(name: "Staging", safeModeLevel: .alert, aiAlwaysAllowedTools: tools) + try #require(storage.saveConnections([connection])) + return connection + } + + @Test("A tool forgotten in storage asks again while the pane stays open") + func forgottenToolAsksAgain() throws { + var paneCopy = try stored(alwaysAllowed: []) + paneCopy.aiAlwaysAllowedTools = ["writer"] + + let state = viewModel(holding: paneCopy).computeInitialApprovalState(for: "writer", registry: registry) + + #expect(state == .pending) + } + + @Test("A grant that reached storage after the pane opened skips the card") + func grantInStorageSkipsTheCard() throws { + var paneCopy = try stored(alwaysAllowed: ["writer"]) + paneCopy.aiAlwaysAllowedTools = [] + + let state = viewModel(holding: paneCopy).computeInitialApprovalState(for: "writer", registry: registry) + + #expect(state == .approved) + } + + @Test("A connection with no stored record keeps the pane's own grants") + func unsavedConnectionUsesThePaneCopy() { + let unsaved = DatabaseConnection(name: "Scratch", safeModeLevel: .alert, aiAlwaysAllowedTools: ["writer"]) + + let state = viewModel(holding: unsaved).computeInitialApprovalState(for: "writer", registry: registry) + + #expect(state == .approved) + } + + @Test("Always Allow after a revoke is stored again") + func alwaysAllowAfterARevokeIsStored() throws { + var paneCopy = try stored(alwaysAllowed: []) + paneCopy.aiAlwaysAllowedTools = ["writer"] + + viewModel(holding: paneCopy).persistAlwaysAllowed(toolName: "writer") + + #expect(storage.loadConnection(id: paneCopy.id)?.aiAlwaysAllowedTools == ["writer"]) + } + + private func services() -> AppServices { + let live = AppServices.live + return AppServices( + appEvents: live.appEvents, + appSettings: live.appSettings, + appSettingsStorage: live.appSettingsStorage, + connectionStorage: storage, + databaseManager: live.databaseManager, + pluginManager: live.pluginManager, + schemaService: live.schemaService, + schemaRefreshService: live.schemaRefreshService, + schemaProviderRegistry: live.schemaProviderRegistry, + catalogChangeService: live.catalogChangeService, + sqlFavoriteManager: live.sqlFavoriteManager, + favoriteTablesStorage: live.favoriteTablesStorage, + favoriteDatabasesStorage: live.favoriteDatabasesStorage, + tableFolderStorage: live.tableFolderStorage, + aiChatStorage: live.aiChatStorage, + aiKeyStorage: live.aiKeyStorage, + aiAccessApprovals: live.aiAccessApprovals, + groupStorage: live.groupStorage, + tagStorage: live.tagStorage, + sshProfileStorage: live.sshProfileStorage, + credentialProfileStorage: live.credentialProfileStorage, + featureAccess: live.featureAccess, + syncMetadataStorage: live.syncMetadataStorage, + favoritesExpansionState: live.favoritesExpansionState, + linkedFolderWatcher: live.linkedFolderWatcher, + queryHistoryManager: live.queryHistoryManager, + dateFormattingService: live.dateFormattingService, + copilotService: live.copilotService, + mcpServerManager: live.mcpServerManager, + syncTracker: live.syncTracker, + welcomeRouter: live.welcomeRouter, + telemetry: live.telemetry + ) + } +} diff --git a/apple/TableProTests/ViewModels/AIChatStoppedToolRoundTests.swift b/apple/TableProTests/ViewModels/AIChatStoppedToolRoundTests.swift index 7c566967fb..afd654a5bf 100644 --- a/apple/TableProTests/ViewModels/AIChatStoppedToolRoundTests.swift +++ b/apple/TableProTests/ViewModels/AIChatStoppedToolRoundTests.swift @@ -99,6 +99,51 @@ struct ChatToolCallRepairTests { #expect(block.isError) #expect(block.content == ChatToolCallRepair.interruptedContent) } + + private func call(_ id: String, _ state: ToolApprovalState) -> ChatContentBlockWire { + .toolUse(ToolUseBlock(id: id, name: "execute_query", input: .object([:]), approvalState: state)) + } + + private func approvalStates(_ turn: ChatTurnWire) -> [ToolApprovalState] { + turn.blocks.compactMap { block -> ToolApprovalState? in + guard case .toolUse(let call) = block.kind else { return nil } + return call.approvalState + } + } + + @Test("Settling a stored transcript cancels a waiting card and leaves every other state alone") + func settlingCancelsOnlyWaitingCards() { + let turns = [ + ChatTurnWire(role: .assistant, blocks: [ + .text("running it"), + call("a", .pending), + call("b", .approved), + call("c", .denied(reason: "Read-only")), + call("d", .cancelled) + ]), + ChatTurnWire(role: .user, blocks: [result("b")]) + ] + + let settled = ChatToolCallRepair.settlingPendingApprovals(turns) + + #expect(approvalStates(settled[0]) == [.cancelled, .approved, .denied(reason: "Read-only"), .cancelled]) + let blockIds = settled[0].blocks.map { $0.id } + #expect(blockIds == turns[0].blocks.map { $0.id }) + #expect(settled[1] == turns[1]) + } + + @Test("A cancelled or denied call with no result is answered with what happened to it") + func unansweredCancelledAndDeniedCalls() { + let repaired = ChatToolCallRepair.answeringEveryToolCall([ + ChatTurnWire(role: .assistant, blocks: [call("a", .cancelled), call("b", .denied(reason: "Read-only"))]) + ]) + + let contents = (repaired.last?.blocks ?? []).compactMap { block -> String? in + guard case .toolResult(let result) = block.kind else { return nil } + return result.content + } + #expect(contents == [ChatToolCallRepair.stoppedBeforeRunContent, "Read-only"]) + } } @MainActor @@ -215,6 +260,29 @@ struct AIChatStoppedToolRoundTests { } } + private func toolUseStates(in turn: ChatTurn) -> [ToolApprovalState] { + turn.blocks.compactMap { block -> ToolApprovalState? in + guard case .toolUse(let use) = block.kind else { return nil } + return use.approvalState + } + } + + private func toolUseStates(in turn: ChatTurnWire) -> [ToolApprovalState] { + turn.blocks.compactMap { block -> ToolApprovalState? in + guard case .toolUse(let use) = block.kind else { return nil } + return use.approvalState + } + } + + private static func storedWaitingCard() -> AIConversation { + AIConversation(messages: [ + ChatTurnWire(role: .user, blocks: [.text("raise every price by ten percent")]), + ChatTurnWire(role: .assistant, blocks: [ + .toolUse(ToolUseBlock(id: "call_7", name: "execute_query", input: .object([:]), approvalState: .pending)) + ]) + ]) + } + @Test("Stop while a tool call waits for approval leaves a result for that call") func stopWhileAwaitingApproval() async throws { let transport = ScriptedTransport(rounds: [Self.toolRound(id: "call_1", name: "writer")]) @@ -287,4 +355,141 @@ struct AIChatStoppedToolRoundTests { } #expect(answered == ["call_9"]) } + + @Test("Stop marks a waiting card cancelled before the transcript is saved") + func stopSettlesAWaitingCardAtOnce() async throws { + let transport = ScriptedTransport(rounds: [Self.toolRound(id: "call_1", name: "writer")]) + let viewModel = AIChatViewModel() + let assistant = start( + viewModel, + transport: transport, + tool: GatedTool(name: "writer", mode: .write, gate: AsyncGate()) + ) + let task = viewModel.streamingTask + try await waitUntil { self.hasToolUse(assistant) } + + viewModel.cancelStream() + + #expect(toolUseStates(in: assistant) == [.cancelled]) + await task?.value + } + + @Test("New Conversation while a card waits stores that card as cancelled") + func newConversationStoresAWaitingCardCancelled() async throws { + let transport = ScriptedTransport(rounds: [Self.toolRound(id: "call_1", name: "writer")]) + let viewModel = AIChatViewModel() + let assistant = start( + viewModel, + transport: transport, + tool: GatedTool(name: "writer", mode: .write, gate: AsyncGate()) + ) + let task = viewModel.streamingTask + try await waitUntil { self.hasToolUse(assistant) } + + viewModel.startNewConversation() + await task?.value + + #expect(viewModel.messages.isEmpty) + let stored = try #require(viewModel.conversations.first) + let storedStates = stored.messages.flatMap { toolUseStates(in: $0) } + #expect(storedStates == [.cancelled]) + #expect(!ToolApprovalCenter.shared.hasPending(sessionId: viewModel.sessionId)) + } + + @Test("Switching to a conversation stored with a waiting card shows it cancelled") + func switchingSettlesAStoredWaitingCard() { + let viewModel = AIChatViewModel() + let stored = Self.storedWaitingCard() + viewModel.conversations = [stored] + + viewModel.switchConversation(to: stored.id) + + let states = viewModel.messages.flatMap { toolUseStates(in: $0) } + #expect(states == [.cancelled]) + #expect(!ToolApprovalCenter.shared.isAwaiting(sessionId: viewModel.sessionId, toolUseId: "call_7")) + } + + @Test("A conversation saved with a waiting card by an earlier launch reopens with it cancelled") + func restoreSettlesAStoredWaitingCard() async throws { + let stored = Self.storedWaitingCard() + AIChatStorage.shared.saveSynchronously(stored) + let viewModel = AIChatViewModel(restoringConversation: stored.id) + + viewModel.restoreConversationsIfNeeded() + // The delete awaits the storage actor, so it runs after a caught wait instead of in a defer. + var waitFailure: (any Error)? + do { + try await waitUntil { !viewModel.messages.isEmpty } + } catch { + waitFailure = error + } + await AIChatStorage.shared.delete(stored.id) + if let waitFailure { throw waitFailure } + + let states = viewModel.messages.flatMap { toolUseStates(in: $0) } + #expect(states == [.cancelled]) + } + + @Test("A Run click that lands after Stop leaves the card cancelled and runs nothing") + func runClickBehindStopStaysCancelled() async throws { + let gate = AsyncGate() + await gate.open() + let transport = ScriptedTransport(rounds: [Self.toolRound(id: "call_1", name: "writer")]) + let viewModel = AIChatViewModel() + let assistant = start( + viewModel, + transport: transport, + tool: GatedTool(name: "writer", mode: .write, gate: gate) + ) + let task = viewModel.streamingTask + try await waitUntil { self.hasToolUse(assistant) && ToolApprovalCenter.shared.hasPending } + + // Both on the main actor in one turn, so the resumed task sees the click only after Stop. + ToolApprovalCenter.shared.resolve(sessionId: viewModel.sessionId, toolUseId: "call_1", decision: .run) + viewModel.cancelStream() + await task?.value + + #expect(toolUseStates(in: assistant) == [.cancelled]) + let results = toolResults(in: try turnAfter(assistant, in: viewModel)) + let contents = results.map { $0.content } + #expect(contents == ["User cancelled this tool call."]) + } + + private final class ReplyLog: @unchecked Sendable { + private let lock = NSLock() + private var results: [ChatToolResult] = [] + + var contents: [String] { + lock.withLock { results.map { $0.content } } + } + + func record(_ result: ChatToolResult) { + lock.withLock { results.append(result) } + } + } + + // Copilot asks through the shared registry, and its path never checks cancellation. + @Test("A Copilot Run click that lands with New Conversation runs nothing") + func copilotRunBehindNewConversationRunsNothing() async throws { + let gate = AsyncGate() + await gate.open() + let tool = GatedTool(name: "copilot_test_writer", mode: .write, gate: gate) + ChatToolRegistry.shared.register(tool) + defer { ChatToolRegistry.shared.unregister(name: tool.name) } + let replies = ReplyLog() + let call = ToolUseBlock(id: "call_1", name: tool.name, input: .object([:])) + let transport = ScriptedTransport(rounds: [[ + .toolInvocationRequest(block: call, replyToken: ToolReplyToken { replies.record($0) }) + ]]) + let viewModel = AIChatViewModel() + let assistant = start(viewModel, transport: transport, tool: tool) + let task = viewModel.streamingTask + try await waitUntil { self.hasToolUse(assistant) && ToolApprovalCenter.shared.hasPending } + + ToolApprovalCenter.shared.resolve(sessionId: viewModel.sessionId, toolUseId: call.id, decision: .run) + viewModel.startNewConversation() + await task?.value + + #expect(replies.contents == ["User cancelled this tool call."]) + } } diff --git a/apple/TableProTests/ViewModels/AIChatStoppedWriteTests.swift b/apple/TableProTests/ViewModels/AIChatStoppedWriteTests.swift new file mode 100644 index 0000000000..9b431f7079 --- /dev/null +++ b/apple/TableProTests/ViewModels/AIChatStoppedWriteTests.swift @@ -0,0 +1,130 @@ +// +// AIChatStoppedWriteTests.swift +// TableProTests +// +// Stop pressed while the assistant's write is in the driver: the write runs to its end, so the transcript must not +// read as a failure the model would retry. +// + +import Foundation +@testable import TablePro +import TableProMCP +import TableProPluginKit +import TableProTestSupport +import Testing + +@MainActor +struct AIChatStoppedWriteTests { + private final class OneToolRoundTransport: ChatTransport, @unchecked Sendable { + private let toolName: String + + init(toolName: String) { + self.toolName = toolName + } + + func streamChat( + turns: [ChatTurnWire], + options: ChatTransportOptions + ) -> AsyncThrowingStream { + let events: [ChatStreamEvent] = [ + .toolUseStart(id: "call_1", name: toolName, providerMetadata: nil), + .toolUseDelta(id: "call_1", inputJSONDelta: "{}"), + .toolUseEnd(id: "call_1") + ] + return AsyncThrowingStream { continuation in + for event in events { + continuation.yield(event) + } + continuation.finish() + } + } + + func fetchAvailableModels() async throws -> [AIModelInfo] { [] } + + func testConnection() async throws -> Bool { true } + } + + // The assistant's own execute_query, on a connection the test controls. + private struct WriteThroughExecuteQuery: ChatTool { + let name = "write_rows" + let mode: ChatToolMode = .readOnly + let description = "" + let inputSchema: JsonValue = .object(["type": .string("object")]) + let target: ChatToolContext + + func execute(input: JsonValue, context: ChatToolContext) async throws -> ChatToolResult { + try await ExecuteQueryChatTool().execute( + input: .object(["query": .string("UPDATE orders SET n = n + 1 WHERE id = 1")]), + context: target + ) + } + } + + private static let settings = AISettings( + enabled: true, + includeSchema: false, + includeCurrentQuery: false, + includeQueryResults: false, + maxToolRoundtrips: AISettings.defaultMaxToolRoundtrips, + maxToolRoundtripsEnabled: false, + chatMode: .agent + ) + + @Test("Stop during a write in the driver leaves a result saying it may have been applied, and history keeps the write") + func stopDuringWriteSaysItMayHaveBeenApplied() async throws { + let gate = AsyncGate() + let driver = AnsweringPluginDriver(hold: gate) + let scope = AnsweringPluginDriver.install(driver, connection: TestFixtures.makeConnection(database: "shop", type: .postgresql)) + defer { DatabaseManager.shared.removeSession(for: scope.connectionId) } + let history = RecordingQueryHistory() + let tool = WriteThroughExecuteQuery(target: ChatToolContext( + connectionId: scope.connectionId, + bridge: MCPConnectionBridge(), + authPolicy: MCPAuthPolicy( + connectionResolver: { _ in nil }, + connectionIdsProvider: { [] }, + historyRecorder: history, + executionGate: AlwaysAllowGate() + ), + policyLookup: { _ in .alwaysAllow } + )) + let registry = ChatToolRegistry() + registry.register(tool) + let viewModel = AIChatViewModel() + let assistant = ChatTurn(role: .assistant, blocks: [], modelId: "test-model", providerId: nil) + viewModel.messages.append(assistant) + viewModel.streamingState = .streaming(assistantID: assistant.id) + viewModel.runStream( + chatMessages: [], + promptContext: nil, + resolved: AIProviderFactory.ResolvedProvider( + provider: OneToolRoundTransport(toolName: tool.name), + model: "test-model", + config: AIProviderConfig(name: "Test", type: .claude) + ), + assistantID: assistant.id, + settings: Self.settings, + registry: registry + ) + let task = viewModel.streamingTask + await gate.arrived(1) + + viewModel.cancelStream() + await task?.value + + let index = try #require(viewModel.messages.firstIndex { $0.id == assistant.id }) + try #require(index + 1 < viewModel.messages.count) + let results = viewModel.messages[index + 1].blocks.compactMap { block -> ToolResultBlock? in + guard case .toolResult(let result) = block.kind else { return nil } + return result + } + #expect(results.map { $0.toolUseId } == ["call_1"]) + #expect(results.first?.isError == true) + #expect(results.first?.content == "Error: \(DatabaseAccessBridge.stoppedWhileRunning.message)") + #expect(history.requests.map { $0.wasSuccessful } == [false]) + #expect(driver.finishedCount == 0) + + await gate.open() + try await waitUntil { driver.finishedCount == 1 } + } +} diff --git a/apple/TableProTests/ViewModels/ConnectionFormEditsTests.swift b/apple/TableProTests/ViewModels/ConnectionFormEditsTests.swift index aae879f871..2bd6541e68 100644 --- a/apple/TableProTests/ViewModels/ConnectionFormEditsTests.swift +++ b/apple/TableProTests/ViewModels/ConnectionFormEditsTests.swift @@ -19,7 +19,8 @@ struct ConnectionFormEditsTests { completionIdentifierCase: SQLIdentifierCase = .default, additionalFields: [String: String] = [:], ownedAdditionalFieldIDs: Set = [], - maskingRules: [MaskingRule] = [] + maskingRules: [MaskingRule] = [], + revokedAIAlwaysAllowedTools: Set = [] ) -> ConnectionFormEdits { ConnectionFormEdits( name: "Edited", @@ -53,7 +54,8 @@ struct ConnectionFormEditsTests { localOnly: false, additionalFields: additionalFields, ownedAdditionalFieldIDs: ownedAdditionalFieldIDs, - maskingRules: maskingRules + maskingRules: maskingRules, + revokedAIAlwaysAllowedTools: revokedAIAlwaysAllowedTools ) } @@ -111,6 +113,29 @@ struct ConnectionFormEditsTests { #expect(result.passwordSource == .env(variable: "PGPASSWORD")) } + @Test("Forgetting an always-allowed tool removes it and keeps a grant recorded after the form opened") + func forgettingRemovesOnlyTheRevokedTool() { + var stored = populatedConnection() + stored.aiAlwaysAllowedTools = ["execute_query", "list_tables"] + + let result = edits(revokedAIAlwaysAllowedTools: ["execute_query"]).applied(to: stored) + + #expect(result.aiAlwaysAllowedTools == ["list_tables"]) + } + + @Test("The form's edits carry what Forget took off the list") + @MainActor + func coordinatorCarriesForgottenTools() { + let coordinator = ConnectionFormCoordinator(connectionId: nil) + coordinator.advanced.load(from: populatedConnection()) + + coordinator.advanced.forgetAlwaysAllowed("run_query") + let saved = coordinator.buildEdits().applied(to: populatedConnection()) + + #expect(coordinator.buildEdits().revokedAIAlwaysAllowedTools == ["run_query"]) + #expect(saved.aiAlwaysAllowedTools == ["list_tables"]) + } + @Test("Applying edits overwrites every property the form owns") func overwritesOwnedProperties() { let result = edits().applied(to: populatedConnection()) diff --git a/apple/TableProTests/Views/ConnectionForm/ViewModels/AdvancedPaneViewModelTests.swift b/apple/TableProTests/Views/ConnectionForm/ViewModels/AdvancedPaneViewModelTests.swift index cb75ba406b..bf5abddb6b 100644 --- a/apple/TableProTests/Views/ConnectionForm/ViewModels/AdvancedPaneViewModelTests.swift +++ b/apple/TableProTests/Views/ConnectionForm/ViewModels/AdvancedPaneViewModelTests.swift @@ -31,6 +31,34 @@ struct AdvancedPaneViewModelTests { #expect(fields["externalAccess"] == nil) } + @Test("Lists the connection's always-allowed tools in name order with nothing forgotten") + func loadsAlwaysAllowedTools() { + let connection = DatabaseConnection(name: "Staging", aiAlwaysAllowedTools: ["list_tables", "execute_query"]) + let viewModel = AdvancedPaneViewModel() + + viewModel.load(from: connection) + + #expect(viewModel.aiAlwaysAllowedTools == ["execute_query", "list_tables"]) + #expect(viewModel.revokedAIAlwaysAllowedTools.isEmpty) + } + + @Test("Forget takes a tool off the list and into the revoked set, and loading again drops it") + func forgetMovesAToolIntoTheRevokedSet() { + let connection = DatabaseConnection(name: "Staging", aiAlwaysAllowedTools: ["list_tables", "execute_query"]) + let viewModel = AdvancedPaneViewModel() + viewModel.load(from: connection) + + viewModel.forgetAlwaysAllowed("execute_query") + + #expect(viewModel.aiAlwaysAllowedTools == ["list_tables"]) + #expect(viewModel.revokedAIAlwaysAllowedTools == ["execute_query"]) + + viewModel.load(from: connection) + + #expect(viewModel.aiAlwaysAllowedTools == ["execute_query", "list_tables"]) + #expect(viewModel.revokedAIAlwaysAllowedTools.isEmpty) + } + @Test("Shows a Redis database index saved as db4 as the number the stepper takes") func loadsDbPrefixedRedisIndexAsANumber() throws { try #require( diff --git a/apple/TableProTests/Views/Main/ReadOnlySafeModeEntryPointTests.swift b/apple/TableProTests/Views/Main/ReadOnlySafeModeEntryPointTests.swift new file mode 100644 index 0000000000..b934944fbf --- /dev/null +++ b/apple/TableProTests/Views/Main/ReadOnlySafeModeEntryPointTests.swift @@ -0,0 +1,544 @@ +// +// ReadOnlySafeModeEntryPointTests.swift +// TableProTests +// +// At Read-Only every read the app runs for the user reaches a driver that declares `.enforcesReadOnlyMode` with +// `enforcesReadOnly` set, so `SELECT purge_orders()`, or a view that calls it, runs in the engine's read-only mode. +// + +import Combine +import Foundation +@testable import TablePro +import TableProPluginKit +import TableProTestSupport +import Testing + +private final class ReadOnlyRecordingDriver: PluginDriverStub, @unchecked Sendable { + struct Call: Equatable { + let entry: String + let query: String + let context: PluginStatementContext? + } + + var capabilities: PluginCapabilities { [.readOnlyStatements, .enforcesReadOnlyMode] } + var supportsSchemas: Bool { false } + var supportsTransactions: Bool { true } + var currentSchema: String? { nil } + var serverVersion: String? { nil } + + private let lock = NSLock() + private var recorded: [Call] = [] + + var calls: [Call] { + lock.withLock { recorded } + } + + func call(containing text: String) -> Call? { + calls.first { $0.query.contains(text) } + } + + var entries: [String] { + calls.map(\.entry) + } + + private static let answer = PluginQueryResult( + columns: ["n"], columnTypeNames: ["INT8"], rows: [[.text("1")]], rowsAffected: 0, executionTime: 0 + ) + + private func record(_ entry: String, _ query: String, _ context: PluginStatementContext?) -> PluginQueryResult { + lock.withLock { recorded.append(Call(entry: entry, query: query, context: context)) } + return Self.answer + } + + func execute(query: String) async throws -> PluginQueryResult { + record("execute", query, nil) + } + + func execute(query: String, context: PluginStatementContext) async throws -> PluginQueryResult { + record("execute", query, context) + } + + func executeParameterized( + query: String, + parameters: [PluginCellValue], + context: PluginStatementContext + ) async throws -> PluginQueryResult { + record("executeParameterized", query, context) + } + + func executeUserQuery( + query: String, + rowCap: Int?, + parameters: [PluginCellValue]?, + context: PluginStatementContext + ) async throws -> PluginQueryResult { + record("executeUserQuery", query, context) + } + + func executeBoundedQuery(query: String, rowCap: Int) async throws -> PluginQueryResult? { + record("executeBoundedQuery", query, nil) + } + + func executeBoundedQuery( + query: String, + rowCap: Int, + context: PluginStatementContext + ) async throws -> PluginQueryResult? { + record("executeBoundedQuery", query, context) + } + + func streamRows(query: String, context: PluginStatementContext) -> AsyncThrowingStream { + _ = record("streamRows", query, context) + return AsyncThrowingStream { $0.finish() } + } + + func beginTransaction() async throws { + _ = record("BEGIN", "", nil) + } + + func beginTransaction(mode: PluginTransactionAccessMode) async throws { + _ = record("BEGIN", "", nil) + } + + func commitTransaction() async throws { + _ = record("COMMIT", "", nil) + } + + func rollbackTransaction() async throws { + _ = record("ROLLBACK", "", nil) + } + + func sessionTransactionState() async -> PluginSessionTransactionState { .idle } +} + +private final class ReadingExportFormat: ExportFormatPlugin, @unchecked Sendable { + static let pluginName = "Reading" + static let pluginVersion = "1.0.0" + static let pluginDescription = "Reads every table's rows and writes an empty file" + static let formatId = "read-only-reading" + static let formatDisplayName = "Reading" + static let defaultFileExtension = "txt" + static let iconName = "doc" + + private(set) var wasAskedToWrite = false + + required init() {} + + func export( + tables: [PluginExportTable], + dataSource: any PluginExportDataSource, + destination: URL, + progress: PluginExportProgress + ) async throws -> ExportFormatResult { + wasAskedToWrite = true + for table in tables { + for try await _ in dataSource.streamExportRows(for: table) {} + } + try Data().write(to: destination) + return ExportFormatResult() + } +} + +@MainActor +struct ReadOnlySafeModeEntryPointTests { + private static let purge = "purge_orders()" + + private func makeConnection(_ level: SafeModeLevel, type: DatabaseType = .postgresql) -> DatabaseConnection { + DatabaseConnection(name: "Shop", database: "app", type: type, safeModeLevel: level) + } + + private func makeFixture( + _ level: SafeModeLevel, + type: DatabaseType = .postgresql + ) -> (CoordinatorFixture, ReadOnlyRecordingDriver) { + let driver = ReadOnlyRecordingDriver() + let connection = makeConnection(level, type: type) + let fixture = CoordinatorFixture( + connection: connection, + driver: PluginDriverAdapter(connection: connection, pluginDriver: driver) + ) + fixture.coordinator.toolbarState.safeModeLevel = level + fixture.coordinator.executionGate = AlwaysAllowGate() + return (fixture, driver) + } + + private func rawFilterTableTab() -> QueryTab { + var tab = QueryTab( + title: "orders", + query: "SELECT * FROM orders WHERE (purge_orders() IS NULL) LIMIT 100", + tabType: .table, + tableName: "orders" + ) + let filter = TableFilter.rawCondition("purge_orders() IS NULL") + tab.filterState.filters = [filter] + tab.filterState.commit = .all + tab.filterState.executedFilters = [filter] + tab.filterState.executedLogicMode = .and + return tab + } + + // MARK: - Run + + @Test("Run at Read-Only sends the statement in the engine's read-only mode") + func runCarriesTheMode() async throws { + let (fixture, driver) = makeFixture(.readOnly) + defer { fixture.coordinator.cancelAllQueryTasks() } + fixture.addTab(QueryTab(title: "Query", query: "SELECT purge_orders()", tabType: .query)) + + #expect(fixture.coordinator.runStatement("SELECT purge_orders()")) + try await waitUntil { driver.call(containing: Self.purge) != nil } + + #expect(driver.call(containing: Self.purge)?.context?.enforcesReadOnly == true) + } + + @Test("Run at Silent sends the statement as it always did") + func runAtSilentIsUnchanged() async throws { + let (fixture, driver) = makeFixture(.silent) + defer { fixture.coordinator.cancelAllQueryTasks() } + fixture.addTab(QueryTab(title: "Query", query: "SELECT purge_orders()", tabType: .query)) + + #expect(fixture.coordinator.runStatement("SELECT purge_orders()")) + try await waitUntil { driver.call(containing: Self.purge) != nil } + + #expect(driver.call(containing: Self.purge)?.context?.enforcesReadOnly != true) + } + + // MARK: - Run All + + @Test("Run All at Read-Only opens no transaction and sends each statement in the read-only mode") + func runAllRunsInAutocommit() async throws { + let (fixture, driver) = makeFixture(.readOnly) + defer { fixture.coordinator.cancelAllQueryTasks() } + fixture.addTab(QueryTab(title: "Query", query: "SELECT purge_orders();\nSELECT 42", tabType: .query)) + + fixture.coordinator.queryExecutionCoordinator.runAllStatements() + try await waitUntil { driver.call(containing: "42") != nil } + + let statements = driver.calls.filter { $0.query.contains(Self.purge) || $0.query.contains("42") } + #expect(!driver.entries.contains("BEGIN")) + #expect(statements.count == 2) + #expect(statements.allSatisfy { $0.context?.enforcesReadOnly == true }) + } + + @Test("Run All at Silent still wraps the batch in the app's transaction") + func runAllAtSilentWraps() async throws { + let (fixture, driver) = makeFixture(.silent) + defer { fixture.coordinator.cancelAllQueryTasks() } + fixture.addTab(QueryTab(title: "Query", query: "SELECT purge_orders();\nSELECT 42", tabType: .query)) + + fixture.coordinator.queryExecutionCoordinator.runAllStatements() + try await waitUntil { driver.entries.contains("COMMIT") } + + let entries = driver.entries + let begin = try #require(entries.firstIndex(of: "BEGIN")) + let first = try #require(driver.calls.firstIndex { $0.query.contains(Self.purge) }) + #expect(begin < first) + #expect(driver.calls.allSatisfy { $0.context?.enforcesReadOnly != true }) + } + + // MARK: - Table browse + + @Test("A raw filter that calls a function goes to the gate at Read-Only and runs in the read-only mode") + func rawFilterBrowseIsGatedAndCarriesTheMode() async throws { + let (fixture, driver) = makeFixture(.readOnly) + let gate = RequestRecordingGate(allows: true) + fixture.coordinator.executionGate = gate + defer { fixture.coordinator.cancelAllQueryTasks() } + let tab = fixture.addTab(rawFilterTableTab()) + + let confirmation = fixture.coordinator.executeTableTabQueryDirectly(viewport: .firstRow) + #expect(confirmation != nil) + await confirmation?.value + try await waitUntil { driver.call(containing: Self.purge) != nil } + + #expect(await gate.requests.first?.sql == tab.content.query) + #expect(driver.call(containing: Self.purge)?.context?.enforcesReadOnly == true) + } + + @Test("A page turn or sort under the same raw filter is not asked about again") + func rawFilterIsAskedOncePerFilter() async throws { + let (fixture, driver) = makeFixture(.readOnly) + let gate = RequestRecordingGate(allows: true) + fixture.coordinator.executionGate = gate + defer { fixture.coordinator.cancelAllQueryTasks() } + fixture.addTab(rawFilterTableTab()) + + await fixture.coordinator.executeTableTabQueryDirectly(viewport: .firstRow)?.value + try await waitUntil { driver.call(containing: Self.purge) != nil } + let again = fixture.coordinator.executeTableTabQueryDirectly(viewport: .firstRow) + try await waitUntil { driver.calls.filter { $0.query.contains(Self.purge) }.count >= 2 } + + #expect(again == nil) + #expect(await gate.requests.count == 1) + } + + // A view, or a row security policy, can call a writing function with no call in the browse's text. + @Test("A table browse with no raw filter runs in the read-only mode at Read-Only without asking") + func plainBrowseCarriesTheMode() async throws { + let (fixture, driver) = makeFixture(.readOnly) + let gate = RequestRecordingGate(allows: true) + fixture.coordinator.executionGate = gate + defer { fixture.coordinator.cancelAllQueryTasks() } + fixture.addTab(QueryTab(title: "order_cleanup", query: "SELECT * FROM order_cleanup LIMIT 100", tabType: .table, tableName: "order_cleanup")) + + #expect(fixture.coordinator.executeTableTabQueryDirectly(viewport: .firstRow) == nil) + try await waitUntil { driver.call(containing: "FROM order_cleanup") != nil } + + #expect(await gate.requests.isEmpty) + #expect(driver.call(containing: "FROM order_cleanup")?.context?.enforcesReadOnly == true) + } + + // MARK: - Filtered count + + @Test("The host's COUNT(*) under a raw filter runs in the read-only mode at Read-Only") + func hostCountCarriesTheMode() async throws { + let driver = ReadOnlyRecordingDriver() + let adapter = PluginDriverAdapter(connection: makeConnection(.readOnly), pluginDriver: driver) + let filters = [TableFilter.rawCondition("purge_orders() IS NULL")] + let countSQL = "SELECT COUNT(*) FROM orders WHERE (purge_orders() IS NULL)" + + _ = try await ExactRowCounter.count( + on: adapter, table: "orders", filters: filters, logicMode: .and, countSQL: countSQL, safeModeIsReadOnly: true + ) + _ = try await ExactRowCounter.count( + on: adapter, table: "orders", filters: filters, logicMode: .and, countSQL: countSQL + ) + + #expect(driver.calls.map { $0.context?.enforcesReadOnly == true } == [true, false]) + } + + // DuckDB counts on its session driver, which this test installs; a pooled engine would open a connection of its own. + @Test("Count Exactly with a raw filter that calls a function asks the gate and runs in the read-only mode") + func countExactlyIsGatedAndCarriesTheMode() async throws { + let (fixture, driver) = makeFixture(.readOnly, type: .duckdb) + let gate = RequestRecordingGate(allows: true) + fixture.coordinator.executionGate = gate + defer { fixture.coordinator.cancelAllQueryTasks() } + fixture.addTab(rawFilterTableTab()) + + fixture.coordinator.paginationCoordinator.requestExactRowCount() + try await waitUntil { driver.call(containing: "COUNT") != nil } + + #expect(await gate.requests.contains { $0.sql?.contains("COUNT") == true }) + #expect(driver.call(containing: "COUNT")?.context?.enforcesReadOnly == true) + } + + // MARK: - Export + + @Test("Every export read runs in the read-only mode at Read-Only, filtered or not, and Silent changes nothing") + func exportRowScopeCarriesTheMode() async throws { + let driver = ReadOnlyRecordingDriver() + let adapter = PluginDriverAdapter(connection: makeConnection(.readOnly), pluginDriver: driver) + let filtered = PluginExportTable( + name: "orders", databaseName: "", tableType: "table", schema: nil, kind: .table, + rowScope: PluginExportRowScope(filter: "purge_orders() IS NULL") + ) + let limited = PluginExportTable( + name: "orders", databaseName: "", tableType: "table", schema: nil, kind: .table, + rowScope: PluginExportRowScope(rowLimit: 5) + ) + + let atReadOnly = ExportDataSourceAdapter(driver: adapter, databaseType: .postgresql, safeModeIsReadOnly: true) + for try await _ in atReadOnly.streamExportRows(for: filtered) {} + for try await _ in atReadOnly.streamExportRows(for: limited) {} + let atSilent = ExportDataSourceAdapter(driver: adapter, databaseType: .postgresql) + for try await _ in atSilent.streamExportRows(for: filtered) {} + + #expect(driver.calls.map { $0.context?.enforcesReadOnly == true } == [true, true, false]) + #expect(driver.calls.first?.query.contains(Self.purge) == true) + } + + @Test("At Read-Only an export's snapshot transaction is not opened, and its other statements run read-only") + func exportSnapshotIsSkippedAtReadOnly() async throws { + let driver = ReadOnlyRecordingDriver() + let adapter = PluginDriverAdapter(connection: makeConnection(.readOnly), pluginDriver: driver) + let atReadOnly = ExportDataSourceAdapter(driver: adapter, databaseType: .postgresql, safeModeIsReadOnly: true) + + _ = try await atReadOnly.execute(query: "BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY") + _ = try await atReadOnly.execute(query: "SELECT 1") + _ = try await atReadOnly.execute(query: "COMMIT") + + #expect(driver.calls.map { $0.query } == ["SELECT 1"]) + #expect(driver.calls.first?.context?.enforcesReadOnly == true) + } + + private func installReadOnlySession(type: DatabaseType = .postgresql) -> (DatabaseConnection, ReadOnlyRecordingDriver, PluginDriverAdapter) { + let driver = ReadOnlyRecordingDriver() + let connection = makeConnection(.readOnly, type: type) + let adapter = PluginDriverAdapter(connection: connection, pluginDriver: driver) + var session = ConnectionSession(connection: connection, driver: adapter) + session.status = .connected + DatabaseManager.shared.injectSession(session, for: connection.id) + return (connection, driver, adapter) + } + + private func exportURL() -> URL { + FileManager.default.temporaryDirectory.appendingPathComponent("\(UUID().uuidString).txt") + } + + @Test("A filtered export at Read-Only asks the gate with its filter before any count or row is read") + func filteredExportAsksTheGateFirst() async throws { + let (connection, driver, adapter) = installReadOnlySession() + defer { DatabaseManager.shared.removeSession(for: connection.id) } + let format = ReadingExportFormat() + let service = ExportService(driver: adapter, databaseType: .postgresql) { _ in format } + let gate = RequestRecordingGate(allows: false) + service.executionGate = gate + var item = ExportObjectItem(name: "orders", kind: .table) + item.rowScope = PluginExportRowScope(filter: "purge_orders() IS NULL") + var config = ExportConfiguration() + config.formatId = ReadingExportFormat.formatId + let url = exportURL() + defer { try? FileManager.default.removeItem(at: url) } + + await #expect(throws: ExecutionGateError.self) { + try await service.export(objects: [item], config: config, to: url) + } + + let requests = await gate.requests + #expect(requests.count == 1) + #expect(requests.first?.connectionId == connection.id) + #expect(requests.first?.sql?.contains(Self.purge) == true) + #expect(driver.calls.isEmpty) + #expect(!format.wasAskedToWrite) + } + + @Test("An export at Read-Only counts and reads its rows in the read-only mode") + func exportCountAndRowsCarryTheMode() async throws { + let (connection, driver, adapter) = installReadOnlySession() + defer { DatabaseManager.shared.removeSession(for: connection.id) } + let format = ReadingExportFormat() + let service = ExportService(driver: adapter, databaseType: .postgresql) { _ in format } + service.executionGate = AlwaysAllowGate() + var config = ExportConfiguration() + config.formatId = ReadingExportFormat.formatId + let url = exportURL() + defer { try? FileManager.default.removeItem(at: url) } + + try await service.export(objects: [ExportObjectItem(name: "order_cleanup", kind: .table)], config: config, to: url) + + let count = try #require(driver.call(containing: "COUNT(")) + let rows = try #require(driver.calls.first { $0.entry == "streamRows" }) + #expect(count.context?.enforcesReadOnly == true) + #expect(rows.context?.enforcesReadOnly == true) + } + + @Test("Export Query Results at Read-Only asks the gate with its query before it runs") + func queryExportAsksTheGateFirst() async throws { + let (connection, driver, adapter) = installReadOnlySession() + defer { DatabaseManager.shared.removeSession(for: connection.id) } + let format = ReadingExportFormat() + let service = ExportService(driver: adapter, databaseType: .postgresql) { _ in format } + let gate = RequestRecordingGate(allows: false) + service.executionGate = gate + var config = ExportConfiguration() + config.formatId = ReadingExportFormat.formatId + let url = exportURL() + defer { try? FileManager.default.removeItem(at: url) } + + await #expect(throws: ExecutionGateError.self) { + try await service.exportStreamingQuery(query: "SELECT purge_orders()", config: config, to: url) + } + + #expect(await gate.requests.map { $0.sql } == ["SELECT purge_orders()"]) + #expect(driver.calls.isEmpty) + #expect(!format.wasAskedToWrite) + } + + @Test("Transfer To at Read-Only asks the gate with the source filter before anything else") + func transferFilterAsksTheGateFirst() async throws { + let (connection, _, _) = installReadOnlySession() + defer { DatabaseManager.shared.removeSession(for: connection.id) } + let gate = RequestRecordingGate(allows: false) + var item = ExportObjectItem(name: "orders", kind: .table) + item.rowScope = PluginExportRowScope(filter: "purge_orders() IS NULL") + let request = TableTransferService.Request(objects: [item], sourceType: .postgresql, destinationType: .mysql) + + await #expect(throws: ExecutionGateError.self) { + try await TableTransferService().run( + request: request, + source: DatabaseScope(connectionId: connection.id, database: "app", schema: nil), + destination: DatabaseScope(connectionId: UUID(), database: "archive", schema: nil), + operationDescription: "Transfer 1 table into archive?", + confirmedByCaller: false, + gate: gate + ) + } + + let requests = await gate.requests + #expect(requests.count == 1) + #expect(requests.first?.connectionId == connection.id) + #expect(requests.first?.sql?.contains(Self.purge) == true) + } + + // The gate's own decision on a fallback engine held at Read-Only: the export is refused without a question. + @Test("A held Read-Only refuses an export filter that calls a function on a driver without the mode") + func heldReadOnlyRefusesAnExportFilter() async throws { + let (connection, driver, adapter) = installReadOnlySession(type: .oracle) + defer { DatabaseManager.shared.removeSession(for: connection.id) } + let format = ReadingExportFormat() + let service = ExportService(driver: adapter, databaseType: .oracle) { _ in format } + let confirm = StubConfirming(answer: true) + service.executionGate = DefaultExecutionGate( + confirming: confirm, + authenticating: StubAuthenticating(answer: true), + safeModeLevelResolver: { _ in .readOnly }, + forcesWriteResolver: { _ in false }, + readOnlyFunctionPolicyResolver: { _, _ in ReadOnlyFunctionPolicy(functionsCanWrite: true, levelIsHeld: true) } + ) + var item = ExportObjectItem(name: "ORDERS", kind: .table) + item.rowScope = PluginExportRowScope(filter: "purge_orders() = 1") + var config = ExportConfiguration() + config.formatId = ReadingExportFormat.formatId + let url = exportURL() + defer { try? FileManager.default.removeItem(at: url) } + + await #expect(throws: ExecutionGateError.self) { + try await service.export(objects: [item], config: config, to: url) + } + + #expect(confirm.callCount == 0) + #expect(driver.calls.isEmpty) + } + + // MARK: - Dashboard + + @Test("A dashboard tile runs its saved query in the read-only mode at Read-Only") + func dashboardTileCarriesTheMode() async throws { + let driver = ReadOnlyRecordingDriver() + let connection = makeConnection(.readOnly) + var session = ConnectionSession( + connection: connection, + driver: PluginDriverAdapter(connection: connection, pluginDriver: driver) + ) + session.status = .connected + DatabaseManager.shared.injectSession(session, for: connection.id) + defer { DatabaseManager.shared.removeSession(for: connection.id) } + + let suite = try EphemeralDefaults() + let defaults = suite.defaults + let saved = SQLFavorite(name: "Purge", query: "SELECT purge_orders()") + let viewModel = ServerDashboardViewModel( + connectionId: connection.id, + databaseType: .postgresql, + services: .live, + tileEnvironment: DashboardTileBoard.Environment( + loadCatalog: { _ in DashboardSavedQueryCatalog(favorites: [saved], folders: []) }, + savedQueriesDidChange: Empty().eraseToAnyPublisher(), + gate: AlwaysAllowGate(), + forcesWrite: { _ in false }, + safeModeLevel: { _ in .readOnly }, + chartsAvailable: { false }, + nullDisplay: { "NULL" }, + storage: ServerDashboardTileStorage(defaults: { defaults }) + ) + ) + + viewModel.tileBoard.add(favoriteId: saved.id) + try await waitUntil { driver.call(containing: Self.purge) != nil } + + let call = driver.call(containing: Self.purge) + #expect(call?.entry == "executeBoundedQuery") + #expect(call?.context?.enforcesReadOnly == true) + } +} diff --git a/apple/scripts/probes/check-postgresql-read-only-functions.sh b/apple/scripts/probes/check-postgresql-read-only-functions.sh new file mode 100755 index 0000000000..bd391a3539 --- /dev/null +++ b/apple/scripts/probes/check-postgresql-read-only-functions.sh @@ -0,0 +1,59 @@ +#!/usr/bin/env bash +# +# Checks the server fact Read-Only Safe Mode rests on for the PostgreSQL family: a read the driver runs the way it runs +# one at Read-Only (`START TRANSACTION READ ONLY; SELECT 1`, the statement, `ROLLBACK`) cannot write through a function +# it calls. A SQL function that deletes rows, `nextval`, and `set_config` at session level are each sent that way; the +# delete and the sequence must be refused with SQLSTATE 25006 and leave the data as it was, and the setting must be +# gone after the ROLLBACK. The same delete sent without the wrap must write, so the probe proves it can see a write. +# +# Usage: scripts/probes/check-postgresql-read-only-functions.sh [psql connection arguments…] +# scripts/probes/check-postgresql-read-only-functions.sh -h 127.0.0.1 -p 5432 -U postgres -d postgres +# The login needs CREATE on the database: the probe makes and drops its own schema. +# Prints OK, or BUG with what the server let through. Exits 2 when the server cannot be reached. +set -uo pipefail + +PSQL="${PSQL:-$(command -v psql || true)}" +[[ -n "$PSQL" ]] || { echo "FAIL: psql not found; set PSQL" >&2; exit 2; } +SCHEMA="tablepro_read_only_probe_$$" +BEGIN_READ_ONLY="START TRANSACTION READ ONLY; SELECT 1" + +run() { "$PSQL" -X -q -At -v VERBOSITY=verbose "$@" 2>&1; } + +run "$@" -c 'SELECT 1' > /dev/null || { echo "FAIL: cannot connect" >&2; exit 2; } +cleanup() { run "$@" -c "DROP SCHEMA IF EXISTS $SCHEMA CASCADE" > /dev/null; } +trap 'cleanup "$@"' EXIT + +run "$@" -v ON_ERROR_STOP=1 \ + -c "CREATE SCHEMA $SCHEMA" \ + -c "CREATE TABLE $SCHEMA.orders (id integer)" \ + -c "INSERT INTO $SCHEMA.orders SELECT generate_series(1, 5)" \ + -c "CREATE SEQUENCE $SCHEMA.ticket" \ + -c "CREATE FUNCTION $SCHEMA.purge_orders() RETURNS void LANGUAGE sql AS 'DELETE FROM $SCHEMA.orders'" \ + > /dev/null || { echo "FAIL: cannot create the probe schema" >&2; exit 2; } +echo "PostgreSQL $(run "$@" -c 'SHOW server_version')" + +failures=() +wrapped() { run "$@" -c "$BEGIN_READ_ONLY" -c "$STATEMENT" -c "ROLLBACK"; } + +STATEMENT="SELECT $SCHEMA.purge_orders()" +answer="$(wrapped "$@")" +[[ "$answer" == *25006* ]] || failures+=("purge_orders() was not refused: $answer") +[[ "$(run "$@" -c "SELECT count(*) FROM $SCHEMA.orders")" == 5 ]] || failures+=("purge_orders() deleted rows") + +STATEMENT="SELECT nextval('$SCHEMA.ticket')" +answer="$(wrapped "$@")" +[[ "$answer" == *25006* ]] || failures+=("nextval() was not refused: $answer") + +answer="$(run "$@" -c "$BEGIN_READ_ONLY" -c "SELECT set_config('application_name', 'probe_leak', false)" \ + -c "ROLLBACK" -c "SHOW application_name")" +[[ "$answer" != *$'\n'probe_leak ]] || failures+=("set_config() outlived the ROLLBACK") + +run "$@" -c "SELECT $SCHEMA.purge_orders()" > /dev/null +[[ "$(run "$@" -c "SELECT count(*) FROM $SCHEMA.orders")" == 0 ]] || failures+=("control: the unwrapped purge did not write") + +if ((${#failures[@]} == 0)); then + echo "OK: the read-only transaction refused every write through a function" + exit 0 +fi +printf 'BUG: %s\n' "${failures[@]}" +exit 1 diff --git a/core/crates/core/src/external_access.rs b/core/crates/core/src/external_access.rs index 7bfd6d0ec9..3819b006ef 100644 --- a/core/crates/core/src/external_access.rs +++ b/core/crates/core/src/external_access.rs @@ -38,6 +38,9 @@ pub enum ExternalRefusal { ReadOnlyAccess, /// Safe Mode is Read-Only, which no client can get past. SafeModeReadOnly, + /// The statement reads or writes files, reaches another server or runs + /// code outside the database, which a read-only transaction does not stop. + ReachesOutside, } impl ExternalAccess { diff --git a/core/crates/core/src/sql_syntax/mod.rs b/core/crates/core/src/sql_syntax/mod.rs index ae9dbe4dfc..8cb99eabd1 100644 --- a/core/crates/core/src/sql_syntax/mod.rs +++ b/core/crates/core/src/sql_syntax/mod.rs @@ -10,6 +10,7 @@ mod keywords; mod literal_token; pub mod mongo; mod order_by; +mod outside_reach; mod parse_expression; mod parse_type; pub mod redis; @@ -31,6 +32,7 @@ pub use highlight::{SyntaxKind, SyntaxToken, syntax_tokens}; pub use history_recording::{HistoryRecording, WithholdReason, classify_for_history}; pub use literal_token::LiteralToken; pub use order_by::with_order_by; +pub use outside_reach::reaches_outside_database; pub use parse_expression::{MAX_EXPRESSION_LEN, ValidatedExpression, parse_expression}; pub use parse_type::{MAX_TYPE_LEN, TypeShape, ValidatedType, parse_type}; pub use sort_reference::{SortReference, sort_reference}; diff --git a/core/crates/core/src/sql_syntax/outside_reach.rs b/core/crates/core/src/sql_syntax/outside_reach.rs new file mode 100644 index 0000000000..af7f37a3c9 --- /dev/null +++ b/core/crates/core/src/sql_syntax/outside_reach.rs @@ -0,0 +1,640 @@ +use crate::sql_syntax::SqlGrammar; +use crate::sql_syntax::script::{ScriptPlan, ScriptToken}; +use crate::sql_syntax::statement_access::{ + CLIENT_SPLITS, Code, Kind, code_of, decodes_unicode_names, is, may_be_space, spelled_in_escapes, split_at_blanks, +}; +use crate::unicode_blank::is_blank; + +const FILESYSTEM_WORD_RUNS: [[&str; 2]; 5] = [ + ["into", "outfile"], + ["into", "dumpfile"], + ["to", "program"], + ["from", "program"], + ["vacuum", "into"], +]; + +// A statement that opens with one of these reads or writes files, attaches another database or runs code. +const FILESYSTEM_OR_CODE_KEYWORDS: &[&str] = &[ + "copy", "attach", "detach", "do", "load", "install", "import", "export", "backup", "restore", "dump", "source", + "unload", "bulk", +]; + +// Reached by name, called or not. +const FILESYSTEM_NAMES: &[&str] = &[ + "xp_cmdshell", + "sp_oacreate", + "xp_dirtree", + "xp_fileexist", + "xp_subdirs", + "xp_regread", + "xp_regwrite", + "sp_execute_external_script", + "utl_http", + "utl_file", + "utl_tcp", + "utl_smtp", + "utl_inaddr", +]; + +const FILESYSTEM_FUNCTIONS: &[&str] = &[ + "load_file", + "pg_read_file", + "pg_read_binary_file", + "pg_ls_dir", + "sys_read_file", + "sys_read_binary_file", + "sys_ls_dir", + "lo_import", + "lo_export", + "pg_file_write", + "load_extension", + "readfile", + "writefile", + "fn_get_audit_file", + "openrowset", + "opendatasource", + "fsdir", + "pg_stat_file", + "pg_ls_logdir", + "pg_ls_waldir", + "pg_ls_tmpdir", + "pg_ls_archive_statusdir", + "pg_ls_logicalsnapdir", + "pg_ls_logicalmapdir", + "pg_ls_replslotdir", + "pg_file_unlink", + "pg_file_rename", + "pg_file_sync", + "pg_file_read", + "pg_logdir_ls", + "fn_xe_file_target_read_file", + "fn_trace_gettable", + "fn_dump_dblog", + "dm_os_enumerate_filesystem", + "dm_os_file_exists", + "bfilename", + "httpuritype", +]; + +const DUCKDB_FUNCTIONS: &[&str] = &[ + "glob", + "sniff_csv", + "getenv", + "query", + "query_table", + "json_execute_serialized_sql", + "from_substrait", + "from_substrait_json", + "import_database", + "arrow_scan", + "arrow_scan_dumb", + "load_aws_credentials", + "enable_logging", + "start_ui", + "start_ui_server", +]; + +const DUCKDB_FAMILIES: &[&str] = &[ + "read_", + "parquet_", + "st_read", + "iceberg_", + "delta_", + "sqlite_", + "postgres_", + "mysql_", +]; + +const CLICKHOUSE_FUNCTIONS: &[&str] = &[ + "file", + "filecluster", + "url", + "urlcluster", + "s3", + "s3cluster", + "gcs", + "cosn", + "oss", + "hdfs", + "hdfscluster", + "remote", + "remotesecure", + "mysql", + "postgresql", + "mongodb", + "redis", + "sqlite", + "jdbc", + "odbc", + "executable", + "arrowflight", + "ytsaurus", +]; + +const CLICKHOUSE_FAMILIES: &[&str] = &["azureblobstorage", "iceberg", "deltalake", "hudi", "paimon"]; + +// An allowlist: an editor LOAD, or an extension DuckDB loads on its own, brings table functions no list knows. +const DUCKDB_TABLE_FUNCTIONS: &[&str] = &[ + "range", + "generate_series", + "unnest", + "repeat", + "json_each", + "json_tree", + "duckdb_columns", + "duckdb_constraints", + "duckdb_databases", + "duckdb_dependencies", + "duckdb_functions", + "duckdb_indexes", + "duckdb_keywords", + "duckdb_schemas", + "duckdb_sequences", + "duckdb_settings", + "duckdb_tables", + "duckdb_types", + "duckdb_variables", + "duckdb_views", + "pragma_database_size", + "pragma_platform", + "pragma_show", + "pragma_storage_info", + "pragma_table_info", + "pragma_version", +]; + +// DuckDB joins a dotted table name with `.` and reads it as a file in the working folder. +const FILE_EXTENSIONS: &[&str] = &[ + "csv", "tsv", "gz", "zst", "parquet", "json", "jsonl", "ndjson", "geojson", "geojsonl", "db", "ddb", "duckdb", +]; + +const TABLE_INTRODUCERS: &[&str] = &[ + "from", + "join", + "using", + "table", + "describe", + "desc", + "show", + "summarize", + "pivot", + "unpivot", +]; + +// Reserved words, so none is the alias in `FROM notes AS n, 'payroll.csv'`. +const TABLE_LIST_ENDS: &[&str] = &[ + "select", + "where", + "group", + "having", + "window", + "qualify", + "order", + "limit", + "offset", + "union", + "intersect", + "except", + "returning", + "do", +]; + +// Only a FROM or JOIN list holds table functions; PIVOT's and USING's parentheses hold expressions. +const CALLING_INTRODUCERS: &[&str] = &["from", "join"]; + +// Their arguments may hold a FROM that names no table. +const FROM_TAKING_FUNCTIONS: &[&str] = &["extract", "trim", "substring", "overlay"]; + +// Read every way statement_access reads the text. A read-only transaction stops none of these. +pub fn reaches_outside_database(sql: &str, grammar: SqlGrammar) -> bool { + if matches!(grammar, SqlGrammar::Redis | SqlGrammar::Mongo) { + return false; + } + if reaches_as_sent(sql, grammar, CLIENT_SPLITS) { + return true; + } + let letters = matches!(grammar, SqlGrammar::PostgreSql | SqlGrammar::Sqlite); + !letters && sql.contains(may_be_space) && reaches_as_sent(&sql.replace(may_be_space, " "), grammar, CLIENT_SPLITS) +} + +fn reaches_as_sent(sql: &str, grammar: SqlGrammar, splits: u8) -> bool { + reaches_every_way(sql, grammar) + || (splits > 0 + && ScriptPlan::client_pieces(sql, grammar) + .into_iter() + .any(|piece| reaches_as_sent(piece, grammar, splits - 1))) +} + +fn reaches_every_way(sql: &str, grammar: SqlGrammar) -> bool { + let Some(mut plans) = ScriptPlan::server_readings(sql, grammar) else { + return true; + }; + plans.any(|plan| { + plan.statements().iter().any(|statement| { + let tokens: Vec = plan.tokens(statement.range.clone()).collect(); + let code = code_of(sql, &tokens); + let blanks_inside = code + .iter() + .any(|token| token.kind == Kind::Word && token.text.contains(is_blank)); + reaches(&code, grammar) || (blanks_inside && reaches(&split_at_blanks(&code), grammar)) + }) + }) +} + +fn reaches(code: &[Code<'_>], grammar: SqlGrammar) -> bool { + code.split(|token| token.kind == Kind::End) + .filter(|part| !part.is_empty()) + .any(|part| { + part.first() + .is_some_and(|first| first.is_word(FILESYSTEM_OR_CODE_KEYWORDS)) + || names_filesystem(part) + || (0..part.len()).any(|index| calls_outside(part, index, grammar)) + || (grammar == SqlGrammar::DuckDb && duckdb_reads_a_file_as_a_table(part)) + }) +} + +fn names_filesystem(part: &[Code<'_>]) -> bool { + part.iter().enumerate().any(|(index, token)| { + (matches!(token.kind, Kind::Word | Kind::Quoted) && is(token.text, FILESYSTEM_NAMES)) + || FILESYSTEM_WORD_RUNS.iter().any(|[first, second]| { + token.is_word(&[*first]) && part.get(index + 1).is_some_and(|next| next.is_word(&[*second])) + }) + }) +} + +// A name spelled U&"…" may be any of them. +fn calls_outside(part: &[Code<'_>], index: usize, grammar: SqlGrammar) -> bool { + let token = &part[index]; + if !matches!(token.kind, Kind::Word | Kind::Quoted) || !opens_a_call(part, index) { + return false; + } + if decodes_unicode_names(grammar) && spelled_in_escapes(part, index) { + return true; + } + let name = token.text; + is(name, FILESYSTEM_FUNCTIONS) + || match grammar { + SqlGrammar::DuckDb => is(name, DUCKDB_FUNCTIONS) || starts_with_any(name, DUCKDB_FAMILIES), + SqlGrammar::ClickHouse => is(name, CLICKHOUSE_FUNCTIONS) || starts_with_any(name, CLICKHOUSE_FAMILIES), + _ => false, + } +} + +// UESCAPE '!' may stand between a U&"…" name and its arguments. +fn opens_a_call(part: &[Code<'_>], index: usize) -> bool { + let mut next = index + 1; + if part.get(next).is_some_and(|word| word.is_word(&["uescape"])) { + next += 1 + part[next + 1..] + .iter() + .take_while(|token| token.kind == Kind::Literal) + .count(); + } + part.get(next).is_some_and(|token| token.kind == Kind::Open) +} + +fn starts_with_any(name: &str, prefixes: &[&str]) -> bool { + prefixes.iter().any(|prefix| { + name.get(..prefix.len()) + .is_some_and(|start| start.eq_ignore_ascii_case(prefix)) + }) +} + +#[derive(Clone, Copy, PartialEq, Eq)] +enum Expectation { + Nothing, + Table, + RestOfName, +} + +#[derive(Clone, Copy)] +struct Level { + separates_arguments_with_from: bool, + lists_tables: bool, + expects: Expectation, + introducer: &'static str, + name_parts: usize, +} + +impl Level { + fn new(separates_arguments_with_from: bool, expects: Expectation, introducer: &'static str) -> Level { + Level { + separates_arguments_with_from, + lists_tables: false, + expects, + introducer, + name_parts: 0, + } + } +} + +// The walk of the macOS app's SQLFileNamedAsTable, plus the table functions and dotted file names it misses. +// DESCRIBE and EXPLAIN open the file too, so they get no shortcut here. +fn duckdb_reads_a_file_as_a_table(part: &[Code<'_>]) -> bool { + let mut levels = vec![Level::new(false, Expectation::Nothing, "")]; + for (index, token) in part.iter().enumerate() { + let Some(level) = levels.last_mut() else { + return true; + }; + let expected = level.expects; + level.expects = Expectation::Nothing; + match token.kind { + Kind::Word + if token.is_word(&["from"]) + && (level.separates_arguments_with_from || compares_distinct(part, index)) => {} + Kind::Word | Kind::Other if let Some(word) = introducer(token) => { + level.expects = Expectation::Table; + level.introducer = word; + level.name_parts = 0; + if word == "from" { + level.lists_tables = true; + } + } + Kind::Word if is(token.text, TABLE_LIST_ENDS) && expected != Expectation::Table => { + level.lists_tables = false; + } + Kind::Word if token.is_word(&["only", "lateral"]) && expected == Expectation::Table => { + level.expects = Expectation::Table; + } + Kind::Word | Kind::Quoted if expected == Expectation::Table => { + if token.kind == Kind::Quoted && token.text.contains(['/', '.', '\\']) { + return true; + } + level.name_parts += 1; + if level.name_parts > 1 && is(token.text, FILE_EXTENSIONS) { + return true; + } + level.expects = Expectation::RestOfName; + } + Kind::Literal if expected == Expectation::Table || prefixes_string(part, index) => return true, + Kind::Open => { + let callee = index.checked_sub(1).map(|before| part[before]); + if expected == Expectation::RestOfName + && is(level.introducer, CALLING_INTRODUCERS) + && callee.is_some_and(|name| !is(name.text, DUCKDB_TABLE_FUNCTIONS) && !name.is_word(&["values"])) + { + return true; + } + let introducer = match expected { + Expectation::Table => level.introducer, + _ => "", + }; + let separates = calls_function_taking_from(part, index); + levels.push(Level::new(separates, expected_inside(expected), introducer)); + } + Kind::Close => { + if levels.len() > 1 { + levels.pop(); + } + } + Kind::Comma => { + if level.lists_tables { + level.expects = Expectation::Table; + level.name_parts = 0; + } + } + Kind::Other if expected == Expectation::RestOfName && matches!(token.text, "." | ":" | "&") => { + level.expects = Expectation::Table; + if token.text != "." { + level.name_parts = 0; + } + } + _ => {} + } + } + false +} + +// `FROM (notes JOIN 'payroll.csv' USING (id))` groups tables, and any other `(` starts afresh. +fn expected_inside(expected: Expectation) -> Expectation { + match expected { + Expectation::Table => Expectation::Table, + _ => Expectation::Nothing, + } +} + +// `SELECT 1_000FROM 'a.csv'` reaches FROM, since a number ends there. +fn introducer(token: &Code<'_>) -> Option<&'static str> { + if let Some(word) = TABLE_INTRODUCERS.iter().find(|word| token.is_word(&[**word])) { + return Some(word); + } + let starts_a_name = token + .text + .chars() + .next() + .is_some_and(|first| first.is_alphabetic() || first == '_'); + match starts_a_name { + true => None, + false => TABLE_INTRODUCERS.iter().copied().find(|word| { + token.text.len() > word.len() + && token + .text + .get(token.text.len() - word.len()..) + .is_some_and(|end| end.eq_ignore_ascii_case(word)) + }), + } +} + +// `E'payroll.csv'`: a letter written against the string is its prefix, not a table's name. +fn prefixes_string(part: &[Code<'_>], index: usize) -> bool { + index > 0 && { + let before = part[index - 1]; + before.kind == Kind::Word + && before.text.as_ptr() as usize + before.text.len() == part[index].text.as_ptr() as usize + } +} + +fn compares_distinct(part: &[Code<'_>], index: usize) -> bool { + index >= 2 && part[index - 1].is_word(&["distinct"]) && part[index - 2].is_word(&["is", "not"]) +} + +// `INSERT INTO trim (FROM 'a.csv')` holds a query: a table may carry one of the names. +fn calls_function_taking_from(part: &[Code<'_>], index: usize) -> bool { + if index == 0 || !part[index - 1].is_word(FROM_TAKING_FUNCTIONS) { + return false; + } + index < 2 + || !(part[index - 2].is_word(&["into"]) || (part[index - 2].kind == Kind::Other && part[index - 2].text == ".")) +} + +#[cfg(test)] +mod tests { + use std::collections::BTreeMap; + + use super::*; + + fn reaches_on_duckdb(sql: &str) -> bool { + reaches_outside_database(sql, SqlGrammar::DuckDb) + } + + #[test] + fn duckdb_file_readers_reach_outside() { + for sql in [ + "SELECT content FROM read_text('/home/ada/.ssh/id_ed25519')", + "SELECT * FROM read_blob('/etc/passwd')", + "SELECT * FROM read_csv('/x.csv')", + "SELECT * FROM read_csv_auto('/x.csv')", + "SELECT * FROM read_json('/x.json')", + "SELECT * FROM read_ndjson_objects('/x.json')", + "SELECT * FROM read_parquet('/x.parquet')", + "SELECT * FROM READ_PARQUET (['a', 'b'])", + "SELECT * FROM parquet_metadata('/x.parquet')", + "SELECT * FROM glob('/home/ada/**')", + "SELECT * FROM sniff_csv('/x.csv')", + "SELECT * FROM query_table('/x.csv')", + "SELECT * FROM query('SELECT 1')", + "SELECT * FROM 'x.csv'", + "SELECT * FROM \"/home/ada/x.csv\"", + "SELECT * FROM finances.csv", + "SELECT * FROM notes.\"csv\"", + "SELECT * FROM backup.duckdb", + "TABLE \"/x.csv\"", + "DESCRIBE '/x.csv'", + "SHOW '/x.csv'", + "DESCRIBE SELECT * FROM read_csv('/f', header = true, skip = 3)", + "EXPLAIN SELECT * FROM read_csv('/f', header = true, skip = 3)", + "SELECT * FROM enable_logging('QueryLog', storage_path = '/tmp/x')", + "SELECT * FROM system.main.read_blob/* c */('/etc/passwd')", + "WITH f AS (SELECT * FROM \"read_blob\" ('/etc/passwd')) SELECT * FROM f", + "SELECT * FROM read_text\u{a0}('/etc/passwd')", + "SELECT * FROM U&\"read_text\"('x')", + "SELECT * FROM (FROM read_text('x'))", + "FROM read_text('x')", + "SELECT * FROM odbc_query('DSN=x', 'DROP TABLE t')", + "SELECT * FROM quack_serve('x')", + "SELECT * FROM notes, read_text('x')", + "SELECT * FROM notes JOIN read_text('x') ON true", + "SELECT getenv('HOME')", + "SELECT * FROM notes AS values, 'payroll.csv'", + "SELECT 1_000FROM 'payroll.csv'", + "SELECT * FROM E'payroll.csv'", + "SELECT * FROM U&'payroll.csv'", + "SELECT * FROM p: 'payroll.csv'", + "DELETE FROM notes USING 'payroll.csv' p WHERE p.id = notes.id", + "SELECT * FROM notes LEFT OUTER JOIN 'payroll.parquet' p ON p.id = notes.id", + ] { + assert!(reaches_on_duckdb(sql), "{sql}"); + } + } + + #[test] + fn duckdb_reads_inside_the_database_stay_inside() { + for sql in [ + "SELECT 'a.csv' FROM notes WHERE path = 'c.csv'", + "SELECT * FROM \"main\".\"notes\"", + "SELECT * FROM range(1, 10)", + "SELECT * FROM main.range(1, 10)", + "SELECT extract(year FROM '2026-01-01'::DATE)", + "SELECT trim('x' FROM 'xax')", + "SELECT substring('abc' FROM 2 FOR 1)", + "SELECT read_at, glob, query FROM notes WHERE name GLOB 'a*'", + "SELECT * FROM duckdb_tables()", + "SELECT count(*) FROM notes GROUP BY kind ORDER BY kind DESC, count(*)", + "SELECT * FROM notes n JOIN tags t ON t.note = n.id WHERE t.name IN ('a', 'b')", + "SELECT * FROM (SELECT * FROM notes) AS n", + "DESCRIBE notes", + "SHOW TABLES", + "TABLE notes", + "SELECT * FROM notes WHERE id IN (SELECT note FROM tags)", + "SELECT * FROM read_text", + "SELECT * FROM notes, (SELECT 'a.csv', 'b.csv') s", + "SELECT * FROM (VALUES ('a.csv'), ('b.csv')) AS t(path)", + "SELECT * FROM notes WHERE body IS DISTINCT FROM 'a.csv' OR body IS NOT DISTINCT FROM 'b.csv'", + "SELECT * FROM notes ORDER BY id DESC, \"meta.path\"", + "CREATE TABLE notes (path VARCHAR DEFAULT 'a.csv')", + "SELECT * FROM notes n JOIN tags t USING (id) WHERE t.name = 'x'", + "PIVOT notes ON year USING sum(salary)", + "SELECT * FROM generate_series(1, 3) AS t(x), notes", + "SELECT _from, 'a.csv' FROM notes", + ] { + assert!(!reaches_on_duckdb(sql), "{sql}"); + } + } + + #[test] + fn file_and_server_functions_reach_outside_on_their_engines() { + for (sql, grammar) in [ + ("SELECT pg_read_file('/etc/passwd')", SqlGrammar::PostgreSql), + ("SELECT * FROM pg_ls_dir('.')", SqlGrammar::PostgreSql), + ("SELECT pg_stat_file('postgresql.conf')", SqlGrammar::PostgreSql), + ("SELECT U&\"pg_read_file\"('/etc/passwd')", SqlGrammar::PostgreSql), + ("SELECT LOAD_FILE('/etc/passwd')", SqlGrammar::MySql), + ("SELECT * FROM t INTO OUTFILE '/tmp/x'", SqlGrammar::MySql), + ( + "SELECT * FROM sys.dm_os_enumerate_filesystem('C:\\', '*')", + SqlGrammar::MsSql, + ), + ( + "SELECT * FROM sys.fn_get_audit_file('C:\\a\\*', default, default)", + SqlGrammar::MsSql, + ), + ("EXEC xp_cmdshell 'dir'", SqlGrammar::MsSql), + ("SELECT * FROM file('x.csv')", SqlGrammar::ClickHouse), + ("SELECT * FROM url('http://x', CSV)", SqlGrammar::ClickHouse), + ("SELECT * FROM remoteSecure('host', db.t)", SqlGrammar::ClickHouse), + ("SELECT * FROM icebergS3('s3://b/t')", SqlGrammar::ClickHouse), + ("SELECT readfile('/etc/passwd')", SqlGrammar::Sqlite), + ("SELECT load_extension('x')", SqlGrammar::Sqlite), + ("COPY orders TO '/tmp/orders.csv'", SqlGrammar::PostgreSql), + ("COPY orders TO '/tmp/orders.csv'", SqlGrammar::DuckDb), + ("ATTACH '/home/ada/other.duckdb' AS other", SqlGrammar::DuckDb), + ("INSTALL httpfs", SqlGrammar::DuckDb), + ("LOAD httpfs", SqlGrammar::DuckDb), + ("EXPORT DATABASE '/tmp/dump'", SqlGrammar::DuckDb), + ("DO $$ BEGIN PERFORM 1; END $$", SqlGrammar::PostgreSql), + ("BULK INSERT t FROM 'C:\\x.csv'", SqlGrammar::MsSql), + ] { + assert!(reaches_outside_database(sql, grammar), "{sql} on {grammar:?}"); + } + } + + #[test] + fn engine_lists_stay_on_their_engine() { + for (sql, grammar) in [ + ("SELECT * FROM 'x.csv'", SqlGrammar::PostgreSql), + ("SELECT file, url FROM documents", SqlGrammar::ClickHouse), + ("SELECT glob('a*', name) FROM t", SqlGrammar::Sqlite), + ("SELECT * FROM read_text('x')", SqlGrammar::PostgreSql), + ("SELECT query FROM history", SqlGrammar::DuckDb), + ("SELECT * FROM orders", SqlGrammar::MySql), + ] { + assert!(!reaches_outside_database(sql, grammar), "{sql} on {grammar:?}"); + } + } + + #[test] + fn a_users_own_file_read_stays_a_read_in_the_editor() { + use crate::sql_syntax::{StatementAccess, statement_access}; + + assert_eq!( + statement_access("SELECT * FROM read_csv('x.csv')", SqlGrammar::DuckDb), + StatementAccess::Read + ); + } + + #[test] + fn the_names_are_the_ones_both_apps_share() { + let shared: BTreeMap> = + serde_json::from_str(include_str!("../../tests/fixtures/outside_reach.json")).unwrap(); + let named = |names: &[&str]| names.iter().map(|name| name.to_string()).collect::>(); + let listed: BTreeMap> = [ + ("clickhouse_families", named(CLICKHOUSE_FAMILIES)), + ("clickhouse_functions", named(CLICKHOUSE_FUNCTIONS)), + ("duckdb_families", named(DUCKDB_FAMILIES)), + ("duckdb_functions", named(DUCKDB_FUNCTIONS)), + ("filesystem_functions", named(FILESYSTEM_FUNCTIONS)), + ("filesystem_names", named(FILESYSTEM_NAMES)), + ("filesystem_or_code_keywords", named(FILESYSTEM_OR_CODE_KEYWORDS)), + ( + "filesystem_word_runs", + FILESYSTEM_WORD_RUNS.iter().map(|run| run.join(" ")).collect(), + ), + ] + .into_iter() + .map(|(family, mut names)| { + names.sort(); + (family.to_owned(), names) + }) + .collect(); + assert_eq!(listed, shared); + } +} diff --git a/core/crates/core/src/sql_syntax/statement_access.rs b/core/crates/core/src/sql_syntax/statement_access.rs index 254236ac6c..2115002330 100644 --- a/core/crates/core/src/sql_syntax/statement_access.rs +++ b/core/crates/core/src/sql_syntax/statement_access.rs @@ -231,7 +231,7 @@ pub fn statement_access(sql: &str, grammar: SqlGrammar) -> StatementAccess { // SQL Server ends a word at a control character, a non-ASCII space or a zero-width character, so // `NEXT\u{1}VALUE FOR` takes a number from a sequence; the lexer reads them as punctuation or letters. -fn may_be_space(character: char) -> bool { +pub(super) fn may_be_space(character: char) -> bool { !character.is_ascii_whitespace() && (character.is_whitespace() || character.is_control() @@ -478,13 +478,13 @@ fn runs_duckdb_call(token: &Code<'_>, grammar: SqlGrammar) -> bool { } /// The grammars whose scanner is PostgreSQL's, which reads `U&"…"`. -fn decodes_unicode_names(grammar: SqlGrammar) -> bool { +pub(super) fn decodes_unicode_names(grammar: SqlGrammar) -> bool { matches!(grammar, SqlGrammar::PostgreSql | SqlGrammar::DuckDb) } /// A `U&"…"` name, whose prefix the lexer reads as the word `U` and `&`, /// written with nothing between them, since `u & "f"(1)` is an AND. -fn spelled_in_escapes(part: &[Code<'_>], index: usize) -> bool { +pub(super) fn spelled_in_escapes(part: &[Code<'_>], index: usize) -> bool { part[index].kind == Kind::Quoted && index >= 2 && part[index - 1].kind == Kind::Other @@ -524,7 +524,7 @@ fn opens_as_read(part: &[Code<'_>], grammar: SqlGrammar) -> bool { } #[derive(Debug, Clone, Copy, PartialEq, Eq)] -enum Kind { +pub(super) enum Kind { Word, Open, Close, @@ -542,19 +542,19 @@ enum Kind { /// A significant token and the parenthesis depth it sits at. A close /// sits at the depth of its open. #[derive(Debug, Clone, Copy)] -struct Code<'a> { - kind: Kind, - text: &'a str, - depth: usize, +pub(super) struct Code<'a> { + pub(super) kind: Kind, + pub(super) text: &'a str, + pub(super) depth: usize, } impl Code<'_> { - fn is_word(&self, set: &[&str]) -> bool { + pub(super) fn is_word(&self, set: &[&str]) -> bool { self.kind == Kind::Word && is(self.text, set) } } -fn code_of<'a>(sql: &'a str, tokens: &[ScriptToken]) -> Vec> { +pub(super) fn code_of<'a>(sql: &'a str, tokens: &[ScriptToken]) -> Vec> { let mut depth = 0usize; tokens .iter() @@ -599,7 +599,7 @@ fn code_of<'a>(sql: &'a str, tokens: &[ScriptToken]) -> Vec> { } /// `code` with each word cut where it holds a Unicode blank. -fn split_at_blanks<'a>(code: &[Code<'a>]) -> Vec> { +pub(super) fn split_at_blanks<'a>(code: &[Code<'a>]) -> Vec> { code.iter() .flat_map(|token| { let pieces: Vec> = match token.kind { @@ -772,7 +772,7 @@ pub fn sqlite_pragma_reports(name: &str) -> bool { is(name, &REPORTING_PRAGMAS) } -fn is(word: &str, set: &[&str]) -> bool { +pub(super) fn is(word: &str, set: &[&str]) -> bool { set.iter().any(|known| word.eq_ignore_ascii_case(known)) } diff --git a/core/crates/core/tests/fixtures/outside_reach.json b/core/crates/core/tests/fixtures/outside_reach.json new file mode 100644 index 0000000000..c8b62133e1 --- /dev/null +++ b/core/crates/core/tests/fixtures/outside_reach.json @@ -0,0 +1,138 @@ +{ + "clickhouse_families": [ + "azureblobstorage", + "deltalake", + "hudi", + "iceberg", + "paimon" + ], + "clickhouse_functions": [ + "arrowflight", + "cosn", + "executable", + "file", + "filecluster", + "gcs", + "hdfs", + "hdfscluster", + "jdbc", + "mongodb", + "mysql", + "odbc", + "oss", + "postgresql", + "redis", + "remote", + "remotesecure", + "s3", + "s3cluster", + "sqlite", + "url", + "urlcluster", + "ytsaurus" + ], + "duckdb_families": [ + "delta_", + "iceberg_", + "mysql_", + "parquet_", + "postgres_", + "read_", + "sqlite_", + "st_read" + ], + "duckdb_functions": [ + "arrow_scan", + "arrow_scan_dumb", + "enable_logging", + "from_substrait", + "from_substrait_json", + "getenv", + "glob", + "import_database", + "json_execute_serialized_sql", + "load_aws_credentials", + "query", + "query_table", + "sniff_csv", + "start_ui", + "start_ui_server" + ], + "filesystem_functions": [ + "bfilename", + "dm_os_enumerate_filesystem", + "dm_os_file_exists", + "fn_dump_dblog", + "fn_get_audit_file", + "fn_trace_gettable", + "fn_xe_file_target_read_file", + "fsdir", + "httpuritype", + "lo_export", + "lo_import", + "load_extension", + "load_file", + "opendatasource", + "openrowset", + "pg_file_read", + "pg_file_rename", + "pg_file_sync", + "pg_file_unlink", + "pg_file_write", + "pg_logdir_ls", + "pg_ls_archive_statusdir", + "pg_ls_dir", + "pg_ls_logdir", + "pg_ls_logicalmapdir", + "pg_ls_logicalsnapdir", + "pg_ls_replslotdir", + "pg_ls_tmpdir", + "pg_ls_waldir", + "pg_read_binary_file", + "pg_read_file", + "pg_stat_file", + "readfile", + "sys_ls_dir", + "sys_read_binary_file", + "sys_read_file", + "writefile" + ], + "filesystem_names": [ + "sp_execute_external_script", + "sp_oacreate", + "utl_file", + "utl_http", + "utl_inaddr", + "utl_smtp", + "utl_tcp", + "xp_cmdshell", + "xp_dirtree", + "xp_fileexist", + "xp_regread", + "xp_regwrite", + "xp_subdirs" + ], + "filesystem_or_code_keywords": [ + "attach", + "backup", + "bulk", + "copy", + "detach", + "do", + "dump", + "export", + "import", + "install", + "load", + "restore", + "source", + "unload" + ], + "filesystem_word_runs": [ + "from program", + "into dumpfile", + "into outfile", + "to program", + "vacuum into" + ] +} diff --git a/core/crates/mcp/src/query_tools.rs b/core/crates/mcp/src/query_tools.rs index c1364a03c7..c5b427e6ae 100644 --- a/core/crates/mcp/src/query_tools.rs +++ b/core/crates/mcp/src/query_tools.rs @@ -5,7 +5,9 @@ use serde::Serialize; use tablepro_core::browse::{DeclaredPrecisionSink, ExportFields, ExportRequest, build_count, build_export}; use tablepro_core::meta::{ObjectScope, RowIdentity}; use tablepro_core::sql_syntax::script::{LexicalSettings, ScriptPlan}; -use tablepro_core::sql_syntax::{SqlGrammar, StatementAccess, changes_catalog, statement_access}; +use tablepro_core::sql_syntax::{ + SqlGrammar, StatementAccess, changes_catalog, reaches_outside_database, statement_access, +}; use tablepro_core::{ CallOptions, ColumnInfo, EditorSessionOptions, ExternalGate, ExternalRefusal, ReadStatement, ResultSink, RowLimit, RunEnd, RunOptions, TableRef, TransactionState, UserSql, @@ -55,6 +57,9 @@ pub async fn execute_query( .ok_or_else(|| ToolError::NotConnected(format!("“{}” has no connection right now", connection.name)))?; let grammar = dialect.grammar(); let text = single_batch(&args.query, grammar)?; + if reaches_outside_database(&text, grammar) { + return Err(refusal(&connection, ExternalRefusal::ReachesOutside)); + } let access = statement_access(&text, grammar); // Before the gate, so a client that asked for a read never puts a write to the user. if args.read_only && access != StatementAccess::Read { @@ -175,6 +180,9 @@ fn refusal(connection: &SavedConnection, reason: ExternalRefusal) -> ToolError { "Safe Mode on “{}” is Read-Only, which refuses every write", connection.name )), + ExternalRefusal::ReachesOutside => ToolError::Denied( + "the statement reads or writes files, reaches another server or runs code outside the database, which TablePro refuses for MCP clients".to_owned(), + ), } } diff --git a/core/crates/mcp/tests/mcp_server.rs b/core/crates/mcp/tests/mcp_server.rs index 3e4a7b807e..6c327a3a80 100644 --- a/core/crates/mcp/tests/mcp_server.rs +++ b/core/crates/mcp/tests/mcp_server.rs @@ -597,6 +597,28 @@ async fn a_write_where_clients_only_read_is_denied_without_asking() { assert_eq!(order_count(&harness).await, 3); } +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn a_statement_that_reaches_a_file_is_refused_at_every_level_without_asking() { + let harness = harness().await; + harness.host.approves.store(true, Ordering::SeqCst); + + for connection in [harness.ids.readable, harness.ids.writable] { + let (failed, text) = call( + &harness, + "execute_query", + json!({ + "connection_id": connection.to_string(), + "query": "SELECT readfile('/etc/passwd')", + }), + ) + .await; + + assert!(failed); + assert!(text.starts_with("denied:"), "{text}"); + } + assert!(harness.host.asked.lock().expect("asked").is_empty()); +} + #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn a_write_the_user_declines_changes_nothing() { let harness = harness().await; From 9cf4fa57714b4f0df7462747c0cef21c482b9968 Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Thu, 12 Nov 2026 06:54:25 +0700 Subject: [PATCH 2/7] fix(macos): read every Read-Only browse, compare and copy in the engine's read-only mode and bound the error redactor --- .../LibPQParameterBuffers.swift | 65 +++++ .../LibPQPluginConnection+ResultReading.swift | 7 + .../LibPQPluginConnection.swift | 169 ++++++++----- .../LibPQReadBound.swift | 33 +++ .../LibPQBoundedReadOnlyTests.swift | 153 +++++++++++- .../LibPQReadBoundTests.swift | 26 ++ .../LibPQScriptedServer.swift | 15 +- .../Core/Compare/CompareRowService.swift | 58 ++++- .../Core/MCP/MCPConnectionBridge+Data.swift | 13 +- .../MCP/Protocol/Tools/MCPErrorRedactor.swift | 20 +- .../Core/ObjectCopy/ObjectCopyRowCopier.swift | 13 +- .../Core/ObjectCopy/ObjectCopyRunner.swift | 19 +- .../Execution/ReadOnlyFunctionCalls.swift | 68 ++++-- .../Core/Services/Export/ExportService.swift | 6 +- .../Export/TableTransferService.swift | 4 +- .../TablePro/Resources/Localizable.xcstrings | 76 ++++++ ...nContentCoordinator+ReadOnlySafeMode.swift | 15 +- .../MCP/MCPHistoryAnswerBudgetTests.swift | 4 +- .../Tools/MCPToolErrorSurfaceTests.swift | 12 + .../Execution/ExecutionGateTests.swift | 22 ++ .../Core/WritePolicy/WritePolicyMatrix.swift | 4 +- .../ReadOnlySafeModeEntryPointTests.swift | 222 +++++++++++++++++- .../core/src/sql_syntax/outside_reach.rs | 34 ++- 23 files changed, 946 insertions(+), 112 deletions(-) create mode 100644 apple/Plugins/PostgreSQLDriverPlugin/LibPQParameterBuffers.swift diff --git a/apple/Plugins/PostgreSQLDriverPlugin/LibPQParameterBuffers.swift b/apple/Plugins/PostgreSQLDriverPlugin/LibPQParameterBuffers.swift new file mode 100644 index 0000000000..d353374ac2 --- /dev/null +++ b/apple/Plugins/PostgreSQLDriverPlugin/LibPQParameterBuffers.swift @@ -0,0 +1,65 @@ +// +// LibPQParameterBuffers.swift +// PostgreSQLDriverPlugin +// + +import Foundation +import TableProPluginKit + +// The C buffers `PQexecParams` and `PQsendQueryParams` read the parameters from: text as a C string, bytes in binary +// format, null as a null pointer. +internal enum LibPQParameterBuffers { + // libpq reads the buffers only while the call sends them, so they live for `body` alone. + static func with( + _ parameters: [PluginCellValue], + _ body: (_ values: [UnsafePointer?], _ lengths: UnsafePointer?, _ formats: UnsafePointer?) throws -> T + ) throws -> T { + var values: [UnsafePointer?] = [] + var lengths: [Int32] = [] + var formats: [Int32] = [] + var allocations: [UnsafeMutableRawPointer] = [] + defer { + for pointer in allocations { + free(pointer) + } + } + values.reserveCapacity(parameters.count) + lengths.reserveCapacity(parameters.count) + formats.reserveCapacity(parameters.count) + + for parameter in parameters { + switch parameter { + case .null: + values.append(nil) + lengths.append(0) + formats.append(0) + case .text(let text): + guard let cString = strdup(text) else { + throw LibPQPluginError(message: "Failed to allocate parameter buffer", sqlState: nil, detail: nil) + } + allocations.append(UnsafeMutableRawPointer(cString)) + values.append(UnsafePointer(cString)) + lengths.append(0) + formats.append(0) + case .bytes(let data): + let byteCount = data.count + guard let raw = malloc(max(byteCount, 1)) else { + throw LibPQPluginError(message: "Failed to allocate parameter buffer", sqlState: nil, detail: nil) + } + allocations.append(raw) + if byteCount > 0 { + data.copyBytes(to: raw.assumingMemoryBound(to: UInt8.self), count: byteCount) + } + values.append(UnsafePointer(raw.assumingMemoryBound(to: CChar.self))) + lengths.append(Int32(byteCount)) + formats.append(1) + } + } + + return try lengths.withUnsafeBufferPointer { lengthsBuffer in + try formats.withUnsafeBufferPointer { formatsBuffer in + try body(values, lengthsBuffer.baseAddress, formatsBuffer.baseAddress) + } + } + } +} diff --git a/apple/Plugins/PostgreSQLDriverPlugin/LibPQPluginConnection+ResultReading.swift b/apple/Plugins/PostgreSQLDriverPlugin/LibPQPluginConnection+ResultReading.swift index d2e7ea8c04..af6cf4a197 100644 --- a/apple/Plugins/PostgreSQLDriverPlugin/LibPQPluginConnection+ResultReading.swift +++ b/apple/Plugins/PostgreSQLDriverPlugin/LibPQPluginConnection+ResultReading.swift @@ -87,6 +87,13 @@ internal extension LibPQPluginConnection { return LibPQCellDecoding.value(from: UnsafeRawBufferPointer(start: valuePtr, count: length), oid: oid) } + // The one row a single-row mode result holds. + static func singleRow(of result: OpaquePointer, oids: [UInt32]) -> [PluginCellValue] { + oids.enumerated().map { column in + decodeCell(from: result, row: 0, column: Int32(column.offset), oid: column.element) + } + } + private func parseRows( from result: OpaquePointer, columns: [String], diff --git a/apple/Plugins/PostgreSQLDriverPlugin/LibPQPluginConnection.swift b/apple/Plugins/PostgreSQLDriverPlugin/LibPQPluginConnection.swift index c5e1d6832b..2edca91a34 100644 --- a/apple/Plugins/PostgreSQLDriverPlugin/LibPQPluginConnection.swift +++ b/apple/Plugins/PostgreSQLDriverPlugin/LibPQPluginConnection.swift @@ -1359,7 +1359,7 @@ final class LibPQPluginConnection: @unchecked Sendable { let cancelsOutput = cancelsAbandonedOutput(conn) let localQuery = String(query) - let result: OpaquePointer? = try Self.withParameterBuffers(parameters) { values, lengths, formats in + let result: OpaquePointer? = try LibPQParameterBuffers.with(parameters) { values, lengths, formats in if let ended = sessionEndedBeforeSending(conn) { throw ended } return localQuery.withCString { queryPtr in PQexecParams(conn, queryPtr, Int32(parameters.count), nil, values, lengths, formats, 0) @@ -1834,9 +1834,9 @@ extension LibPQPluginConnection { // Outside the class body, which is at its length limit. An extension in this file still reaches its private members. extension LibPQPluginConnection { - // `executeReadOnlyStatement` bounded at the source: the statement goes out in single-row mode and stops at `bound`, - // so no more than that is ever held. Stopping cancels only the read-only transaction this opened. Unknown column - // types are looked up after its ROLLBACK, since a cancel can leave it aborted. + // `executeReadOnlyStatement` bounded at the source: the statement stops at `bound`, so no more than that is ever + // held. Stopping ends only the read-only transaction this opened. Unknown column types are looked up after its + // ROLLBACK, since a cancel can leave it aborted. func boundedReadOnlyStatement( _ statement: String, parameters: [PluginCellValue]?, @@ -1854,7 +1854,11 @@ extension LibPQPluginConnection { reason: reason, send: { try runReadOnlyControl($0, on: conn) } ) { - try boundedReadSync(statementToRun, parameters: params, bound: bound, conn: conn) + if suppressServerSideCancel, + let cursorRead = try cursorBoundedReadSync(statementToRun, parameters: params, bound: bound, conn: conn) { + return cursorRead + } + return try boundedReadSync(statementToRun, parameters: params, bound: bound, conn: conn) } let metadata = read.metadata.map { resolvingUnknownTypes($0, conn: conn) } var result = PostGISTypes.rendering( @@ -1887,8 +1891,9 @@ extension LibPQPluginConnection { // Runs inside the read-only transaction `boundedReadOnlyStatement` opened. The session reports a block open, but // the block is this read's own and ends in ROLLBACK, so the server is cancelled wherever it takes a cancel, which - // `cancelsAbandonedOutput` would refuse inside a block the user opened. PGlite takes none, so its rest is drained. - // The column sources come from the first row description, as every single-row result carries one. + // `cancelsAbandonedOutput` would refuse inside a block the user opened. A server that takes none reaches this only + // for a statement no cursor can hold or whose DECLARE it refused, and its rest is drained. The column sources come + // from the first row description, as every single-row result carries one. private func boundedReadSync( _ query: String, parameters: [PluginCellValue], @@ -1902,7 +1907,7 @@ extension LibPQPluginConnection { let sentAt = Date() let suppressCancel = suppressServerSideCancel let localQuery = String(query) - let sent: Int32 = try Self.withParameterBuffers(parameters) { values, lengths, formats in + let sent: Int32 = try LibPQParameterBuffers.with(parameters) { values, lengths, formats in if let ended = sessionEndedBeforeSending(conn) { throw ended } return localQuery.withCString { queryPtr in PQsendQueryParams(conn, queryPtr, Int32(parameters.count), nil, values, lengths, formats, 0) @@ -1933,12 +1938,7 @@ extension LibPQPluginConnection { } if status == PGRES_SINGLE_TUPLE { - let oids = read.metadata?.columnOids ?? [] - var row: [PluginCellValue] = [] - row.reserveCapacity(oids.count) - for (columnIndex, oid) in oids.enumerated() { - row.append(Self.decodeCell(from: result, row: 0, column: Int32(columnIndex), oid: oid)) - } + let row = Self.singleRow(of: result, oids: read.metadata?.columnOids ?? []) PQclear(result) bytes += LibPQReadBound.byteCount(of: row) read.rows.append(row) @@ -1999,57 +1999,106 @@ extension LibPQPluginConnection { return read } - // libpq reads the parameter buffers only while the call sends them, so they live for `body` alone. - private static func withParameterBuffers( - _ parameters: [PluginCellValue], - _ body: (_ values: [UnsafePointer?], _ lengths: UnsafePointer?, _ formats: UnsafePointer?) throws -> T - ) throws -> T { - var values: [UnsafePointer?] = [] - var lengths: [Int32] = [] - var formats: [Int32] = [] - var allocations: [UnsafeMutableRawPointer] = [] - defer { - for pointer in allocations { - free(pointer) - } + // A server that takes no cancel (PGlite has one connection) would make `boundedReadSync` read every row after the + // stop. There the statement goes through `LibPQBoundedCursor` and is fetched a batch at a time, so no row past the + // stop is produced. A savepoint holds the DECLARE: a refused one is undone, and nil sends the statement the plain + // way, which reports its own error in the user's text. + private func cursorBoundedReadSync( + _ query: String, + parameters: [PluginCellValue], + bound: LibPQReadBound, + conn: OpaquePointer + ) throws -> BoundedRead? { + guard bound.rowCap < Int(Int32.max), let body = LibPQCappedCursorRead.cursorBody(of: query) else { return nil } + let generation = cancellationGate.beginQuery() + defer { cancellationGate.endQuery(generation) } + defer { refreshStandardConformingStrings(from: conn) } + let sentAt = Date() + + try runReadOnlyControl(LibPQBoundedCursor.savepoint, on: conn) + if cancellationGate.isCancelled(generation) { throw CancellationError() } + let declaration = LibPQBoundedCursor.declaration(of: body) + let declared: OpaquePointer? = try LibPQParameterBuffers.with(parameters) { values, lengths, formats in + declaration.withCString { PQexecParams(conn, $0, Int32(parameters.count), nil, values, lengths, formats, 0) } } - values.reserveCapacity(parameters.count) - lengths.reserveCapacity(parameters.count) - formats.reserveCapacity(parameters.count) - - for parameter in parameters { - switch parameter { - case .null: - values.append(nil) - lengths.append(0) - formats.append(0) - case .text(let text): - guard let cString = strdup(text) else { - throw LibPQPluginError(message: "Failed to allocate parameter buffer", sqlState: nil, detail: nil) - } - allocations.append(UnsafeMutableRawPointer(cString)) - values.append(UnsafePointer(cString)) - lengths.append(0) - formats.append(0) - case .bytes(let data): - let byteCount = data.count - guard let raw = malloc(max(byteCount, 1)) else { - throw LibPQPluginError(message: "Failed to allocate parameter buffer", sqlState: nil, detail: nil) - } - allocations.append(raw) - if byteCount > 0 { - data.copyBytes(to: raw.assumingMemoryBound(to: UInt8.self), count: byteCount) - } - values.append(UnsafePointer(raw.assumingMemoryBound(to: CChar.self))) - lengths.append(Int32(byteCount)) - formats.append(1) + guard let declared else { throw lostConnection(getError(from: conn), on: conn, sent: true) } + let refused = PQresultStatus(declared) == PGRES_COMMAND_OK ? nil : getResultError(from: declared) + PQclear(declared) + recordTransactionState(of: conn) + if let refused { + guard PQstatus(conn) == CONNECTION_OK else { throw lostConnection(refused, on: conn, sent: true) } + try runReadOnlyControl(LibPQBoundedCursor.savepointRollback, on: conn) + if cancellationGate.isCancelled(generation) { throw CancellationError() } + return nil + } + + var read = BoundedRead() + var bytes = 0 + while true { + let count = bound.nextFetchCount(rows: read.rows.count, bytes: bytes) + let batch = try fetchCursorBatch( + count: count, bound: bound, read: &read, bytes: &bytes, sentAt: sentAt, generation: generation, conn: conn + ) + if cancellationGate.isCancelled(generation) { throw CancellationError() } + if batch.stopped { + read.isTruncated = true + break } + if batch.received < count { break } } + read.rows = bound.kept(read.rows) + return read + } - return try lengths.withUnsafeBufferPointer { lengthsBuffer in - try formats.withUnsafeBufferPointer { formatsBuffer in - try body(values, lengthsBuffer.baseAddress, formatsBuffer.baseAddress) + // One FETCH in single-row mode. A stop inside it drains only the rest of this batch, since the server takes no + // cancel; the cursor keeps everything after it unproduced. + private func fetchCursorBatch( + count: Int, + bound: LibPQReadBound, + read: inout BoundedRead, + bytes: inout Int, + sentAt: Date, + generation: Int, + conn: OpaquePointer + ) throws -> (received: Int, stopped: Bool) { + let sendOk = LibPQBoundedCursor.fetch(count).withCString { PQsendQuery(conn, $0) } + guard sendOk != 0 else { throw lostConnection(getError(from: conn), on: conn, sent: false) } + guard PQsetSingleRowMode(conn) != 0 else { + _ = finishPendingResults(conn, cancellingOutput: false) + recordTransactionState(of: conn) + throw LibPQPluginError(message: "Failed to enter single-row mode", sqlState: nil, detail: nil) + } + + var received = 0 + var stopped = false + var failure: LibPQPluginError? + while let result = PQgetResult(conn) { + let status = PQresultStatus(result) + if read.firstRowTime == nil { read.firstRowTime = Date().timeIntervalSince(sentAt) } + if read.metadata == nil, Self.carriesRowDescription(status) { + read.metadata = readColumnMetadata(from: result) + read.columnSources = PostgreSQLColumnOrigins.sources(of: result) } + if status == PGRES_SINGLE_TUPLE { + let row = Self.singleRow(of: result, oids: read.metadata?.columnOids ?? []) + PQclear(result) + received += 1 + bytes += LibPQReadBound.byteCount(of: row) + read.rows.append(row) + if cancellationGate.isCancelled(generation) { break } + if bound.isReached(rows: read.rows.count, bytes: bytes) { + stopped = true + break + } + continue + } + if status != PGRES_TUPLES_OK, status != PGRES_COMMAND_OK { failure = getResultError(from: result) } + PQclear(result) + if failure != nil { break } } + _ = finishPendingResults(conn, cancellingOutput: false) + recordTransactionState(of: conn) + if let failure { throw lostConnection(failure, on: conn, sent: true) } + return (received, stopped) } } diff --git a/apple/Plugins/PostgreSQLDriverPlugin/LibPQReadBound.swift b/apple/Plugins/PostgreSQLDriverPlugin/LibPQReadBound.swift index 9bee15c2a2..2969124f51 100644 --- a/apple/Plugins/PostgreSQLDriverPlugin/LibPQReadBound.swift +++ b/apple/Plugins/PostgreSQLDriverPlugin/LibPQReadBound.swift @@ -35,6 +35,20 @@ struct LibPQReadBound: Equatable, Sendable { rows.count > rowCap ? Array(rows.prefix(rowCap)) : rows } + static let largestFetch = 1_000 + + // How many rows the next cursor FETCH asks for, given what was read so far. Without a byte limit, all the cap still + // allows. With one, a single row first, then as many as the rows so far say still fit: a FETCH cannot be cancelled + // where the cursor is used, so a batch the limit stops has the rest of its rows drained. + func nextFetchCount(rows: Int, bytes: Int) -> Int { + let remaining = max(rowCap - rows + 1, 1) + guard let byteLimit else { return remaining } + guard rows > 0 else { return 1 } + let average = max(bytes / rows, 1) + let fitting = max(byteLimit - bytes, 0) / average + 1 + return min(remaining, fitting, Self.largestFetch) + } + // The decoded size, at most what the host counts for the same row, so the read never stops before the host's own // budget is full. static func byteCount(of row: [PluginCellValue]) -> Int { @@ -47,3 +61,22 @@ struct LibPQReadBound: Equatable, Sendable { } } } + +// The cursor a bounded read-only read goes through where the server takes no cancel. It lives in the read's own +// read-only transaction, whose ROLLBACK closes it, so the rows after the stop are never produced or sent. A FETCH result +// describes the cursor query's own columns, so the column origins survive. +enum LibPQBoundedCursor { + static let name = "tablepro_bounded_read" + // A round trip of its own, so a DECLARE the server refuses is undone and the statement can run the plain way. + static let savepoint = "SAVEPOINT \(name)" + static let savepointRollback = "ROLLBACK TO SAVEPOINT \(name)" + + // The body on lines of its own, so a trailing `--` comment in it stays a comment. + static func declaration(of body: String) -> String { + "DECLARE \(name) NO SCROLL CURSOR FOR\n\(body)\n" + } + + static func fetch(_ count: Int) -> String { + "FETCH FORWARD \(count) FROM \(name)" + } +} diff --git a/apple/Plugins/PostgreSQLDriverTests/LibPQBoundedReadOnlyTests.swift b/apple/Plugins/PostgreSQLDriverTests/LibPQBoundedReadOnlyTests.swift index e886af72bf..bd41cc39a6 100644 --- a/apple/Plugins/PostgreSQLDriverTests/LibPQBoundedReadOnlyTests.swift +++ b/apple/Plugins/PostgreSQLDriverTests/LibPQBoundedReadOnlyTests.swift @@ -8,7 +8,8 @@ import TableProPluginKit import Testing // A scripted server sends every row at once and, where the answer says so, holds the statement open until a cancel -// arrives, so a read that does not stop and cancel would end only after the server gives up waiting. +// arrives, so a read that does not stop and cancel would end only after the server gives up waiting. Where the server +// takes no cancel, `CursorTable` counts the rows the cursor's FETCHes were sent. @Suite(.timeLimit(.minutes(1))) struct LibPQBoundedReadOnlyTests { private static let events = "SELECT * FROM events" @@ -122,21 +123,163 @@ struct LibPQBoundedReadOnlyTests { #expect(sent == [LibPQReadOnlyTransaction.begin, Self.events, LibPQReadOnlyTransaction.end]) } + // Serves the cursor's FETCHes from a table and counts every row it sent. + private final class CursorTable: @unchecked Sendable { + private static let fetchPrefix = "FETCH FORWARD " + private let lock = NSLock() + private let rows: [[String?]] + private var position = 0 + + init(_ rows: [[String?]]) { + self.rows = rows + } + + var served: Int { + lock.withLock { position } + } + + func answer(_ query: String) -> LibPQScriptedServer.Answer? { + guard query.hasPrefix(Self.fetchPrefix), + let count = Int(query.dropFirst(Self.fetchPrefix.count).prefix { $0.isNumber }) + else { return nil } + return lock.withLock { + let batch = Array(rows[position.. LibPQScriptedServer { + try LibPQScriptedServer( + status: { query, earlier in Self.status(query, earlier) }, + answer: { query in table.answer(query) } + ) + } + + private static let declaredEvents = LibPQBoundedCursor.declaration(of: LibPQBoundedReadOnlyTests.events) + // PGlite has one connection and takes no cancel request. - @Test("Without a server cancel the rest is drained, and the read still stops at the cap") - func drainsWhereNoCancelIsTaken() async throws { - let server = try Self.server(answering: .init(columns: Self.columns, rows: Self.rows(50))) + @Test("Without a server cancel the read goes through a cursor and no row past the cap is sent") + func cursorStopsAtTheRowCap() async throws { + let table = CursorTable(Self.rows(50)) + let server = try Self.cursorServer(table) defer { server.close() } server.start() let connection = try await Self.connected(to: server, suppressingCancel: true) defer { connection.disconnect() } + let before = server.queries.count let result = try await Self.read(connection, rowCap: 3) + let sent = Array(server.queries.dropFirst(before)) #expect(result.rows.count == 3) #expect(result.isTruncated) + #expect(table.served == 4) #expect(server.cancelRequests == 0) - #expect(server.queries.last == LibPQReadOnlyTransaction.end) + #expect(result.columnSources == [ + PostgreSQLColumnOrigins.Source(relation: 16_400, attribute: 1), + PostgreSQLColumnOrigins.Source(relation: 16_400, attribute: 3), + ]) + #expect(sent == [ + LibPQReadOnlyTransaction.begin, + LibPQBoundedCursor.savepoint, + Self.declaredEvents, + LibPQBoundedCursor.fetch(4), + LibPQReadOnlyTransaction.end, + ]) + } + + @Test("Through a cursor, the byte limit sizes each fetch, so the read stops with no row left to drain") + func cursorStopsAtTheByteLimit() async throws { + let table = CursorTable(Self.rows(50, width: 100)) + let server = try Self.cursorServer(table) + defer { server.close() } + server.start() + let connection = try await Self.connected(to: server, suppressingCancel: true) + defer { connection.disconnect() } + let before = server.queries.count + + let result = try await Self.read(connection, rowCap: 1_000, byteLimit: 250) + + let sent = Array(server.queries.dropFirst(before)) + #expect(result.rows.count == 3) + #expect(result.isTruncated) + #expect(table.served == 3) + #expect(sent.filter { $0.hasPrefix("FETCH") } == [LibPQBoundedCursor.fetch(1), LibPQBoundedCursor.fetch(2)]) + #expect(sent.last == LibPQReadOnlyTransaction.end) + } + + @Test("Through a cursor, a result within the cap ends with the first short fetch") + func cursorReadsAShortResultWhole() async throws { + let table = CursorTable(Self.rows(2)) + let server = try Self.cursorServer(table) + defer { server.close() } + server.start() + let connection = try await Self.connected(to: server, suppressingCancel: true) + defer { connection.disconnect() } + + let result = try await Self.read(connection, rowCap: 3) + + #expect(result.rows.count == 2) + #expect(!result.isTruncated) + #expect(server.queries.filter { $0.hasPrefix("FETCH") } == [LibPQBoundedCursor.fetch(4)]) + } + + @Test("A DECLARE the server refuses is rolled back to its savepoint, and the statement runs the plain way") + func refusedDeclarationRunsThePlainWay() async throws { + let refused = LibPQScriptedServer.Failure(sqlState: "42601", message: "syntax error at or near \"DECLARE\"") + let server = try LibPQScriptedServer( + status: { query, earlier in Self.status(query, earlier) }, + answer: { query in + if query.hasPrefix("DECLARE") { return LibPQScriptedServer.Answer(failure: refused) } + return query == Self.events ? LibPQScriptedServer.Answer(columns: Self.columns, rows: Self.rows(2)) : nil + } + ) + defer { server.close() } + server.start() + let connection = try await Self.connected(to: server, suppressingCancel: true) + defer { connection.disconnect() } + let before = server.queries.count + + let result = try await Self.read(connection, rowCap: 3) + + let sent = Array(server.queries.dropFirst(before)) + #expect(result.rows.count == 2) + #expect(sent == [ + LibPQReadOnlyTransaction.begin, + LibPQBoundedCursor.savepoint, + Self.declaredEvents, + LibPQBoundedCursor.savepointRollback, + Self.events, + LibPQReadOnlyTransaction.end, + ]) + } + + // `LibPQCappedCursorRead.cursorBody` refuses any semicolon, even one inside a literal. + @Test("Without a server cancel, a statement no cursor can hold still stops at the cap and drains the rest") + func drainsWhatNoCursorCanHold() async throws { + let marked = "SELECT ';' AS mark FROM events" + let server = try LibPQScriptedServer( + status: { query, earlier in Self.status(query, earlier) }, + answer: { query in + query == marked ? LibPQScriptedServer.Answer(columns: Self.columns, rows: Self.rows(50)) : nil + } + ) + defer { server.close() } + server.start() + let connection = try await Self.connected(to: server, suppressingCancel: true) + defer { connection.disconnect() } + let before = server.queries.count + + let result = try await Self.read(connection, rowCap: 3, query: marked) + + #expect(result.rows.count == 3) + #expect(result.isTruncated) + #expect(server.cancelRequests == 0) + #expect(Array(server.queries.dropFirst(before)) == [ + LibPQReadOnlyTransaction.begin, marked, LibPQReadOnlyTransaction.end, + ]) } @Test("Each column keeps the relation column the first row description named, so masking by origin still works") diff --git a/apple/Plugins/PostgreSQLDriverTests/LibPQReadBoundTests.swift b/apple/Plugins/PostgreSQLDriverTests/LibPQReadBoundTests.swift index 644a572321..593cbdcb0f 100644 --- a/apple/Plugins/PostgreSQLDriverTests/LibPQReadBoundTests.swift +++ b/apple/Plugins/PostgreSQLDriverTests/LibPQReadBoundTests.swift @@ -48,6 +48,32 @@ struct LibPQReadBoundTests { #expect(bound.kept(four) == three) } + @Test("Without a byte limit a cursor fetches all the cap still allows, the row that tells 'more' included") + func fetchCountWithoutByteLimit() { + let bound = LibPQReadBound(rowCap: 3, byteLimit: nil) + + #expect(bound.nextFetchCount(rows: 0, bytes: 0) == 4) + #expect(bound.nextFetchCount(rows: 2, bytes: 9_999) == 2) + } + + @Test("With a byte limit a cursor fetches one row, then as many as the rows so far say still fit") + func fetchCountWithByteLimit() { + let bound = LibPQReadBound(rowCap: 1_000, byteLimit: 250) + + #expect(bound.nextFetchCount(rows: 0, bytes: 0) == 1) + #expect(bound.nextFetchCount(rows: 1, bytes: 101) == 2) + #expect(bound.nextFetchCount(rows: 2, bytes: 250) == 1) + } + + @Test("A fetch never passes the cap or the largest batch") + func fetchCountIsCapped() { + let roomy = LibPQReadBound(rowCap: 100_000, byteLimit: 1 << 30) + let nearTheCap = LibPQReadBound(rowCap: 3, byteLimit: 1 << 30) + + #expect(roomy.nextFetchCount(rows: 1, bytes: 1) == LibPQReadBound.largestFetch) + #expect(nearTheCap.nextFetchCount(rows: 2, bytes: 2) == 2) + } + @Test("A row's size is its text in UTF-8 and its bytes, with nulls free") func measuresDecodedSize() { let row: [PluginCellValue] = [.text("é"), .bytes(Data([1, 2, 3])), .null, .text("ab")] diff --git a/apple/Plugins/PostgreSQLDriverTests/LibPQScriptedServer.swift b/apple/Plugins/PostgreSQLDriverTests/LibPQScriptedServer.swift index e41be9da74..42b236ae40 100644 --- a/apple/Plugins/PostgreSQLDriverTests/LibPQScriptedServer.swift +++ b/apple/Plugins/PostgreSQLDriverTests/LibPQScriptedServer.swift @@ -9,7 +9,7 @@ import Foundation // A loopback PostgreSQL server for one plaintext session. It refuses SSL and GSS encryption, accepts any startup // without a password, records each simple query and each statement the extended protocol parses, runs `onQuery` before // it answers, and reports the transaction status `status` picks from the query and the ones before it. A simple query -// gets an empty completion; a parsed statement gets what `answer` returns for it, an empty completion for nil. Cancel +// or a parsed statement gets what `answer` returns for it, an empty completion for nil. Cancel // requests arrive on connections of their own: each is counted, and a statement waiting for one then fails as a // cancelled statement does. final class LibPQScriptedServer: @unchecked Sendable { @@ -143,7 +143,18 @@ final class LibPQScriptedServer: @unchecked Sendable { let query = Self.cString(body, from: 0).text let earlier = record(query) onQuery(query) - reply = Self.message("C", Array("SELECT 0".utf8) + [0]) + Self.message("Z", [status(query, earlier)]) + var simpleFailed = false + if let scripted = answer(query) { + if scripted.failure == nil, !scripted.columns.isEmpty { + guard send(client, Self.rowDescription(scripted.columns)) else { return } + } + guard execute(scripted, on: client, failed: &simpleFailed) else { return } + } else { + reply = Self.message("C", Array("SELECT 0".utf8) + [0]) + } + var state = status(query, earlier) + if simpleFailed, state == Self.inTransaction { state = Self.failedTransaction } + reply += Self.message("Z", [state]) case UInt8(ascii: "P"): let query = Self.cString(body, from: Self.cString(body, from: 0).next).text let earlier = record(query) diff --git a/apple/TablePro/Core/Compare/CompareRowService.swift b/apple/TablePro/Core/Compare/CompareRowService.swift index 909a199607..2e2b78109e 100644 --- a/apple/TablePro/Core/Compare/CompareRowService.swift +++ b/apple/TablePro/Core/Compare/CompareRowService.swift @@ -39,9 +39,11 @@ internal enum ConcurrentReadRefusal: Equatable, Sendable { @MainActor internal struct CompareRowService { private let manager: DatabaseManager + private let gate: any ExecutionGate - internal init(manager: DatabaseManager = .shared) { + internal init(manager: DatabaseManager = .shared, gate: any ExecutionGate = ExecutionGateProvider.shared) { self.manager = manager + self.gate = gate } internal func concurrentReadRefusal( @@ -132,6 +134,29 @@ internal struct CompareRowService { // MARK: - Plumbing + // A filter is the user's own text, read on a connection that may be at Read-Only: it meets the gate as a Run would. + private func authorizeReadOnlyFilter( + _ filter: String?, + on endpoint: DatabaseEndpoint, + schema: String?, + table: String + ) async throws { + let driver = manager.driver(for: endpoint.connectionId) + let statement = ReadOnlyReadGate.filterStatement( + table: table, schema: schema, filter: filter, databaseType: endpoint.databaseType + ) { name in + driver?.quoteIdentifier(name) ?? name + } + try await ReadOnlyReadGate.authorize( + statement.map { [$0] } ?? [], + connectionId: endpoint.connectionId, + databaseType: endpoint.databaseType, + gate: gate, + operationDescription: String(localized: "Execute Query"), + manager: manager + ) + } + private func withBothSides( source: DatabaseEndpoint, sourceConnection: DatabaseConnection, @@ -149,6 +174,10 @@ internal struct CompareRowService { } try await manager.ensureConnected(sourceConnection) try await manager.ensureConnected(targetConnection) + try await authorizeReadOnlyFilter(plan.scope.effectiveSourceFilter, on: source, schema: source.schema ?? plan.schema, table: plan.table) + try await authorizeReadOnlyFilter(plan.scope.effectiveTargetFilter, on: target, schema: plan.targetSchema, table: plan.table) + let sourceIsReadOnly = ExecutionGateProvider.runsAtReadOnly(source.connectionId, in: manager) + let targetIsReadOnly = ExecutionGateProvider.runsAtReadOnly(target.connectionId, in: manager) let engine = DataDiffEngine(options: options, shape: plan.comparisonShape) let sourceRead = SideRead( @@ -182,18 +211,20 @@ internal struct CompareRowService { String(localized: "The source driver cannot stream rows for a comparison.") ) } + let sourceSide = sourceRead.carrying(sourceDriver.readOnlyRunContext(safeModeIsReadOnly: sourceIsReadOnly)) return try await DatabaseManager.shared.withMetadataDriver(scope: target.scope) { targetDriver in guard let targetPlugin = CompareMetadataService.pluginDriver(from: targetDriver) else { throw CompareSyncError.unsupportedOperation( String(localized: "The target driver cannot stream rows for a comparison.") ) } + let targetSide = targetRead.carrying(targetDriver.readOnlyRunContext(safeModeIsReadOnly: targetIsReadOnly)) let sides = RowSides( - source: sourceRead.provider(driver: sourcePlugin), - target: targetRead.provider(driver: targetPlugin), + source: sourceSide.provider(driver: sourcePlugin), + target: targetSide.provider(driver: targetPlugin), resolver: KeyLookupResolver( - source: (sourceRead, sourcePlugin), - target: (targetRead, targetPlugin) + source: (sourceSide, sourcePlugin), + target: (targetSide, targetPlugin) ), targetDriver: targetPlugin ) @@ -214,6 +245,19 @@ private struct SideRead: Sendable { let rowLimit: Int? let dialect: SQLDialectDescriptor? let side: ComparisonSide + // Read-Only's, when the side's connection runs at it and its driver holds the engine to it. + var context: PluginStatementContext? + + func carrying(_ context: PluginStatementContext?) -> SideRead { + var read = self + read.context = context + return read + } + + private func stream(_ query: String, driver: any PluginDatabaseDriver) -> AsyncThrowingStream { + guard let context else { return driver.streamRows(query: query) } + return driver.streamRows(query: query, context: context) + } func provider(driver: any PluginDatabaseDriver) -> StreamingRowProvider { let query = KeyOrderedQuery.build( @@ -228,7 +272,7 @@ private struct SideRead: Sendable { dialect: dialect ) return StreamingRowProvider( - stream: driver.streamRows(query: query), + stream: stream(query, driver: driver), columns: columns, rowLimit: rowLimit, context: RowReadContext(side: side, isFiltered: filter != nil) @@ -247,7 +291,7 @@ private struct SideRead: Sendable { databaseType: endpoint.databaseType ) return StreamingRowProvider( - stream: driver.streamRows(query: query), + stream: stream(query, driver: driver), columns: columns, context: RowReadContext(side: side, isFiltered: false) ) diff --git a/apple/TablePro/Core/MCP/MCPConnectionBridge+Data.swift b/apple/TablePro/Core/MCP/MCPConnectionBridge+Data.swift index b463c0ef6f..4034f8353c 100644 --- a/apple/TablePro/Core/MCP/MCPConnectionBridge+Data.swift +++ b/apple/TablePro/Core/MCP/MCPConnectionBridge+Data.swift @@ -339,9 +339,16 @@ extension MCPConnectionBridge { var spent = budget let planText = routed.map { spent.keep(text: $0.rawText) } // The tree repeats every node's text, so it goes out only when it fits whole; plan_text already carries it. - let encodedPlan = routed?.plan.map(Self.encode(plan:)) - let plan = encodedPlan.flatMap { spent.keepWhole(bytes: Self.encodedBytes(of: $0)) ? $0 : nil } - let planLeftOut = encodedPlan != nil && plan == nil + // No `nil` literal near a JsonValue here: it is ExpressibleByNilLiteral, so `nil` can become `.null`. + var plan: JsonValue? + var planLeftOut = false + if let encodedPlan = routed?.plan.map(Self.encode(plan:)) { + if spent.keepWhole(bytes: Self.encodedBytes(of: encodedPlan)) { + plan = encodedPlan + } else { + planLeftOut = true + } + } let kept = spent.keep(outcome.result) var payload: [String: JsonValue] = [ "statement": .string(request.statement), diff --git a/apple/TablePro/Core/MCP/Protocol/Tools/MCPErrorRedactor.swift b/apple/TablePro/Core/MCP/Protocol/Tools/MCPErrorRedactor.swift index b5f466803f..8e37e6c8e0 100644 --- a/apple/TablePro/Core/MCP/Protocol/Tools/MCPErrorRedactor.swift +++ b/apple/TablePro/Core/MCP/Protocol/Tools/MCPErrorRedactor.swift @@ -3,6 +3,9 @@ import Foundation enum MCPErrorRedactor { static let placeholder = "[redacted]" static let maximumLength = 600 + // The URL pattern still costs more than linear time on a long run of scheme:// with no `@`, and only the first + // maximumLength characters are sent, so the patterns read no further than this. + static let scannedLength = 8_192 private static let credentialKeys: [String] = [ "host", "hostaddr", "hostname", "server", "port", "user", "username", "uid", @@ -11,10 +14,12 @@ enum MCPErrorRedactor { "service_account", "connection_string", "dsn" ] + // The DSN and host:port patterns start only where a run of their characters starts, and never give characters + // back: unanchored, each position inside a long run rescanned the rest of it, and a 200 KB error took minutes. private static let patterns: [String] = [ - "[A-Za-z][A-Za-z0-9+.-]*://[^\\s]*@[^\\s]*", + "(?= 3 { output = output.replacingOccurrences(of: secret, with: placeholder, options: [.caseInsensitive]) } + output = scannedPrefix(output) for expression in compiled { let range = NSRange(output.startIndex..., in: output) output = expression.stringByReplacingMatches( @@ -71,4 +77,14 @@ enum MCPErrorRedactor { guard (output as NSString).length > maximumLength else { return output } return (output as NSString).substring(to: maximumLength) + "…" } + + // Cut at a blank, so no piece of a token is left without the rest a pattern needs to see it. + private static func scannedPrefix(_ text: String) -> String { + let source = text as NSString + guard source.length > scannedLength else { return text } + let head = source.substring(to: scannedLength) as NSString + let blank = head.rangeOfCharacter(from: .whitespacesAndNewlines, options: .backwards) + guard blank.location != NSNotFound else { return placeholder } + return head.substring(to: blank.location) + } } diff --git a/apple/TablePro/Core/ObjectCopy/ObjectCopyRowCopier.swift b/apple/TablePro/Core/ObjectCopy/ObjectCopyRowCopier.swift index 5b6028255f..d5ef8ca34e 100644 --- a/apple/TablePro/Core/ObjectCopy/ObjectCopyRowCopier.swift +++ b/apple/TablePro/Core/ObjectCopy/ObjectCopyRowCopier.swift @@ -55,14 +55,18 @@ internal struct ObjectCopyRowCopier: Sendable { internal let sourceDatabaseType: DatabaseType internal let targetDatabaseType: DatabaseType + // `readContext` is Read-Only's, for a source whose connection runs at it: a view as the source runs its functions. internal func copy( from sourceDriver: any PluginDatabaseDriver, to targetDriver: any PluginDatabaseDriver, + readContext: PluginStatementContext? = nil, onProgress: @Sendable (Int) -> Void ) async throws -> Outcome { let scopes = await openSessionScopes(source: sourceDriver, target: targetDriver) do { - let outcome = try await transfer(from: sourceDriver, to: targetDriver, onProgress: onProgress) + let outcome = try await transfer( + from: sourceDriver, to: targetDriver, readContext: readContext, onProgress: onProgress + ) await scopes?.close() return outcome } catch { @@ -88,6 +92,7 @@ internal struct ObjectCopyRowCopier: Sendable { private func transfer( from sourceDriver: any PluginDatabaseDriver, to targetDriver: any PluginDatabaseDriver, + readContext: PluginStatementContext?, onProgress: @Sendable (Int) -> Void ) async throws -> Outcome { if let statement = step.serverSideInsert { @@ -98,9 +103,9 @@ internal struct ObjectCopyRowCopier: Sendable { var filler = SQLWriteBatchFiller<[PluginCellValue]>(budget: budget) let cut = OSAllocatedUnfairLock(initialState: nil) // The copy's SELECT states no engine ceiling, so only a limit the driver declares applies, as for its own export query. - var stream = ExportRowCut.reporting( - sourceDriver.streamRows(query: step.sourceQuery), engineCeiling: nil - ) { limit in + let rows = readContext.map { sourceDriver.streamRows(query: step.sourceQuery, context: $0) } + ?? sourceDriver.streamRows(query: step.sourceQuery) + var stream = ExportRowCut.reporting(rows, engineCeiling: nil) { limit in cut.withLock { $0 = limit } }.makeAsyncIterator() diff --git a/apple/TablePro/Core/ObjectCopy/ObjectCopyRunner.swift b/apple/TablePro/Core/ObjectCopy/ObjectCopyRunner.swift index f8c04fd656..9816c66301 100644 --- a/apple/TablePro/Core/ObjectCopy/ObjectCopyRunner.swift +++ b/apple/TablePro/Core/ObjectCopy/ObjectCopyRunner.swift @@ -131,6 +131,15 @@ internal struct ObjectCopyRunner { ) defer { ProcessInfo.processInfo.endActivity(activity) } + // A row filter is the user's text, read from a source that may be at Read-Only: it meets the gate as a Run would. + try await ReadOnlyReadGate.authorize( + plan.dataSteps.filter(\.hasRowFilter).map(\.sourceQuery), + connectionId: request.source.connectionId, + databaseType: request.source.databaseType, + gate: gate, + operationDescription: String(localized: "Execute Query") + ) + return try await gate.authorizing(authorizationRequest(for: plan)) { try await self.execute(plan, request: request, progress: progress) } @@ -587,6 +596,7 @@ internal struct ObjectCopyRunner { let sourceType = request.source.databaseType let targetType = request.target.databaseType let sourceScope = request.source.scope + let sourceIsReadOnly = ExecutionGateProvider.runsAtReadOnly(request.source.connectionId) let errorHandling = request.errorHandling let relaxesRows = plan.keysInside.rows @@ -628,6 +638,7 @@ internal struct ObjectCopyRunner { manager: manager, sourceType: sourceType, targetType: targetType, + sourceIsReadOnly: sourceIsReadOnly, errorHandling: errorHandling, progress: progress ) @@ -680,6 +691,7 @@ internal struct ObjectCopyRunner { manager: any ScopedMetadataProviding, sourceType: DatabaseType, targetType: DatabaseType, + sourceIsReadOnly: Bool, errorHandling: ImportErrorHandling, progress: ObjectCopyProgress ) async -> DataResult { @@ -702,7 +714,8 @@ internal struct ObjectCopyRunner { guard let sourcePlugin = CompareMetadataService.pluginDriver(from: sourceDriver) else { throw ObjectCopyError.refused(noSourceDriver) } - return try await copier.copy(from: sourcePlugin, to: targetPlugin) { rows in + let readContext = sourceDriver.readOnlyRunContext(safeModeIsReadOnly: sourceIsReadOnly) + return try await copier.copy(from: sourcePlugin, to: targetPlugin, readContext: readContext) { rows in progress.setRowsForCurrentObject(rows, completedBefore: completedBefore) } } @@ -784,6 +797,7 @@ internal struct ObjectCopyRunner { ) async throws -> CopiedRows { let sourceScope = request.source.scope let targetScope = request.target.scope + let sourceIsReadOnly = ExecutionGateProvider.runsAtReadOnly(request.source.connectionId) let copier = ObjectCopyRowCopier( step: step, sourceDatabaseType: request.source.databaseType, targetDatabaseType: targetType ) @@ -804,7 +818,8 @@ internal struct ObjectCopyRunner { guard let sourcePlugin = CompareMetadataService.pluginDriver(from: sourceDriver) else { throw ObjectCopyError.refused(Self.noSourceDriver) } - let outcome = try await copier.copy(from: sourcePlugin, to: targetPlugin) { rows in + let readContext = sourceDriver.readOnlyRunContext(safeModeIsReadOnly: sourceIsReadOnly) + let outcome = try await copier.copy(from: sourcePlugin, to: targetPlugin, readContext: readContext) { rows in progress.setRowsForCurrentObject(rows, completedBefore: completedBefore) } return CopiedRows(outcome: outcome, joinedSessionTransaction: owner == .session) diff --git a/apple/TablePro/Core/Services/Execution/ReadOnlyFunctionCalls.swift b/apple/TablePro/Core/Services/Execution/ReadOnlyFunctionCalls.swift index 7563abf951..9763858675 100644 --- a/apple/TablePro/Core/Services/Execution/ReadOnlyFunctionCalls.swift +++ b/apple/TablePro/Core/Services/Execution/ReadOnlyFunctionCalls.swift @@ -152,11 +152,20 @@ internal enum ReadOnlyFunctionCalls { "MINUS": [.afterOperand], "LIMIT": [.afterOperand], "OFFSET": [.afterOperand], + // Word operators: `10 DIV (2)`, `a XOR (b)`. + "DIV": [.afterOperand], + "XOR": [.afterOperand], + "REGEXP": [.afterOperand], + "RLIKE": [.afterOperand], + "ILIKE": [.afterOperand], + "ESCAPE": [.afterOperand], ] - // After one of these a call can start, so a word that follows it is not standing after an operand. + // After one of these a call can start, so a word that follows it is not standing after an operand. Word operators + // are among them: `10 DIV pivot(1)` calls `pivot`. private static let introducers: Set = reservedWords.union(syntax.keys).union([ "SET", "INTO", "RETURN", "RETURNING", "PRIOR", "CALL", "DO", "EXECUTE", "FOR", "CONNECT", + "MOD", "COLLATE", "BINARY", "UNIQUE", "SOUNDS", "MEMBER", "OF", "SIMILAR", "TO", "ASC", "DESC", "NULLS", ]) private static let openParenthesis = UInt16(UnicodeScalar("(").value) @@ -164,7 +173,6 @@ internal enum ReadOnlyFunctionCalls { private static let period = UInt16(UnicodeScalar(".").value) private static let colon = UInt16(UnicodeScalar(":").value) private static let quoteOpeners: Set = [0x22, 0x60, 0x5B] - private static let quoteUnits: Set = [0x22, 0x27, 0x60, 0x5B, 0x5D] private static let comparisons: Set = [0x3D, 0x3C, 0x3E, 0x21] // Under every way the engine could lex the text, as the classifier reads it. A type with a length, such as @@ -199,6 +207,7 @@ internal enum ReadOnlyFunctionCalls { } let source = statement as NSString let code = SQLCodeProjection.code(of: statement, grammar: grammar) as NSString + var quoted: [Range]? return SQLFunctionCallScanner.calls(in: source, code: code).contains { call in guard case .named(let name, let start) = call.callee, !quoteOpeners.contains(source.character(at: start)) @@ -214,11 +223,28 @@ internal enum ReadOnlyFunctionCalls { } if builtins.contains(word) || typeNames.contains(word) || reservedWords.contains(word) { return false } guard let rules = syntax[word] else { return true } - let site = CallSite(source: source, code: code, nameStart: start, parenthesis: call.parenthesis) + let spans = quoted ?? quotedSpans(in: source, grammar: grammar) + quoted = spans + let site = CallSite(code: code, quotedSpans: spans, nameStart: start, parenthesis: call.parenthesis) return !rules.contains { site.matches($0) } } } + // Where a literal or a quoted name sits. The projection blanks comments too, and a comment is not a token. + private static func quotedSpans(in source: NSString, grammar: SQLLexicalGrammar) -> [Range] { + var spans: [Range] = [] + var index = 0 + while index < source.length { + guard let span = SQLNonCodeSpan.span(at: index, in: source, grammar: grammar) else { + index += 1 + continue + } + if span.kind == .quoted { spans.append(span.start..] let nameStart: Int let parenthesis: Int @@ -286,7 +312,7 @@ internal enum ReadOnlyFunctionCalls { private var before: Neighbor { var cursor = nameStart - 1 while cursor >= 0, SQLFunctionCallScanner.isBlank(code.character(at: cursor)) { - if ReadOnlyFunctionCalls.quoteUnits.contains(source.character(at: cursor)) { return .quoted } + if quotedSpans.contains(where: { $0.contains(cursor) }) { return .quoted } cursor -= 1 } guard cursor >= 0 else { return .start } @@ -328,15 +354,16 @@ internal enum ReadOnlyFunctionCalls { } } -// Export and Transfer To read rows outside the editor, so text the user wrote into them, a row filter or the query an -// export runs again, meets the gate's Read-Only decision before any row is read. -internal enum ReadOnlyExportGate { +// Export, Transfer To, Data Compare and Copy Objects read rows outside the editor, so text the user wrote into them, a +// row filter or the query an export runs again, meets the gate's Read-Only decision before any row is read. +internal enum ReadOnlyReadGate { @MainActor static func authorize( _ statements: [String], connectionId: UUID, databaseType: DatabaseType, gate: any ExecutionGate, + operationDescription: String = String(localized: "Export"), canPrompt: Bool = true, manager: DatabaseManager? = nil ) async throws { @@ -349,7 +376,7 @@ internal enum ReadOnlyExportGate { kind: .readQuery, caller: .userInterface, capabilities: canPrompt ? .interactiveUser : CallerCapabilities.interactiveUser.union(.cannotPrompt), - operationDescription: String(localized: "Export"), + operationDescription: operationDescription, statements: statements ) ) @@ -363,12 +390,25 @@ internal enum ReadOnlyExportGate { quote: (String) -> String ) -> [String] { objects.compactMap { object in - let filter = object.rowScope.sanitizedFilter - guard !filter.isEmpty else { return nil } - let table = SchemaQualifiedName.render( - name: object.name, schema: object.databaseName, databaseType: databaseType, quote: quote + filterStatement( + table: object.name, + schema: object.databaseName, + filter: object.rowScope.sanitizedFilter, + databaseType: databaseType, + quote: quote ) - return "SELECT * FROM \(table) WHERE \(filter)" } } + + static func filterStatement( + table: String, + schema: String?, + filter: String?, + databaseType: DatabaseType, + quote: (String) -> String + ) -> String? { + guard let filter, !filter.isEmpty else { return nil } + let name = SchemaQualifiedName.render(name: table, schema: schema, databaseType: databaseType, quote: quote) + return "SELECT * FROM \(name) WHERE \(filter)" + } } diff --git a/apple/TablePro/Core/Services/Export/ExportService.swift b/apple/TablePro/Core/Services/Export/ExportService.swift index 4f65b45341..3fdee14f15 100644 --- a/apple/TablePro/Core/Services/Export/ExportService.swift +++ b/apple/TablePro/Core/Services/Export/ExportService.swift @@ -209,8 +209,8 @@ final class ExportService: ObservableObject { try throwIfStopped() let rowObjects = objects.filter { plugin.readsRows(optionValues: $0.optionValues, kind: $0.kind) } - try await ReadOnlyExportGate.authorize( - ReadOnlyExportGate.rowScopeStatements(rowObjects, databaseType: databaseType) { driver.quoteIdentifier($0) }, + try await ReadOnlyReadGate.authorize( + ReadOnlyReadGate.rowScopeStatements(rowObjects, databaseType: databaseType) { driver.quoteIdentifier($0) }, connectionId: driver.connection.id, databaseType: databaseType, gate: executionGate @@ -444,7 +444,7 @@ final class ExportService: ObservableObject { currentProgress = nil } try throwIfStopped() - try await ReadOnlyExportGate.authorize( + try await ReadOnlyReadGate.authorize( [query], connectionId: driver.connection.id, databaseType: databaseType, gate: executionGate ) try throwIfStopped() diff --git a/apple/TablePro/Core/Services/Export/TableTransferService.swift b/apple/TablePro/Core/Services/Export/TableTransferService.swift index 443cbd9b2c..2bfc66fd80 100644 --- a/apple/TablePro/Core/Services/Export/TableTransferService.swift +++ b/apple/TablePro/Core/Services/Export/TableTransferService.swift @@ -150,8 +150,8 @@ final class TableTransferService: ObservableObject { gate: ExecutionGate = ExecutionGateProvider.shared ) async throws { let sourceSession = manager.driver(for: source.connectionId) - try await ReadOnlyExportGate.authorize( - ReadOnlyExportGate.rowScopeStatements(request.objects, databaseType: request.sourceType) { name in + try await ReadOnlyReadGate.authorize( + ReadOnlyReadGate.rowScopeStatements(request.objects, databaseType: request.sourceType) { name in sourceSession?.quoteIdentifier(name) ?? name }, connectionId: source.connectionId, diff --git a/apple/TablePro/Resources/Localizable.xcstrings b/apple/TablePro/Resources/Localizable.xcstrings index 59ea7fab79..11df588e8d 100644 --- a/apple/TablePro/Resources/Localizable.xcstrings +++ b/apple/TablePro/Resources/Localizable.xcstrings @@ -24341,6 +24341,12 @@ } } } + }, + "Always Allow on an assistant tool card adds the tool here. Forget makes its next call ask again." : { + + }, + "Always allowed" : { + }, "Always count" : { "localizations" : { @@ -32421,6 +32427,9 @@ } } } + }, + "Bring an open tab to the front, using a tab id from list_recent_tabs. TablePro asks the user about the tab's connection first, as it does for any call that names a connection." : { + }, "Broadcast" : { "extractionState" : "manual", @@ -34708,6 +34717,9 @@ } } } + }, + "Cannot run this query: it calls a function that can change data, and Safe Mode is held at Read-Only for this connection." : { + }, "Cannot save changes to '%@'." : { "localizations" : { @@ -85582,6 +85594,9 @@ } } } + }, + "Forget %@" : { + }, "Forget All" : { "localizations" : { @@ -105260,6 +105275,9 @@ } } } + }, + "List the tabs open in TablePro. With connection_id, TablePro asks the user about that connection first. Without it, the list covers only connections this client may already reach without asking. Each entry carries the tab id that focus_query_tab takes and the window id that open_table_tab returns." : { + }, "List the triggers attached to one table, with their timing, event, and body." : { "localizations" : { @@ -137181,6 +137199,9 @@ } } } + }, + "Present and true when the answer's size limit cut plan_text or left the parsed plan or rows out" : { + }, "Preserve all values as strings" : { "extractionState" : "stale", @@ -151874,6 +151895,9 @@ } } } + }, + "Restrict to one connection. Omit for every connection already approved." : { + }, "Restrict to one connection. Omit for every readable connection." : { "localizations" : { @@ -156704,6 +156728,14 @@ }, "Safe Mode is read-only for this connection." : { + }, + "Safe Mode is set to Read-Only for this connection, and it has a transaction open. Roll it back, then try again." : { + "extractionState" : "manual", + "generatesSymbol" : false + }, + "Safe Mode is set to Read-Only for this connection, and the database refused the statement." : { + "extractionState" : "manual", + "generatesSymbol" : false }, "Safe mode level" : { "localizations" : { @@ -160984,6 +161016,9 @@ } } } + }, + "Search the query history TablePro keeps on this Mac. With connection_id, TablePro asks the user about that connection first. Without it, results cover only connections this client may already reach without asking." : { + }, "Search values" : { "localizations" : { @@ -167515,6 +167550,9 @@ } } } + }, + "Show the connection this conversation works on, with its status." : { + }, "Show the messages that arrived while paused" : { @@ -170953,6 +170991,9 @@ } } } + }, + "Some values were longer than 64 KB and were cut there; each one cut ends in a mark with its full length. Read such a value in parts with a narrower query." : { + }, "Someone in %@ changed this item after TablePro last downloaded the team library. Choose the version the team library keeps." : { @@ -190953,6 +190994,9 @@ } } } + }, + "The parsed plan did not fit in TablePro's %@ limit for one answer, so it is left out. Read plan_text instead." : { + }, "The partial backup file is removed." : { @@ -192320,6 +192364,12 @@ }, "The rows go in as one transaction. Cancel rolls it back." : { + }, + "The rows reached TablePro's %@ limit for one answer, so a result marked truncated holds only the rows that fit. Ask for fewer columns or rows." : { + + }, + "The rows reached TablePro's %@ limit for one answer, so an export marked truncated holds only the rows that fit, and the exports after it may hold none. Export fewer tables, columns or rows per call." : { + }, "The rules file is not a regular file." : { @@ -194707,6 +194757,9 @@ } } } + }, + "The statement was still running in the database when it was stopped, and it runs to the end. It may have been applied; check before running it again." : { + }, "The statement was stopped, and the server rolled back the transaction it ran in. Every change since that transaction began is undone." : { "extractionState" : "manual", @@ -201692,6 +201745,9 @@ } } } + }, + "This query calls a function that can change data" : { + }, "This query language has no formatter." : { "localizations" : { @@ -216767,6 +216823,16 @@ } } }, + "Whether the row limit or the answer's size limit left rows out" : { + + }, + "Whether the row limit or the answer's size limit left rows out of any export" : { + + }, + "Whether the row limit or the answer's size limit left rows out of the result" : { + "extractionState" : "manual", + "generatesSymbol" : false + }, "Whether the session is open" : { "localizations" : { "fr" : { @@ -216806,6 +216872,9 @@ } } } + }, + "Whether the size limit left out matching entries" : { + }, "Whether the statement names an object, the database, or either" : { "localizations" : { @@ -217208,6 +217277,13 @@ }, "Why fix_sql is left out: running it with the other fixes would do harm" : { + }, + "Why rows were left out or values cut, when they were" : { + "extractionState" : "manual", + "generatesSymbol" : false + }, + "Why rows were left out, when the size limit left them out" : { + }, "Why the check was skipped or failed" : { diff --git a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+ReadOnlySafeMode.swift b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+ReadOnlySafeMode.swift index 65911a3449..65a0a04884 100644 --- a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+ReadOnlySafeMode.swift +++ b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+ReadOnlySafeMode.swift @@ -27,18 +27,27 @@ extension MainContentCoordinator { // A raw filter is text the user typed, inside a query no gate reads below the Full levels. At Read-Only one that // calls a function goes to the gate before the browse or a count sends it, and the gate asks where the driver cannot - // hold the engine to read-only. Once per filter text and tab: its page turns, sorts and counts run what was approved. + // hold the engine to read-only. Once per filter text and tab: its page turns, sorts and counts run what was approved, + // until a profile or a floor holds the level, after which every run goes to the gate, which refuses. func rawFilterNeedsReadOnlyGate(_ filters: [TableFilter], tabId: UUID) -> Bool { let expressions = Self.rawFilterExpressions(filters) - guard !expressions.isEmpty, runsAtReadOnly, approvedReadOnlyRawFilters[tabId] != Self.rawFilterKey(filters) - else { return false } + guard !expressions.isEmpty, runsAtReadOnly else { return false } + let approved = approvedReadOnlyRawFilters[tabId] == Self.rawFilterKey(filters) && !readOnlyLevelIsHeld + guard !approved else { return false } return expressions.contains { ReadOnlyFunctionCalls.callsUnlistedFunction($0, databaseType: connection.type) } } func approveReadOnlyRawFilter(_ filters: [TableFilter], tabId: UUID) { + guard !readOnlyLevelIsHeld else { return } approvedReadOnlyRawFilters[tabId] = Self.rawFilterKey(filters) } + private var readOnlyLevelIsHeld: Bool { + ExecutionGateProvider.readOnlyFunctionPolicy( + connectionId: connectionId, databaseType: connection.type, in: services.databaseManager + ).levelIsHeld + } + func rawFilterRequest(sql: String, canPrompt: Bool) -> OperationRequest { OperationRequest( connectionId: connectionId, diff --git a/apple/TableProTests/Core/MCP/MCPHistoryAnswerBudgetTests.swift b/apple/TableProTests/Core/MCP/MCPHistoryAnswerBudgetTests.swift index 2f688dfd9e..a2350bbb93 100644 --- a/apple/TableProTests/Core/MCP/MCPHistoryAnswerBudgetTests.swift +++ b/apple/TableProTests/Core/MCP/MCPHistoryAnswerBudgetTests.swift @@ -37,7 +37,9 @@ struct MCPHistoryAnswerBudgetTests { #expect(query.utf8.count < statement.utf8.count) #expect(query.hasPrefix(String(statement.prefix(1_000)))) #expect(error.utf8.count < failure.utf8.count) - #expect(error.hasPrefix("syntax error near ")) + // The redactor drops a token too long to read whole, since part of one can hold half a secret. + #expect(error.hasPrefix("syntax error near")) + #expect(!error.contains("yyy")) } @Test("Short history text passes through whole") diff --git a/apple/TableProTests/Core/MCP/Protocol/Tools/MCPToolErrorSurfaceTests.swift b/apple/TableProTests/Core/MCP/Protocol/Tools/MCPToolErrorSurfaceTests.swift index ece2d3f664..1366d0d896 100644 --- a/apple/TableProTests/Core/MCP/Protocol/Tools/MCPToolErrorSurfaceTests.swift +++ b/apple/TableProTests/Core/MCP/Protocol/Tools/MCPToolErrorSurfaceTests.swift @@ -48,6 +48,18 @@ struct MCPToolErrorSurfaceTests { #expect(MCPToolTestHarness.errorText(result) == "query_failed: syntax error at or near \"SELEC\"") } + @Test("A long error of URLs with no @ is redacted at once, and a token cut at the scan's end is dropped", .timeLimit(.minutes(1))) + func longErrorIsRedactedQuickly() { + let urls = String(repeating: "a://", count: 50_000) + #expect(MCPErrorRedactor.redact(urls) == MCPErrorRedactor.placeholder) + + let head = String(repeating: "x ", count: MCPErrorRedactor.scannedLength / 2 - 4) + let secret = "postgres://ada:hunter2@db.internal.example:5432/app" + let redacted = MCPErrorRedactor.redact(head + secret, secrets: []) + #expect(!redacted.contains("hunter2")) + #expect(!redacted.contains("db.internal")) + } + @Test("The assistant's transcript reads a tool failure's own message, not Foundation's generic text") func localizedDescriptionCarriesTheMessage() { let error: Error = MCPToolExecutionError.queryFailed("relation \"ordrs\" does not exist") diff --git a/apple/TableProTests/Core/Services/Execution/ExecutionGateTests.swift b/apple/TableProTests/Core/Services/Execution/ExecutionGateTests.swift index 666d183d39..986ea8a82a 100644 --- a/apple/TableProTests/Core/Services/Execution/ExecutionGateTests.swift +++ b/apple/TableProTests/Core/Services/Execution/ExecutionGateTests.swift @@ -988,6 +988,10 @@ struct ExecutionGateTests { "SELECT id FROM orders WHERE total = offset(1)", "SELECT join(id) FROM orders", "SELECT id FROM orders WHERE status IN (purge_orders())", + "SELECT 10 DIV pivot(1)", + "SELECT 10 MOD pivot(1)", + "SELECT paid XOR offset(1) FROM orders", + "SELECT id FROM orders WHERE /* don't */ offset(1) = 1", ] ) func keywordNamedFunctionIsAsked(sql: String) async { @@ -1024,6 +1028,24 @@ struct ExecutionGateTests { #expect(confirm.callCount == 0) } + @Test( + "A word operator before a parenthesis runs without asking", + arguments: [ + "SELECT 10 DIV (2), paid XOR (refunded) FROM orders", + "SELECT id FROM orders WHERE name REGEXP ('^a') OR name RLIKE ('b$')", + "SELECT id FROM orders WHERE note LIKE 'a!%' ESCAPE ('!')", + ] + ) + func wordOperatorsRun(sql: String) async { + let confirm = StubConfirming(answer: false) + let gate = makeReadOnlyGate(policy: Self.writableFunctions, confirm: confirm) + + let decision = await gate.authorize(makeRequest(sql: sql, kind: .readQuery, databaseType: .mysql)) + + #expect(decision.isAuthorized) + #expect(confirm.callCount == 0) + } + @Test( "The fallback engines' everyday built-ins run without asking", arguments: [ diff --git a/apple/TableProTests/Core/WritePolicy/WritePolicyMatrix.swift b/apple/TableProTests/Core/WritePolicy/WritePolicyMatrix.swift index c455669a61..1b366f3a6d 100644 --- a/apple/TableProTests/Core/WritePolicy/WritePolicyMatrix.swift +++ b/apple/TableProTests/Core/WritePolicy/WritePolicyMatrix.swift @@ -102,7 +102,8 @@ enum MCPWritePath: String, CaseIterable, Sendable, CustomTestStringConvertible { case session // The connection stays unsaved, so a call the gates pass stops at the tool's lookup before a window opens. case window - // Names no connection: the tab list hides the ones the client may not reach. A hosted test has no window to raise. + // Names a tab, and asks about the tab's connection; the tab list hides the ones the client may not reach. + // A hosted test has no window to raise. case tab } @@ -209,7 +210,6 @@ enum MCPWritePath: String, CaseIterable, Sendable, CustomTestStringConvertible { static func expected(_ path: MCPWritePath, _ cell: MCPWriteCell) -> WriteOutcome { guard path.scopes.isSubset(of: cell.token.scopes) else { return .refused } guard cell.aiPolicy != .never, cell.externalAccess != .blocked else { return .refused } - guard path.reach != .tab else { return .runs(after: []) } var prompts: [WritePrompt] = cell.aiPolicy == .askEachTime ? [.connectionAccess] : [] guard path.reach == .statement else { return .runs(after: prompts) } guard cell.externalAccess == .readWrite, cell.safeMode != .readOnly else { return .refused } diff --git a/apple/TableProTests/Views/Main/ReadOnlySafeModeEntryPointTests.swift b/apple/TableProTests/Views/Main/ReadOnlySafeModeEntryPointTests.swift index b934944fbf..ff6c749947 100644 --- a/apple/TableProTests/Views/Main/ReadOnlySafeModeEntryPointTests.swift +++ b/apple/TableProTests/Views/Main/ReadOnlySafeModeEntryPointTests.swift @@ -109,6 +109,9 @@ private final class ReadOnlyRecordingDriver: PluginDriverStub, @unchecked Sendab } func sessionTransactionState() async -> PluginSessionTransactionState { .idle } + + // DuckDB switches databases in place, so its session route pins the scope's database before every read. + func switchDatabase(to database: String) async throws {} } private final class ReadingExportFormat: ExportFormatPlugin, @unchecked Sendable { @@ -139,6 +142,28 @@ private final class ReadingExportFormat: ExportFormatPlugin, @unchecked Sendable } } +@MainActor +private final class FixedDriverProvider: ScopedMetadataProviding { + private let drivers: [UUID: DatabaseDriver] + + init(_ drivers: [UUID: DatabaseDriver]) { + self.drivers = drivers + } + + func withMetadataDriver( + scope: DatabaseScope, + workload: MetadataConnectionPool.Workload, + _ body: @Sendable @escaping (DatabaseDriver) async throws -> T + ) async throws -> T { + guard let driver = drivers[scope.connectionId] else { + throw ObjectCopyError.refused("No driver for \(scope.connectionId)") + } + return try await body(driver) + } + + func browseScope(for connectionId: UUID) -> DatabaseScope? { nil } +} + @MainActor struct ReadOnlySafeModeEntryPointTests { private static let purge = "purge_orders()" @@ -363,8 +388,16 @@ struct ReadOnlySafeModeEntryPointTests { } private func installReadOnlySession(type: DatabaseType = .postgresql) -> (DatabaseConnection, ReadOnlyRecordingDriver, PluginDriverAdapter) { + installSession(.readOnly, type: type) + } + + private func installSession( + _ level: SafeModeLevel, + type: DatabaseType = .postgresql, + id: UUID = UUID() + ) -> (DatabaseConnection, ReadOnlyRecordingDriver, PluginDriverAdapter) { let driver = ReadOnlyRecordingDriver() - let connection = makeConnection(.readOnly, type: type) + let connection = DatabaseConnection(id: id, name: "Shop", database: "app", type: type, safeModeLevel: level) let adapter = PluginDriverAdapter(connection: connection, pluginDriver: driver) var session = ConnectionSession(connection: connection, driver: adapter) session.status = .connected @@ -541,4 +574,191 @@ struct ReadOnlySafeModeEntryPointTests { #expect(call?.entry == "executeBoundedQuery") #expect(call?.context?.enforcesReadOnly == true) } + + // MARK: - Held level + + // A profile or a floor that comes after an approval leaves nothing to approve: the gate refuses every run. + @Test("A held Read-Only never runs a raw filter approved before it was held") + func heldLevelIgnoresAnEarlierApproval() async throws { + let (fixture, driver) = makeFixture(.readOnly) + let gate = RequestRecordingGate(allows: false) + fixture.coordinator.executionGate = gate + defer { fixture.coordinator.cancelAllQueryTasks() } + let tab = fixture.addTab(rawFilterTableTab()) + let filters = MainContentCoordinator.browseFilters(tab.filterState) + fixture.coordinator.approveReadOnlyRawFilter(filters, tabId: tab.id) + #expect(!fixture.coordinator.rawFilterNeedsReadOnlyGate(filters, tabId: tab.id)) + + var held = fixture.connection + held.type = .cloudflareR2SQL + var session = ConnectionSession(connection: held, driver: PluginDriverAdapter(connection: held, pluginDriver: driver)) + session.status = .connected + DatabaseManager.shared.injectSession(session, for: held.id) + + #expect(fixture.coordinator.rawFilterNeedsReadOnlyGate(filters, tabId: tab.id)) + let confirmation = fixture.coordinator.executeTableTabQueryDirectly(viewport: .firstRow) + #expect(confirmation != nil) + await confirmation?.value + #expect(await gate.requests.count == 1) + #expect(driver.call(containing: Self.purge) == nil) + } + + // MARK: - Data Compare + + private func compareEndpoint(_ connection: DatabaseConnection) -> DatabaseEndpoint { + DatabaseEndpoint( + scope: DatabaseScope(connectionId: connection.id, database: connection.database, schema: nil), + connectionName: connection.name, + databaseType: connection.type, + safeModeLevel: connection.safeModeLevel, + color: .blue + ) + } + + private func comparePlan(sourceFilter: String = "") -> DataComparePlan { + DataComparePlan( + table: "orders", + schema: nil, + columns: [CompareColumn(name: "id", sourceType: "INTEGER")], + scope: DataTableScope(keyColumns: ["id"], sourceFilter: sourceFilter), + isEnabled: true + ) + } + + // DuckDB reads both sides on the session drivers this test installs; a pooled engine would open connections of its own. + @Test("Data Compare reads the Read-Only side in the read-only mode, and the other side as before") + func compareReadsTheReadOnlySideReadOnly() async throws { + let (source, sourceDriver, _) = installSession(.readOnly, type: .duckdb) + let (target, targetDriver, _) = installSession(.silent, type: .duckdb) + defer { + DatabaseManager.shared.removeSession(for: source.id) + DatabaseManager.shared.removeSession(for: target.id) + } + let service = CompareRowService(manager: .shared, gate: AlwaysAllowGate()) + + _ = try? await service.compare( + plan: comparePlan(), + source: compareEndpoint(source), sourceConnection: source, + target: compareEndpoint(target), targetConnection: target, + options: DataCompareOptions() + ) + + let read = try #require(sourceDriver.call(containing: "orders")) + #expect(read.context?.enforcesReadOnly == true) + #expect(targetDriver.calls.allSatisfy { $0.context?.enforcesReadOnly != true }) + } + + @Test("A Data Compare filter on a Read-Only connection asks the gate before either side is read") + func compareFilterAsksTheGateFirst() async throws { + let (source, sourceDriver, _) = installSession(.readOnly, type: .duckdb) + let (target, targetDriver, _) = installSession(.silent, type: .duckdb) + defer { + DatabaseManager.shared.removeSession(for: source.id) + DatabaseManager.shared.removeSession(for: target.id) + } + let gate = RequestRecordingGate(allows: false) + let service = CompareRowService(manager: .shared, gate: gate) + + await #expect(throws: ExecutionGateError.self) { + _ = try await service.compare( + plan: self.comparePlan(sourceFilter: "purge_orders() IS NULL"), + source: self.compareEndpoint(source), sourceConnection: source, + target: self.compareEndpoint(target), targetConnection: target, + options: DataCompareOptions() + ) + } + + let requests = await gate.requests + #expect(requests.count == 1) + #expect(requests.first?.connectionId == source.id) + #expect(requests.first?.sql?.contains(Self.purge) == true) + #expect(sourceDriver.call(containing: "orders") == nil) + #expect(targetDriver.call(containing: "orders") == nil) + } + + // MARK: - Copy Objects + + private func copyRequest(source: DatabaseConnection, target: DatabaseConnection) -> ObjectCopyRequest { + ObjectCopyRequest( + source: compareEndpoint(source), + destination: .existing(compareEndpoint(target)), + objects: [ObjectCopySelection(kind: .table, name: "order_cleanup", schema: nil)], + content: .data, + existingPolicy: .replace, + errorHandling: .stopAndRollback, + wrapEachTableInTransaction: true + ) + } + + private func copyPlan(source: DatabaseConnection, target: DatabaseConnection, query: String, filtered: Bool) -> ObjectCopyPlan { + let step = ObjectCopyTableStep( + selection: ObjectCopySelection(kind: .table, name: "order_cleanup", schema: nil), + dropStatements: [], + sequenceStatements: [], + createStatements: [], + truncateStatements: [], + columns: ["id"], + primaryKeyColumns: ["id"], + sourceQuery: query, + targetTable: "order_cleanup", + targetSchema: nil, + estimatedRows: nil, + copiesData: true, + copiesIdentityColumn: false, + hasRowFilter: filtered, + note: nil + ) + return ObjectCopyPlan( + request: copyRequest(source: source, target: target), + createsDatabase: false, + tableSteps: [step], + definitionSteps: [], + keysInside: ObjectCopyKeysInside(created: true, rows: true) + ) + } + + @Test("Copy Objects reads a Read-Only source, a view among them, in the read-only mode") + func objectCopyReadsTheSourceReadOnly() async throws { + let (source, sourceDriver, sourceAdapter) = installSession(.readOnly) + let target = DatabaseConnection(name: "Archive", database: "archive", type: .mysql) + let targetAdapter = PluginDriverAdapter(connection: target, pluginDriver: ReadOnlyRecordingDriver()) + defer { DatabaseManager.shared.removeSession(for: source.id) } + let runner = ObjectCopyRunner( + manager: FixedDriverProvider([source.id: sourceAdapter, target.id: targetAdapter]), + gate: AlwaysAllowGate() + ) + + _ = try await runner.run( + copyPlan(source: source, target: target, query: "SELECT id FROM order_cleanup", filtered: false), + progress: ObjectCopyProgress(progress: Progress()) + ) + + let read = try #require(sourceDriver.call(containing: "order_cleanup")) + #expect(read.context?.enforcesReadOnly == true) + } + + @Test("A Copy Objects row filter on a Read-Only source asks the gate before anything runs") + func objectCopyFilterAsksTheGateFirst() async throws { + let (source, sourceDriver, sourceAdapter) = installSession(.readOnly) + let target = DatabaseConnection(name: "Archive", database: "archive", type: .mysql) + let targetAdapter = PluginDriverAdapter(connection: target, pluginDriver: ReadOnlyRecordingDriver()) + defer { DatabaseManager.shared.removeSession(for: source.id) } + let gate = RequestRecordingGate(allows: false) + let runner = ObjectCopyRunner( + manager: FixedDriverProvider([source.id: sourceAdapter, target.id: targetAdapter]), + gate: gate + ) + let query = "SELECT id FROM order_cleanup WHERE purge_orders() IS NULL" + + await #expect(throws: ExecutionGateError.self) { + _ = try await runner.run( + self.copyPlan(source: source, target: target, query: query, filtered: true), + progress: ObjectCopyProgress(progress: Progress()) + ) + } + + #expect(await gate.requests.map { $0.sql } == [query]) + #expect(await gate.requests.first?.connectionId == source.id) + #expect(sourceDriver.call(containing: "order_cleanup") == nil) + } } diff --git a/core/crates/core/src/sql_syntax/outside_reach.rs b/core/crates/core/src/sql_syntax/outside_reach.rs index af7f37a3c9..e26983bbee 100644 --- a/core/crates/core/src/sql_syntax/outside_reach.rs +++ b/core/crates/core/src/sql_syntax/outside_reach.rs @@ -245,7 +245,7 @@ fn reaches(code: &[Code<'_>], grammar: SqlGrammar) -> bool { code.split(|token| token.kind == Kind::End) .filter(|part| !part.is_empty()) .any(|part| { - part.first() + part.get(explained_start(part)) .is_some_and(|first| first.is_word(FILESYSTEM_OR_CODE_KEYWORDS)) || names_filesystem(part) || (0..part.len()).any(|index| calls_outside(part, index, grammar)) @@ -253,6 +253,28 @@ fn reaches(code: &[Code<'_>], grammar: SqlGrammar) -> bool { }) } +// `EXPLAIN ANALYZE COPY ... TO '/path'` runs the COPY, so the keyword is read past the EXPLAIN. +fn explained_start(part: &[Code<'_>]) -> usize { + let explains = part.first().is_some_and(|first| first.is_word(&["explain"])); + let mut index = 0; + while let Some(token) = part.get(index) { + if token.is_word(&["explain"]) + || (explains && token.is_word(&["analyze", "analyse", "verbose", "query", "plan"])) + { + index += 1; + } else if explains && token.kind == Kind::Open { + let depth = token.depth; + let close = part[index + 1..] + .iter() + .position(|close| close.kind == Kind::Close && close.depth == depth); + index += 2 + close.unwrap_or(part.len()); + } else { + break; + } + } + index +} + fn names_filesystem(part: &[Code<'_>]) -> bool { part.iter().enumerate().any(|(index, token)| { (matches!(token.kind, Kind::Word | Kind::Quoted) && is(token.text, FILESYSTEM_NAMES)) @@ -582,6 +604,14 @@ mod tests { ("EXPORT DATABASE '/tmp/dump'", SqlGrammar::DuckDb), ("DO $$ BEGIN PERFORM 1; END $$", SqlGrammar::PostgreSql), ("BULK INSERT t FROM 'C:\\x.csv'", SqlGrammar::MsSql), + ( + "EXPLAIN ANALYZE COPY (SELECT 1) TO '/home/ada/.bashrc' (HEADER false)", + SqlGrammar::DuckDb, + ), + ( + "EXPLAIN (ANALYZE, FORMAT JSON) COPY t TO '/tmp/x'", + SqlGrammar::PostgreSql, + ), ] { assert!(reaches_outside_database(sql, grammar), "{sql} on {grammar:?}"); } @@ -596,6 +626,8 @@ mod tests { ("SELECT * FROM read_text('x')", SqlGrammar::PostgreSql), ("SELECT query FROM history", SqlGrammar::DuckDb), ("SELECT * FROM orders", SqlGrammar::MySql), + ("EXPLAIN ANALYZE SELECT * FROM orders", SqlGrammar::PostgreSql), + ("EXPLAIN (ANALYZE) SELECT copy FROM orders", SqlGrammar::PostgreSql), ] { assert!(!reaches_outside_database(sql, grammar), "{sql} on {grammar:?}"); } From 3fde73b288633264ea0e8cf2eee823b3ad2eda1c Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Thu, 12 Nov 2026 07:13:48 +0700 Subject: [PATCH 3/7] chore(changelog): add fragments for the ai-security fixes --- apple/changelog.d/4219.added.md | 1 + apple/changelog.d/4219.fixed.1.md | 1 + apple/changelog.d/4219.fixed.2.md | 1 + apple/changelog.d/4219.fixed.3.md | 1 + apple/changelog.d/4219.fixed.4.md | 1 + apple/changelog.d/4219.fixed.md | 1 + apple/changelog.d/4219.security.1.md | 1 + apple/changelog.d/4219.security.2.md | 1 + apple/changelog.d/4219.security.3.md | 1 + apple/changelog.d/4219.security.4.md | 1 + apple/changelog.d/4219.security.md | 1 + linux/changelog.d/4219.security.md | 1 + 12 files changed, 12 insertions(+) create mode 100644 apple/changelog.d/4219.added.md create mode 100644 apple/changelog.d/4219.fixed.1.md create mode 100644 apple/changelog.d/4219.fixed.2.md create mode 100644 apple/changelog.d/4219.fixed.3.md create mode 100644 apple/changelog.d/4219.fixed.4.md create mode 100644 apple/changelog.d/4219.fixed.md create mode 100644 apple/changelog.d/4219.security.1.md create mode 100644 apple/changelog.d/4219.security.2.md create mode 100644 apple/changelog.d/4219.security.3.md create mode 100644 apple/changelog.d/4219.security.4.md create mode 100644 apple/changelog.d/4219.security.md create mode 100644 linux/changelog.d/4219.security.md diff --git a/apple/changelog.d/4219.added.md b/apple/changelog.d/4219.added.md new file mode 100644 index 0000000000..ecbd4e8753 --- /dev/null +++ b/apple/changelog.d/4219.added.md @@ -0,0 +1 @@ +Always Allow grants for assistant tools can be removed with Forget under AI Policy in the connection form. diff --git a/apple/changelog.d/4219.fixed.1.md b/apple/changelog.d/4219.fixed.1.md new file mode 100644 index 0000000000..df1d9ca804 --- /dev/null +++ b/apple/changelog.d/4219.fixed.1.md @@ -0,0 +1 @@ +A large MCP or assistant query result on PostgreSQL, MySQL, SQLite, DuckDB or ClickHouse could use up memory. diff --git a/apple/changelog.d/4219.fixed.2.md b/apple/changelog.d/4219.fixed.2.md new file mode 100644 index 0000000000..76a8511fa0 --- /dev/null +++ b/apple/changelog.d/4219.fixed.2.md @@ -0,0 +1 @@ +Stopping the assistant during a write reported a failure while the write went on and committed. diff --git a/apple/changelog.d/4219.fixed.3.md b/apple/changelog.d/4219.fixed.3.md new file mode 100644 index 0000000000..0a54c93703 --- /dev/null +++ b/apple/changelog.d/4219.fixed.3.md @@ -0,0 +1 @@ +A conversation left while a tool card waited reopened with Run buttons that did nothing. diff --git a/apple/changelog.d/4219.fixed.4.md b/apple/changelog.d/4219.fixed.4.md new file mode 100644 index 0000000000..405b8c509d --- /dev/null +++ b/apple/changelog.d/4219.fixed.4.md @@ -0,0 +1 @@ +The assistant saw a generic error instead of the database's message when a query failed. diff --git a/apple/changelog.d/4219.fixed.md b/apple/changelog.d/4219.fixed.md new file mode 100644 index 0000000000..bd6c3f678c --- /dev/null +++ b/apple/changelog.d/4219.fixed.md @@ -0,0 +1 @@ +Claude Agent replies stopped streaming and hung past 64 KB, and never streamed with GitHub Copilot on. diff --git a/apple/changelog.d/4219.security.1.md b/apple/changelog.d/4219.security.1.md new file mode 100644 index 0000000000..d21513e43d --- /dev/null +++ b/apple/changelog.d/4219.security.1.md @@ -0,0 +1 @@ +MCP clients could read the query history and tabs of connections the user had denied or never approved. diff --git a/apple/changelog.d/4219.security.2.md b/apple/changelog.d/4219.security.2.md new file mode 100644 index 0000000000..e9f626948d --- /dev/null +++ b/apple/changelog.d/4219.security.2.md @@ -0,0 +1 @@ +Claude Agent could run queries and read rows in Ask mode. diff --git a/apple/changelog.d/4219.security.3.md b/apple/changelog.d/4219.security.3.md new file mode 100644 index 0000000000..f0bfd79504 --- /dev/null +++ b/apple/changelog.d/4219.security.3.md @@ -0,0 +1 @@ +The assistant's list_connections sent every saved connection's host and database to the AI provider. diff --git a/apple/changelog.d/4219.security.4.md b/apple/changelog.d/4219.security.4.md new file mode 100644 index 0000000000..de0e79612d --- /dev/null +++ b/apple/changelog.d/4219.security.4.md @@ -0,0 +1 @@ +An MCP client could read files through a DuckDB table function or a file name used as a table. diff --git a/apple/changelog.d/4219.security.md b/apple/changelog.d/4219.security.md new file mode 100644 index 0000000000..9dcf1b7a1e --- /dev/null +++ b/apple/changelog.d/4219.security.md @@ -0,0 +1 @@ +Read-Only Safe Mode ran a query that called a function which writes data. diff --git a/linux/changelog.d/4219.security.md b/linux/changelog.d/4219.security.md new file mode 100644 index 0000000000..ee5a315ee7 --- /dev/null +++ b/linux/changelog.d/4219.security.md @@ -0,0 +1 @@ +An MCP client could read or write any file through a DuckDB connection. From 2327ae7d3146196397176f09d458cafe0a53bc52 Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Thu, 12 Nov 2026 07:39:52 +0700 Subject: [PATCH 4/7] fix(ios): add the ClickHouse Read-Only refusal to the iOS string catalog --- apple/TableProMobile/TableProMobile/Localizable.xcstrings | 3 +++ 1 file changed, 3 insertions(+) diff --git a/apple/TableProMobile/TableProMobile/Localizable.xcstrings b/apple/TableProMobile/TableProMobile/Localizable.xcstrings index 2588102bc1..23b7fc1724 100644 --- a/apple/TableProMobile/TableProMobile/Localizable.xcstrings +++ b/apple/TableProMobile/TableProMobile/Localizable.xcstrings @@ -19980,6 +19980,9 @@ } } } + }, + "Safe Mode is set to Read-Only for this connection, and the database refused the statement." : { + }, "Same Key as SSH Server" : { From 8793ef0fea66415695e880ff6b2b4dbb1dc30f9e Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Thu, 12 Nov 2026 15:37:13 +0700 Subject: [PATCH 5/7] style(macos): keep the Explain run under the function length limit --- .../Views/Main/Extensions/MainContentCoordinator+Explain.swift | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Explain.swift b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Explain.swift index 0def9d0cde..b767f87f62 100644 --- a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Explain.swift +++ b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Explain.swift @@ -139,8 +139,7 @@ extension MainContentCoordinator { ) } return try await queryExecutor.executeQuery( - driver: driver, sql: request.sql, parameters: nil, rowCap: nil, - safeModeIsReadOnly: safeModeIsReadOnly + driver: driver, sql: request.sql, parameters: nil, rowCap: nil, safeModeIsReadOnly: safeModeIsReadOnly ) } } From 9d5f6b3fa0855c896d9d71406ffc88615fd0ce39 Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Thu, 12 Nov 2026 16:13:46 +0700 Subject: [PATCH 6/7] test(macos): read the stored chat transcript the way main now saves and restores it --- .../AIChatStoppedToolRoundTests.swift | 41 ++++++++++--------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/apple/TableProTests/ViewModels/AIChatStoppedToolRoundTests.swift b/apple/TableProTests/ViewModels/AIChatStoppedToolRoundTests.swift index afd654a5bf..f7b8e55054 100644 --- a/apple/TableProTests/ViewModels/AIChatStoppedToolRoundTests.swift +++ b/apple/TableProTests/ViewModels/AIChatStoppedToolRoundTests.swift @@ -274,13 +274,16 @@ struct AIChatStoppedToolRoundTests { } } - private static func storedWaitingCard() -> AIConversation { - AIConversation(messages: [ - ChatTurnWire(role: .user, blocks: [.text("raise every price by ten percent")]), - ChatTurnWire(role: .assistant, blocks: [ - .toolUse(ToolUseBlock(id: "call_7", name: "execute_query", input: .object([:]), approvalState: .pending)) - ]) - ]) + private static func storedWaitingCard(connectionId: UUID? = nil) -> AIConversation { + AIConversation( + messages: [ + ChatTurnWire(role: .user, blocks: [.text("raise every price by ten percent")]), + ChatTurnWire(role: .assistant, blocks: [ + .toolUse(ToolUseBlock(id: "call_7", name: "execute_query", input: .object([:]), approvalState: .pending)) + ]) + ], + connectionId: connectionId + ) } @Test("Stop while a tool call waits for approval leaves a result for that call") @@ -388,10 +391,13 @@ struct AIChatStoppedToolRoundTests { viewModel.startNewConversation() await task?.value + await viewModel.lastConversationWrite?.value #expect(viewModel.messages.isEmpty) - let stored = try #require(viewModel.conversations.first) - let storedStates = stored.messages.flatMap { toolUseStates(in: $0) } + let summary = try #require(viewModel.conversations.first) + let stored = await viewModel.chatStorage.load(id: summary.id) + await viewModel.chatStorage.delete(summary.id) + let storedStates = (stored?.messages ?? []).flatMap { toolUseStates(in: $0) } #expect(storedStates == [.cancelled]) #expect(!ToolApprovalCenter.shared.hasPending(sessionId: viewModel.sessionId)) } @@ -411,20 +417,15 @@ struct AIChatStoppedToolRoundTests { @Test("A conversation saved with a waiting card by an earlier launch reopens with it cancelled") func restoreSettlesAStoredWaitingCard() async throws { - let stored = Self.storedWaitingCard() - AIChatStorage.shared.saveSynchronously(stored) + let connection = TestFixtures.makeConnection(type: .postgresql) + let stored = Self.storedWaitingCard(connectionId: connection.id) let viewModel = AIChatViewModel(restoringConversation: stored.id) + await viewModel.chatStorage.save(stored) + viewModel.connection = connection viewModel.restoreConversationsIfNeeded() - // The delete awaits the storage actor, so it runs after a caught wait instead of in a defer. - var waitFailure: (any Error)? - do { - try await waitUntil { !viewModel.messages.isEmpty } - } catch { - waitFailure = error - } - await AIChatStorage.shared.delete(stored.id) - if let waitFailure { throw waitFailure } + await viewModel.transcriptLoad?.value + await viewModel.chatStorage.delete(stored.id) let states = viewModel.messages.flatMap { toolUseStates(in: $0) } #expect(states == [.cancelled]) From 0124e91cc87bcc25a47fbf6086b3129bdb9f8240 Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Thu, 12 Nov 2026 23:16:01 +0700 Subject: [PATCH 7/7] fix(macos): name the MCP client when an analyzed write's plan is logged --- apple/TablePro/Core/MCP/Protocol/Tools/ToolQueryExecutor.swift | 1 + 1 file changed, 1 insertion(+) diff --git a/apple/TablePro/Core/MCP/Protocol/Tools/ToolQueryExecutor.swift b/apple/TablePro/Core/MCP/Protocol/Tools/ToolQueryExecutor.swift index 2222d2dc5f..7341213ccd 100644 --- a/apple/TablePro/Core/MCP/Protocol/Tools/ToolQueryExecutor.swift +++ b/apple/TablePro/Core/MCP/Protocol/Tools/ToolQueryExecutor.swift @@ -120,6 +120,7 @@ enum ToolQueryExecutor { services: services, approved: operation, principal: context.principal, + client: MCPAuditLogger.clientName(for: context.meta), secrets: secrets, source: historySource(for: context) ) {