From c2ae380b82f4d9204bcd9b729fe19bec8c32233a Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Fri, 13 Nov 2026 08:47:10 +0700 Subject: [PATCH 1/5] fix(ai): hold Edit mode to data commands on every engine, and refuse Oracle and Dameng functions named bare at Read-Only --- .../TableProRedisCore/RedisReadCommands.swift | 23 +- .../JavaScript/MongoCommandCatalog.swift | 13 + .../JavaScript/MongoStatementParser.swift | 169 ++-- .../SQLBareNameCalls+Parser.swift | 782 ++++++++++++++++++ .../TableProSQLGrammar/SQLBareNameCalls.swift | 361 ++++++++ .../SQLCallableCatalogCache.swift | 80 ++ .../SQLReadOnlyFunctionCalls.swift | 10 +- ...QLStatementClassifier+DataStatements.swift | 20 +- .../SQLStatementClassifier+Surfaces.swift | 12 + .../SQLStatementClassifier.swift | 1 + .../MongoStatementParserTests.swift | 82 ++ .../SQLBareNameCallsTests.swift | 177 ++++ .../SQLCallableCatalogCacheTests.swift | 103 +++ .../SQLDataStatementTests.swift | 64 ++ .../SQLReadOnlyFunctionCallsTests.swift | 8 + .../AI/Chat/Tools/ExecuteQueryChatTool.swift | 4 +- .../Core/Compare/CompareRowService.swift | 3 +- .../Coordinators/PaginationCoordinator.swift | 2 +- .../QueryExecutionCoordinator+Helpers.swift | 2 +- .../QueryExecutionCoordinator.swift | 7 +- .../Core/ObjectCopy/ObjectCopyRunner.swift | 3 +- ...ginMetadataRegistry+BigtableDefaults.swift | 6 +- ...taRegistry+CommandEngineDataCommands.swift | 45 + ...tadataRegistry+ElasticsearchDefaults.swift | 15 +- ...ginMetadataRegistry+FirebaseDefaults.swift | 9 +- ...PluginMetadataRegistry+KafkaDefaults.swift | 6 +- ...inMetadataRegistry+MemcachedDefaults.swift | 8 +- ...MetadataRegistry+MongoDBDataCommands.swift | 21 + ...PluginMetadataRegistry+Neo4jDefaults.swift | 10 +- ...luginMetadataRegistry+QdrantDefaults.swift | 16 +- ...ginMetadataRegistry+RegistryDefaults.swift | 17 +- ...inMetadataRegistry+SurrealDBDefaults.swift | 5 +- ...inMetadataRegistry+TypesenseDefaults.swift | 13 +- ...ginMetadataRegistry+WeaviateDefaults.swift | 18 +- .../Core/Plugins/PluginMetadataRegistry.swift | 9 +- .../Execution/DefaultExecutionGate.swift | 19 +- .../Execution/ExecutionGateProvider.swift | 38 + .../Execution/ExternalStatementGate.swift | 2 +- .../Services/Execution/OperationRequest.swift | 6 +- .../Execution/ReadOnlyFunctionCalls.swift | 12 +- .../Export/TableTransferService.swift | 3 +- ...ueryClassifier+CommandDataStatements.swift | 130 +++ .../SQL/QueryClassifier+Cypher.swift | 129 +++ .../SQL/QueryClassifier+DataStatements.swift | 93 +++ .../SQL/QueryClassifier+MongoDB.swift | 40 + ...ueryClassifier+RequestDataStatements.swift | 256 ++++++ .../Core/Utilities/SQL/QueryClassifier.swift | 23 +- apple/TablePro/Models/AI/AIModels.swift | 9 +- .../TablePro/Resources/Localizable.xcstrings | 3 + ...nContentCoordinator+ReadOnlySafeMode.swift | 10 +- .../Views/Main/MainContentCoordinator.swift | 4 +- .../Helpers/ReadOnlyFunctionGuard.swift | 26 + .../Views/QueryEditorView.swift | 19 + .../Helpers/ReadOnlyBareNameGuardTests.swift | 39 + .../EditModeDataCommandsCommandTests.swift | 130 +++ .../EditModeDataCommandsLanguageTests.swift | 188 +++++ .../EditModeDataCommandsRequestTests.swift | 236 ++++++ .../ExternalStatementGateTests.swift | 26 +- .../BareFunctionCatalogReadTests.swift | 130 +++ .../Execution/ExecutionGateTests.swift | 55 ++ .../ReadOnlySafeModeEntryPointTests.swift | 13 + apple/project.yml | 5 +- .../probes/check-bare-function-names.sh | 77 ++ 63 files changed, 3720 insertions(+), 125 deletions(-) create mode 100644 apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLBareNameCalls+Parser.swift create mode 100644 apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLBareNameCalls.swift create mode 100644 apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLCallableCatalogCache.swift create mode 100644 apple/Packages/TableProCore/Tests/TableProSQLGrammarTests/MongoStatementParserTests.swift create mode 100644 apple/Packages/TableProCore/Tests/TableProSQLGrammarTests/SQLBareNameCallsTests.swift create mode 100644 apple/Packages/TableProCore/Tests/TableProSQLGrammarTests/SQLCallableCatalogCacheTests.swift create mode 100644 apple/TablePro/Core/Plugins/PluginMetadataRegistry+CommandEngineDataCommands.swift create mode 100644 apple/TablePro/Core/Plugins/PluginMetadataRegistry+MongoDBDataCommands.swift create mode 100644 apple/TablePro/Core/Utilities/SQL/QueryClassifier+CommandDataStatements.swift create mode 100644 apple/TablePro/Core/Utilities/SQL/QueryClassifier+DataStatements.swift create mode 100644 apple/TablePro/Core/Utilities/SQL/QueryClassifier+RequestDataStatements.swift create mode 100644 apple/TableProMobile/TableProMobileTests/Helpers/ReadOnlyBareNameGuardTests.swift create mode 100644 apple/TableProTests/Core/Execution/EditModeDataCommandsCommandTests.swift create mode 100644 apple/TableProTests/Core/Execution/EditModeDataCommandsLanguageTests.swift create mode 100644 apple/TableProTests/Core/Execution/EditModeDataCommandsRequestTests.swift create mode 100644 apple/TableProTests/Core/Services/Execution/BareFunctionCatalogReadTests.swift create mode 100755 apple/scripts/probes/check-bare-function-names.sh diff --git a/apple/Packages/TableProCore/Sources/TableProRedisCore/RedisReadCommands.swift b/apple/Packages/TableProCore/Sources/TableProRedisCore/RedisReadCommands.swift index 83ec3f7cc0..80538d1b93 100644 --- a/apple/Packages/TableProCore/Sources/TableProRedisCore/RedisReadCommands.swift +++ b/apple/Packages/TableProCore/Sources/TableProRedisCore/RedisReadCommands.swift @@ -1,14 +1,15 @@ import Foundation // A command reads only when its verb is on this list, so a verb the list has never heard of counts as a write. +// Checked by name against Redis 8.10.3 `COMMAND DOCS`: a module command flagged readonly can still change state. public enum RedisReadCommands { public static let verbs: Set = [ - "GET", "MGET", "STRLEN", "GETRANGE", "SUBSTR", "EXISTS", "TYPE", "TTL", "PTTL", + "GET", "MGET", "STRLEN", "GETRANGE", "SUBSTR", "LCS", "DIGEST", "EXISTS", "TYPE", "TTL", "PTTL", "EXPIRETIME", "PEXPIRETIME", "KEYS", "SCAN", "RANDOMKEY", "DBSIZE", "DUMP", "HGET", "HMGET", "HGETALL", "HKEYS", "HVALS", "HLEN", "HEXISTS", "HRANDFIELD", - "HSCAN", "HSTRLEN", "LRANGE", "LLEN", "LINDEX", "LPOS", + "HSCAN", "HSTRLEN", "HTTL", "HPTTL", "HEXPIRETIME", "HPEXPIRETIME", "LRANGE", "LLEN", "LINDEX", "LPOS", "SMEMBERS", "SISMEMBER", "SMISMEMBER", "SCARD", "SRANDMEMBER", "SSCAN", - "SDIFF", "SINTER", "SUNION", "SINTERCARD", + "SDIFF", "SINTER", "SUNION", "SINTERCARD", "SDIFFCARD", "SUNIONCARD", "ZRANGE", "ZRANGEBYSCORE", "ZRANGEBYLEX", "ZREVRANGE", "ZREVRANGEBYSCORE", "ZREVRANGEBYLEX", "ZRANK", "ZREVRANK", "ZSCORE", "ZMSCORE", "ZCARD", "ZCOUNT", "ZLEXCOUNT", "ZSCAN", "ZRANDMEMBER", "ZDIFF", "ZINTER", "ZUNION", "ZINTERCARD", @@ -16,8 +17,20 @@ public enum RedisReadCommands { "PFCOUNT", "BITCOUNT", "BITPOS", "GETBIT", "BITFIELD_RO", "GEOPOS", "GEODIST", "GEOHASH", "GEOSEARCH", "GEORADIUS_RO", "GEORADIUSBYMEMBER_RO", "SORT_RO", "OBJECT", "COMMAND", "INFO", "TIME", "LASTSAVE", "PING", "ECHO", "LOLWUT", - "JSON.GET", "JSON.MGET", "JSON.TYPE", "JSON.OBJKEYS", "JSON.ARRLEN", "JSON.STRLEN", - "TS.RANGE", "TS.REVRANGE", "TS.GET", "TS.MGET", "TS.INFO", "FT.SEARCH", "FT.INFO" + "ARCOUNT", "ARGET", "ARGETRANGE", "ARGREP", "ARINFO", "ARLASTITEMS", "ARLEN", "ARMGET", "ARNEXT", "AROP", + "ARSCAN", + "JSON.GET", "JSON.MGET", "JSON.TYPE", "JSON.OBJKEYS", "JSON.ARRLEN", "JSON.STRLEN", "JSON.ARRINDEX", + "JSON.OBJLEN", "JSON.RESP", + "TS.RANGE", "TS.REVRANGE", "TS.GET", "TS.MGET", "TS.INFO", "TS.MRANGE", "TS.MREVRANGE", "TS.NRANGE", + "TS.NREVRANGE", "TS.QUERYINDEX", "TS.QUERYLABELS", "TS.READ", + "FT.SEARCH", "FT.INFO", "FT.AGGREGATE", "FT.HYBRID", "FT.PROFILE", "FT.EXPLAIN", "FT.EXPLAINCLI", "FT.GET", + "FT.MGET", "FT.SPELLCHECK", "FT.SUGGET", "FT.SUGLEN", "FT.SYNDUMP", "FT.TAGVALS", "FT.DICTDUMP", + "FT.ALIASLIST", + "BF.CARD", "BF.EXISTS", "BF.INFO", "BF.MEXISTS", "CF.COUNT", "CF.EXISTS", "CF.INFO", "CF.MEXISTS", + "CMS.INFO", "CMS.QUERY", "TOPK.COUNT", "TOPK.INFO", "TOPK.LIST", "TOPK.QUERY", + "TDIGEST.BYRANK", "TDIGEST.BYREVRANK", "TDIGEST.CDF", "TDIGEST.INFO", "TDIGEST.MAX", "TDIGEST.MIN", + "TDIGEST.QUANTILE", "TDIGEST.RANK", "TDIGEST.REVRANK", "TDIGEST.TRIMMED_MEAN", + "VCARD", "VDIM", "VEMB", "VGETATTR", "VINFO", "VISMEMBER", "VLINKS", "VRANDMEMBER", "VRANGE", "VSIM" ] // `CONFIG GET` reads, and every other `CONFIG` subcommand changes the server. diff --git a/apple/Packages/TableProCore/Sources/TableProSQLGrammar/JavaScript/MongoCommandCatalog.swift b/apple/Packages/TableProCore/Sources/TableProSQLGrammar/JavaScript/MongoCommandCatalog.swift index f851b5d9e2..a14bb38353 100644 --- a/apple/Packages/TableProCore/Sources/TableProSQLGrammar/JavaScript/MongoCommandCatalog.swift +++ b/apple/Packages/TableProCore/Sources/TableProSQLGrammar/JavaScript/MongoCommandCatalog.swift @@ -24,4 +24,17 @@ public enum MongoCommandCatalog { public static func preserves(_ name: String) -> Bool { name.unicodeScalars.allSatisfy(\.isASCII) && preservingCommands.contains(name.lowercased()) } + + // The commands among `preservingCommands` that read only, whatever their options: no `validate`, whose `repair` + // fixes the collection, no `explain`, `getMore` or `killCursors`. + public static let readCommands: Set = [ + "ping", "hello", "ismaster", "buildinfo", "hostinfo", "serverstatus", "connectionstatus", "whatsmyuri", + "listcommands", "getcmdlineopts", "getparameter", "getlog", "currentop", "top", "replsetgetstatus", + "replsetgetconfig", "connpoolstats", "listdatabases", "listcollections", "listindexes", "dbstats", + "collstats", "datasize", "usersinfo", "rolesinfo", "find", "count", "distinct", "aggregate", + ] + + public static func readsOnly(_ name: String) -> Bool { + name.unicodeScalars.allSatisfy(\.isASCII) && readCommands.contains(name.lowercased()) + } } diff --git a/apple/Packages/TableProCore/Sources/TableProSQLGrammar/JavaScript/MongoStatementParser.swift b/apple/Packages/TableProCore/Sources/TableProSQLGrammar/JavaScript/MongoStatementParser.swift index f69d2a3e6c..0655ccfe0a 100644 --- a/apple/Packages/TableProCore/Sources/TableProSQLGrammar/JavaScript/MongoStatementParser.swift +++ b/apple/Packages/TableProCore/Sources/TableProSQLGrammar/JavaScript/MongoStatementParser.swift @@ -1,25 +1,32 @@ import Foundation -/// What a statement the grammar accepted does: every call in order, and every key its literals spell. public struct MongoStatement: Equatable, Sendable { + public enum Receiver: Equatable, Sendable { + // `use` and `show`, which the driver rewrites into calls of the shell's own functions. + case shell + case database, collection, cursor, explainer + } + public struct Call: Equatable, Sendable { public let name: String public let isRead: Bool - /// The first key of a `runCommand` or `adminCommand` document, which names the command. + // The first key of a `runCommand` or `adminCommand` document, which names the command. public let commandName: String? + public let receiver: Receiver + // Named by `getSiblingDB`, `getDB`, `use` or a command's `$db`, `admin` for `adminCommand`, nil for the current one. + public let database: String? + // `db.`, `db["name"]`, `getCollection("name")`, or a command document's first value when it is a string. + public let collection: String? } public var calls: [Call] = [] public var objectKeys: Set = [] } -/// Accepts one mongosh statement whose arguments are literal data, and nothing else. -/// -/// Rejecting is always safe, since the caller then treats the statement as a write. Accepting has -/// to mean JavaScriptCore reads the text the same way, so the lexical rules for whitespace, -/// comments, strings, numbers and regular expression literals follow ECMAScript exactly, and -/// anything they would read differently is refused: escapes in identifiers, legacy octal, `?.`, -/// template literals, a second statement. +// Accepts one mongosh statement whose arguments are literal data, and nothing else. Rejecting is always safe, since +// the caller then treats the statement as a write. Accepting has to mean JavaScriptCore reads the text the same way, +// so whitespace, comments, strings, numbers and regular expressions follow ECMAScript exactly, and anything they +// would read differently is refused: escapes in identifiers, legacy octal, `?.`, template literals, a second statement. public struct MongoStatementParser { private struct Rejected: Error {} @@ -27,7 +34,15 @@ public struct MongoStatementParser { case database, client, collection, cursor, explainer, value } - /// Deep enough for any real filter, shallow enough that a crafted `[[[...]]]` cannot exhaust the stack. + private struct Literal { + var string: String? + var firstKey: String? + var firstValue: String? + // A command's own `$db`, which libmongoc 1.28.1 sends instead of the database it was called on (mongoc-cmd.c). + var database: String? + } + + // Deep enough for any real filter, shallow enough that a crafted `[[[...]]]` cannot exhaust the stack. private static let maximumDepth = 64 private static let databaseReads: Set = [ @@ -55,7 +70,7 @@ public struct MongoStatementParser { private static let commandMethods: Set = ["runCommand", "adminCommand"] - /// Pure value constructors from the shell prelude. None of them reaches the server. + // Pure value constructors from the shell prelude. None of them reaches the server. private static let constructors: Set = [ "ObjectId", "ISODate", "Date", "NumberLong", "NumberInt", "NumberDecimal", "Int32", "Long", "Decimal128", "Double", "Timestamp", "BinData", "HexData", "UUID", "LegacyJavaUUID", @@ -63,7 +78,7 @@ public struct MongoStatementParser { "BSONRegExp", "BSONSymbol", "DBRef" ] - /// Non-writable globals, so no earlier statement can rebind them. + // Non-writable globals, so no earlier statement can rebind them. private static let numericConstants: Set = ["NaN", "Infinity"] private static let boundaryKeys: Set = ["MinKey", "MaxKey"] @@ -76,6 +91,8 @@ public struct MongoStatementParser { private var index: String.UnicodeScalarView.Index private var depth = 0 private var statement = MongoStatement() + private var database: String? + private var collection: String? private let shadowsCollection: (String) -> Bool private init(_ text: String, shadowsCollection: @escaping (String) -> Bool = { _ in false }) { @@ -84,7 +101,7 @@ public struct MongoStatementParser { self.shadowsCollection = shadowsCollection } - /// Whitespace and comments only, as JavaScript reads them: a `--` line is a decrement, not a comment. + // As JavaScript reads it: a `--` line is a decrement, not a comment. public static func isTrivia(_ text: String) -> Bool { var parser = MongoStatementParser(text) guard (try? parser.skipTrivia()) != nil else { return false } @@ -94,23 +111,40 @@ public struct MongoStatementParser { // `shadowsCollection` names what `db.` reaches instead of a collection: PluginKit's // MongoCollectionAccessor.isShadowedByDatabaseMember, which this module cannot import. public static func parse(_ text: String, shadowsCollection: @escaping (String) -> Bool) -> MongoStatement? { - if isShellCommand(text) { return MongoStatement() } + if let command = shellCommand(text) { + let call = MongoStatement.Call( + name: command.keyword, + isRead: true, + commandName: nil, + receiver: .shell, + database: command.keyword == "use" ? command.argument : nil, + collection: nil + ) + return MongoStatement(calls: [call]) + } var parser = MongoStatementParser(text, shadowsCollection: shadowsCollection) return try? parser.statementText() } - /// `use orders` and `show collections`, in exactly the shape the driver rewrites into a call - /// (`MongoShellCommandLine.rewrite`): anything looser reaches JavaScriptCore as typed. public static func isShellCommand(_ text: String) -> Bool { + shellCommand(text) != nil + } + + // `use orders` and `show collections`, in exactly the shape the driver rewrites into a call + // (`MongoShellCommandLine.rewrite`): anything looser reaches JavaScriptCore as typed. + private static func shellCommand(_ text: String) -> (keyword: String, argument: String)? { let trimmed = text.trimmingCharacters(in: .whitespacesAndNewlines) let lowered = trimmed.lowercased() for keyword in ["use", "show"] where lowered.hasPrefix(keyword + " ") { let remainder = trimmed.dropFirst(keyword.count).trimmingCharacters(in: .whitespaces) let word = remainder.hasSuffix(";") ? String(remainder.dropLast()) : remainder - guard keyword == "use" else { return shownTopics.contains(word.lowercased()) } - return !word.isEmpty && word.unicodeScalars.allSatisfy(isDatabaseNameScalar) + guard keyword == "use" else { + return shownTopics.contains(word.lowercased()) ? (keyword: keyword, argument: word) : nil + } + guard !word.isEmpty, word.unicodeScalars.allSatisfy(isDatabaseNameScalar) else { return nil } + return (keyword: keyword, argument: word) } - return false + return nil } private static func isDatabaseNameScalar(_ scalar: Unicode.Scalar) -> Bool { @@ -135,7 +169,7 @@ public struct MongoStatementParser { return statement } - /// `.name` or `["name"]`, with trivia allowed around the dot as JavaScript allows it. + // Trivia is allowed around the dot, as JavaScript allows it. private mutating func memberName() throws -> String? { try skipTrivia() switch peek() { @@ -162,77 +196,96 @@ public struct MongoStatementParser { case .database: guard isCall else { guard !shadowsCollection(member) else { throw Rejected() } + collection = member return .collection } switch member { case "getSiblingDB": - try stringArgument() + database = try stringArgument() return .database case "getCollection": - try stringArgument() + collection = try stringArgument() return .collection case "getMongo": - try call(member, isRead: true) + try call(member, on: .database, isRead: true) return .client default: - try call(member, isRead: Self.databaseReads.contains(member)) + try call(member, on: .database, isRead: Self.databaseReads.contains(member)) return .value } case .client: guard isCall, member == "getDB" else { throw Rejected() } - try stringArgument() + database = try stringArgument() return .database case .collection: guard isCall else { throw Rejected() } switch member { case "find", "aggregate": - try call(member, isRead: true) + try call(member, on: .collection, isRead: true) return .cursor case "explain": - try call(member, isRead: true) + try call(member, on: .collection, isRead: true) return .explainer default: - try call(member, isRead: Self.collectionReads.contains(member)) + try call(member, on: .collection, isRead: Self.collectionReads.contains(member)) return .value } case .cursor: guard isCall else { throw Rejected() } let modifies = Self.cursorModifiers.contains(member) - try call(member, isRead: modifies || Self.cursorReads.contains(member)) + try call(member, on: .cursor, isRead: modifies || Self.cursorReads.contains(member)) return modifies ? .cursor : .value case .explainer: guard isCall else { throw Rejected() } - try call(member, isRead: Self.explainedReads.contains(member)) + try call(member, on: .explainer, isRead: Self.explainedReads.contains(member)) return .value case .value: throw Rejected() } } - private mutating func call(_ name: String, isRead: Bool) throws { - let firstKey = try arguments() - let commandName = Self.commandMethods.contains(name) ? firstKey : nil - statement.calls.append(MongoStatement.Call(name: name, isRead: isRead, commandName: commandName)) + private mutating func call(_ name: String, on receiver: MongoStatement.Receiver, isRead: Bool) throws { + let first = try arguments() + guard Self.commandMethods.contains(name) else { + statement.calls.append(MongoStatement.Call( + name: name, + isRead: isRead, + commandName: nil, + receiver: receiver, + database: database, + collection: receiver == .database ? nil : collection + )) + return + } + statement.calls.append(MongoStatement.Call( + name: name, + isRead: isRead, + commandName: first.firstKey, + receiver: receiver, + database: first.database ?? (name == "adminCommand" ? "admin" : database), + collection: first.firstValue + )) } - private mutating func stringArgument() throws { + private mutating func stringArgument() throws -> String { try expect("(") try skipTrivia() - _ = try string() + let argument = try string() try skipTrivia() try expect(")") + return argument } - /// The literal arguments of a call. Returns the first key of the first argument when it is a document. - private mutating func arguments() throws -> String? { + // Only the first argument's literal: a command is its first argument. + private mutating func arguments() throws -> Literal { try expect("(") try skipTrivia() - var firstKey: String? + var first = Literal() var isFirst = true while peek() != ")" { - let key = try value() + let literal = try value() if isFirst { - firstKey = key + first = literal isFirst = false } try skipTrivia() @@ -241,13 +294,12 @@ public struct MongoStatementParser { try skipTrivia() } try expect(")") - return firstKey + return first } // MARK: - Literals - /// One literal value. Returns its first key when it is a document. - private mutating func value() throws -> String? { + private mutating func value() throws -> Literal { depth += 1 defer { depth -= 1 } guard depth <= Self.maximumDepth else { throw Rejected() } @@ -259,7 +311,7 @@ public struct MongoStatementParser { case "[": try array() case "\"", "'": - _ = try string() + return Literal(string: try string()) case "/": try regularExpression() case "-", ".", "0"..."9": @@ -267,29 +319,32 @@ public struct MongoStatementParser { default: try word() } - return nil + return Literal() } - private mutating func document() throws -> String? { + private mutating func document() throws -> Literal { try expect("{") try skipTrivia() - var firstKey: String? + var literal = Literal() while peek() != "}" { let key = try propertyKey() // A literal's `__proto__` key sets its prototype rather than a field. guard key != "__proto__" else { throw Rejected() } statement.objectKeys.insert(key) - if firstKey == nil { firstKey = key } + if literal.firstKey == nil { literal.firstKey = key } try skipTrivia() try expect(":") - _ = try value() + let entry = try value() + // A key written twice keeps its first place and takes its last value. + if key == literal.firstKey { literal.firstValue = entry.string } + if key == "$db" { literal.database = entry.string } try skipTrivia() guard peek() == "," else { break } advance() try skipTrivia() } try expect("}") - return firstKey + return literal } private mutating func propertyKey() throws -> String { @@ -318,7 +373,7 @@ public struct MongoStatementParser { try expect("]") } - /// `true`, `false`, `null`, a prelude constructor called on literals, or `MinKey`/`MaxKey`. + // `true`, `false`, `null`, a prelude constructor called on literals, or `MinKey`/`MaxKey`. private mutating func word() throws { var name = try identifier() if Self.numericConstants.contains(name) { return } @@ -431,7 +486,7 @@ public struct MongoStatementParser { return scalar } - /// A decimal literal, optionally negated. Hex, octal, binary, BigInt and separators are refused. + // Decimal only: hex, octal, binary, BigInt and separators are refused. private mutating func number() throws { if peek() == "-" { advance() @@ -466,7 +521,7 @@ public struct MongoStatementParser { return count } - /// The lexical grammar of a regular expression literal: a `/` inside a class does not end it. + // A `/` inside a class does not end the literal. private mutating func regularExpression() throws { try expect("/") var inClass = false @@ -501,8 +556,8 @@ public struct MongoStatementParser { // MARK: - Lexing - /// An ASCII identifier. One followed by `\` or by a non-ASCII character JavaScript might read as - /// part of the name is refused, since the name the parser saw would not be the name that runs. + // ASCII only. One followed by `\` or by a non-ASCII character JavaScript might read as part of the name is + // refused, since the name the parser saw would not be the name that runs. private mutating func identifier() throws -> String { guard let first = peek(), isIdentifierStart(first) else { throw Rejected() } var name = String.UnicodeScalarView() @@ -522,7 +577,7 @@ public struct MongoStatementParser { isIdentifierStart(scalar) || ("0"..."9").contains(scalar) } - /// ECMAScript WhiteSpace and LineTerminator, and comments. An unterminated block comment is refused. + // ECMAScript WhiteSpace and LineTerminator, and comments. An unterminated block comment is refused. private mutating func skipTrivia() throws { while let scalar = peek() { if isTrivia(scalar) { diff --git a/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLBareNameCalls+Parser.swift b/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLBareNameCalls+Parser.swift new file mode 100644 index 0000000000..497359755d --- /dev/null +++ b/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLBareNameCalls+Parser.swift @@ -0,0 +1,782 @@ +import Foundation + +extension SQLBareNameCalls { + // One query's blocks: each SELECT, the tables it reads and the names it holds. A shape it cannot read, such as a + // `WITH` that is not a list of named queries, counts as a call. + struct Parser { + typealias Block = SQLBareNameBlock + + private struct CommonTable { + var block: Int? + var columns: [String]? + } + + private enum Clause { + case from + case orderBy + case other + } + + private enum FromState { + case expectTable + case afterTable + case afterAlias + } + + private let code: StatementCode + private let grammar: SQLLexicalGrammar + private let reserved: Set + private let tokens: [TSQLTokens.Token] + private var closing: [Int: Int] = [:] + private(set) var blocks: [Block] = [] + private(set) var callsOutright = false + + init(_ code: StatementCode, grammar: SQLLexicalGrammar, reserved: Set) { + self.code = code + self.grammar = grammar + self.reserved = reserved + tokens = code.tokens + var open: [Int] = [] + for (index, token) in tokens.enumerated() { + if token.kind == .open { + open.append(index) + } else if token.kind == .close, let start = open.popLast() { + closing[start] = index + } + } + } + + // A query tab hands the gate its statements joined by line breaks, which Oracle does not split on, so a SELECT + // standing where a clause would is read as the next query. + // A write is refused before this is asked, so only queries and EXPLAIN PLAN reach it. + mutating func parse() { + let end = (tokens.lastIndex { $0.kind != .terminator }).map { $0 + 1 } ?? 0 + var index = 0 + while index < end { + let token = tokens[index] + guard token.depth == 0 else { + index += 1 + continue + } + if token.word == "EXPLAIN" { + // EXPLAIN PLAN plans its statement without running it, so the statement is read and dropped. + guard let plan = (index.., + parent: Int?, + commonTables: [String: CommonTable] + ) -> (first: Int?, end: Int) { + var index = range.lowerBound + let end = range.upperBound + var scope = commonTables + if word(at: index) == "WITH" { + guard let next = commonTableList(from: index + 1, end: end, scope: &scope) else { + callsOutright = true + return (nil, end) + } + index = next + } + var first: Int? + var last: Int? + var lastIsSelect = false + var compound = false + while index < end { + if tokens[index].kind == .open, let close = closing[index], close < end { + let block = queryExpression((index + 1).. Int { + let depth = tokens[start].depth + var index = start + while index < end { + if tokens[index].depth == depth, startsNextQuery(at: index, end: end) { return index } + index += 1 + } + return end + } + + private func startsNextQuery(at index: Int, end: Int) -> Bool { + switch word(at: index) { + case "SELECT": + return true + case "WITH": + guard identifier(at: index + 1) != nil, index + 2 < end else { return false } + return word(at: index + 2) == "AS" || tokens[index + 2].kind == .open + default: + return false + } + } + + // `name [(columns)] AS (query) [SEARCH ... | CYCLE ...], ...`. Nil when it is not that shape, as with an inline + // PL/SQL `WITH FUNCTION`. + private mutating func commonTableList( + from start: Int, + end: Int, + scope: inout [String: CommonTable] + ) -> Int? { + var index = start + while index < end { + guard let name = identifier(at: index) else { return nil } + index += 1 + var columns: [String]? + if index < end, tokens[index].kind == .open, let close = closing[index] { + columns = identifierList((index + 1)..= depth, tokens[index].kind != .comma, + tokens[index].kind != .open, word(at: index) != "SELECT" { + index += 1 + } + guard index < end, tokens[index].kind == .comma else { return index } + index += 1 + } + return nil + } + + private func branchEnd(from start: Int, end: Int) -> Int { + let depth = tokens[start].depth + var index = start + 1 + while index < end { + if tokens[index].depth == depth { + if let word = tokens[index].word, Self.setOperators.contains(word) { return index } + if startsNextQuery(at: index, end: end) { return index } + } + index += 1 + } + return end + } + + private mutating func selectBlock( + _ range: Range, + parent: Int?, + commonTables: [String: CommonTable] + ) -> Int { + let block = newBlock(parent: parent) + let depth = tokens[range.lowerBound].depth + var marks: [(index: Int, clause: Clause)] = [] + for index in (range.lowerBound + 1).. Clause? { + guard let word = word(at: index) else { return nil } + let next = index + 1 < end ? self.word(at: index + 1) : nil + switch word { + case "FROM": + return afterFrom ? nil : .from + case "WHERE", "HAVING", "INTO": + return .other + case "GROUP": + return next == "BY" && self.word(at: index - 1) != "WITHIN" ? .other : nil + case "ORDER": + return next == "BY" || next == "SIBLINGS" ? .orderBy : nil + case "CONNECT": + return next == "BY" ? .other : nil + case "START": + return next == "WITH" ? .other : nil + case "FOR": + return next == "UPDATE" ? .other : nil + case "MODEL", "WINDOW", "OFFSET": + return afterFrom ? .other : nil + case "FETCH": + return afterFrom && (next == "FIRST" || next == "NEXT") ? .other : nil + default: + return nil + } + } + + // MARK: - Select list + + private mutating func selectList(_ range: Range, block: Int, _ commonTables: [String: CommonTable]) { + var start = range.lowerBound + while start < range.upperBound, let word = word(at: start), ["DISTINCT", "UNIQUE", "ALL"].contains(word) { + start += 1 + } + // Dameng's `TOP n [PERCENT] [WITH TIES]`, whose count would otherwise read as an operand an alias follows. + if word(at: start) == "TOP", start + 1 < range.upperBound { + start += 1 + if tokens[start].kind == .open, let close = closing[start] { + expression(start..<(close + 1), block: block, commonTables: commonTables, ordersBy: false) + start = close + 1 + } else { + start += 1 + } + while start < range.upperBound, let word = word(at: start), ["PERCENT", "WITH", "TIES"].contains(word) { + start += 1 + } + } + guard start < range.upperBound else { return } + let depth = tokens[start].depth + var itemStart = start + for index in start...range.upperBound { + guard index == range.upperBound || (tokens[index].kind == .comma && tokens[index].depth == depth) else { + continue + } + if itemStart < index { selectItem(itemStart.., block: Int, _ commonTables: [String: CommonTable]) { + let last = item.upperBound - 1 + if isSymbol(Self.star, at: last) { + let qualifiers = chainNames(item.lowerBound..= 2, let alias = identifier(at: last) { + if word(at: last - 1) == "AS" { + expressionEnd = last - 1 + } else if endsOperand(at: last - 1) { + expressionEnd = last + } + if expressionEnd != item.upperBound { + blocks[block].aliases.insert(alias) + blocks[block].outputs.append(.name(alias)) + } + } + if expressionEnd == item.upperBound, let names = chainNames(item), let name = names.last { + blocks[block].outputs.append(.name(name)) + } + expression(item.lowerBound..) -> [String]? { + guard !range.isEmpty, range.count % 2 == 1 else { return nil } + var names: [String] = [] + for index in range { + if (index - range.lowerBound) % 2 == 0 { + guard let name = (index == range.lowerBound ? identifier(at: index) : chainPart(at: index)) else { + return nil + } + names.append(name) + } else if !isSymbol(Self.period, at: index) { + return nil + } + } + return names + } + + // MARK: - FROM + + private mutating func fromClause(_ range: Range, block: Int, commonTables: [String: CommonTable]) { + guard !range.isEmpty else { return } + let depth = tokens[range.lowerBound].depth + var state = FromState.expectTable + var lateral = false + var current: Int? + var index = range.lowerBound + let end = range.upperBound + while index < end { + let token = tokens[index] + let word = token.word + if token.kind == .comma, token.depth == depth { + state = .expectTable + current = nil + index += 1 + continue + } + if let word, Self.joinWords.contains(word) { + state = .expectTable + current = nil + index += 1 + continue + } + if let word, Self.joinPrefixes.contains(word) { + index += 1 + continue + } + switch word { + case "LATERAL": + lateral = true + index += 1 + continue + case "ON": + let stop = fromBoundary(after: index, end: end, depth: depth) + expression((index + 1).. (end: Int, source: Int?)? { + if tokens[index].kind == .open, let close = closing[index], close < end { + // Oracle 23ai's `(VALUES (...), (...)) t (a, b)`, which sees no table beside it, as an inline view does. + if let first = ((index + 1)..= 2 ? parts[parts.count - 2] : nil, name: name) + return (last + 1, append(.table(table, alias: nil), to: block)) + } + + private mutating func naming(_ source: Int, in block: Int, alias: String, columns: [String]?) { + switch blocks[block].sources[source] { + case .table(let table, _): + blocks[block].sources[source] = .table(table, alias: alias) + case .derived(let derived, let declared, _): + blocks[block].sources[source] = .derived(block: derived, columns: columns ?? declared, alias: alias) + case .opaque: + blocks[block].sources[source] = columns.map { .derived(block: nil, columns: $0, alias: alias) } + ?? .opaque(alias: alias) + } + } + + private mutating func append(_ source: Block.Source, to block: Int) -> Int { + blocks[block].sources.append(source) + return blocks[block].sources.count - 1 + } + + private func fromBoundary(after index: Int, end: Int, depth: Int) -> Int { + var next = index + 1 + while next < end { + let token = tokens[next] + if token.depth == depth { + if token.kind == .comma { return next } + if let word = token.word, Self.joinWords.contains(word) || Self.joinPrefixes.contains(word) { + return next + } + } + next += 1 + } + return end + } + + // MARK: - Expressions + + // `readsAfterAs`: MATCH_RECOGNIZE's `DEFINE v AS `, where AS leads an expression, not a type. + private mutating func expression( + _ range: Range, + block: Int, + commonTables: [String: CommonTable], + ordersBy: Bool, + readsAfterAs: Bool = false + ) { + var index = range.lowerBound + let end = range.upperBound + var afterAs = false + while index < end { + let token = tokens[index] + if token.kind == .open { + guard let close = closing[index], close < end else { return } + let inner = (index + 1).. 1 { + blocks[block].chains.append(parts) + } + index = last + 1 + } + } + + private mutating func resolveChains() { + for index in blocks.indices { + for parts in blocks[index].chains { + if visibleSource(answering: parts[0], from: index) { + blocks[index].candidates.append(.member(root: parts[0], member: parts[1])) + } else if parts.count >= 3, visibleTable(owner: parts[0], name: parts[1], from: index) { + continue + } else if parts.count <= 3, let last = parts.last, last == "NEXTVAL" || last == "CURRVAL" { + continue + } else { + callsOutright = true + } + } + } + } + + private func visibleSource(answering qualifier: String, from index: Int) -> Bool { + scope(from: index).contains { $0.answers(to: qualifier) } + } + + private func visibleTable(owner: String, name: String, from index: Int) -> Bool { + scope(from: index).contains { source in + guard case .table(let table, nil) = source else { return false } + return table.owner == owner && table.name == name + } + } + + private func scope(from index: Int) -> [Block.Source] { + var sources: [Block.Source] = [] + var current: Int? = index + var hops = 0 + while let block = current, hops <= blocks.count { + sources += blocks[block].sources + current = blocks[block].parent + hops += 1 + } + return sources + } + + // MARK: - Tokens + + private mutating func newBlock(parent: Int?) -> Int { + blocks.append(Block(parent: parent)) + return blocks.count - 1 + } + + private func startsQuery(_ range: Range) -> Bool { + guard let first = range.first(where: { tokens[$0].kind != .open }) else { return false } + return word(at: first) == "SELECT" || word(at: first) == "WITH" + } + + private func identifierList(_ range: Range) -> [String] { + guard !range.isEmpty else { return [] } + let depth = tokens[range.lowerBound].depth + return range.compactMap { tokens[$0].depth == depth ? identifier(at: $0) : nil } + } + + private func word(at index: Int) -> String? { + tokens.indices.contains(index) ? tokens[index].word : nil + } + + private func isSymbol(_ symbol: UInt16, at index: Int) -> Bool { + index >= 0 && code.isSymbol(symbol, at: index) + } + + // A name as the catalog stores it: unquoted in capitals, quoted as written. Nil for a reserved word, a literal or + // a symbol. + private func identifier(at index: Int) -> String? { + guard tokens.indices.contains(index) else { return nil } + if case .word(let word) = tokens[index].kind { + return reserved.contains(word) ? nil : word + } + return quotedOrWideName(at: index) + } + + // After a period any word names a member, a reserved one too. + private func chainPart(at index: Int) -> String? { + guard tokens.indices.contains(index) else { return nil } + if case .word(let word) = tokens[index].kind { return word } + return quotedOrWideName(at: index) + } + + private func quotedOrWideName(at index: Int) -> String? { + let token = tokens[index] + switch token.kind { + case .name: + var end = token.location + while end < code.text.length, SQLFunctionCallScanner.isIdentifierUnit(code.text.character(at: end)) { + end += 1 + } + return code.text.substring(with: NSRange(location: token.location, length: end - token.location)) + .uppercased() + case .quoted: + guard let span = SQLNonCodeSpan.span(at: token.location, in: code.text, grammar: grammar), + code.text.character(at: span.start) == Self.doubleQuote + else { return nil } + let inner = NSRange(location: span.start + 1, length: max(0, span.contentEnd - span.start - 1)) + return code.text.substring(with: inner).replacingOccurrences(of: "\"\"", with: "\"", options: .literal) + default: + return nil + } + } + + // A name, a literal, a number or a closing parenthesis, which an alias may follow. + private func endsOperand(at index: Int) -> Bool { + guard tokens.indices.contains(index) else { return false } + switch tokens[index].kind { + case .quoted, .name, .close: + return true + case .word(let word): + return word == "NULL" + || (!SQLBareNameCalls.oracleReservedWords.contains(word) && !Self.operatorWords.contains(word)) + case .other: + let unit = code.text.character(at: tokens[index].location) + return unit >= Self.digitZero && unit <= Self.digitZero + 9 + default: + return false + } + } + + private static let setOperators: Set = ["UNION", "INTERSECT", "MINUS", "EXCEPT"] + private static let joinWords: Set = ["JOIN", "APPLY"] + private static let joinPrefixes: Set = ["INNER", "LEFT", "RIGHT", "FULL", "OUTER", "CROSS", "NATURAL"] + + // Words a table can be followed by that are not its alias. + private static let fromKeywords: Set = joinWords.union(joinPrefixes).union([ + "USING", "PARTITION", "SUBPARTITION", "SAMPLE", "SEED", "PIVOT", "UNPIVOT", "MATCH_RECOGNIZE", "VERSIONS", + "LATERAL", "MODEL", "WINDOW", "OFFSET", "FETCH", "LIMIT", + ]) + + // Words after which an operand follows, so a name after one is never an alias. + private static let operatorWords: Set = [ + "CASE", "WHEN", "THEN", "ELSE", "LIKEC", "LIKE2", "LIKE4", "ESCAPE", "MEMBER", "SUBMULTISET", "MULTISET", + "EXCEPT", "SOME", "ANY", "ALL", "AT", "ZONE", "TIME", "LOCAL", "INTERVAL", "DATE", "TIMESTAMP", "NEW", + "CURSOR", "CONNECT_BY_ROOT", "COLLATE", "TO", "DAY", "MONTH", "YEAR", "HOUR", "MINUTE", "SECOND", "OVER", + "KEEP", "WITHIN", "TYPE", "JSON", "A", "EMPTY", "DANGLING", "PRESENT", "NAN", "INFINITE", "RETURNING", + "FORMAT", "DISTINCT", "PRIOR", "NOT", "AND", "OR", "IS", "IN", "LIKE", "BETWEEN", "EXISTS", "AS", "DIV", + "MOD", "XOR", "REGEXP", "RLIKE", "BINARY", + ] + + private static let period = UInt16(UnicodeScalar(".").value) + private static let star = UInt16(UnicodeScalar("*").value) + private static let colon = UInt16(UnicodeScalar(":").value) + private static let at = UInt16(UnicodeScalar("@").value) + private static let doubleQuote = UInt16(UnicodeScalar("\"").value) + private static let digitZero = UInt16(UnicodeScalar("0").value) + } +} diff --git a/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLBareNameCalls.swift b/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLBareNameCalls.swift new file mode 100644 index 0000000000..a15e45623d --- /dev/null +++ b/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLBareNameCalls.swift @@ -0,0 +1,361 @@ +import Foundation + +// Oracle and Dameng call a function that takes no arguments when a query names it without parentheses, once the name +// is not a column of a table in scope. Measured on Oracle 23ai and DM8: a column wins; a correlated subquery sees the +// outer query's tables and an inline view does not; a select-list alias is seen by ORDER BY only; `pkg.f` and +// `owner.f` call; on Dameng `alias.f` calls package `alias` when its table has no column F. This reads every such name +// a query holds, and `SQLCallableCatalog` is what the session sees, so together they say whether the query calls one. +public enum SQLBareNameCalls { + public static let engines: Set = ["Oracle", "Dameng"] + + // Under every way the engine could lex the text. Only a query reads anything: a write is refused before this. + public static func read(_ sql: String, databaseTypeId: String, readings: SQLLexicalReadings) -> SQLBareNameReading { + let reserved = reservedWords(forDatabaseTypeId: databaseTypeId) + var reading = SQLBareNameReading() + reading.foldsFunctionNames = databaseTypeId == "Dameng" + for grammar in readings.all { + for statement in SQLStatementClassifier.statements(of: sql, grammar: grammar) { + var parser = Parser(StatementCode(statement, grammar: grammar), grammar: grammar, reserved: reserved) + parser.parse() + reading.callsOutright = reading.callsOutright || parser.callsOutright + if !parser.blocks.isEmpty { reading.queries.append(parser.blocks) } + } + } + return reading + } + + // V$RESERVED_WORDS with RESERVED = 'Y' on Oracle 23ai, and the ones of those DM8 reserves too. No function carries + // one of these names unquoted, so none is asked about. + static let oracleReservedWords: Set = [ + "ALL", "ALTER", "AND", "ANY", "AS", "ASC", "BETWEEN", "BY", "CHAR", "CHECK", "CLUSTER", "COMPRESS", "CONNECT", + "CREATE", "DATE", "DECIMAL", "DEFAULT", "DELETE", "DESC", "DISTINCT", "DROP", "ELSE", "EXCEPT", "EXCLUSIVE", + "EXISTS", "FLOAT", "FOR", "FROM", "GRANT", "GROUP", "HAVING", "IDENTIFIED", "IN", "INDEX", "INSERT", "INTEGER", + "INTERSECT", "INTO", "IS", "LIKE", "LOCK", "LONG", "MINUS", "MODE", "NOCOMPRESS", "NOT", "NOWAIT", "NULL", + "NUMBER", "OF", "ON", "OPTION", "OR", "ORDER", "PCTFREE", "PRIOR", "PUBLIC", "RAW", "RENAME", "RESOURCE", + "REVOKE", "SELECT", "SET", "SHARE", "SIZE", "SMALLINT", "START", "SYNONYM", "TABLE", "THEN", "TO", "TRIGGER", + "UNION", "UNIQUE", "UPDATE", "VALUES", "VARCHAR", "VARCHAR2", "VIEW", "WHERE", "WITH", + ] + + static let damengReservedWords: Set = oracleReservedWords.subtracting([ + "COMPRESS", "DATE", "EXCEPT", "EXCLUSIVE", "IDENTIFIED", "INTEGER", "LOCK", "LONG", "MODE", "NOCOMPRESS", + "NOWAIT", "NUMBER", "OPTION", "PCTFREE", "RAW", "RENAME", "RESOURCE", "SHARE", "SIZE", "SMALLINT", "THEN", + "VARCHAR", "VARCHAR2", + ]) + + static func reservedWords(forDatabaseTypeId typeId: String) -> Set { + typeId == "Dameng" ? damengReservedWords : oracleReservedWords + } +} + +public struct SQLTableName: Hashable, Sendable { + // Nil resolves as the session does: the current schema, its synonyms, then public synonyms. + public let owner: String? + public let name: String + + public init(owner: String?, name: String) { + self.owner = owner + self.name = name + } +} + +// What the session can call bare, read once per connection and schema with `query`. +public struct SQLCallableCatalog: Sendable, Equatable { + // Functions in the current schema and synonyms that reach a function, a synonym or a remote object. + public let functions: Set + // What a dotted call can start with: packages and synonyms to them, and schemas. + public let roots: Set + + public init(functions: Set, roots: Set) { + self.functions = functions + self.roots = roots + } + + // Rows of `query`: a kind, `F` or `R`, and a name. + public init(rows: [[String?]]) { + var functions: Set = [] + var roots: Set = [] + for row in rows where row.count >= 2 { + guard let kind = row[0], let name = row[1] else { continue } + if kind == "F" { functions.insert(name) } else if kind == "R" { roots.insert(name) } + } + self.init(functions: functions, roots: roots) + } + + // Owners are named, since a schema object called ALL_OBJECTS would otherwise answer instead. Oracle and DM8 both + // answer it. + public static let query = """ + SELECT 'F', o.OBJECT_NAME FROM SYS.ALL_OBJECTS o + WHERE o.OWNER = SYS_CONTEXT('USERENV', 'CURRENT_SCHEMA') AND o.OBJECT_TYPE = 'FUNCTION' + UNION + SELECT 'F', s.SYNONYM_NAME FROM SYS.ALL_SYNONYMS s + WHERE s.OWNER IN (SYS_CONTEXT('USERENV', 'CURRENT_SCHEMA'), 'PUBLIC') + AND (s.DB_LINK IS NOT NULL OR EXISTS (SELECT 1 FROM SYS.ALL_OBJECTS t WHERE t.OWNER = s.TABLE_OWNER + AND t.OBJECT_NAME = s.TABLE_NAME AND t.OBJECT_TYPE IN ('FUNCTION', 'SYNONYM'))) + UNION + SELECT 'R', o.OBJECT_NAME FROM SYS.ALL_OBJECTS o + WHERE o.OWNER = SYS_CONTEXT('USERENV', 'CURRENT_SCHEMA') AND o.OBJECT_TYPE = 'PACKAGE' + UNION + SELECT 'R', s.SYNONYM_NAME FROM SYS.ALL_SYNONYMS s + WHERE s.OWNER IN (SYS_CONTEXT('USERENV', 'CURRENT_SCHEMA'), 'PUBLIC') + AND (s.DB_LINK IS NOT NULL OR EXISTS (SELECT 1 FROM SYS.ALL_OBJECTS t WHERE t.OWNER = s.TABLE_OWNER + AND t.OBJECT_NAME = s.TABLE_NAME AND t.OBJECT_TYPE IN ('PACKAGE', 'SYNONYM'))) + UNION + SELECT 'R', u.USERNAME FROM SYS.ALL_USERS u + """ + + // The columns of each table as the session resolves its name: an object in the schema first, then a synonym there, + // then, for a name without an owner, a public synonym. Rows: the table's position, the rank, the database link and + // a column. Nil when a name cannot be written as a literal. + public static func columnsQuery(for tables: [SQLTableName]) -> String? { + var branches: [String] = [] + for (index, table) in tables.enumerated() { + guard let name = literal(table.name) else { return nil } + let owner: String + if let tableOwner = table.owner { + guard let quoted = literal(tableOwner) else { return nil } + owner = quoted + } else { + owner = "SYS_CONTEXT('USERENV', 'CURRENT_SCHEMA')" + } + branches.append(""" + SELECT \(index) I, 1 R, o.OWNER, o.OBJECT_NAME N, CAST(NULL AS VARCHAR2(128)) L FROM SYS.ALL_OBJECTS o \ + WHERE o.OWNER = \(owner) AND o.OBJECT_NAME = \(name) \ + AND o.OBJECT_TYPE IN ('TABLE', 'VIEW', 'MATERIALIZED VIEW') + """) + branches.append(""" + SELECT \(index), 2, s.TABLE_OWNER, s.TABLE_NAME, s.DB_LINK FROM SYS.ALL_SYNONYMS s \ + WHERE s.OWNER = \(owner) AND s.SYNONYM_NAME = \(name) + """) + if table.owner == nil { + branches.append(""" + SELECT \(index), 3, s.TABLE_OWNER, s.TABLE_NAME, s.DB_LINK FROM SYS.ALL_SYNONYMS s \ + WHERE s.OWNER = 'PUBLIC' AND s.SYNONYM_NAME = \(name) + """) + } + } + guard !branches.isEmpty else { return nil } + return """ + SELECT t.I, t.R, t.L, c.COLUMN_NAME FROM (\(branches.joined(separator: " UNION ALL "))) t \ + LEFT JOIN SYS.ALL_TAB_COLUMNS c ON c.OWNER = t.OWNER AND c.TABLE_NAME = t.N + """ + } + + // The first rank that resolves wins. A synonym to a remote object, or to another synonym, has no columns here. + public static func columns(fromRows rows: [[String?]], tables: [SQLTableName]) -> [SQLTableName: Set] { + var ranked: [Int: (rank: Int, remote: Bool, columns: Set)] = [:] + for row in rows where row.count >= 4 { + guard let index = row[0].flatMap({ Int($0) }), tables.indices.contains(index), + let rank = row[1].flatMap({ Int($0) }) + else { continue } + let remote = row[2].map { !$0.isEmpty } ?? false + if let current = ranked[index], current.rank < rank { continue } + var entry = ranked[index].flatMap { $0.rank == rank ? $0 : nil } + ?? (rank: rank, remote: remote, columns: Set()) + entry.remote = entry.remote || remote + if let column = row[3] { entry.columns.insert(column) } + ranked[index] = entry + } + var columns: [SQLTableName: Set] = [:] + for (index, entry) in ranked where !entry.remote { + columns[tables[index]] = entry.columns + } + return columns + } + + // A backslash escapes the quote on a DM8 server with BACKSLASH_ESCAPE set, so a name holding one is not written. + private static func literal(_ name: String) -> String? { + guard !name.isEmpty, !name.contains("\\"), + !name.unicodeScalars.contains(where: { $0.properties.generalCategory == .control }) + else { return nil } + return "'" + name.replacingOccurrences(of: "'", with: "''", options: .literal) + "'" + } +} + +// The names a query holds outside parentheses, block by block, before the catalog says which ones call. +public struct SQLBareNameReading: Sendable, Equatable { + // A dotted name that no table in scope starts, such as `pkg.f` or `owner.f`, or a name at a database link: a call, + // or an error the engine raises before it runs anything. + public internal(set) var callsOutright = false + var queries: [[SQLBareNameBlock]] = [] + // A DM8 instance set up with CASE_SENSITIVE=N keeps a name as written, so on Dameng a function or root matches in + // any case. Columns still match exactly, which can only count more names as calls. + var foldsFunctionNames = false + + // Nothing in the query could be a function's name. + public var namesNothing: Bool { + !callsOutright && queries.allSatisfy { blocks in blocks.allSatisfy(\.candidates.isEmpty) } + } + + // The tables whose columns decide, once the catalog holds a name the query uses. Empty when none does. + public func tablesToResolve(_ catalog: SQLCallableCatalog) -> Set { + let catalog = matching(catalog) + var tables: Set = [] + for blocks in queries where blocks.contains(where: { $0.namesSomething(in: catalog, folding: foldsFunctionNames) }) { + for block in blocks { + for case .table(let table, _) in block.sources { tables.insert(table) } + } + } + return tables + } + + // `columns` holds what `SQLCallableCatalog.columns(fromRows:tables:)` read. A table missing from it has columns + // nobody knows, so a name it might hold counts as a call. + public func callsFunction(_ catalog: SQLCallableCatalog, columns: [SQLTableName: Set]) -> Bool { + if callsOutright { return true } + let catalog = matching(catalog) + return queries.contains { blocks in + let resolver = SQLBareNameResolver(blocks: blocks, columns: columns, folding: foldsFunctionNames) + return blocks.indices.contains { resolver.blockCalls($0, catalog: catalog) } + } + } + + private func matching(_ catalog: SQLCallableCatalog) -> SQLCallableCatalog { + guard foldsFunctionNames else { return catalog } + return SQLCallableCatalog( + functions: Set(catalog.functions.map { $0.uppercased() }), + roots: Set(catalog.roots.map { $0.uppercased() }) + ) + } +} + +struct SQLBareNameBlock: Sendable, Equatable { + enum Source: Sendable, Equatable { + case table(SQLTableName, alias: String?) + // An inline view or a common table expression. Nil block: one that refers to itself, read by its column list. + case derived(block: Int?, columns: [String]?, alias: String?) + // `TABLE(...)`, `JSON_TABLE(...)`, a pipelined function, a remote table, a pivot: columns nobody knows. + case opaque(alias: String?) + + var alias: String? { + switch self { + case .table(_, let alias), .derived(_, _, let alias), .opaque(let alias): return alias + } + } + + // The name a qualifier can use: the alias, or else the table's own name. + func answers(to qualifier: String) -> Bool { + if let alias { return alias == qualifier } + if case .table(let table, _) = self { return table.name == qualifier } + return false + } + } + + enum Output: Sendable, Equatable { + case name(String) + case all + case allOf(String) + } + + enum Candidate: Sendable, Equatable { + // `seesAliases`: an ORDER BY name, which resolves to a select-list alias first. + case name(String, seesAliases: Bool) + // `root.member` where a table in scope answers to `root`. + case member(root: String, member: String) + } + + var parent: Int? + var sources: [Source] = [] + var aliases: Set = [] + var outputs: [Output] = [] + var candidates: [Candidate] = [] + // A compound query's trailing ORDER BY sees only its first branch's names. + var orderByNamesOf: Int? + // Dotted names, resolved once every block's tables are read. + var chains: [[String]] = [] + + func namesSomething(in catalog: SQLCallableCatalog, folding: Bool) -> Bool { + candidates.contains { candidate in + switch candidate { + case .name(let name, _): return catalog.functions.contains(folding ? name.uppercased() : name) + case .member(let root, _): return catalog.roots.contains(folding ? root.uppercased() : root) + } + } + } +} + +private struct SQLBareNameResolver { + let blocks: [SQLBareNameBlock] + let columns: [SQLTableName: Set] + let folding: Bool + + func blockCalls(_ index: Int, catalog: SQLCallableCatalog) -> Bool { + let block = blocks[index] + return block.candidates.contains { candidate in + switch candidate { + case .name(let name, let seesAliases): + guard catalog.functions.contains(folding ? name.uppercased() : name) else { return false } + if seesAliases { + if let named = block.orderByNamesOf { + return outputNames(of: named, depth: 0)?.contains(name) != true + } + if block.aliases.contains(name) { return false } + } + return !isColumn(name, from: index) + case .member(let root, let member): + guard catalog.roots.contains(folding ? root.uppercased() : root) else { return false } + guard let source = source(answering: root, from: index) else { return true } + return columns(of: source, depth: 0)?.contains(member) != true + } + } + } + + private func isColumn(_ name: String, from index: Int) -> Bool { + var current: Int? = index + var hops = 0 + while let blockIndex = current, hops <= blocks.count { + if blocks[blockIndex].sources.contains(where: { columns(of: $0, depth: 0)?.contains(name) == true }) { + return true + } + current = blocks[blockIndex].parent + hops += 1 + } + return false + } + + private func source(answering qualifier: String, from index: Int) -> SQLBareNameBlock.Source? { + var current: Int? = index + var hops = 0 + while let blockIndex = current, hops <= blocks.count { + if let source = blocks[blockIndex].sources.first(where: { $0.answers(to: qualifier) }) { return source } + current = blocks[blockIndex].parent + hops += 1 + } + return nil + } + + private func columns(of source: SQLBareNameBlock.Source, depth: Int) -> Set? { + guard depth <= SQLStatementTiering.deepestNesting else { return nil } + switch source { + case .table(let table, _): + return columns[table] + case .derived(let block, let explicit, _): + if let explicit { return Set(explicit) } + return block.flatMap { outputNames(of: $0, depth: depth + 1) } + case .opaque: + return nil + } + } + + private func outputNames(of index: Int, depth: Int) -> Set? { + guard blocks.indices.contains(index), depth <= SQLStatementTiering.deepestNesting else { return nil } + let block = blocks[index] + var names: Set = [] + for output in block.outputs { + switch output { + case .name(let name): + names.insert(name) + case .all: + for source in block.sources { + guard let sourceColumns = columns(of: source, depth: depth + 1) else { return nil } + names.formUnion(sourceColumns) + } + case .allOf(let qualifier): + guard let source = block.sources.first(where: { $0.answers(to: qualifier) }), + let sourceColumns = columns(of: source, depth: depth + 1) + else { return nil } + names.formUnion(sourceColumns) + } + } + return names + } +} diff --git a/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLCallableCatalogCache.swift b/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLCallableCatalogCache.swift new file mode 100644 index 0000000000..ea3a0f1e9d --- /dev/null +++ b/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLCallableCatalogCache.swift @@ -0,0 +1,80 @@ +import Foundation + +// What Oracle and Dameng let a query call without parentheses, read from the session's own catalog and kept per +// connection and schema, so a query names its functions to Read-Only at the cost of one catalog read a minute. +public actor SQLCallableCatalogCache { + public typealias Reader = @Sendable (String) async throws -> [[String?]] + + // A function or a table another session creates is seen within this long. + private let lifetime: Duration + // Starts timing an entry's age on the clock. + private let stopwatch: @Sendable () -> @Sendable () -> Duration + + private struct Key: Hashable { + let connectionId: UUID + let schema: String? + } + + private struct Entry { + let value: Value + let age: @Sendable () -> Duration + } + + private var catalogs: [Key: Entry] = [:] + private var columns: [Key: [SQLTableName: Entry>]] = [:] + + public init>(lifetime: Duration = .seconds(60), clock: C = ContinuousClock()) { + self.lifetime = lifetime + stopwatch = { + let start = clock.now + return { start.duration(to: clock.now) } + } + } + + // Whether the query calls a function by a bare name. `read` runs one catalog query on the connection. + public func callsFunction( + _ reading: SQLBareNameReading, + connectionId: UUID, + schema: String?, + read: Reader + ) async throws -> Bool { + if reading.callsOutright { return true } + if reading.namesNothing { return false } + let key = Key(connectionId: connectionId, schema: schema) + let catalog = try await catalog(for: key, read: read) + let known = try await columns(of: reading.tablesToResolve(catalog), for: key, read: read) + return reading.callsFunction(catalog, columns: known) + } + + private func catalog(for key: Key, read: Reader) async throws -> SQLCallableCatalog { + if let cached = catalogs[key], cached.age() < lifetime { + return cached.value + } + let age = stopwatch() + let catalog = SQLCallableCatalog(rows: try await read(SQLCallableCatalog.query)) + catalogs[key] = Entry(value: catalog, age: age) + return catalog + } + + private func columns( + of tables: Set, + for key: Key, + read: Reader + ) async throws -> [SQLTableName: Set] { + guard !tables.isEmpty else { return [:] } + var known = (columns[key] ?? [:]).filter { $0.value.age() < lifetime } + let missing = tables.filter { known[$0] == nil }.sorted { ($0.owner ?? "", $0.name) < ($1.owner ?? "", $1.name) } + if !missing.isEmpty, let query = SQLCallableCatalog.columnsQuery(for: missing) { + let age = stopwatch() + let resolved = SQLCallableCatalog.columns(fromRows: try await read(query), tables: missing) + for table in missing { + // A table the catalog does not resolve, or one behind a database link, is kept with no columns. + known[table] = Entry(value: resolved[table] ?? [], age: age) + } + } + columns[key] = known + return tables.reduce(into: [:]) { result, table in + if let entry = known[table] { result[table] = entry.value } + } + } +} diff --git a/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLReadOnlyFunctionCalls.swift b/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLReadOnlyFunctionCalls.swift index 1264a0ed5f..55b33b16de 100644 --- a/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLReadOnlyFunctionCalls.swift +++ b/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLReadOnlyFunctionCalls.swift @@ -7,13 +7,14 @@ import Foundation // which Oracle and Dameng accept for a function with no arguments, cannot be told from a column and is not seen. public enum SQLReadOnlyFunctionCalls { // An autonomous-transaction function on Oracle and Dameng, a stored function on MySQL, MariaDB and OceanBase, PL/pgSQL - // on the PostgreSQL family. TiDB, StarRocks and Databend have no stored function or UDF that writes; SQL Server, + // on the PostgreSQL family, a `fn::` body on SurrealDB. 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. public static let queryFunctionsCanWrite: Set = [ "Oracle", "Dameng", "MySQL", "MariaDB", "OceanBase", "PostgreSQL", "CockroachDB", "PGlite", "KingbaseES", + "SurrealDB", ] static let commonBuiltins: Set = [ @@ -179,9 +180,14 @@ public enum SQLReadOnlyFunctionCalls { ) -> Bool { let typeNames = Set(typeNames.map { $0.uppercased() }) let known = builtins(forDatabaseTypeId: databaseTypeId) + let tiering = SQLStatementTiering(databaseTypeId: databaseTypeId, readings: readings, request: nil) return readings.all.contains { grammar in SQLStatementClassifier.statements(of: sql, grammar: grammar).contains { statement in - statementCallsUnlistedFunction(statement, grammar: grammar, builtins: known, typeNames: typeNames) + // SurrealQL's built-ins are namespaced, `time::now()`; only a stored body can write. + if databaseTypeId == SQLStatementTiering.surrealDB { + return tiering.callsSurrealStoredCode(StatementCode(statement, grammar: grammar)) + } + return statementCallsUnlistedFunction(statement, grammar: grammar, builtins: known, typeNames: typeNames) } } } diff --git a/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLStatementClassifier+DataStatements.swift b/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLStatementClassifier+DataStatements.swift index ff235e5336..1d74f1ee71 100644 --- a/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLStatementClassifier+DataStatements.swift +++ b/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLStatementClassifier+DataStatements.swift @@ -6,15 +6,19 @@ public extension SQLStatementClassifier { // Whether every statement `sql` runs, under every way the engine could lex it, only reads or changes rows. A // request form `requestIsData` cannot read, but `request` tiers, is refused. + // `rowChanges` replaces the SQL words that change rows on an engine whose language reads as SQL but differs there: + // SurrealQL's CREATE and RELATE make records. static func isDataStatement( _ sql: String, databaseTypeId: String, readings: SQLLexicalReadings, request: RequestClassification? = nil, - requestIsData: RequestDataStatement? = nil + requestIsData: RequestDataStatement? = nil, + rowChanges: Set? = nil ) -> Bool { guard let lexings = readings.lexings(of: sql) else { return false } - let tiering = SQLStatementTiering(databaseTypeId: databaseTypeId, readings: readings, request: request) + var tiering = SQLStatementTiering(databaseTypeId: databaseTypeId, readings: readings, request: request) + tiering.rowChanges = rowChanges var seen: Set = [] return lexings.allSatisfy { lexing in runnableStatements(of: lexing.text, grammar: lexing.grammar).allSatisfy { statement in @@ -64,10 +68,18 @@ extension SQLStatementTiering { case "Oracle": return ["SYS", "SYSTEM"] case "Dameng": return ["SYS"] case "ClickHouse": return ["SYSTEM"] + // A row change there passes the server's MODIFY check: `UPDATE system_auth.roles SET is_superuser = true`, + // ScyllaDB's `system.roles` and its live `system.config`, Cassandra's writable `system_views` cache tables. + case "Cassandra", "ScyllaDB": return cassandraSystemKeyspaces default: return [] } } + static let cassandraSystemKeyspaces: Set = [ + "SYSTEM", "SYSTEM_SCHEMA", "SYSTEM_AUTH", "SYSTEM_DISTRIBUTED", "SYSTEM_DISTRIBUTED_EVERYWHERE", + "SYSTEM_TRACES", "SYSTEM_VIEWS", "SYSTEM_VIRTUAL_SCHEMA", "SYSTEM_REPLICATED_KEYS", + ] + // A ClickHouse ALTER action that is not a row change, after a comma at the statement's depth. static let clickHouseAlterActions: Set = [ "ADD", "DROP", "MODIFY", "RENAME", "CLEAR", "COMMENT", "MATERIALIZE", "FREEZE", "UNFREEZE", "ATTACH", "DETACH", @@ -75,6 +87,8 @@ extension SQLStatementTiering { ] func isDataStatement(_ statement: String, grammar: SQLLexicalGrammar) -> Bool { + // A SurrealQL stored body tiers as a read, as any unlisted call does, but what it runs is not read. + if callsSurrealStoredCode(StatementCode(statement, grammar: grammar)) { return false } if statementClassification(statement, grammar: grammar).tier == .safe { return true } if let batch = CQLBatch.statements(in: statement, grammar: grammar) { return batch.allSatisfy { isDataStatement($0, grammar: grammar) } @@ -138,7 +152,7 @@ extension SQLStatementTiering { } else if keyword == "ALTER" { changesRows = databaseTypeId == Self.clickHouse && Self.changesClickHouseRows(code) } else { - changesRows = Self.rowChangeKeywords.contains(keyword) + changesRows = (rowChanges ?? Self.rowChangeKeywords).contains(keyword) && !Self.updatesStatistics(code) && !Self.insertsProcedureResults(code) } return changesRows && !namesSystemRelation(code, grammar: grammar) diff --git a/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLStatementClassifier+Surfaces.swift b/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLStatementClassifier+Surfaces.swift index dbb2cd9348..3535708a15 100644 --- a/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLStatementClassifier+Surfaces.swift +++ b/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLStatementClassifier+Surfaces.swift @@ -139,6 +139,18 @@ extension SQLStatementTiering { } } + // SurrealQL's fn:: and api:: calls run a body the database stores, which can define, grant or write, and file:: + // reads and writes a bucket. Measured on 2.7.0 and 3.3.5: `SELECT fn::mk()` ran the DEFINE TABLE in its body. + func callsSurrealStoredCode(_ code: StatementCode) -> Bool { + guard databaseTypeId == Self.surrealDB else { return false } + return code.tokens.indices.contains { index in + code.tokens[index].word.map(Self.surrealStoredCodeNamespaces.contains) == true + && code.isSymbol(Self.colon, at: index + 1) && code.isSymbol(Self.colon, at: index + 2) + } + } + + private static let surrealStoredCodeNamespaces: Set = ["FN", "API", "FILE"] + // KingbaseES has it in Oracle mode only, and its type id cannot say which mode a server runs. static let enginesWithSequencePseudoColumns: Set = [ "Oracle", "SAP HANA", "Snowflake", "Dameng", "MariaDB", "KingbaseES", diff --git a/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLStatementClassifier.swift b/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLStatementClassifier.swift index 2a220e964c..59527cd0e1 100644 --- a/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLStatementClassifier.swift +++ b/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLStatementClassifier.swift @@ -121,6 +121,7 @@ struct SQLStatementTiering { let databaseTypeId: String let readings: SQLLexicalReadings let request: SQLStatementClassifier.RequestClassification? + var rowChanges: Set? // Each nested statement copies and reads what is left of the text, so nesting past this is refused. static let deepestNesting = 32 diff --git a/apple/Packages/TableProCore/Tests/TableProSQLGrammarTests/MongoStatementParserTests.swift b/apple/Packages/TableProCore/Tests/TableProSQLGrammarTests/MongoStatementParserTests.swift new file mode 100644 index 0000000000..d9cee9bd79 --- /dev/null +++ b/apple/Packages/TableProCore/Tests/TableProSQLGrammarTests/MongoStatementParserTests.swift @@ -0,0 +1,82 @@ +import Foundation +import TableProSQLGrammar +import Testing + +struct MongoStatementParserTests { + private func calls(_ text: String) -> [MongoStatement.Call] { + MongoStatementParser.parse(text) { _ in false }?.calls ?? [] + } + + @Test("A collection call records the collection it reaches and the database the text names", arguments: [ + ("db.orders.insertOne({a: 1})", nil, "orders"), + (#"db["user-events"].find({})"#, nil, "user-events"), + (#"db.getCollection("system.js").insertOne({})"#, nil, "system.js"), + (#"db.getSiblingDB("shop").orders.updateOne({}, {$set: {a: 1}})"#, "shop", "orders"), + (#"db.getMongo().getDB("shop").getCollection("orders").deleteOne({_id: 1})"#, "shop", "orders"), + (#"db.getSiblingDB("admin").getSiblingDB("config").settings.updateOne({}, {$set: {a: 1}})"#, "config", "settings") + ] as [(String, String?, String?)]) + func collectionTargets(text: String, database: String?, collection: String?) throws { + let call = try #require(calls(text).last) + #expect(call.receiver == .collection) + #expect(call.database == database) + #expect(call.collection == collection) + #expect(call.commandName == nil) + } + + @Test("A cursor or an explain keeps the database and collection it came from") + func chainedReceivers() { + let found = calls(#"db.getSiblingDB("shop").orders.find({}).limit(1)"#) + #expect(found.map { $0.receiver } == [MongoStatement.Receiver.collection, .cursor]) + #expect(found.map { $0.database } == ["shop", "shop"]) + #expect(found.map { $0.collection } == ["orders", "orders"]) + #expect(calls("db.orders.explain().find({})").map { $0.receiver } == [MongoStatement.Receiver.collection, .explainer]) + } + + @Test("A database call reaches no collection", arguments: ["db.stats()", #"db.createCollection("orders")"#, "db.killOp(1)"]) + func databaseCallsReachNoCollection(text: String) throws { + let call = try #require(calls(text).first) + #expect(call.receiver == .database) + #expect(call.collection == nil) + #expect(call.database == nil) + } + + @Test("A command's collection is its document's first value, and its database the call's or the document's $db", arguments: [ + (#"db.runCommand({insert: "orders", documents: []})"#, nil, "orders"), + (#"db.adminCommand({insert: "system.version", documents: []})"#, "admin", "system.version"), + (#"db.getSiblingDB("shop").runCommand({update: "orders", updates: []})"#, "shop", "orders"), + (#"db.runCommand({update: "settings", updates: [], $db: "config"})"#, "config", "settings"), + (#"db.adminCommand({insert: "orders", "$db": "shop"})"#, "shop", "orders"), + (#"db.runCommand({insert: "orders", $db: "shop", $db: "admin"})"#, "admin", "orders"), + (#"db.runCommand({insert: "orders", documents: [], insert: "system.js"})"#, nil, "system.js"), + (#"db.runCommand({insert: "orders", insert: 1})"#, nil, nil), + ("db.runCommand({insert: 1})", nil, nil), + (#"db.runCommand({insert: {name: "orders"}})"#, nil, nil) + ] as [(String, String?, String?)]) + func commandTargets(text: String, database: String?, collection: String?) throws { + let call = try #require(calls(text).first) + #expect(call.receiver == .database) + #expect(call.database == database) + #expect(call.collection == collection) + } + + @Test("A command is named by its document's first key, and a document-less call names none") + func commandNames() { + #expect(calls(#"db.runCommand({findAndModify: "orders", query: {}})"#).first?.commandName == "findAndModify") + #expect(calls(#"db.runCommand({a: {toEJSON: MinKey}, insert: "orders"})"#).first?.commandName == "a") + #expect(calls(#"db.runCommand("insert")"#).first?.commandName == nil) + #expect(calls(#"db.runCommand("insert")"#).first?.collection == nil) + } + + @Test("use and show are shell calls that read, and use names the database it moves to") + func shellCommands() throws { + let use = try #require(calls("use admin").first) + #expect(use.name == "use") + #expect(use.receiver == .shell) + #expect(use.isRead) + #expect(use.database == "admin") + let show = try #require(calls("show collections").first) + #expect(show.name == "show") + #expect(show.receiver == .shell) + #expect(show.database == nil) + } +} diff --git a/apple/Packages/TableProCore/Tests/TableProSQLGrammarTests/SQLBareNameCallsTests.swift b/apple/Packages/TableProCore/Tests/TableProSQLGrammarTests/SQLBareNameCallsTests.swift new file mode 100644 index 0000000000..7635d9b244 --- /dev/null +++ b/apple/Packages/TableProCore/Tests/TableProSQLGrammarTests/SQLBareNameCallsTests.swift @@ -0,0 +1,177 @@ +import Foundation +import TableProSQLGrammar +import Testing + +// The cases are the ones measured on Oracle 23ai and DM8: a function PURGE and WIPE, a package PK with F, a table +// ORDERS(ID, PURGE). Each answer is whether the engine ran a function. +struct SQLBareNameCallsTests { + private static let orders = SQLTableName(owner: nil, name: "ORDERS") + private static let dual = SQLTableName(owner: nil, name: "DUAL") + private static let ownedOrders = SQLTableName(owner: "PROBE", name: "ORDERS") + private static let catalog = SQLCallableCatalog(functions: ["PURGE", "WIPE"], roots: ["PK", "PROBE"]) + private static let columns: [SQLTableName: Set] = [ + orders: ["ID", "PURGE"], dual: ["DUMMY"], ownedOrders: ["ID", "PURGE"], + ] + + private static func reading(_ sql: String, _ engine: String = "Oracle") -> SQLBareNameReading { + let readings = SQLLexicalReadings.resolve(databaseTypeId: engine, declared: nil, session: nil) + return SQLBareNameCalls.read(sql, databaseTypeId: engine, readings: readings) + } + + private static func calls(_ sql: String, _ engine: String = "Oracle", catalog: SQLCallableCatalog = catalog) -> Bool { + reading(sql, engine).callsFunction(catalog, columns: columns) + } + + @Test("A name no table in scope has as a column calls the function", arguments: [ + "SELECT purge FROM dual", + "SELECT wipe FROM orders", + "SELECT * FROM orders, (SELECT purge FROM dual)", + "SELECT * FROM orders o JOIN (SELECT purge p FROM dual) d ON 1 = 1", + "SELECT 1 AS wipe FROM dual WHERE wipe = 7", + "SELECT 1 wipe FROM dual GROUP BY wipe", + "SELECT COUNT(*) FROM orders WHERE wipe = 7", + "SELECT \"WIPE\" FROM dual", + "SELECT * FROM orders WHERE id = 1 ORDER BY wipe", + "SELECT id, purge FROM orders UNION ALL SELECT 1, purge FROM dual", + "SELECT * FROM orders WHERE purge BETWEEN wipe AND 1000", + "SELECT CASE WHEN wipe = 7 THEN 1 END FROM dual", + "SELECT t.* FROM (SELECT id x FROM orders) t WHERE wipe = 7", + "SELECT wipe FROM orders PIVOT (SUM(purge) FOR id IN (1 AS a))", + "SELECT id FROM orders o WHERE EXISTS (SELECT 1 FROM orders i WHERE i.id = o.id AND wipe = 7)", + "SELECT id FROM orders\nSELECT purge FROM dual", + "SELECT 1 FROM dual\nWITH c AS (SELECT purge FROM dual) SELECT * FROM c", + "EXPLAIN PLAN FOR SELECT 1 FROM dual\nSELECT purge FROM dual", + "SELECT wipe", + ]) + func bareCalls(sql: String) { + #expect(Self.calls(sql)) + #expect(Self.calls(sql, "Dameng")) + } + + @Test("A column, an alias ORDER BY sees and a name the query defines call nothing", arguments: [ + "SELECT purge FROM orders", + "SELECT o.purge FROM orders o", + "SELECT id FROM orders WHERE EXISTS (SELECT 1 FROM dual WHERE purge = 100)", + "SELECT * FROM orders o, LATERAL (SELECT purge FROM dual)", + "SELECT (SELECT purge FROM dual) FROM orders", + "SELECT 1 AS wipe FROM dual ORDER BY wipe", + "SELECT purge p FROM orders ORDER BY p", + "WITH c AS (SELECT 1 wipe FROM dual) SELECT wipe FROM c", + "WITH c (wipe) AS (SELECT 1 FROM dual) SELECT wipe FROM c", + "SELECT purge FROM (SELECT * FROM orders)", + "SELECT * FROM (SELECT purge FROM orders) WHERE purge > 0", + "SELECT probe.orders.purge FROM probe.orders", + "SELECT \"wipe\" FROM dual", + "SELECT id wipe FROM orders", + "SELECT id FROM orders WHERE id = :wipe", + "SELECT seq.NEXTVAL FROM dual", + "SELECT LISTAGG(id, ',') WITHIN GROUP (ORDER BY id) FROM orders", + "SELECT * FROM wipe", + "SELECT CAST(id AS wipe) FROM orders", + "SELECT id FROM orders o WHERE o.id IN (SELECT id FROM orders o2 WHERE o2.purge = o.purge)", + "SELECT 1 wipe FROM dual UNION SELECT 2 FROM dual ORDER BY wipe", + "EXPLAIN PLAN FOR SELECT purge FROM dual", + ]) + func noCalls(sql: String) { + #expect(!Self.calls(sql)) + } + + @Test("A dotted name no table in scope starts calls without the catalog", arguments: [ + "SELECT pk.f FROM dual", + "SELECT probe.wipe FROM dual", + "SELECT probe.pk.f FROM dual", + "SELECT * FROM orders WHERE id IN (SELECT pk.f FROM dual)", + "SELECT wipe@remote FROM dual", + "WITH FUNCTION f RETURN NUMBER IS BEGIN RETURN 1; END; SELECT f FROM dual", + ]) + func dottedCalls(sql: String) { + let reading = Self.reading(sql) + #expect(reading.callsOutright) + #expect(reading.callsFunction(SQLCallableCatalog(functions: [], roots: []), columns: [:])) + } + + @Test("Dameng calls package PK through an alias whose table has no such column") + func aliasFallsToPackage() { + #expect(Self.calls("SELECT pk.f FROM orders pk", "Dameng")) + #expect(!Self.calls("SELECT pk.id FROM orders pk", "Dameng")) + #expect(!Self.calls("SELECT pk.f FROM orders pk", "Dameng", catalog: SQLCallableCatalog(functions: [], roots: []))) + } + + @Test("A VALUES row, a MATCH_RECOGNIZE condition and a PARTITION FOR value are read") + func rowValuesAndConditions() { + #expect(Self.calls("SELECT * FROM (VALUES (wipe)) t (a)")) + #expect(Self.calls("SELECT * FROM orders, (VALUES (purge)) v (a)")) + #expect(!Self.calls("SELECT a FROM (VALUES (1)) t (a)")) + #expect(Self.calls("SELECT * FROM orders MATCH_RECOGNIZE (PATTERN (x) DEFINE x AS wipe = 1)")) + #expect(!Self.calls("SELECT * FROM orders MATCH_RECOGNIZE (PATTERN (x) DEFINE x AS purge = 100)")) + #expect(Self.calls("SELECT * FROM orders, dual PIVOT (MAX(purge) FOR dummy IN ('X' AS x))")) + #expect(!Self.calls("SELECT * FROM orders, dual PIVOT (MAX(dummy) FOR dummy IN ('X' AS x)) WHERE purge = 1")) + #expect(Self.calls("SELECT * FROM orders PARTITION FOR (wipe)")) + } + + @Test("Dameng matches a function in any case; Oracle only as stored") + func damengFoldsFunctionCase() { + let lower = SQLCallableCatalog(functions: ["wipe"], roots: []) + #expect(Self.calls("SELECT wipe FROM dual", "Dameng", catalog: lower)) + #expect(!Self.calls("SELECT wipe FROM dual", "Oracle", catalog: lower)) + } + + @Test("A name holding a backslash is never written into the columns query") + func backslashNamesAreNotWritten() { + #expect(SQLCallableCatalog.columnsQuery(for: [SQLTableName(owner: nil, name: "A\\B")]) == nil) + } + + @Test("Dameng's TOP count and LIMIT do not hide a name") + func damengTopAndLimit() { + #expect(Self.calls("SELECT TOP 5 wipe FROM dual", "Dameng")) + #expect(Self.calls("SELECT TOP (5) PERCENT wipe FROM dual", "Dameng")) + #expect(!Self.calls("SELECT orders.purge FROM orders LIMIT 10", "Dameng")) + } + + @Test("Only a name the catalog holds asks for columns") + func columnsOnlyOnAHit() { + let reading = Self.reading("SELECT purge FROM orders") + #expect(!reading.namesNothing) + #expect(reading.tablesToResolve(Self.catalog) == [Self.orders]) + #expect(reading.tablesToResolve(SQLCallableCatalog(functions: ["OTHER"], roots: [])).isEmpty) + #expect(Self.reading("SELECT 1 FROM dual").namesNothing) + #expect(Self.reading("UPDATE orders SET purge = wipe").namesNothing) + } + + @Test("A table whose columns nobody read counts a matching name as a call") + func unknownColumnsCall() { + #expect(Self.reading("SELECT purge FROM orders").callsFunction(Self.catalog, columns: [:])) + } + + @Test("Catalog rows read into functions and roots") + func catalogRows() { + let catalog = SQLCallableCatalog(rows: [["F", "PURGE"], ["R", "PK"], ["F", nil], ["X", "Y"], ["F"]]) + #expect(catalog == SQLCallableCatalog(functions: ["PURGE"], roots: ["PK"])) + } + + @Test("The first rank that resolves a table wins, and a remote one has no columns") + func columnRows() { + let tables = [Self.orders, Self.dual, SQLTableName(owner: nil, name: "LINKED")] + let rows: [[String?]] = [ + ["0", "3", nil, "OTHER"], + ["0", "1", nil, "ID"], + ["0", "1", nil, "PURGE"], + ["1", "3", nil, "DUMMY"], + ["2", "2", "REMOTE", nil], + ] + let columns = SQLCallableCatalog.columns(fromRows: rows, tables: tables) + #expect(columns == [Self.orders: ["ID", "PURGE"], Self.dual: ["DUMMY"]]) + } + + @Test("The columns query writes each name as a literal and asks public synonyms only for an unowned name") + func columnsQuery() throws { + let query = try #require(SQLCallableCatalog.columnsQuery(for: [ + SQLTableName(owner: nil, name: "O'NEIL"), SQLTableName(owner: "APP", name: "T"), + ])) + #expect(query.contains("'O''NEIL'")) + #expect(query.contains("o.OWNER = 'APP'")) + #expect(query.components(separatedBy: "s.OWNER = 'PUBLIC'").count == 2) + #expect(SQLCallableCatalog.columnsQuery(for: [SQLTableName(owner: nil, name: "A\u{0}B")]) == nil) + #expect(SQLCallableCatalog.columnsQuery(for: []) == nil) + } +} diff --git a/apple/Packages/TableProCore/Tests/TableProSQLGrammarTests/SQLCallableCatalogCacheTests.swift b/apple/Packages/TableProCore/Tests/TableProSQLGrammarTests/SQLCallableCatalogCacheTests.swift new file mode 100644 index 0000000000..5c2f53df50 --- /dev/null +++ b/apple/Packages/TableProCore/Tests/TableProSQLGrammarTests/SQLCallableCatalogCacheTests.swift @@ -0,0 +1,103 @@ +import Foundation +import TableProSQLGrammar +import Testing + +struct SQLCallableCatalogCacheTests { + private struct Unreachable: Error {} + + // Answers the two catalog queries the way Oracle 23ai answered them for the probe schema. + private final class CatalogServer: @unchecked Sendable { + private let lock = NSLock() + private var queries: [String] = [] + private var failing = false + + var catalogReads: Int { lock.withLock { queries.filter { $0 == SQLCallableCatalog.query }.count } } + var columnReads: Int { lock.withLock { queries.filter { $0 != SQLCallableCatalog.query }.count } } + + func fail() { lock.withLock { failing = true } } + + func read(_ query: String) throws -> [[String?]] { + let failing = lock.withLock { + queries.append(query) + return self.failing + } + if failing { throw Unreachable() } + if query == SQLCallableCatalog.query { + return [["F", "PURGE"], ["F", "WIPE"], ["R", "PK"]] + } + return [["0", "1", nil, "ID"], ["0", "1", nil, "PURGE"]] + } + } + + private static func reading(_ sql: String) -> SQLBareNameReading { + let readings = SQLLexicalReadings.resolve(databaseTypeId: "Oracle", declared: nil, session: nil) + return SQLBareNameCalls.read(sql, databaseTypeId: "Oracle", readings: readings) + } + + @Test("A column of the table runs, a function name does not, and the catalog is read once") + func columnsAndFunctions() async throws { + let server = CatalogServer() + let cache = SQLCallableCatalogCache() + let connection = UUID() + + let column = try await cache.callsFunction( + Self.reading("SELECT purge FROM orders"), connectionId: connection, schema: nil, read: server.read + ) + let call = try await cache.callsFunction( + Self.reading("SELECT wipe FROM orders"), connectionId: connection, schema: nil, read: server.read + ) + + #expect(!column) + #expect(call) + #expect(server.catalogReads == 1) + #expect(server.columnReads == 1) + } + + @Test("A query naming no function asks for no columns") + func noHitNoColumns() async throws { + let server = CatalogServer() + let calls = try await SQLCallableCatalogCache().callsFunction( + Self.reading("SELECT id FROM orders"), connectionId: UUID(), schema: nil, read: server.read + ) + + #expect(!calls) + #expect(server.columnReads == 0) + } + + @Test("The catalog is kept per schema for its lifetime, and read again after it") + func lifetimeAndSchema() async throws { + let server = CatalogServer() + let kept = SQLCallableCatalogCache(lifetime: .seconds(3_600)) + let expired = SQLCallableCatalogCache(lifetime: .zero) + let connection = UUID() + let reading = Self.reading("SELECT id FROM orders") + + _ = try await kept.callsFunction(reading, connectionId: connection, schema: nil, read: server.read) + _ = try await kept.callsFunction(reading, connectionId: connection, schema: nil, read: server.read) + #expect(server.catalogReads == 1) + _ = try await kept.callsFunction(reading, connectionId: connection, schema: "HR", read: server.read) + #expect(server.catalogReads == 2) + + _ = try await expired.callsFunction(reading, connectionId: connection, schema: nil, read: server.read) + _ = try await expired.callsFunction(reading, connectionId: connection, schema: nil, read: server.read) + #expect(server.catalogReads == 4) + } + + @Test("A dotted call is refused without reading the catalog, and a failed read throws") + func outrightAndFailure() async throws { + let server = CatalogServer() + let cache = SQLCallableCatalogCache() + + #expect(try await cache.callsFunction( + Self.reading("SELECT pk.f FROM dual"), connectionId: UUID(), schema: nil, read: server.read + )) + #expect(server.catalogReads == 0) + + server.fail() + await #expect(throws: Unreachable.self) { + try await cache.callsFunction( + Self.reading("SELECT id FROM orders"), connectionId: UUID(), schema: nil, read: server.read + ) + } + } +} diff --git a/apple/Packages/TableProCore/Tests/TableProSQLGrammarTests/SQLDataStatementTests.swift b/apple/Packages/TableProCore/Tests/TableProSQLGrammarTests/SQLDataStatementTests.swift index f58010a799..7c78e4123d 100644 --- a/apple/Packages/TableProCore/Tests/TableProSQLGrammarTests/SQLDataStatementTests.swift +++ b/apple/Packages/TableProCore/Tests/TableProSQLGrammarTests/SQLDataStatementTests.swift @@ -134,4 +134,68 @@ struct SQLDataStatementTests { requestIsData: { statement, _ in statement.hasPrefix("REQ ") ? statement.hasSuffix("PutItem") : nil } )) } + + private static let surrealRowChanges: Set = ["CREATE", "INSERT", "UPDATE", "UPSERT", "DELETE", "RELATE"] + + private static func isSurrealData(_ sql: String) -> Bool { + let readings = SQLLexicalReadings.resolve(databaseTypeId: "SurrealDB", declared: nil, session: nil) + return SQLStatementClassifier.isDataStatement( + sql, databaseTypeId: "SurrealDB", readings: readings, rowChanges: surrealRowChanges + ) + } + + // Measured on SurrealDB 2.7.0 and 3.3.5. + @Test("SurrealQL record writes are data", arguments: [ + "SELECT * FROM person LIMIT 10", + "CREATE person:one SET name = 'a'", + "CREATE person CONTENT { name: 'a' }", + "INSERT INTO person { name: 'b' }", + "RELATE person:one->knows->person:two", + "UPDATE person:one SET name = 'c'", + "UPSERT person:three SET name = 'd'", + "DELETE person:three", + "INFO FOR DB", + "SELECT * FROM person WHERE seen = time::now()", + "SELECT sleep FROM person", + ]) + func surrealRecordWritesAreData(sql: String) { + #expect(Self.isSurrealData(sql)) + } + + @Test("SurrealQL definitions, sessions, transactions, nested statements and stored bodies are not", arguments: [ + "DEFINE TABLE person SCHEMAFULL", + "DEFINE USER bob ON DATABASE PASSWORD 'x' ROLES OWNER", + "ACCESS acc ON DATABASE GRANT FOR USER bob", + "ALTER TABLE person COMMENT 'x'", + "REBUILD INDEX i ON person", + "USE NS a DB b", + "LET $x = 1", + "BEGIN TRANSACTION", + "OPTION IMPORT", + "SLEEP 10s", + "RETURN (DEFINE USER x ON DATABASE PASSWORD 'p' ROLES OWNER)", + "SELECT * FROM person WHERE name = (DEFINE TABLE t)", + "SELECT fn::mk() FROM ONLY person:one", + "RETURN api::invoke('/x')", + ]) + func surrealDefinitionsAreNot(sql: String) { + #expect(!Self.isSurrealData(sql)) + } + + @Test("A CQL row change in a system keyspace is not data, and a read of one is", arguments: ["Cassandra", "ScyllaDB"]) + func cassandraSystemKeyspaces(engine: String) { + for sql in [ + "UPDATE system_auth.roles SET is_superuser = true WHERE role = 'app'", + "INSERT INTO system_distributed.partition_denylist (ks_name, table_name, key) VALUES ('ks', 't', 0x00)", + "DELETE FROM system_views.roles_cache_keys WHERE role = 'app'", + "UPDATE system.config SET value = 'false' WHERE name = 'enable_cql_config_updates'", + "BEGIN BATCH UPDATE system_auth.roles SET can_login = true WHERE role = 'x'; APPLY BATCH", + ] { + #expect(!Self.isData(sql, engine)) + } + #expect(Self.isData("SELECT * FROM system_auth.roles", engine)) + #expect(Self.isData("UPDATE ks.users SET name = 'b' WHERE id = 1", engine)) + #expect(SQLStatementClassifier.isSystemScope(database: "system_auth", schema: nil, databaseTypeId: engine)) + #expect(!SQLStatementClassifier.isSystemScope(database: "shop", schema: nil, databaseTypeId: engine)) + } } diff --git a/apple/Packages/TableProCore/Tests/TableProSQLGrammarTests/SQLReadOnlyFunctionCallsTests.swift b/apple/Packages/TableProCore/Tests/TableProSQLGrammarTests/SQLReadOnlyFunctionCallsTests.swift index 2a39159880..ca24b57fd7 100644 --- a/apple/Packages/TableProCore/Tests/TableProSQLGrammarTests/SQLReadOnlyFunctionCallsTests.swift +++ b/apple/Packages/TableProCore/Tests/TableProSQLGrammarTests/SQLReadOnlyFunctionCallsTests.swift @@ -30,8 +30,16 @@ struct SQLReadOnlyFunctionCallsTests { func writingEnginesAreTypeIds() { #expect(SQLReadOnlyFunctionCalls.queryFunctionsCanWrite == [ "Oracle", "Dameng", "MySQL", "MariaDB", "OceanBase", "PostgreSQL", "CockroachDB", "PGlite", "KingbaseES", + "SurrealDB", ]) #expect(SQLReadOnlyFunctionCalls.builtins(forDatabaseTypeId: "Oracle").contains("NVL")) #expect(!SQLReadOnlyFunctionCalls.builtins(forDatabaseTypeId: "MySQL").contains("NVL")) } + + @Test("On SurrealDB only a stored body is unlisted: its built-ins are namespaced") + func surrealStoredBodies() { + #expect(Self.callsUnlisted("SELECT fn::mk() FROM ONLY person:one", "SurrealDB")) + #expect(Self.callsUnlisted("RETURN api::invoke('/x')", "SurrealDB")) + #expect(!Self.callsUnlisted("SELECT time::now(), string::lowercase(name), count() FROM person", "SurrealDB")) + } } diff --git a/apple/TablePro/Core/AI/Chat/Tools/ExecuteQueryChatTool.swift b/apple/TablePro/Core/AI/Chat/Tools/ExecuteQueryChatTool.swift index d2a5ee1185..6aceb67145 100644 --- a/apple/TablePro/Core/AI/Chat/Tools/ExecuteQueryChatTool.swift +++ b/apple/TablePro/Core/AI/Chat/Tools/ExecuteQueryChatTool.swift @@ -101,8 +101,8 @@ struct ExecuteQueryChatTool: ChatTool { ) // A table named without a schema lands in the scope's, and the engine's own schemas hold its grant, setup and // settings tables. - if context.chatMode != .agent, classification.tier != .safe, SQLStatementClassifier.isSystemScope( - database: scope.database, schema: scope.schema, databaseTypeId: meta.databaseType.rawValue + if context.chatMode != .agent, classification.tier != .safe, QueryClassifier.writesSystemScope( + database: scope.database, schema: scope.schema, databaseType: meta.databaseType ) { return ChatToolResult(content: ExternalStatementGate.dataStatementRefusal, isError: true) } diff --git a/apple/TablePro/Core/Compare/CompareRowService.swift b/apple/TablePro/Core/Compare/CompareRowService.swift index 355ce1ddcc..6244a89580 100644 --- a/apple/TablePro/Core/Compare/CompareRowService.swift +++ b/apple/TablePro/Core/Compare/CompareRowService.swift @@ -153,7 +153,8 @@ internal struct CompareRowService { databaseType: endpoint.databaseType, gate: gate, operationDescription: String(localized: "Execute Query"), - manager: manager + manager: manager, + runsIn: endpoint.scope ) } diff --git a/apple/TablePro/Core/Coordinators/PaginationCoordinator.swift b/apple/TablePro/Core/Coordinators/PaginationCoordinator.swift index 5d1dbde6ac..7836d93b37 100644 --- a/apple/TablePro/Core/Coordinators/PaginationCoordinator.swift +++ b/apple/TablePro/Core/Coordinators/PaginationCoordinator.swift @@ -174,7 +174,7 @@ final class PaginationCoordinator: ObservableObject { ) let safeModeIsReadOnly = parent.runsAtReadOnly let gate = browseFilters == nil && parent.rawFilterNeedsReadOnlyGate(filters) - ? countSQL.map { parent.rawFilterRequest(sql: $0, canPrompt: true) } + ? countSQL.map { parent.rawFilterRequest(sql: $0, canPrompt: true, scope: scope) } : nil // Claims the superseded automatic count's flag too: its task finds the token changed and won't clear a successor's state. diff --git a/apple/TablePro/Core/Coordinators/QueryExecutionCoordinator+Helpers.swift b/apple/TablePro/Core/Coordinators/QueryExecutionCoordinator+Helpers.swift index 55bb572c96..ecad7c5c02 100644 --- a/apple/TablePro/Core/Coordinators/QueryExecutionCoordinator+Helpers.swift +++ b/apple/TablePro/Core/Coordinators/QueryExecutionCoordinator+Helpers.swift @@ -768,7 +768,7 @@ extension QueryExecutionCoordinator { logicMode: logicMode, safeModeIsReadOnly: parent.runsAtReadOnly, gate: parent.rawFilterNeedsReadOnlyGate(filters) - ? sql.map { parent.rawFilterRequest(sql: $0, canPrompt: false) } + ? sql.map { parent.rawFilterRequest(sql: $0, canPrompt: false, scope: scope) } : nil ) return (plan, input, scope) diff --git a/apple/TablePro/Core/Coordinators/QueryExecutionCoordinator.swift b/apple/TablePro/Core/Coordinators/QueryExecutionCoordinator.swift index 69afa07217..243da63feb 100644 --- a/apple/TablePro/Core/Coordinators/QueryExecutionCoordinator.swift +++ b/apple/TablePro/Core/Coordinators/QueryExecutionCoordinator.swift @@ -75,6 +75,7 @@ final class QueryExecutionCoordinator: ObservableObject { parent.isShowingSafeModePrompt = true let request = makeExecuteRequest( statements: batches.flatMap(\.statements), + tabIndex: index, extraCapabilities: extraCapabilities ) Task { [parent] in @@ -132,9 +133,11 @@ final class QueryExecutionCoordinator: ObservableObject { private func makeExecuteRequest( statements: [SQLStatementScanner.ExecutableStatement], + tabIndex index: Int, extraCapabilities: CallerCapabilities = [] ) -> OperationRequest { let sql = statements.map(\.sql) + let tabs = parent.tabManager.tabs return OperationRequest( connectionId: parent.connectionId, databaseType: parent.connection.type, @@ -142,7 +145,8 @@ final class QueryExecutionCoordinator: ObservableObject { kind: OperationKind.worst(of: sql, databaseType: parent.connection.type), caller: .userInterface, capabilities: CallerCapabilities.interactiveUser.union(extraCapabilities), - operationDescription: String(localized: "Execute Query") + operationDescription: String(localized: "Execute Query"), + runsIn: tabs.indices.contains(index) ? parent.scope(for: tabs[index]) : nil ) } @@ -159,6 +163,7 @@ final class QueryExecutionCoordinator: ObservableObject { parent.isShowingSafeModePrompt = true let request = makeExecuteRequest( statements: batches.flatMap(\.statements), + tabIndex: index, extraCapabilities: extraCapabilities ) Task { [parent] in diff --git a/apple/TablePro/Core/ObjectCopy/ObjectCopyRunner.swift b/apple/TablePro/Core/ObjectCopy/ObjectCopyRunner.swift index e5e5c79c14..78076942f4 100644 --- a/apple/TablePro/Core/ObjectCopy/ObjectCopyRunner.swift +++ b/apple/TablePro/Core/ObjectCopy/ObjectCopyRunner.swift @@ -139,7 +139,8 @@ internal struct ObjectCopyRunner { connectionId: request.source.connectionId, databaseType: request.source.databaseType, gate: gate, - operationDescription: String(localized: "Execute Query") + operationDescription: String(localized: "Execute Query"), + runsIn: request.source.scope ) return try await gate.authorizing(authorizationRequest(for: plan)) { diff --git a/apple/TablePro/Core/Plugins/PluginMetadataRegistry+BigtableDefaults.swift b/apple/TablePro/Core/Plugins/PluginMetadataRegistry+BigtableDefaults.swift index 3e10fedf55..87a4fba493 100644 --- a/apple/TablePro/Core/Plugins/PluginMetadataRegistry+BigtableDefaults.swift +++ b/apple/TablePro/Core/Plugins/PluginMetadataRegistry+BigtableDefaults.swift @@ -39,7 +39,8 @@ extension PluginMetadataRegistry { exactRowCountIsFullScan: true, supportsColumnSort: false, columnsAreSampled: true, - supportsPrincipalConnectionLimit: false + supportsPrincipalConnectionLimit: false, + dataCommands: bigtableDataCommands ), schema: PluginMetadataSnapshot.SchemaInfo( defaultSchemaName: "", @@ -71,6 +72,9 @@ extension PluginMetadataRegistry { } } +// The driver's writes, each one MutateRow on the Bigtable Data API v2. +private let bigtableDataCommands: Set = ["INSERT", "PUT", "DELETE"] + private let bigtableCompletions: [CompletionEntry] = [ CompletionEntry(label: "GET row", insertText: "GET events/user#42"), CompletionEntry(label: "SCAN prefix", insertText: "SCAN events/user#?limit=20"), diff --git a/apple/TablePro/Core/Plugins/PluginMetadataRegistry+CommandEngineDataCommands.swift b/apple/TablePro/Core/Plugins/PluginMetadataRegistry+CommandEngineDataCommands.swift new file mode 100644 index 0000000000..65c8788f4c --- /dev/null +++ b/apple/TablePro/Core/Plugins/PluginMetadataRegistry+CommandEngineDataCommands.swift @@ -0,0 +1,45 @@ +// +// PluginMetadataRegistry+CommandEngineDataCommands.swift +// TablePro +// + +import Foundation + +extension PluginMetadataRegistry { + // Redis 8.10.3 `COMMAND DOCS` with its modules (search, timeseries, bf, vectorset, ReJSON). A container command is + // named `CONTAINER|SUB`. Keys only: no index, alias, rule, user, function, session or server state. + static let redisDataCommands: Set = [ + "APPEND", "DECR", "DECRBY", "DELEX", "GETDEL", "GETEX", "GETSET", "INCR", "INCRBY", "INCRBYFLOAT", "INCREX", + "MSET", "MSETEX", "MSETNX", "PSETEX", "SET", "SETEX", "SETNX", "SETRANGE", + "COPY", "DEL", "EXPIRE", "EXPIREAT", "MOVE", "PERSIST", "PEXPIRE", "PEXPIREAT", "RENAME", "RENAMENX", + "RESTORE", "SORT", "UNLINK", + "HDEL", "HEXPIRE", "HEXPIREAT", "HGETDEL", "HGETEX", "HINCRBY", "HINCRBYFLOAT", "HMSET", "HPERSIST", + "HPEXPIRE", "HPEXPIREAT", "HSET", "HSETEX", "HSETNX", + "LINSERT", "LMOVE", "LMOVEM", "LMPOP", "LPOP", "LPUSH", "LPUSHX", "LREM", "LSET", "LTRIM", "RPOP", + "RPOPLPUSH", "RPUSH", "RPUSHX", "BLMOVE", "BLMOVEM", "BLMPOP", "BLPOP", "BRPOP", "BRPOPLPUSH", + "SADD", "SDIFFSTORE", "SINTERSTORE", "SMOVE", "SPOP", "SREM", "SUNIONSTORE", + "ZADD", "ZDIFFSTORE", "ZINCRBY", "ZINTERSTORE", "ZMPOP", "ZPOPMAX", "ZPOPMIN", "ZRANGESTORE", "ZREM", + "ZREMRANGEBYLEX", "ZREMRANGEBYRANK", "ZREMRANGEBYSCORE", "ZUNIONSTORE", "BZMPOP", "BZPOPMAX", "BZPOPMIN", + "XACK", "XACKDEL", "XADD", "XAUTOCLAIM", "XCFGSET", "XCLAIM", "XDEL", "XDELEX", "XGROUP|CREATE", + "XGROUP|CREATECONSUMER", "XGROUP|DELCONSUMER", "XGROUP|DESTROY", "XGROUP|SETID", "XNACK", "XREADGROUP", + "XSETID", "XTRIM", + "BITFIELD", "BITOP", "SETBIT", "GEOADD", "GEORADIUS", "GEORADIUSBYMEMBER", "GEOSEARCHSTORE", "PFADD", + "PFMERGE", + "ARDEL", "ARDELRANGE", "ARINSERT", "ARMSET", "ARRING", "ARSEEK", "ARSET", + "JSON.ARRAPPEND", "JSON.ARRINSERT", "JSON.ARRPOP", "JSON.ARRTRIM", "JSON.CLEAR", "JSON.DEL", "JSON.FORGET", + "JSON.MERGE", "JSON.MSET", "JSON.NUMINCRBY", "JSON.NUMMULTBY", "JSON.NUMPOWBY", "JSON.SET", "JSON.STRAPPEND", + "JSON.TOGGLE", + "TS.ADD", "TS.ALTER", "TS.CREATE", "TS.DECRBY", "TS.DEL", "TS.INCRBY", "TS.MADD", + "FT.ADD", "FT.SAFEADD", "FT.DEL", "FT.SUGADD", "FT.SUGDEL", + "BF.ADD", "BF.INSERT", "BF.LOADCHUNK", "BF.MADD", "BF.RESERVE", "CF.ADD", "CF.ADDNX", "CF.COMPACT", "CF.DEL", + "CF.INSERT", "CF.INSERTNX", "CF.LOADCHUNK", "CF.RESERVE", "CMS.INCRBY", "CMS.INITBYDIM", "CMS.INITBYPROB", + "CMS.MERGE", "TDIGEST.ADD", "TDIGEST.CREATE", "TDIGEST.MERGE", "TDIGEST.RESET", "TOPK.ADD", "TOPK.INCRBY", + "TOPK.RESERVE", + "VADD", "VREM", "VSETATTR", + ] + + // etcd v3.6 API reference, in the driver's etcdctl notation. `create` and `mv` are the driver's own KV verbs. + static let etcdDataCommands: Set = [ + "put", "create", "mv", "del", "lease grant", "lease keep-alive", + ] +} diff --git a/apple/TablePro/Core/Plugins/PluginMetadataRegistry+ElasticsearchDefaults.swift b/apple/TablePro/Core/Plugins/PluginMetadataRegistry+ElasticsearchDefaults.swift index ff8979dbe2..8335a590d7 100644 --- a/apple/TablePro/Core/Plugins/PluginMetadataRegistry+ElasticsearchDefaults.swift +++ b/apple/TablePro/Core/Plugins/PluginMetadataRegistry+ElasticsearchDefaults.swift @@ -30,7 +30,8 @@ extension PluginMetadataRegistry { supportsQueryProgress: false, requiresReconnectForDatabaseSwitch: false, supportsDropDatabase: false, - supportsOpportunisticTLS: false + supportsOpportunisticTLS: false, + dataCommands: elasticsearchDataCommands ), schema: PluginMetadataSnapshot.SchemaInfo( defaultSchemaName: "", @@ -76,6 +77,18 @@ private let elasticsearchCompletions: [CompletionEntry] = [ CompletionEntry(label: "aggs", insertText: "\"aggs\": {\n \"name\": {\n \"terms\": { \"field\": \"field\" }\n }\n}"), ] +// The Document APIs of the REST API spec, 8.19: index, create, update, delete, bulk, update and delete by query. +// `_reindex` is left out: it can create its destination index and make the cluster fetch from another host. +private let elasticsearchDataCommands: Set = [ + "PUT /{target}/_doc/{id}", "POST /{target}/_doc/{id}", "POST /{target}/_doc", + "PUT /{target}/_create/{id}", "POST /{target}/_create/{id}", + "POST /{target}/_update/{id}", + "DELETE /{target}/_doc/{id}", + "POST /_bulk", "PUT /_bulk", "POST /{target}/_bulk", "PUT /{target}/_bulk", + "POST /{target}/_update_by_query", + "POST /{target}/_delete_by_query", +] + private let elasticsearchColumnTypes: [String: [String]] = [ "Text": ["text", "keyword", "match_only_text", "search_as_you_type"], "Numeric": ["long", "integer", "short", "byte", "double", "float", "half_float", "scaled_float", "unsigned_long"], diff --git a/apple/TablePro/Core/Plugins/PluginMetadataRegistry+FirebaseDefaults.swift b/apple/TablePro/Core/Plugins/PluginMetadataRegistry+FirebaseDefaults.swift index 7b1ef747a5..aac3f6a0e0 100644 --- a/apple/TablePro/Core/Plugins/PluginMetadataRegistry+FirebaseDefaults.swift +++ b/apple/TablePro/Core/Plugins/PluginMetadataRegistry+FirebaseDefaults.swift @@ -75,7 +75,8 @@ extension PluginMetadataRegistry { exactRowCountIsFullScan: realtimeDatabase, supportsMatchAnyFilters: !realtimeDatabase, columnsAreSampled: true, - supportsPrincipalConnectionLimit: false + supportsPrincipalConnectionLimit: false, + dataCommands: realtimeDatabase ? realtimeDatabaseDataCommands : firestoreDataCommands ) } @@ -104,6 +105,12 @@ extension PluginMetadataRegistry { } } +// Firestore REST v1 as the driver sends it: createDocument on a collection, patch and delete on a document. +private let firestoreDataCommands: Set = ["POST /{collection}", "PATCH /{document}", "DELETE /{document}"] + +// Realtime Database REST reference: PUT, PATCH, POST and DELETE on a node. +private let realtimeDatabaseDataCommands: Set = ["PUT /{path}", "PATCH /{path}", "POST /{path}", "DELETE /{path}"] + private let firebaseCompletions: [CompletionEntry] = [ CompletionEntry(label: "GET collection", insertText: "GET users?pageSize=20"), CompletionEntry(label: "GET document", insertText: "GET users/alice"), diff --git a/apple/TablePro/Core/Plugins/PluginMetadataRegistry+KafkaDefaults.swift b/apple/TablePro/Core/Plugins/PluginMetadataRegistry+KafkaDefaults.swift index aded48a792..57d569afc9 100644 --- a/apple/TablePro/Core/Plugins/PluginMetadataRegistry+KafkaDefaults.swift +++ b/apple/TablePro/Core/Plugins/PluginMetadataRegistry+KafkaDefaults.swift @@ -44,7 +44,8 @@ extension PluginMetadataRegistry { requestedTLSVerifiesIdentity: true, supportsColumnSort: false, supportsRawFilter: false, - statementPolicyInDriver: true + statementPolicyInDriver: true, + dataCommands: kafkaDataCommands ), schema: PluginMetadataSnapshot.SchemaInfo( defaultSchemaName: "", @@ -86,6 +87,9 @@ private let kafkaCompletions: [CompletionEntry] = [ CompletionEntry(label: "DESCRIBE TOPIC", insertText: "DESCRIBE TOPIC \"topic\"") ] +// KafkaQL (`TableProKafkaCore/KafkaQL.swift`): CONSUME, SHOW and DESCRIBE read, DROP TOPIC drops the topic. +private let kafkaDataCommands: Set = ["PRODUCE"] + private let kafkaColumnTypes: [String: [String]] = [ "Message": ["TEXT", "BLOB", "JSON"], "Coordinates": ["INTEGER", "BIGINT", "TIMESTAMP"] diff --git a/apple/TablePro/Core/Plugins/PluginMetadataRegistry+MemcachedDefaults.swift b/apple/TablePro/Core/Plugins/PluginMetadataRegistry+MemcachedDefaults.swift index c6543201af..1d58c1df50 100644 --- a/apple/TablePro/Core/Plugins/PluginMetadataRegistry+MemcachedDefaults.swift +++ b/apple/TablePro/Core/Plugins/PluginMetadataRegistry+MemcachedDefaults.swift @@ -28,7 +28,8 @@ extension PluginMetadataRegistry { supportsOpportunisticTLS: false, supportsColumnSort: false, supportsRawFilter: false, - readOnlyEnforcement: .clientGuard + readOnlyEnforcement: .clientGuard, + dataCommands: memcachedDataCommands ), schema: PluginMetadataSnapshot.SchemaInfo( defaultSchemaName: "", @@ -59,6 +60,11 @@ extension PluginMetadataRegistry { } } +// memcached `doc/protocol.txt`, the text protocol commands `MemcachedCommandParser` reads. +private let memcachedDataCommands: Set = [ + "set", "add", "replace", "append", "prepend", "cas", "touch", "incr", "decr", "gat", "gats", "delete", +] + private let memcachedCompletions: [CompletionEntry] = [ "get", "gets", "gat", "set", "add", "replace", "append", "prepend", "cas", "touch", "delete", "incr", "decr", ].map { CompletionEntry(label: $0, insertText: "\($0) ") } + [ diff --git a/apple/TablePro/Core/Plugins/PluginMetadataRegistry+MongoDBDataCommands.swift b/apple/TablePro/Core/Plugins/PluginMetadataRegistry+MongoDBDataCommands.swift new file mode 100644 index 0000000000..044700515c --- /dev/null +++ b/apple/TablePro/Core/Plugins/PluginMetadataRegistry+MongoDBDataCommands.swift @@ -0,0 +1,21 @@ +// +// PluginMetadataRegistry+MongoDBDataCommands.swift +// TablePro +// + +import Foundation + +extension PluginMetadataRegistry { + // mongosh CRUD methods the plugin's prelude defines, and the write commands of MongoDB 4.0 to 8.x + // (https://www.mongodb.com/docs/manual/reference/command/, Query and Write Operation Commands), named as + // `QueryClassifier.mongoDataCommands` names them. + static let mongoDBDataCommands: Set = [ + "db.collection.insertOne", "db.collection.insertMany", "db.collection.insert", + "db.collection.updateOne", "db.collection.updateMany", "db.collection.update", + "db.collection.replaceOne", "db.collection.save", + "db.collection.deleteOne", "db.collection.deleteMany", "db.collection.remove", + "db.collection.findOneAndUpdate", "db.collection.findOneAndReplace", "db.collection.findOneAndDelete", + "db.collection.bulkWrite", + "insert", "update", "delete", "findAndModify", "findandmodify" + ] +} diff --git a/apple/TablePro/Core/Plugins/PluginMetadataRegistry+Neo4jDefaults.swift b/apple/TablePro/Core/Plugins/PluginMetadataRegistry+Neo4jDefaults.swift index 29e51549c7..b28024673e 100644 --- a/apple/TablePro/Core/Plugins/PluginMetadataRegistry+Neo4jDefaults.swift +++ b/apple/TablePro/Core/Plugins/PluginMetadataRegistry+Neo4jDefaults.swift @@ -39,7 +39,8 @@ extension PluginMetadataRegistry { supportsDropColumn: false, supportsAddIndex: false, supportsDropIndex: false, - supportsModifyPrimaryKey: false + supportsModifyPrimaryKey: false, + dataCommands: neo4jDataCommands ), schema: PluginMetadataSnapshot.SchemaInfo( defaultSchemaName: "Labels", @@ -70,6 +71,13 @@ extension PluginMetadataRegistry { } } +// Cypher Manual clauses index (Cypher 25) and the Operations Manual's WRITE procedures, Neo4j 4.2 to 2026.x, named as +// `QueryClassifier.cypherDataCommands` names them. +private let neo4jDataCommands: Set = [ + "CREATE", "MERGE", "SET", "REMOVE", "DELETE", "DETACH DELETE", "FOREACH", "INSERT", + "db.create.setnodevectorproperty", "db.create.setrelationshipvectorproperty", "db.create.setvectorproperty" +] + private let neo4jColumnTypes: [String: [String]] = [ "Scalar": ["STRING", "INTEGER", "FLOAT", "BOOLEAN", "BYTES"], "Temporal": ["DATE", "LOCAL TIME", "ZONED TIME", "LOCAL DATETIME", "ZONED DATETIME", "DURATION"], diff --git a/apple/TablePro/Core/Plugins/PluginMetadataRegistry+QdrantDefaults.swift b/apple/TablePro/Core/Plugins/PluginMetadataRegistry+QdrantDefaults.swift index 6286e8fee2..162405890a 100644 --- a/apple/TablePro/Core/Plugins/PluginMetadataRegistry+QdrantDefaults.swift +++ b/apple/TablePro/Core/Plugins/PluginMetadataRegistry+QdrantDefaults.swift @@ -35,7 +35,8 @@ extension PluginMetadataRegistry { verifiesServerWithSystemTrust: true, requestedTLSVerifiesIdentity: true, columnsAreSampled: true, - supportsPrincipalConnectionLimit: false + supportsPrincipalConnectionLimit: false, + dataCommands: qdrantDataCommands ), schema: PluginMetadataSnapshot.SchemaInfo( defaultSchemaName: "", @@ -67,6 +68,19 @@ extension PluginMetadataRegistry { } } +// The point operations of the Qdrant OpenAPI, v1.9 to v1.20. +private let qdrantDataCommands: Set = [ + "PUT /collections/{collection_name}/points", + "POST /collections/{collection_name}/points/delete", + "POST /collections/{collection_name}/points/payload", + "PUT /collections/{collection_name}/points/payload", + "POST /collections/{collection_name}/points/payload/delete", + "POST /collections/{collection_name}/points/payload/clear", + "PUT /collections/{collection_name}/points/vectors", + "POST /collections/{collection_name}/points/vectors/delete", + "POST /collections/{collection_name}/points/batch" +] + private let qdrantCompletions: [CompletionEntry] = [ CompletionEntry(label: "GET /collections", insertText: "GET /collections"), CompletionEntry(label: "GET /collections/{name}", insertText: "GET /collections/docs"), diff --git a/apple/TablePro/Core/Plugins/PluginMetadataRegistry+RegistryDefaults.swift b/apple/TablePro/Core/Plugins/PluginMetadataRegistry+RegistryDefaults.swift index dd3765b193..8936d1b620 100644 --- a/apple/TablePro/Core/Plugins/PluginMetadataRegistry+RegistryDefaults.swift +++ b/apple/TablePro/Core/Plugins/PluginMetadataRegistry+RegistryDefaults.swift @@ -79,7 +79,8 @@ extension PluginMetadataRegistry { patternFiltersReadAnyColumn: true, filtersTypedByReadRows: true, readOnlyEnforcement: .clientGuard, - statementPolicyInDriver: true + statementPolicyInDriver: true, + dataCommands: Self.mongoDBDataCommands ), schema: PluginMetadataSnapshot.SchemaInfo( defaultSchemaName: "public", @@ -181,7 +182,8 @@ extension PluginMetadataRegistry { supportsDropDatabase: false, supportsOpportunisticTLS: false, supportsColumnSort: false, - readOnlyEnforcement: .clientGuard + readOnlyEnforcement: .clientGuard, + dataCommands: Self.redisDataCommands ), schema: PluginMetadataSnapshot.SchemaInfo( defaultSchemaName: "public", @@ -738,8 +740,8 @@ extension PluginMetadataRegistry { defaultPrimaryKeyColumn: nil, immutableColumns: [], systemDatabaseNames: [ - "system", "system_schema", "system_auth", - "system_distributed", "system_traces", "system_virtual_schema" + "system", "system_schema", "system_auth", "system_distributed", "system_distributed_everywhere", + "system_traces", "system_views", "system_virtual_schema", "system_replicated_keys" ], systemSchemaNames: [], fileExtensions: [], @@ -784,8 +786,8 @@ extension PluginMetadataRegistry { defaultPrimaryKeyColumn: nil, immutableColumns: [], systemDatabaseNames: [ - "system", "system_schema", "system_auth", - "system_distributed", "system_traces", "system_virtual_schema" + "system", "system_schema", "system_auth", "system_distributed", "system_distributed_everywhere", + "system_traces", "system_views", "system_virtual_schema", "system_replicated_keys" ], systemSchemaNames: [], fileExtensions: [], @@ -833,7 +835,8 @@ extension PluginMetadataRegistry { supportsDropDatabase: false, supportsOpportunisticTLS: false, supportsColumnSort: false, - supportsRawFilter: false + supportsRawFilter: false, + dataCommands: Self.etcdDataCommands ), schema: PluginMetadataSnapshot.SchemaInfo( defaultSchemaName: "public", diff --git a/apple/TablePro/Core/Plugins/PluginMetadataRegistry+SurrealDBDefaults.swift b/apple/TablePro/Core/Plugins/PluginMetadataRegistry+SurrealDBDefaults.swift index c0907de2e7..550e30e225 100644 --- a/apple/TablePro/Core/Plugins/PluginMetadataRegistry+SurrealDBDefaults.swift +++ b/apple/TablePro/Core/Plugins/PluginMetadataRegistry+SurrealDBDefaults.swift @@ -33,7 +33,10 @@ extension PluginMetadataRegistry { requiresReconnectForDatabaseSwitch: false, supportsDropDatabase: true, supportsDropSchema: true, - supportsOpportunisticTLS: false + supportsOpportunisticTLS: false, + // The statements that make, change and remove records (SurrealQL statements reference, 2.x and 3.x). + // CREATE makes records only: `CREATE TABLE` does not parse. + dataCommands: ["CREATE", "INSERT", "UPDATE", "UPSERT", "DELETE", "RELATE"] ), schema: PluginMetadataSnapshot.SchemaInfo( defaultSchemaName: "", diff --git a/apple/TablePro/Core/Plugins/PluginMetadataRegistry+TypesenseDefaults.swift b/apple/TablePro/Core/Plugins/PluginMetadataRegistry+TypesenseDefaults.swift index 0fbb78d139..362ef7bfcb 100644 --- a/apple/TablePro/Core/Plugins/PluginMetadataRegistry+TypesenseDefaults.swift +++ b/apple/TablePro/Core/Plugins/PluginMetadataRegistry+TypesenseDefaults.swift @@ -30,7 +30,8 @@ extension PluginMetadataRegistry { supportsQueryProgress: false, requiresReconnectForDatabaseSwitch: false, supportsDropDatabase: false, - supportsOpportunisticTLS: false + supportsOpportunisticTLS: false, + dataCommands: typesenseDataCommands ), schema: PluginMetadataSnapshot.SchemaInfo( defaultSchemaName: "", @@ -61,6 +62,16 @@ extension PluginMetadataRegistry { } } +// The document routes of the Typesense server, v26.0 to v30.2. +private let typesenseDataCommands: Set = [ + "POST /collections/{collection}/documents", + "POST /collections/{collection}/documents/import", + "PATCH /collections/{collection}/documents/{id}", + "PATCH /collections/{collection}/documents", + "DELETE /collections/{collection}/documents/{id}", + "DELETE /collections/{collection}/documents", +] + private let typesenseCompletions: [CompletionEntry] = [ CompletionEntry(label: "GET /collections", insertText: "GET /collections"), CompletionEntry(label: "GET /collections/:name", insertText: "GET /collections/my-collection"), diff --git a/apple/TablePro/Core/Plugins/PluginMetadataRegistry+WeaviateDefaults.swift b/apple/TablePro/Core/Plugins/PluginMetadataRegistry+WeaviateDefaults.swift index c6ed93f3b2..219b17658b 100644 --- a/apple/TablePro/Core/Plugins/PluginMetadataRegistry+WeaviateDefaults.swift +++ b/apple/TablePro/Core/Plugins/PluginMetadataRegistry+WeaviateDefaults.swift @@ -33,7 +33,8 @@ extension PluginMetadataRegistry { supportsModifyPrimaryKey: false, supportsOpportunisticTLS: false, supportsRawFilter: false, - supportsPrincipalConnectionLimit: false + supportsPrincipalConnectionLimit: false, + dataCommands: weaviateDataCommands ), schema: PluginMetadataSnapshot.SchemaInfo( defaultSchemaName: "", @@ -65,6 +66,21 @@ extension PluginMetadataRegistry { } } +// The object and batch operations of the Weaviate OpenAPI, v1.25.0 to v1.40.5. +private let weaviateDataCommands: Set = [ + "POST /v1/objects", + "PUT /v1/objects/{className}/{id}", "PATCH /v1/objects/{className}/{id}", "DELETE /v1/objects/{className}/{id}", + "PUT /v1/objects/{id}", "PATCH /v1/objects/{id}", "DELETE /v1/objects/{id}", + "POST /v1/objects/{className}/{id}/references/{propertyName}", + "PUT /v1/objects/{className}/{id}/references/{propertyName}", + "DELETE /v1/objects/{className}/{id}/references/{propertyName}", + "POST /v1/objects/{id}/references/{propertyName}", + "PUT /v1/objects/{id}/references/{propertyName}", + "DELETE /v1/objects/{id}/references/{propertyName}", + "POST /v1/batch/objects", "DELETE /v1/batch/objects", + "POST /v1/batch/references" +] + private let weaviateCompletions: [CompletionEntry] = [ CompletionEntry( label: "Get", diff --git a/apple/TablePro/Core/Plugins/PluginMetadataRegistry.swift b/apple/TablePro/Core/Plugins/PluginMetadataRegistry.swift index 196da8f824..77b7e765db 100644 --- a/apple/TablePro/Core/Plugins/PluginMetadataRegistry.swift +++ b/apple/TablePro/Core/Plugins/PluginMetadataRegistry.swift @@ -179,6 +179,12 @@ struct PluginMetadataSnapshot: Sendable { // cannot read its language in full. A loaded driver without `.statementPolicy` runs no external write. var statementPolicyInDriver: Bool = false + // What the assistant's Edit mode runs besides reads on an engine with a query language of its own: the commands + // that change data, from the engine's command reference, named as `QueryClassifier.dataCommands(of:)` names + // them. Edit refuses every other command. Nil on an engine whose language is SQL, which the SQL reading decides, + // and on an engine nobody curated, where Edit runs reads only. + var dataCommands: Set? + static let defaults = CapabilityFlags( supportsSchemaSwitching: false, supportsImport: true, @@ -829,7 +835,8 @@ final class PluginMetadataRegistry: @unchecked Sendable { supportsRoleLogin: existingSnapshot?.capabilities.supportsRoleLogin ?? true, binaryLengthFunction: existingSnapshot?.capabilities.binaryLengthFunction, readOnlyEnforcement: existingSnapshot?.capabilities.readOnlyEnforcement ?? .loginPrivileges, - statementPolicyInDriver: existingSnapshot?.capabilities.statementPolicyInDriver ?? false + statementPolicyInDriver: existingSnapshot?.capabilities.statementPolicyInDriver ?? false, + dataCommands: existingSnapshot?.capabilities.dataCommands ), schema: PluginMetadataSnapshot.SchemaInfo( defaultSchemaName: driverType.defaultSchemaName, diff --git a/apple/TablePro/Core/Services/Execution/DefaultExecutionGate.swift b/apple/TablePro/Core/Services/Execution/DefaultExecutionGate.swift index a27dd9a569..992f3af9c6 100644 --- a/apple/TablePro/Core/Services/Execution/DefaultExecutionGate.swift +++ b/apple/TablePro/Core/Services/Execution/DefaultExecutionGate.swift @@ -13,6 +13,7 @@ internal actor DefaultExecutionGate: ExecutionGate { private let forcesWriteResolver: @Sendable (DatabaseType) async -> Bool private let connectionNameResolver: @Sendable (UUID) async -> String? private let readOnlyFunctionPolicyResolver: @Sendable (UUID, DatabaseType) async -> ReadOnlyFunctionPolicy + private let bareNameCallResolver: @Sendable (OperationRequest, SQLBareNameReading) async -> Bool private let auditLog: any ExecutionAuditLogging /// The end of a sentence macOS writes, not a sentence of its own. `LAContext` shows the reason @@ -33,6 +34,9 @@ internal actor DefaultExecutionGate: ExecutionGate { readOnlyFunctionPolicyResolver: @escaping @Sendable (UUID, DatabaseType) async -> ReadOnlyFunctionPolicy = { _, _ in ReadOnlyFunctionPolicy() }, + bareNameCallResolver: @escaping @Sendable (OperationRequest, SQLBareNameReading) async -> Bool = { _, reading in + reading.callsOutright + }, auditLog: any ExecutionAuditLogging = ExecutionAuditLog.shared ) { self.confirming = confirming @@ -41,6 +45,7 @@ internal actor DefaultExecutionGate: ExecutionGate { self.forcesWriteResolver = forcesWriteResolver self.connectionNameResolver = connectionNameResolver self.readOnlyFunctionPolicyResolver = readOnlyFunctionPolicyResolver + self.bareNameCallResolver = bareNameCallResolver self.auditLog = auditLog } @@ -141,12 +146,18 @@ 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 one the gate cannot tell from a - // write, so Read-Only refuses it like one, whatever the caller was cleared for. Nil runs it. + // write, so Read-Only refuses it like one, whatever the caller was cleared for. Oracle and Dameng also call a + // function named bare, as in `SELECT purge_orders FROM dual`, which the session's catalog tells from a column. + // 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 } + guard policy.functionsCanWrite else { return nil } + var callsFunction = ReadOnlyFunctionCalls.callsUnlistedFunction(sql, databaseType: request.databaseType) + if !callsFunction, let reading = ReadOnlyFunctionCalls.bareNameReading(sql, databaseType: request.databaseType), + !reading.namesNothing { + callsFunction = await bareNameCallResolver(request, reading) + } + guard callsFunction else { return nil } guard let level = policy.levelThatRunsIt else { 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." diff --git a/apple/TablePro/Core/Services/Execution/ExecutionGateProvider.swift b/apple/TablePro/Core/Services/Execution/ExecutionGateProvider.swift index c6f8dac2ca..2d35ed2aa5 100644 --- a/apple/TablePro/Core/Services/Execution/ExecutionGateProvider.swift +++ b/apple/TablePro/Core/Services/Execution/ExecutionGateProvider.swift @@ -4,6 +4,8 @@ // import Foundation +import TableProPluginKit +import TableProSQLGrammar internal enum ExecutionGateProvider { /// A driver that declares no read-only mode gets every statement treated as a write, unless the @@ -86,6 +88,42 @@ internal enum ExecutionGateProvider { }, readOnlyFunctionPolicyResolver: { connectionId, databaseType in await readOnlyFunctionPolicy(connectionId: connectionId, databaseType: databaseType) + }, + bareNameCallResolver: { request, reading in + await callsBareFunction(request, reading: reading) } ) + + static let callableCatalog = SQLCallableCatalogCache() + + // Asks the session's catalog. A catalog that cannot be read leaves the name unexplained, so it counts as a call. + static func callsBareFunction( + _ request: OperationRequest, + reading: SQLBareNameReading, + catalog: SQLCallableCatalogCache = callableCatalog, + metadata: (any ScopedMetadataProviding)? = nil + ) async -> Bool { + let (provider, resolved) = await MainActor.run { + let provider: any ScopedMetadataProviding = metadata ?? DatabaseManager.shared + return (provider, request.scope ?? request.runsIn ?? provider.browseScope(for: request.connectionId)) + } + guard let scope = resolved else { return true } + do { + return try await catalog.callsFunction(reading, connectionId: scope.connectionId, schema: scope.schema) { query in + try await provider.withMetadataRead(scope: scope, workload: .interactive) { driver in + try await driver.executeCatalogRead(query: query).rows.map { row in + row.map { cell -> String? in + switch cell { + case .null: return nil + case .text(let text): return text + case .bytes(let data): return String(bytes: data, encoding: .utf8) + } + } + } + } + } + } catch { + return true + } + } } diff --git a/apple/TablePro/Core/Services/Execution/ExternalStatementGate.swift b/apple/TablePro/Core/Services/Execution/ExternalStatementGate.swift index 8c7380dae9..13c37d6591 100644 --- a/apple/TablePro/Core/Services/Execution/ExternalStatementGate.swift +++ b/apple/TablePro/Core/Services/Execution/ExternalStatementGate.swift @@ -106,7 +106,7 @@ internal enum ExternalStatementGate { } if statement.allowsOnlyDataStatements, - QueryClassifier.isSQLDataStatement(statement.sql, databaseType: statement.databaseType) == false { + !QueryClassifier.isDataStatement(statement.sql, databaseType: statement.databaseType) { throw ExternalStatementGateError.denied(dataStatementRefusal) } diff --git a/apple/TablePro/Core/Services/Execution/OperationRequest.swift b/apple/TablePro/Core/Services/Execution/OperationRequest.swift index 12b3010de3..26925996de 100644 --- a/apple/TablePro/Core/Services/Execution/OperationRequest.swift +++ b/apple/TablePro/Core/Services/Execution/OperationRequest.swift @@ -16,6 +16,8 @@ internal struct OperationRequest: Sendable { let scope: DatabaseScope? // What the confirmation shows, values written in. Empty means `sql` alone. let statements: [String] + // Where a tab's statement runs, which its confirmation does not name: Oracle and Dameng resolve a bare name there. + let runsIn: DatabaseScope? init( connectionId: UUID, @@ -26,7 +28,8 @@ internal struct OperationRequest: Sendable { capabilities: CallerCapabilities, operationDescription: String, scope: DatabaseScope? = nil, - statements: [String] = [] + statements: [String] = [], + runsIn: DatabaseScope? = nil ) { self.connectionId = connectionId self.databaseType = databaseType @@ -37,5 +40,6 @@ internal struct OperationRequest: Sendable { self.operationDescription = operationDescription self.scope = scope self.statements = statements + self.runsIn = runsIn } } diff --git a/apple/TablePro/Core/Services/Execution/ReadOnlyFunctionCalls.swift b/apple/TablePro/Core/Services/Execution/ReadOnlyFunctionCalls.swift index 4b78874bf2..37ae31f7ec 100644 --- a/apple/TablePro/Core/Services/Execution/ReadOnlyFunctionCalls.swift +++ b/apple/TablePro/Core/Services/Execution/ReadOnlyFunctionCalls.swift @@ -33,6 +33,12 @@ internal enum ReadOnlyFunctionCalls { typeNames: Set(PluginMetadataRegistry.shared.snapshot(for: databaseType)?.editor.sqlDialect?.dataTypes ?? []) ) } + + // Nil on an engine that calls a function only with parentheses. + static func bareNameReading(_ sql: String, databaseType: DatabaseType) -> SQLBareNameReading? { + guard SQLBareNameCalls.engines.contains(databaseType.rawValue) else { return nil } + return SQLBareNameCalls.read(sql, databaseTypeId: databaseType.rawValue, readings: databaseType.lexicalReadings) + } } // Export, Transfer To, Data Compare and Copy Objects read rows outside the editor, so text the user wrote into them, a @@ -46,7 +52,8 @@ internal enum ReadOnlyReadGate { gate: any ExecutionGate, operationDescription: String = String(localized: "Export"), canPrompt: Bool = true, - manager: DatabaseManager? = nil + manager: DatabaseManager? = nil, + runsIn: DatabaseScope? = nil ) async throws { guard !statements.isEmpty, ExecutionGateProvider.runsAtReadOnly(connectionId, in: manager) else { return } let decision = await gate.authorize( @@ -58,7 +65,8 @@ internal enum ReadOnlyReadGate { caller: .userInterface, capabilities: canPrompt ? .interactiveUser : CallerCapabilities.interactiveUser.union(.cannotPrompt), operationDescription: operationDescription, - statements: statements + statements: statements, + runsIn: runsIn ) ) if let denial = decision.denialError { throw denial } diff --git a/apple/TablePro/Core/Services/Export/TableTransferService.swift b/apple/TablePro/Core/Services/Export/TableTransferService.swift index 3881639364..2e4806418c 100644 --- a/apple/TablePro/Core/Services/Export/TableTransferService.swift +++ b/apple/TablePro/Core/Services/Export/TableTransferService.swift @@ -157,7 +157,8 @@ final class TableTransferService: ObservableObject { connectionId: source.connectionId, databaseType: request.sourceType, gate: gate, - manager: manager + manager: manager, + runsIn: source ) var capabilities = CallerCapabilities.interactiveUser diff --git a/apple/TablePro/Core/Utilities/SQL/QueryClassifier+CommandDataStatements.swift b/apple/TablePro/Core/Utilities/SQL/QueryClassifier+CommandDataStatements.swift new file mode 100644 index 0000000000..c59404bc58 --- /dev/null +++ b/apple/TablePro/Core/Utilities/SQL/QueryClassifier+CommandDataStatements.swift @@ -0,0 +1,130 @@ +// +// QueryClassifier+CommandDataStatements.swift +// TablePro +// + +import Foundation +import TableProMemcachedCore +import TableProRedisCore +import TableProSQLGrammar + +extension QueryClassifier { + // Redis 8.10.3 `COMMAND`: the commands whose second word names the subcommand that runs. + private static let redisContainerCommands: Set = [ + "ACL", "BACKUP", "CLIENT", "CLUSTER", "COMMAND", "CONFIG", "FT.CONFIG", "FT.CURSOR", "FUNCTION", "HIMPORT", + "HOTKEYS", "LATENCY", "MEMORY", "MODULE", "OBJECT", "PUBSUB", "SCRIPT", "SLOWLOG", "XGROUP", "XINFO", + "_FT.CONFIG", "_FT.DEBUG", + ] + + static func redisDataCommands(_ text: String) -> [DataCommand]? { + RedisCommandSplitter.commands(in: text).map { redisDataCommand(StatementBlank.trimming($0.text)) } + } + + // Named past the prefixes the driver unwraps, as `CONTAINER|SUB` for a container. The server folds ASCII only, so + // a word only Unicode case folding reads as a listed name is one the server does not know. + private static func redisDataCommand(_ command: String) -> DataCommand { + guard let arguments = RedisArgumentCodec.split(command) else { return .refused } + if redisCommandClassification(command).tier == .safe { return .read } + let decoded = arguments.map { String(bytes: $0, encoding: .utf8) } + let words = decoded[redisCommandPastPrefixes(decoded.map { $0 ?? "" }).startIndex...] + guard let first = words.first, let verb = first.map(redisASCIIUppercased) else { return .refused } + guard redisContainerCommands.contains(verb) else { return .named(verb) } + guard let second = words.dropFirst().first, let subcommand = second else { return .refused } + return .named(verb + "|" + redisASCIIUppercased(subcommand)) + } + + private static func redisASCIIUppercased(_ word: String) -> String { + var scalars = String.UnicodeScalarView() + for scalar in word.unicodeScalars { + let isLowercaseLetter = scalar.value >= 0x61 && scalar.value <= 0x7A + scalars.append(isLowercaseLetter ? Unicode.Scalar(UInt8(scalar.value - 0x20)) : scalar) + } + return String(scalars) + } + + // The driver runs only what its parser reads. + static func memcachedDataCommands(_ text: String, readings: SQLLexicalReadings) -> [DataCommand]? { + var commands: [DataCommand] = [] + for statement in readings.all.flatMap({ statements(of: text, grammar: $0) }) { + guard let command = try? MemcachedCommandParser.parse(statement) else { return nil } + let isRead = memcachedClassification(statement).tier == .safe + commands.append(isRead ? .read : .named(memcachedName(of: command))) + } + return commands + } + + private static func memcachedName(of command: MemcachedCommand) -> String { + switch command { + case .get(_, let withCAS): return withCAS ? "gets" : "get" + case .getAndTouch(_, _, let withCAS): return withCAS ? "gats" : "gat" + case .store(let verb, _, _, _, _): return verb.rawValue + case .compareAndSwap: return "cas" + case .touch: return "touch" + case .delete: return "delete" + case .increment: return "incr" + case .decrement: return "decr" + case .flushAll: return "flush_all" + case .stats(let arguments): return (["stats"] + arguments).joined(separator: " ") + case .version: return "version" + case .metadump: return "lru_crawler metadump" + } + } + + static func etcdDataCommands(_ text: String, readings: SQLLexicalReadings) -> [DataCommand]? { + var commands: [DataCommand] = [] + for statement in readings.all.flatMap({ statements(of: text, grammar: $0) }) { + guard let operation = try? EtcdCommandParser.parse(statement) else { return nil } + if etcdClassification(statement).tier == .safe { + commands.append(.read) + } else { + commands.append(etcdName(of: operation).map(DataCommand.named) ?? .refused) + } + } + return commands + } + + private static func etcdName(of operation: EtcdOperation) -> String? { + switch operation { + case .get: return "get" + case .put: return "put" + case .create: return "create" + case .move: return "mv" + case .del: return "del" + case .watch: return "watch" + case .leaseGrant: return "lease grant" + case .leaseRevoke: return "lease revoke" + case .leaseTimetolive: return "lease timetolive" + case .leaseList: return "lease list" + case .leaseKeepAlive: return "lease keep-alive" + case .memberList: return "member list" + case .endpointStatus: return "endpoint status" + case .endpointHealth: return "endpoint health" + case .compaction: return "compaction" + case .authEnable: return "auth enable" + case .authDisable: return "auth disable" + case .userAdd: return "user add" + case .userDelete: return "user delete" + case .userList: return "user list" + case .roleAdd: return "role add" + case .roleDelete: return "role delete" + case .roleList: return "role list" + case .userGrantRole: return "user grant-role" + case .userRevokeRole: return "user revoke-role" + case .unknown: return nil + } + } + + // The whole text, as the driver parses it: `;` is a blank in KafkaQL, so a second statement is a syntax error. + static func kafkaDataCommands(_ text: String) -> [DataCommand]? { + guard let statement = try? KafkaQL.parse(text) else { return nil } + switch statement { + // Reads by the driver's own rule, `KafkaStatement.refusal`. The SQL tier reads CONSUME as a write. + case .consume, .showTopics, .showBrokers, .showGroups, .showCluster, .describeGroup, .describeTopic: + return [.read] + case .produce: + return [.named("PRODUCE")] + case .dropTopic: + return [.named("DROP TOPIC")] + } + } +} diff --git a/apple/TablePro/Core/Utilities/SQL/QueryClassifier+Cypher.swift b/apple/TablePro/Core/Utilities/SQL/QueryClassifier+Cypher.swift index e8a1307f6c..7eefed9d0a 100644 --- a/apple/TablePro/Core/Utilities/SQL/QueryClassifier+Cypher.swift +++ b/apple/TablePro/Core/Utilities/SQL/QueryClassifier+Cypher.swift @@ -73,6 +73,135 @@ extension QueryClassifier { "dbms.components", "dbms.showcurrentuser", "dbms.procedures", "dbms.functions", "apoc.meta." ] + // Cypher Manual, clauses index (Cypher 25, Neo4j 2026.04): the clauses a query opens with. Administration opens + // with any other word, including the ones a later server adds. + static let cypherHeadClauses: Set = [ + "MATCH", "OPTIONAL", "RETURN", "WITH", "UNWIND", "FOR", "CALL", "MERGE", "CREATE", "INSERT", "FOREACH", + "FINISH", "LET", "FILTER", "WHEN", "SHOW" + ] + + // Clauses upper case, procedures lower case as `cypherReadProcedures` compares them. + static func cypherDataCommands(_ text: String) -> [DataCommand]? { + var commands: [DataCommand] = [] + for code in cypherReadings(of: text) { + for statement in code.split(separator: ";") where statement.contains(where: { !$0.isWhitespace }) { + guard let named = cypherDataCommands(statement: Array(statement)) else { return [.refused] } + commands += named + } + } + guard commands.isEmpty else { return commands } + return cypherClassification(text).tier == .safe ? [.read] : [.refused] + } + + private static func cypherDataCommands(statement code: [Character]) -> [DataCommand]? { + guard let head = cypherHeadClause(in: code), cypherHeadClauses.contains(head) else { return nil } + let words = cypherClauseWords(in: code) + var commands: [DataCommand] = [] + for (position, word) in words.enumerated() { + let previous = position > 0 ? words[position - 1].text : nil + switch word.text { + case "DROP", "DENY", "REVOKE", "GRANT", "LOAD": + return nil + case "CREATE" where previous == "ON": + // MERGE's `ON CREATE SET`. + guard cypherNextWord(in: code, after: word.end) == "SET" else { return nil } + case "CREATE": + // `CREATE INDEX`, `CREATE USER`, `CREATE OR REPLACE DATABASE` name an object where a pattern goes. + guard cypherCreatesPattern(in: code, after: word.end) else { return nil } + commands.append(.named("CREATE")) + case "DELETE": + commands.append(.named(previous == "DETACH" ? "DETACH DELETE" : "DELETE")) + case "MERGE", "SET", "REMOVE", "FOREACH", "INSERT": + commands.append(.named(word.text)) + case "CALL": + guard let procedure = cypherProcedureName(in: code, after: word.end) else { continue } + guard !procedure.isEmpty, !procedure.split(separator: ".").contains("_q") else { return nil } + if !cypherReadProcedures.contains(where: procedure.hasPrefix) { + commands.append(.named(procedure)) + } + case let admin where cypherAdminObjects[admin] != nil: + if let next = cypherNextWord(in: code, after: word.end), cypherAdminObjects[admin]?.contains(next) == true { + return nil + } + default: + continue + } + } + return commands + } + + // The first clause after the prefixes a query may carry: `CYPHER` with its version and options, `EXPLAIN` or + // `PROFILE`, and `USE` with its graph. + private static func cypherHeadClause(in code: [Character]) -> String? { + var index = 0 + while let word = cypherWord(in: code, from: &index) { + switch word { + case "CYPHER": + cypherSkipQueryOptions(in: code, from: &index) + case "EXPLAIN", "PROFILE": + continue + case "USE": + cypherSkipGraph(in: code, from: &index) + default: + return word + } + } + return nil + } + + private static func cypherWord(in code: [Character], from index: inout Int) -> String? { + while index < code.count, code[index].isWhitespace { index += 1 } + let start = index + while index < code.count, code[index].isLetter || code[index].isNumber || code[index] == "_" { index += 1 } + return start < index ? String(code[start ..< index]).uppercased() : nil + } + + // `CYPHER 25 runtime=slotted`: a version, then `name=value` options. + private static func cypherSkipQueryOptions(in code: [Character], from index: inout Int) { + while index < code.count, code[index].isWhitespace { index += 1 } + while index < code.count, code[index].isNumber || code[index] == "." { index += 1 } + while true { + var probe = index + guard cypherWord(in: code, from: &probe) != nil else { return } + while probe < code.count, code[probe].isWhitespace { probe += 1 } + guard probe < code.count, code[probe] == "=" else { return } + probe += 1 + guard cypherWord(in: code, from: &probe) != nil else { return } + index = probe + } + } + + // A graph name, dotted or quoted, or a call such as `graph.byName('x')`. + private static func cypherSkipGraph(in code: [Character], from index: inout Int) { + while index < code.count, code[index].isWhitespace { index += 1 } + while index < code.count, code[index].isLetter || code[index].isNumber || code[index] == "_" || code[index] == "." { + index += 1 + } + var probe = index + while probe < code.count, code[probe].isWhitespace { probe += 1 } + guard probe < code.count, code[probe] == "(" else { return } + var depth = 0 + while probe < code.count { + if code[probe] == "(" { depth += 1 } + if code[probe] == ")" { depth -= 1 } + probe += 1 + if depth == 0 { break } + } + index = probe + } + + // A pattern, or a path name assigned one: `CREATE (n)`, `CREATE p = (a)-[:R]->(b)`. + private static func cypherCreatesPattern(in code: [Character], after end: Int) -> Bool { + guard let start = code[end...].firstIndex(where: { !$0.isWhitespace }) else { return false } + if code[start] == "(" { return true } + let name = code[start...].prefix { $0.isLetter || $0.isNumber || $0 == "_" } + guard !name.isEmpty, + let equals = code[(start + name.count)...].firstIndex(where: { !$0.isWhitespace }), + code[equals] == "=" + else { return false } + return equals + 1 == code.count || !"=~>".contains(code[equals + 1]) + } + // JavaCC's unicode-escape rule: a backslash after an even run of backslashes, one or more `u`, four hex digits. static func cypherDecodingUnicodeEscapes(_ text: String) -> String { let units = Array(text.utf16) diff --git a/apple/TablePro/Core/Utilities/SQL/QueryClassifier+DataStatements.swift b/apple/TablePro/Core/Utilities/SQL/QueryClassifier+DataStatements.swift new file mode 100644 index 0000000000..37798a7f54 --- /dev/null +++ b/apple/TablePro/Core/Utilities/SQL/QueryClassifier+DataStatements.swift @@ -0,0 +1,93 @@ +// +// QueryClassifier+DataStatements.swift +// TablePro +// + +import Foundation +import TableProSQLGrammar + +// One command as the assistant's Edit mode reads it. +enum DataCommand: Equatable, Sendable { + // The gate's own tier reads it as a read. + case read + // Runs only when the engine's curated snapshot lists the name in `dataCommands`. + case named(String) + // Changes the engine's own state whatever its name, such as a write into a system collection. + case refused +} + +// Edit runs reads and changes to data, on every engine the rule SQL follows. The lists are allowlists, since each server +// release adds commands: a schema, privilege, session, transaction or server command, and any command a reader cannot +// name, is refused. +extension QueryClassifier { + static func isDataStatement(_ sql: String, databaseType: DatabaseType) -> Bool { + if let answer = isSQLDataStatement(sql, databaseType: databaseType) { return answer } + let snapshot = PluginMetadataRegistry.shared.snapshot(for: databaseType) + let listed = snapshot?.capabilities.dataCommands ?? [] + if databaseType == .surrealdb { + return SQLStatementClassifier.isDataStatement( + sql, + databaseTypeId: databaseType.rawValue, + readings: databaseType.lexicalReadings, + rowChanges: listed + ) + } + guard let commands = dataCommands(of: sql, databaseType: databaseType, snapshot: snapshot) else { return false } + return commands.allSatisfy { command in + switch command { + case .read: return true + case .named(let name): return listed.contains(name) + case .refused: return false + } + } + } + + // Each command the text runs, read as the engine's driver reads it. Nil when the text cannot be read. + static func dataCommands( + of sql: String, + databaseType: DatabaseType, + snapshot: PluginMetadataSnapshot? + ) -> [DataCommand]? { + switch databaseType { + case .redis: + return redisDataCommands(sql) + case .memcached: + return memcachedDataCommands(sql, readings: databaseType.lexicalReadings) + case .etcd: + return etcdDataCommands(sql, readings: databaseType.lexicalReadings) + case .kafka: + return kafkaDataCommands(sql) + case .mongodb: + return mongoDataCommands(sql, systemDatabases: Set(snapshot?.schema.systemDatabaseNames ?? [])) + case .neo4j: + return cypherDataCommands(sql) + case .elasticsearch: + return elasticsearchDataCommands(sql) + case .typesense: + return typesenseDataCommands(sql) + case .qdrant: + return qdrantDataCommands(sql) + case .weaviate: + return weaviateDataCommands(sql) + case .firestore, .firebaseRealtimeDatabase: + return firebaseDataCommands(sql, databaseType: databaseType) + case .bigtable: + return bigtableDataCommands(sql) + default: + // An engine no reader knows runs only what the gate reads as a read. + return classify(sql, databaseType: databaseType).tier == .safe ? [.read] : nil + } + } + + // A write into the scope's database or schema when it is one the engine keeps its own state in. A table named + // without a schema lands there. Engines curated with `dataCommands` name theirs in the snapshot. + static func writesSystemScope(database: String?, schema: String?, databaseType: DatabaseType) -> Bool { + if SQLStatementClassifier.isSystemScope(database: database, schema: schema, databaseTypeId: databaseType.rawValue) { + return true + } + guard let snapshot = PluginMetadataRegistry.shared.snapshot(for: databaseType), + snapshot.capabilities.dataCommands != nil, let database + else { return false } + return snapshot.schema.systemDatabaseNames.contains(database) + } +} diff --git a/apple/TablePro/Core/Utilities/SQL/QueryClassifier+MongoDB.swift b/apple/TablePro/Core/Utilities/SQL/QueryClassifier+MongoDB.swift index ea42892c39..6f0493795a 100644 --- a/apple/TablePro/Core/Utilities/SQL/QueryClassifier+MongoDB.swift +++ b/apple/TablePro/Core/Utilities/SQL/QueryClassifier+MongoDB.swift @@ -20,6 +20,46 @@ extension QueryClassifier { // Neo4j is here so its driver opens a proven read in Bolt's read mode, where the server refuses a write. static let enginesWithProvenReads: Set = [.mongodb, .redis, .etcd, .sapHana, .neo4j] + // Only text the grammar accepts can be named: anything else may reach any method once JavaScript runs it. + static func mongoDataCommands(_ text: String, systemDatabases: Set) -> [DataCommand]? { + let text = StatementBlank.trimming(text) + let located = JavaScriptStatementScanner.locatedStatements(in: text) + let statements = located.count > 1 ? located.map(\.text) : [text] + let systemDatabases = Set(systemDatabases.map { $0.lowercased() }) + var commands: [DataCommand] = [] + for statement in statements where !MongoStatementParser.isTrivia(statement) { + guard let parsed = MongoStatementParser.parse( + statement, shadowsCollection: MongoCollectionAccessor.isShadowedByDatabaseMember + ) else { return nil } + // `$out` and `$merge` write to a collection the pipeline names, and server-side JavaScript to any. + guard !mongoClassification(statement).reachesFilesystemOrExecutesCode else { return [.refused] } + commands += parsed.calls.map { mongoDataCommand($0, systemDatabases: systemDatabases) } + } + return commands.isEmpty ? [.read] : commands + } + + private static func mongoDataCommand(_ call: MongoStatement.Call, systemDatabases: Set) -> DataCommand { + // `use` moves the driver's database for every later statement. + if call.receiver == .shell, call.name == "use" { return .refused } + if call.isRead { return .read } + let name: String + switch call.receiver { + case .collection: + name = "db.collection.\(call.name)" + case .database where call.name == "runCommand": + // A read the command form sends; the code check above already refused `$out`, `$merge` and `$where`. + if let command = call.commandName, MongoCommandCatalog.readsOnly(command) { return .read } + guard let command = call.commandName, call.collection != nil else { return .refused } + name = command + default: + // `adminCommand` and the database helpers create, kill or administer: none of them is a data change. + return .refused + } + if let database = call.database, systemDatabases.contains(database.lowercased()) { return .refused } + if let collection = call.collection, collection.lowercased().hasPrefix("system.") { return .refused } + return .named(name) + } + private static func queryTier(of tier: MongoStatementTier) -> QueryTier { switch tier { case .read: return .safe diff --git a/apple/TablePro/Core/Utilities/SQL/QueryClassifier+RequestDataStatements.swift b/apple/TablePro/Core/Utilities/SQL/QueryClassifier+RequestDataStatements.swift new file mode 100644 index 0000000000..f74ee7a000 --- /dev/null +++ b/apple/TablePro/Core/Utilities/SQL/QueryClassifier+RequestDataStatements.swift @@ -0,0 +1,256 @@ +// +// QueryClassifier+RequestDataStatements.swift +// TablePro +// + +import Foundation +import TableProFirebaseCore +import TableProQdrantCore +import TableProSQLGrammar +import TableProWeaviateCore + +// A request engine's change is named by its method and the route template of the engine's API reference, such as +// `PUT /{target}/_doc/{id}`. Each driver reads the whole text as one request, so each reader here does too. +extension QueryClassifier { + static func elasticsearchDataCommands(_ text: String) -> [DataCommand]? { + let request = text.trimmingCharacters(in: .whitespacesAndNewlines) + var lines = request.components(separatedBy: "\n") + let header = lines.removeFirst().trimmingCharacters(in: .whitespaces) + let parts = header.split(separator: " ", maxSplits: 1, omittingEmptySubsequences: true) + guard let declared = parts.first.map({ $0.uppercased() }), elasticsearchMethods.contains(declared) else { + return nil + } + var path = parts.count == 2 ? parts[1].trimmingCharacters(in: .whitespaces) : "/" + if !path.hasPrefix("/") { path = "/" + path } + // From here on the URL parser reads the path, scalar by scalar. + let route = String(path.unicodeScalars.prefix(while: { $0 != "?" && $0 != "#" })) + if elasticsearchClassification(request).tier == .safe, !elasticsearchRunsServerCommand(route) { + return [.read] + } + guard let segments = elasticsearchSegments(of: route) else { return nil } + let body = lines.joined(separator: "\n").trimmingCharacters(in: .whitespacesAndNewlines) + // The driver sends a GET or HEAD that carries a body as a POST. + let method = !body.isEmpty && (declared == "GET" || declared == "HEAD") ? "POST" : declared + let api = segments.firstIndex { $0.unicodeScalars.first == "_" && $0 != "_all" } ?? segments.count + let rest = path.unicodeScalars.dropFirst(route.unicodeScalars.count) + let query = rest.first == "?" ? String(rest.dropFirst().prefix(while: { $0 != "#" })) : "" + if segments[.. [DataCommand]? { + let request = text.trimmingCharacters(in: .whitespacesAndNewlines) + let header = request.components(separatedBy: "\n")[0].trimmingCharacters(in: .whitespaces) + let parts = header.split(separator: " ", maxSplits: 1, omittingEmptySubsequences: true) + guard let method = parts.first.map({ $0.uppercased() }), typesenseMethods.contains(method) else { return nil } + var path = parts.count == 2 ? parts[1].trimmingCharacters(in: .whitespaces) : "/" + if !path.hasPrefix("/") { path = "/" + path } + // The driver refuses a path with whitespace before its query string. + guard !path.prefix(while: { $0 != "?" }).contains(where: \.isWhitespace) else { return nil } + if typesenseClassification(request).tier == .safe { return [.read] } + let routePath = String(path.unicodeScalars.prefix(while: { $0 != "?" && $0 != "#" })) + guard let route = typesenseRoute(of: routePath) else { return nil } + return [.named(method + " /" + typesenseTemplate(route).joined(separator: "/"))] + } + + static func qdrantDataCommands(_ text: String) -> [DataCommand]? { + let request = text.trimmingCharacters(in: .whitespacesAndNewlines) + // The driver answers it with a ping. + if request.lowercased() == "select 1" { return [.read] } + guard let parsed = QdrantConsoleParser.parse(request) else { return nil } + if qdrantClassification(request).tier == .safe { return [.read] } + guard var route = QdrantPathEncoding.segments(of: parsed.path) else { return nil } + if route.count >= 2, route[0] == "collections" { route[1] = "{collection_name}" } + return [.named(parsed.method + " /" + route.joined(separator: "/"))] + } + + static func weaviateDataCommands(_ text: String) -> [DataCommand]? { + let request = text.trimmingCharacters(in: .whitespacesAndNewlines) + if weaviateClassification(request).tier == .safe { return [.read] } + guard let parsed = WeaviateConsoleParser.parse(request), let segments = weaviateSegments(of: parsed.path) else { + return nil + } + return [.named(parsed.method + " /" + weaviateTemplate(segments).joined(separator: "/"))] + } + + static func firebaseDataCommands(_ text: String, databaseType: DatabaseType) -> [DataCommand]? { + let request = text.trimmingCharacters(in: .whitespacesAndNewlines) + if firebaseClassification(request, databaseType: databaseType).tier == .safe { return [.read] } + guard case .request(let parsed)? = try? FirebaseStatement.parse(request) else { return nil } + let segments = parsed.path.unicodeScalars.split(separator: "/").map(String.init) + if databaseType == .firebaseRealtimeDatabase { + return [realtimeDatabaseCommand(parsed, segments: segments)] + } + return [firestoreCommand(parsed, segments: segments)] + } + + static func bigtableDataCommands(_ text: String) -> [DataCommand]? { + let request = text.trimmingCharacters(in: .whitespacesAndNewlines) + if bigtableClassification(request).tier == .safe { return [.read] } + let verb = String(request.unicodeScalars.prefix(while: { !$0.properties.isWhitespace })).uppercased() + return bigtableChanges.contains(verb) ? [.named(verb)] : nil + } + + private static let elasticsearchMethods: Set = ["GET", "POST", "PUT", "DELETE", "HEAD"] + // Each takes a GET as well, and changes index state. + private static let elasticsearchServerCommands: Set = ["_flush", "_refresh", "_reload_search_analyzers"] + private static let elasticsearchBulkActions: Set = ["index", "create", "update", "delete"] + private static let typesenseMethods: Set = ["GET", "POST", "PUT", "PATCH", "DELETE", "HEAD"] + private static let realtimeDatabaseMetadataKeys: Set = [".value", ".priority", ".sv"] + private static let bigtableChanges: Set = ["INSERT", "PUT", "DELETE"] + + private static func elasticsearchRunsServerCommand(_ route: String) -> Bool { + route.unicodeScalars.split(separator: "/").contains { scalars in + let segment = String(scalars) + let name = segment.removingPercentEncoding ?? segment + return name == "." || name == ".." || elasticsearchServerCommands.contains(name) + } + } + + // Nil when the URL the driver builds could reach another route: a dot segment resolves away, an empty one can + // name another host, and a space, `%` or `\` is encoded or decoded on the way. + private static func elasticsearchSegments(of route: String) -> [String]? { + guard !route.unicodeScalars.contains(where: { $0.properties.isWhitespace || $0 == "%" || $0 == "\\" }) else { + return nil + } + return requestSegments(of: route) + } + + private static func elasticsearchTemplate(_ segments: [String], api: Int) -> [String] { + var template: [String] = Array(repeating: "{target}", count: api) + segments[api...] + if api + 1 < segments.count, ["_doc", "_create", "_update"].contains(segments[api]) { + template[api + 1] = "{id}" + } + return template + } + + // A comma list of index names. A name starting with `.` is a system or hidden index (Elasticsearch 8.19, API + // conventions): `.security-7`, `.watches`, `.kibana`. `+`, `-` and `<` lead an expression, `cluster:` a remote one. + private static func elasticsearchNamesSystemIndex(_ expression: String) -> Bool { + expression.unicodeScalars.split(separator: ",").contains { name in + name.split(separator: ":", omittingEmptySubsequences: false).contains { part in + part.first { $0 != "+" && $0 != "-" && $0 != "<" } == "." + } + } + } + + private static func elasticsearchExpandsToHidden(_ query: String) -> Bool { + for pair in query.unicodeScalars.split(separator: "&") { + // The server decodes `+` in a query string as a space. + let item = pair.split(separator: "=", maxSplits: 1, omittingEmptySubsequences: false) + .map { String($0).replacingOccurrences(of: "+", with: " ") } + guard let name = item[0].removingPercentEncoding else { return true } + guard name.trimmingCharacters(in: .whitespaces).lowercased() == "expand_wildcards" else { continue } + guard item.count == 2, let value = item[1].removingPercentEncoding else { return true } + let states = value.lowercased().split(separator: ",").map { $0.trimmingCharacters(in: .whitespaces) } + if states.contains(where: { $0 == "hidden" || $0 == "all" }) { return true } + } + return false + } + + // The `_index` of each action line. A source line follows every action but `delete`; a blank line is refused, since + // the server and this reading could pair the lines differently around it. + private static func elasticsearchBulkIndices(_ body: String) -> [String]? { + var indices: [String] = [] + var expectsSource = false + for line in body.components(separatedBy: "\n").map({ $0.trimmingCharacters(in: .whitespacesAndNewlines) }) { + guard !line.isEmpty else { return nil } + if expectsSource { + expectsSource = false + continue + } + guard let action = try? JSONSerialization.jsonObject(with: Data(line.utf8)) as? [String: Any], + action.count == 1, let entry = action.first, elasticsearchBulkActions.contains(entry.key), + let metadata = entry.value as? [String: Any] + else { return nil } + if let index = metadata["_index"] { + guard let name = index as? String else { return nil } + indices.append(name) + } + expectsSource = entry.key != "delete" + } + return expectsSource ? nil : indices + } + + private static func typesenseTemplate(_ route: [String]) -> [String] { + var template = route.map { $0.lowercased() } + guard route.count >= 2, route[0] == "COLLECTIONS" else { return template } + template[1] = "{collection}" + if route.count == 4, route[2] == "DOCUMENTS", !["IMPORT", "EXPORT", "SEARCH"].contains(route[3]) { + template[3] = "{id}" + } + return template + } + + // Read as `WeaviatePathEncoding.resolve` sends it, which keeps a dot segment as written. + private static func weaviateSegments(of path: String) -> [String]? { + guard let encoded = URLComponents(string: path)?.percentEncodedPath, + let segments = requestSegments(of: encoded) + else { return nil } + var decoded: [String] = [] + for segment in segments { + guard let text = segment.removingPercentEncoding, !text.isEmpty, text != ".", text != "..", + !text.contains("/"), !text.contains("\\") + else { return nil } + decoded.append(text) + } + return decoded + } + + private static func weaviateTemplate(_ segments: [String]) -> [String] { + guard segments.count >= 3, segments[0] == "v1", segments[1] == "objects" else { return segments } + switch segments.count { + case 3: + return ["v1", "objects", "{id}"] + case 4: + return ["v1", "objects", "{className}", "{id}"] + case 5 where segments[3] == "references": + return ["v1", "objects", "{id}", "references", "{propertyName}"] + case 6 where segments[4] == "references": + return ["v1", "objects", "{className}", "{id}", "references", "{propertyName}"] + default: + return segments + } + } + + // A key cannot hold `.`, so a segment that does names the server's own node: `/.settings/rules` holds the security + // rules. A PATCH names each child it writes by a path of its own. + private static func realtimeDatabaseCommand(_ request: FirebaseRequest, segments: [String]) -> DataCommand { + if segments.contains(where: { $0.unicodeScalars.contains(".") }) { return .refused } + if request.method == .patch { + guard let body = request.body, + let members = try? JSONSerialization.jsonObject(with: Data(body.utf8)) as? [String: Any] + else { return .refused } + let reachesServerNode = members.keys.contains { key in + key.unicodeScalars.split(separator: "/").contains { segment in + segment.contains(".") && !realtimeDatabaseMetadataKeys.contains(String(segment)) + } + } + if reachesServerNode { return .refused } + } + return .named(request.method.rawValue + " /{path}") + } + + // Firestore refuses `.` and `..` as IDs, and the driver sends them as written, where the front end may resolve them. + private static func firestoreCommand(_ request: FirebaseRequest, segments: [String]) -> DataCommand { + if segments.contains(where: { $0 == "." || $0 == ".." }) { return .refused } + guard !segments.isEmpty else { return .named(request.method.rawValue + " /") } + return .named(request.method.rawValue + (segments.count.isMultiple(of: 2) ? " /{document}" : " /{collection}")) + } + + // The segments after the leading `/`, one trailing `/` allowed. Nil on an empty or dot segment. + private static func requestSegments(of route: String) -> [String]? { + var segments = route.unicodeScalars.split(separator: "/", omittingEmptySubsequences: false).map(String.init) + guard segments.first == "" else { return nil } + segments.removeFirst() + if segments.last == "" { segments.removeLast() } + guard !segments.contains(where: { $0.isEmpty || $0 == "." || $0 == ".." }) else { return nil } + return segments + } +} diff --git a/apple/TablePro/Core/Utilities/SQL/QueryClassifier.swift b/apple/TablePro/Core/Utilities/SQL/QueryClassifier.swift index badfc034c4..46d0cd63e1 100644 --- a/apple/TablePro/Core/Utilities/SQL/QueryClassifier.swift +++ b/apple/TablePro/Core/Utilities/SQL/QueryClassifier.swift @@ -189,7 +189,7 @@ enum QueryClassifier { private static let explainPrefixes: [String] = ["EXPLAIN", "ANALYZE"] } -private extension QueryClassifier { +extension QueryClassifier { static let redisDestructiveCommands: Set = [ "FLUSHDB", "FLUSHALL", "SHUTDOWN", "MIGRATE", "SWAPDB", "REPLICAOF", "SLAVEOF", "CLUSTER", "FAILOVER", "RESET" @@ -215,7 +215,7 @@ private extension QueryClassifier { } } - private static func redisCommandClassification(_ command: String) -> QueryClassification { + static func redisCommandClassification(_ command: String) -> QueryClassification { guard let arguments = RedisArgumentCodec.split(command) else { return QueryClassification(tier: .write, reachesFilesystemOrExecutesCode: false) } @@ -313,9 +313,11 @@ private extension QueryClassifier { } } - /// The APIs a POST may reach and still only read. `POST /orders/_mapping` changes the mapping. + // The APIs a POST may reach and still only read (REST API spec 8.19). `POST /orders/_mapping` changes the mapping. static let elasticsearchReadEndpoints: Set = [ - "_search", "_count", "_msearch", "_explain", "_analyze", "_field_caps", "_validate", "_render", "_sql" + "_search", "_count", "_msearch", "_explain", "_analyze", "_field_caps", "_validate", "_render", "_sql", + "_mget", "_query", "_knn_search", "_termvectors", "_mtermvectors", "_terms_enum", "_search_shards", + "_rank_eval", "_mvt" ] /// Read the way `ElasticsearchConsoleParser` reads a console request: the method and path on the @@ -342,7 +344,7 @@ private extension QueryClassifier { tier = .destructive } else { let readsOnly = elasticsearchEndpoint(of: path).map(elasticsearchReadEndpoints.contains) ?? false - tier = readsOnly ? .safe : .write + tier = readsOnly && !elasticsearchKeepsResults(path) ? .safe : .write } case "DELETE": tier = .destructive @@ -361,7 +363,14 @@ private extension QueryClassifier { guard !route.contains(where: { $0.isWhitespace || $0 == "%" || $0 == "\\" }) else { return nil } let segments = route.split(separator: "/") guard !segments.contains(where: { $0 == "." || $0 == ".." }) else { return nil } - return segments.first { $0.hasPrefix("_") }.map(String.init) + // `_all` is a target, not an API: `GET /_all/_search`. + return segments.first { $0.hasPrefix("_") && $0 != "_all" }.map(String.init) + } + + // An async ES|QL query keeps its results on the cluster, as `_async_search` does. + static func elasticsearchKeepsResults(_ path: String) -> Bool { + let segments = path.prefix { $0 != "?" && $0 != "#" }.split(separator: "/") + return zip(segments, segments.dropFirst()).contains { $0 == "_query" && $1 == "async" } } // A snapshot writes to a server path and `/keys` mints API keys. @@ -458,7 +467,7 @@ private extension QueryClassifier { return .write } - private static func weaviateClassification(of request: WeaviateConsoleRequest) -> QueryClassification { + static func weaviateClassification(of request: WeaviateConsoleRequest) -> QueryClassification { switch request.method { case "GET", "HEAD": return .safe diff --git a/apple/TablePro/Models/AI/AIModels.swift b/apple/TablePro/Models/AI/AIModels.swift index 4d64392be3..d806eb1044 100644 --- a/apple/TablePro/Models/AI/AIModels.swift +++ b/apple/TablePro/Models/AI/AIModels.swift @@ -257,7 +257,7 @@ enum AIChatMode: String, Codable, CaseIterable, Identifiable, Sendable { case .ask: return String(localized: "Ask: read-only schema lookups. AI can browse but not run queries.") case .edit: - return String(localized: "Edit: read-only tools plus queries and data changes. On SQL databases, schema, privilege and session statements need Agent mode.") + return String(localized: "Edit: read-only tools plus queries and data changes. Schema, privilege and session commands need Agent mode.") case .agent: return String(localized: "Agent: full tool access including destructive DDL. Safe mode still gates execution.") } @@ -270,9 +270,10 @@ enum AIChatMode: String, Codable, CaseIterable, Identifiable, Sendable { case .edit: return """ You are in Edit mode. You can read schema and run queries and data changes, such as \ - SELECT, INSERT, UPDATE, DELETE and MERGE, via execute_query. On a SQL database, execute_query refuses \ - schema, privilege, session and transaction statements, such as CREATE, ALTER, GRANT, SET or BEGIN: \ - they need Agent mode. Destructive statements, such as DROP, TRUNCATE or a DELETE with no WHERE, are blocked. + SELECT, INSERT, UPDATE, DELETE and MERGE, via execute_query. execute_query refuses schema, privilege, \ + session and transaction commands, such as CREATE, ALTER, GRANT, SET or BEGIN, and their counterparts in \ + a database's own language, such as Redis ACL or MongoDB createIndex: they need Agent mode. \ + Destructive statements, such as DROP, TRUNCATE or a DELETE with no WHERE, are blocked. """ case .agent: return """ diff --git a/apple/TablePro/Resources/Localizable.xcstrings b/apple/TablePro/Resources/Localizable.xcstrings index bc906f39d3..563476de1c 100644 --- a/apple/TablePro/Resources/Localizable.xcstrings +++ b/apple/TablePro/Resources/Localizable.xcstrings @@ -71156,6 +71156,9 @@ }, "Edit: read-only tools plus queries and data changes. On SQL databases, schema, privilege and session statements need Agent mode." : { + }, + "Edit: read-only tools plus queries and data changes. Schema, privilege and session commands need Agent mode." : { + }, "Edit: read-only tools plus running queries. Destructive DDL stays blocked." : { "localizations" : { diff --git a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+ReadOnlySafeMode.swift b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+ReadOnlySafeMode.swift index 7120e5ee3a..20ef1b8972 100644 --- a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+ReadOnlySafeMode.swift +++ b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+ReadOnlySafeMode.swift @@ -4,6 +4,7 @@ // import Foundation +import TableProSQLGrammar 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 @@ -23,7 +24,8 @@ extension MainContentCoordinator { // A raw filter is text the user typed, inside a query no gate reads below the Full levels. At Read-Only, where the // driver cannot hold the engine to read-only, one that calls a function goes to the gate, which refuses it, before - // a browse, page turn, sort or count sends it. + // a browse, page turn, sort or count sends it. On Oracle and Dameng a bare name can call one too, and only the + // catalog the gate reads against the whole query can tell it from a column, so every raw filter goes there. func rawFilterNeedsReadOnlyGate(_ filters: [TableFilter]) -> Bool { let expressions = Self.rawFilterExpressions(filters) guard !expressions.isEmpty, runsAtReadOnly, @@ -31,10 +33,11 @@ extension MainContentCoordinator { connectionId: connectionId, databaseType: connection.type, in: services.databaseManager ).functionsCanWrite else { return false } + if SQLBareNameCalls.engines.contains(connection.type.rawValue) { return true } return expressions.contains { ReadOnlyFunctionCalls.callsUnlistedFunction($0, databaseType: connection.type) } } - func rawFilterRequest(sql: String, canPrompt: Bool) -> OperationRequest { + func rawFilterRequest(sql: String, canPrompt: Bool, scope: DatabaseScope?) -> OperationRequest { OperationRequest( connectionId: connectionId, databaseType: connection.type, @@ -42,7 +45,8 @@ extension MainContentCoordinator { kind: .readQuery, caller: .userInterface, capabilities: canPrompt ? .interactiveUser : CallerCapabilities.interactiveUser.union(.cannotPrompt), - operationDescription: String(localized: "Execute Query") + operationDescription: String(localized: "Execute Query"), + runsIn: scope ) } } diff --git a/apple/TablePro/Views/Main/MainContentCoordinator.swift b/apple/TablePro/Views/Main/MainContentCoordinator.swift index effdcb46fa..465beded46 100644 --- a/apple/TablePro/Views/Main/MainContentCoordinator.swift +++ b/apple/TablePro/Views/Main/MainContentCoordinator.swift @@ -1093,6 +1093,7 @@ final class MainContentCoordinator: ObservableObject { isShowingSafeModePrompt = true let tabId = tab.id let askedContext = tab.tableContext + let askedScope = scope(for: tab) return Task { let decision = await executionGate.authorize( OperationRequest( @@ -1102,7 +1103,8 @@ final class MainContentCoordinator: ObservableObject { kind: .readQuery, caller: .userInterface, capabilities: .interactiveUser, - operationDescription: String(localized: "Execute Query") + operationDescription: String(localized: "Execute Query"), + runsIn: askedScope ) ) isShowingSafeModePrompt = false diff --git a/apple/TableProMobile/TableProMobile/Helpers/ReadOnlyFunctionGuard.swift b/apple/TableProMobile/TableProMobile/Helpers/ReadOnlyFunctionGuard.swift index 9693630ddb..7263ede0fe 100644 --- a/apple/TableProMobile/TableProMobile/Helpers/ReadOnlyFunctionGuard.swift +++ b/apple/TableProMobile/TableProMobile/Helpers/ReadOnlyFunctionGuard.swift @@ -16,4 +16,30 @@ nonisolated enum ReadOnlyFunctionGuard { typeNames: SQLEngineVocabulary.curated(forDatabaseTypeId: typeId)?.dataTypes ?? [] ) } + + static let callableCatalog = SQLCallableCatalogCache() + + // Oracle calls a function named without parentheses once the name is not a column in scope, so the session's own + // catalog says which names call. A catalog that cannot be read leaves the name unexplained, and it counts as a call. + static func refusesBareName( + _ sql: String, + databaseType: DatabaseType, + level: SafeModeLevel, + connectionId: UUID, + schema: String?, + read: @escaping SQLCallableCatalogCache.Reader + ) async -> Bool { + let typeId = databaseType.rawValue + guard level.blocksWrites, SQLBareNameCalls.engines.contains(typeId) else { return false } + let reading = SQLBareNameCalls.read( + sql, + databaseTypeId: typeId, + readings: SQLLexicalReadings.resolve(databaseTypeId: typeId, declared: nil, session: nil) + ) + guard !reading.namesNothing else { return false } + let calls = try? await callableCatalog.callsFunction( + reading, connectionId: connectionId, schema: schema, read: read + ) + return calls ?? true + } } diff --git a/apple/TableProMobile/TableProMobile/Views/QueryEditorView.swift b/apple/TableProMobile/TableProMobile/Views/QueryEditorView.swift index 83688e6ce3..154f67fac4 100644 --- a/apple/TableProMobile/TableProMobile/Views/QueryEditorView.swift +++ b/apple/TableProMobile/TableProMobile/Views/QueryEditorView.swift @@ -863,11 +863,30 @@ struct QueryEditorView: View { } else if ReadOnlyFunctionGuard.refuses(trimmed, databaseType: databaseType, level: safeModeLevel) { showFunctionBlockedAlert = true return + } else if await refusesBareFunctionName(trimmed, on: session) { + showFunctionBlockedAlert = true + return } await executeQueryDirect(trimmed) } + // Read on the session itself, whose current schema the query resolves its names in. + private func refusesBareFunctionName(_ sql: String, on session: ConnectionSession) async -> Bool { + let transactions = appState.sessionTransactions + let connectionId = connectionId + let driver = session.driver + return await ReadOnlyFunctionGuard.refusesBareName( + sql, + databaseType: databaseType, + level: safeModeLevel, + connectionId: connectionId, + schema: activeSchema + ) { query in + try await transactions.track(connectionId, reading: true) { try await driver.execute(query: query).rows } + } + } + // Claimed before the first await, so a second Run while the session reports how it reads literals starts nothing. private func executeQueryDirect(_ trimmed: String) async { guard let session, !isExecuting else { return } diff --git a/apple/TableProMobile/TableProMobileTests/Helpers/ReadOnlyBareNameGuardTests.swift b/apple/TableProMobile/TableProMobileTests/Helpers/ReadOnlyBareNameGuardTests.swift new file mode 100644 index 0000000000..4f5c29c6e9 --- /dev/null +++ b/apple/TableProMobile/TableProMobileTests/Helpers/ReadOnlyBareNameGuardTests.swift @@ -0,0 +1,39 @@ +import Foundation +@testable import TableProMobile +import TableProModels +import TableProSQLGrammar +import Testing + +struct ReadOnlyBareNameGuardTests { + private struct Unreachable: Error {} + + // The catalog Oracle 23ai answered for a schema with function PURGE and table ORDERS(ID, PURGE). + private static let catalog: SQLCallableCatalogCache.Reader = { query in + if query == SQLCallableCatalog.query { return [["F", "PURGE"]] } + return [["0", "1", nil, "ID"], ["0", "1", nil, "PURGE"]] + } + + private static func refuses(_ sql: String, level: SafeModeLevel = .readOnly, read: @escaping SQLCallableCatalogCache.Reader = catalog) async -> Bool { + await ReadOnlyFunctionGuard.refusesBareName( + sql, databaseType: .oracle, level: level, connectionId: UUID(), schema: nil, read: read + ) + } + + @Test("Oracle Read-Only refuses a function named bare and runs the column of the same name") + func functionNamedBare() async { + #expect(await Self.refuses("SELECT purge FROM dual")) + #expect(await Self.refuses("SELECT pkg.purge FROM dual")) + #expect(!(await Self.refuses("SELECT purge FROM orders"))) + #expect(!(await Self.refuses("SELECT id FROM orders"))) + } + + @Test("Only Read-Only refuses, and a catalog that cannot be read refuses") + func onlyReadOnly() async { + #expect(!(await Self.refuses("SELECT purge FROM dual", level: .confirmWrites))) + #expect(await Self.refuses("SELECT id FROM orders", read: { _ in throw Unreachable() })) + #expect(!(await ReadOnlyFunctionGuard.refusesBareName( + "SELECT purge FROM dual", databaseType: .postgresql, level: .readOnly, connectionId: UUID(), schema: nil, + read: Self.catalog + ))) + } +} diff --git a/apple/TableProTests/Core/Execution/EditModeDataCommandsCommandTests.swift b/apple/TableProTests/Core/Execution/EditModeDataCommandsCommandTests.swift new file mode 100644 index 0000000000..be5d5a9f94 --- /dev/null +++ b/apple/TableProTests/Core/Execution/EditModeDataCommandsCommandTests.swift @@ -0,0 +1,130 @@ +// +// EditModeDataCommandsCommandTests.swift +// TableProTests +// + +import Foundation +@testable import TablePro +import Testing + +struct EditModeDataCommandsCommandTests { + private func statement(_ sql: String, databaseType: DatabaseType) -> ExternalStatementGate.Statement { + ExternalStatementGate.Statement( + sql: sql, + connectionId: UUID(), + databaseType: databaseType, + externalAccess: .readWrite, + loadsExtensions: false, + allowsDestructive: false, + allowsOnlyDataStatements: true + ) + } + + private func refusal(_ sql: String, on databaseType: DatabaseType) -> ExternalStatementGateError? { + do { + _ = try ExternalStatementGate.classify(statement(sql, databaseType: databaseType)) + return nil + } catch let error as ExternalStatementGateError { + return error + } catch { + return nil + } + } + + @Test("Edit refuses a Redis command that changes no data", arguments: [ + "ACL SETUSER bob on >pw +@all ~*", #""\x41CL" SETUSER bob on"#, + "SELECT 3", "CLIENT SETNAME x", "CLIENT REPLY OFF", "HELLO 3", + "MULTI", "WATCH k", + "SLOWLOG RESET", "MEMORY PURGE", "HOTKEYS START METRICS 1 CPU", "CLIENT KILL ID 7", "FT.CONFIG SET TIMEOUT 1", + "PFDEBUG GETREG h", + "READWRITE", "SEARCH.CLUSTERREFRESH", "TIMESERIES.REFRESHCLUSTER", + "FT.CREATE idx ON HASH SCHEMA name TEXT", "FT.ALIASADD a idx", "FT.SYNUPDATE idx g a b", + "TS.CREATERULE a b AGGREGATION avg 60000", + "PUBLISH ch hi", "SUBSCRIBE ch", + "DB 2 ACL SETUSER bob on", "OUTSIDEBLOCK CLIENT KILL ID 7", "MUSTAPPLY", + "SOME.NEW.COMMAND x", "XGROUP", #"SET "a b"#, + ]) + func redisRefusesNonDataCommands(sql: String) { + #expect(refusal(sql, on: .redis) == .denied(ExternalStatementGate.dataStatementRefusal)) + } + + @Test("Edit leaves Redis server code to the code rule", arguments: [#"EVAL "return 1" 0"#, "FCALL_RO f 0"]) + func redisCodeStaysRefused(sql: String) { + #expect(QueryClassifier.reachesFilesystemOrExecutesCode(sql, databaseType: .redis)) + #expect(refusal(sql, on: .redis) != nil) + } + + @Test("Edit runs Redis reads and data changes", arguments: [ + "SET k v EX 10", "EXPIRE k 60", "RENAME a b", "HSET h f v", "HEXPIRE h 60 FIELDS 1 f", "LPUSH l a", "SADD s a", + "ZADD z 1 a", "XADD s * f v", "XGROUP CREATE s g $ MKSTREAM", "SETBIT b 7 1", "GEOADD g 13.4 52.5 berlin", + "PFADD h a", "ARSET a 0 x", #"JSON.SET doc $ '{"a":1}'"#, "TS.ADD t * 1", "BF.ADD bf a", + "VADD vs VALUES 2 0.1 0.2 e1", + "DB 1 SET k v", "MUSTAPPLY SET k v NX", + "GET k", "FT.SEARCH idx *", "FT.AGGREGATE idx * LOAD 1 @name", "TS.MRANGE - + FILTER a=b", "HTTL h FIELDS 1 f", + ]) + func redisRunsDataWork(sql: String) { + #expect(refusal(sql, on: .redis) == nil) + } + + @Test("Edit refuses a Memcached command that changes the server", arguments: [ + "stats reset", "stats detail on", "stats sizes_enable", "verbosity 1", "mg k v", #""\x73tats" reset"#, + ]) + func memcachedRefusesServerCommands(sql: String) { + #expect(refusal(sql, on: .memcached) == .denied(ExternalStatementGate.dataStatementRefusal)) + } + + @Test("Edit runs Memcached reads and data changes", arguments: [ + "set k 0 60 v", "add k 0 0 v", "cas k 0 0 123 v", "append k 0 0 x", "incr n 1", "touch k 60", "gat 60 k", + "get k", "stats", "stats items", + ]) + func memcachedRunsDataWork(sql: String) { + #expect(refusal(sql, on: .memcached) == nil) + } + + @Test("A Memcached delete is a data change the destructive rule refuses") + func memcachedDeleteIsData() { + #expect(QueryClassifier.isDataStatement("delete k", databaseType: .memcached)) + #expect(refusal("delete k", on: .memcached) == .denied(ExternalStatementGate.destructiveRefusal)) + } + + @Test("Edit refuses an etcd command that changes no data", arguments: [ + "user add mallory pw", "role add admin", "user grant-role mallory root", "user revoke-role app reader", + "auth enable", "select 1", "frobnicate x", + ]) + func etcdRefusesNonDataCommands(sql: String) { + #expect(refusal(sql, on: .etcd) == .denied(ExternalStatementGate.dataStatementRefusal)) + } + + @Test("Edit still refuses the etcd commands refused before", arguments: [ + "auth disable", "user delete app", "compaction 5", "lease revoke 694da1d22d63c20a", "del app/a", "defrag", + ]) + func etcdKeepsEarlierRefusals(sql: String) { + #expect(refusal(sql, on: .etcd) != nil) + } + + @Test("Edit runs etcd reads and data changes", arguments: [ + "put app/a 1", "put app/a 1 --lease=694da1d22d63c20a", "create app/b 2", "lease grant 60", + "lease keep-alive 694da1d22d63c20a", "get app/ --prefix", "user list", + ]) + func etcdRunsDataWork(sql: String) { + #expect(refusal(sql, on: .etcd) == nil) + } + + @Test("Edit refuses text KafkaQL cannot parse") + func kafkaRefusesUnparsedText() { + #expect(refusal("ALTER TOPIC orders", on: .kafka) == .denied(ExternalStatementGate.dataStatementRefusal)) + } + + @Test("Edit refuses DROP TOPIC as destructive") + func kafkaDropTopicStaysDestructive() { + #expect(refusal("DROP TOPIC \"orders\"", on: .kafka) == .denied(ExternalStatementGate.destructiveRefusal)) + } + + @Test("Edit runs KafkaQL reads and PRODUCE", arguments: [ + "PRODUCE INTO \"orders\" KEY \"k\" VALUE \"v\"", "CONSUME \"orders\" FROM OLDEST LIMIT 10", + "SELECT * FROM orders", "SHOW TOPICS", "DESCRIBE GROUP \"g\"", + ]) + func kafkaRunsDataWork(sql: String) { + #expect(refusal(sql, on: .kafka) == nil) + } +} diff --git a/apple/TableProTests/Core/Execution/EditModeDataCommandsLanguageTests.swift b/apple/TableProTests/Core/Execution/EditModeDataCommandsLanguageTests.swift new file mode 100644 index 0000000000..9594b470bc --- /dev/null +++ b/apple/TableProTests/Core/Execution/EditModeDataCommandsLanguageTests.swift @@ -0,0 +1,188 @@ +// +// EditModeDataCommandsLanguageTests.swift +// TableProTests +// + +import Foundation +@testable import TablePro +import Testing + +struct EditModeDataCommandsLanguageTests { + private func statement(_ sql: String, databaseType: DatabaseType) -> ExternalStatementGate.Statement { + ExternalStatementGate.Statement( + sql: sql, + connectionId: UUID(), + databaseType: databaseType, + externalAccess: .readWrite, + loadsExtensions: false, + allowsDestructive: false, + allowsMultiStatement: false, + destructiveAlternative: nil, + allowsOnlyDataStatements: true + ) + } + + private func refusal(_ statement: ExternalStatementGate.Statement) -> ExternalStatementGateError? { + do { + _ = try ExternalStatementGate.classify(statement) + return nil + } catch let error as ExternalStatementGateError { + return error + } catch { + return nil + } + } + + // MARK: - MongoDB + + @Test("Edit refuses a MongoDB session, schema, cursor or server command, and a command it cannot name", arguments: [ + "use admin", + "db.orders.createIndex({a: 1})", + #"db.createCollection("x")"#, + #"db.createView("v", "orders", [])"#, + #"db.orders.hideIndex("a_1")"#, + #"db.runCommand({collMod: "orders", validator: {}})"#, + #"db.runCommand({create: "x"})"#, + #"db.runCommand({killCursors: "orders", cursors: []})"#, + #"db.runCommand({validate: "orders", repair: true})"#, + "db.killOp(123)", + "sleep(1000)", + #"db.orders[("insert" + "One")]({a: 1})"#, + "db.runCommand({insert: 1, documents: []})" + ]) + func mongoNonDataRefused(sql: String) { + #expect(refusal(statement(sql, databaseType: .mongodb)) == .denied(ExternalStatementGate.dataStatementRefusal)) + } + + @Test("Edit refuses a MongoDB write into admin, config, local or a system collection", arguments: [ + #"db.getSiblingDB("admin").getCollection("system.users").insertOne({user: "x"})"#, + #"db.getSiblingDB("config").settings.updateOne({_id: "chunksize"}, {$set: {value: 1}}, {upsert: true})"#, + #"db.getCollection("system.js").insertOne({_id: "f"})"#, + #"db.getSiblingDB("local").startup_log.insertOne({a: 1})"#, + #"db.getMongo().getDB("Admin").orders.insertOne({a: 1})"#, + #"db.adminCommand({insert: "system.version", documents: [{}]})"#, + #"db.adminCommand({insert: "orders", documents: [{a: 1}]})"#, + #"db.runCommand({update: "settings", updates: [{q: {_id: 1}, u: {$set: {a: 1}}}], $db: "config"})"#, + #"db.runCommand({insert: "orders", documents: [{a: 1}], insert: "system.js"})"# + ]) + func mongoSystemWriteRefused(sql: String) { + #expect(refusal(statement(sql, databaseType: .mongodb)) == .denied(ExternalStatementGate.dataStatementRefusal)) + } + + @Test("Edit runs MongoDB reads and data changes", arguments: [ + "db.orders.find({a: 1}).limit(5)", + "db.orders.aggregate([{$match: {}}])", + "show collections", + "db.orders.insertOne({a: 1})", + "db.orders.insertMany([{a: 1}])", + "db.orders.updateOne({_id: 1}, {$set: {a: 2}})", + "db.orders.updateMany({a: 1}, {$set: {b: 1}})", + "db.orders.replaceOne({_id: 1}, {a: 3})", + "db.orders.deleteOne({_id: 1})", + "db.orders.findOneAndUpdate({_id: 1}, {$inc: {n: 1}})", + "db.orders.bulkWrite([{insertOne: {document: {a: 1}}}])", + #"db.getSiblingDB("shop").orders.insertOne({a: 1})"#, + #"db.getSiblingDB("admin").getCollection("system.users").find({})"#, + #"db.runCommand({insert: "orders", documents: [{a: 1}]})"#, + #"db.runCommand({findAndModify: "orders", query: {_id: 1}, update: {$set: {a: 1}}})"#, + #"db.runCommand({find: "orders"})"#, + #"db.runCommand({listCollections: 1})"# + ]) + func mongoDataPasses(sql: String) { + #expect(refusal(statement(sql, databaseType: .mongodb)) == nil) + } + + @Test("MongoDB data calls are named in mongosh notation and commands by their command name") + func mongoNames() { + #expect(QueryClassifier.mongoDataCommands("db.orders.insertOne({a: 1})", systemDatabases: []) + == [DataCommand.named("db.collection.insertOne")]) + #expect(QueryClassifier.mongoDataCommands(#"db["orders"].updateMany({}, {$set: {a: 1}})"#, systemDatabases: []) + == [DataCommand.named("db.collection.updateMany")]) + #expect(QueryClassifier.mongoDataCommands(#"db.runCommand({insert: "orders"})"#, systemDatabases: []) + == [DataCommand.named("insert")]) + #expect(QueryClassifier.mongoDataCommands("db.orders.find({}).limit(1)", systemDatabases: []) + == [DataCommand.read, .read]) + #expect(QueryClassifier.mongoDataCommands("db.orders.find(", systemDatabases: []) == nil) + } + + // MARK: - Neo4j + + @Test("Edit refuses Cypher schema, privilege, database, transaction and server commands", arguments: [ + "CREATE INDEX person_name FOR (n:Person) ON (n.name)", + "CREATE CONSTRAINT c FOR (n:Person) REQUIRE n.id IS UNIQUE", + "CREATE USER bob SET PASSWORD 'secret' CHANGE NOT REQUIRED", + "GRANT ROLE admin TO bob", + "ALTER USER bob SET PASSWORD 'x'", + "CREATE DATABASE other", + "CREATE OR REPLACE ALIAS a FOR DATABASE neo4j", + "ALTER DATABASE neo4j SET ACCESS READ ONLY", + "START DATABASE neo4j", + "SHOW TRANSACTIONS YIELD transactionId AS id TERMINATE TRANSACTIONS id", + "ALTER CURRENT GRAPH TYPE SET {(p:Person => {name :: STRING})}", + "USE system CREATE USER x SET PASSWORD 'y'", + "CYPHER 25 CREATE INDEX i FOR (n:N) ON (n.x)", + "EXPLAIN CREATE CONSTRAINT c FOR (n:N) REQUIRE n.x IS UNIQUE", + "MERGE (n:N {id: 1}) ON CREATE INDEX i FOR (m:N) ON (m.x)", + #"CREATE INDEX i FOR (n:N) ON (n.x)"# + ]) + func cypherAdministrationRefused(sql: String) { + #expect(refusal(statement(sql, databaseType: .neo4j)) == .denied(ExternalStatementGate.dataStatementRefusal)) + } + + @Test("Edit refuses a Cypher procedure that is neither a read nor a listed data change", arguments: [ + "CALL dbms.setConfigValue('db.logs.query.enabled', 'OFF')", + "CALL tx.setMetaData({app: 'x'})", + "CALL db.createLabel('X')", + "CALL `db`.`createLabel`('X')", + "CALL db.schema.`visualization`()", + "MATCH (n) CALL apoc.periodic.iterate('MATCH (m) RETURN m', 'SET m.x = 1', {})" + ]) + func cypherProcedureRefused(sql: String) { + #expect(refusal(statement(sql, databaseType: .neo4j)) == .denied(ExternalStatementGate.dataStatementRefusal)) + } + + @Test("Edit runs Cypher reads and data changes", arguments: [ + "MATCH (n:Person) RETURN n LIMIT 10", + "CREATE (n:Person {name: 'a'})", + "CREATE p = (a:A)-[:R]->(b:B) RETURN p", + "MERGE (n:Person {id: 1}) ON CREATE SET n.created = timestamp()", + "MATCH (n:Person {id: 1}) SET n.name = 'b'", + "MATCH (n:Person {id: 1}) REMOVE n:Temp", + "MATCH (n:Person {id: 1}) DETACH DELETE n", + "UNWIND [1, 2] AS i CREATE (:N {i: i})", + "MATCH (n:N) CALL (n) { SET n.x = 1 } IN TRANSACTIONS", + "CALL db.labels()", + "MATCH (d:Doc {id: 1}) CALL db.create.setNodeVectorProperty(d, 'embedding', [0.1, 0.2])", + "SHOW INDEXES", + "EXPLAIN MATCH (n) RETURN n", + "CYPHER 25 runtime=slotted MATCH (n) RETURN n", + "USE neo4j MATCH (n) RETURN count(n)", + "USE graph.byName('shop') MATCH (n) RETURN n" + ]) + func cypherDataPasses(sql: String) { + #expect(refusal(statement(sql, databaseType: .neo4j)) == nil) + } + + @Test("Cypher data clauses are named upper case and procedures lower case") + func cypherNames() { + #expect(QueryClassifier.cypherDataCommands("MATCH (n {id: 1}) DETACH DELETE n") + == [DataCommand.named("DETACH DELETE")]) + #expect(QueryClassifier.cypherDataCommands("MERGE (n:N {id: 1}) ON CREATE SET n.x = 1") + == [DataCommand.named("MERGE"), .named("SET")]) + #expect(QueryClassifier.cypherDataCommands("MATCH (d) CALL db.create.setVectorProperty(d, 'e', [1.0])") + == [DataCommand.named("db.create.setvectorproperty")]) + #expect(QueryClassifier.cypherDataCommands("MATCH (n) RETURN n") == [DataCommand.read]) + } + + // MARK: - Refused earlier + + @Test("A destructive command is refused as destructive before the data rule reads it", arguments: [ + (DatabaseType.mongodb, #"db.runCommand({createUser: "bob", pwd: "x", roles: []})"#), + (.mongodb, "db.orders.deleteMany({})"), + (.neo4j, "DROP INDEX person_name"), + (.neo4j, "REVOKE ROLE admin FROM bob") + ]) + func destructiveRefusedFirst(type: DatabaseType, sql: String) { + #expect(refusal(statement(sql, databaseType: type)) == .denied(ExternalStatementGate.destructiveRefusal)) + } +} diff --git a/apple/TableProTests/Core/Execution/EditModeDataCommandsRequestTests.swift b/apple/TableProTests/Core/Execution/EditModeDataCommandsRequestTests.swift new file mode 100644 index 0000000000..a58d5543f6 --- /dev/null +++ b/apple/TableProTests/Core/Execution/EditModeDataCommandsRequestTests.swift @@ -0,0 +1,236 @@ +// +// EditModeDataCommandsRequestTests.swift +// TableProTests +// + +import Foundation +@testable import TablePro +import Testing + +struct EditModeDataCommandsRequestTests { + private func statement(_ sql: String, databaseType: DatabaseType) -> ExternalStatementGate.Statement { + ExternalStatementGate.Statement( + sql: sql, + connectionId: UUID(), + databaseType: databaseType, + externalAccess: .readWrite, + loadsExtensions: false, + allowsDestructive: false, + allowsOnlyDataStatements: true + ) + } + + private func refusal(_ sql: String, on databaseType: DatabaseType) -> ExternalStatementGateError? { + do { + try ExternalStatementGate.classify(statement(sql, databaseType: databaseType)) + return nil + } catch let error as ExternalStatementGateError { + return error + } catch { + return nil + } + } + + @Test("Edit refuses an Elasticsearch request that is not a read or a document change", arguments: [ + "PUT /orders2", + #"PUT /orders/_mapping\#n{"properties": {"note": {"type": "keyword"}}}"#, + #"POST /_aliases\#n{"actions": [{"add": {"index": "orders", "alias": "o"}}]}"#, + #"PUT /_security/user/bob\#n{"password": "secret123", "roles": ["superuser"]}"#, + #"POST /_security/api_key\#n{"name": "k"}"#, + #"PUT /_cluster/settings\#n{"persistent": {"action.destructive_requires_name": false}}"#, + #"PUT /_ilm/policy/purge\#n{"policy": {"phases": {"delete": {"min_age": "0ms", "actions": {"delete": {}}}}}}"#, + "GET /_flush", + #"PUT /_ingest/pipeline/p\#n{"processors": [{"set": {"field": "x", "value": 1}}]}"#, + #"PUT /_watcher/watch/w\#n{"trigger": {"schedule": {"interval": "1m"}}, "actions": {}}"#, + #"PUT /_inference/text_embedding/e\#n{"service": "openai", "service_settings": {"api_key": "k", "model_id": "m"}}"#, + "POST /_update_by_query/abc:1/_rethrottle?requests_per_second=-1", + #"POST /_reindex\#n{"source": {"index": "orders"}, "dest": {"index": "orders2"}}"#, + #"PUT /.watches/_doc/w1\#n{"trigger": {}}"#, + #"POST /_bulk\#n{"index": {"_index": ".security-7", "_id": "x"}}\#n{"a": 1}"#, + #"POST /orders*/_update_by_query?expand_wildcards=all\#n{"query": {"match_all": {}}}"#, + "PUT /orders/_doc/../../_cluster/settings\n{}", + #"POST /_query/async\#n{"query": "FROM orders", "keep_on_completion": true}"#, + ]) + func elasticsearchRefusesTheRest(sql: String) { + #expect(refusal(sql, on: .elasticsearch) == .denied(ExternalStatementGate.dataStatementRefusal)) + } + + @Test("Edit runs Elasticsearch reads and document changes", arguments: [ + #"PUT /orders/_doc/1\#n{"total": 5}"#, + #"POST /orders/_doc\#n{"total": 5}"#, + #"PUT /orders/_create/2\#n{"total": 7}"#, + #"POST /orders/_update/1\#n{"doc": {"total": 6}}"#, + #"POST /orders/_update_by_query\#n{"query": {"term": {"status": "new"}}, "script": {"source": "ctx._source.status = 'old'"}}"#, + #"POST /_bulk\#n{"index": {"_index": "orders", "_id": "3"}}\#n{"total": 1}"#, + #"GET /orders/_update_by_query\#n{"query": {"match_all": {}}, "script": {"source": "ctx._source.n = 1"}}"#, + "GET /orders/_search", + #"POST /_query\#n{"query": "FROM orders | LIMIT 5"}"#, + #"POST /orders/_mget\#n{"ids": ["1", "2"]}"#, + #"GET /_all/_search\#n{"query": {"match_all": {}}}"#, + ]) + func elasticsearchRunsDataRequests(sql: String) { + #expect(refusal(sql, on: .elasticsearch) == nil) + } + + @Test("Edit refuses a Typesense request that is not a read or a document change", arguments: [ + #"POST /collections\#n{"name": "c", "fields": [{"name": "t", "type": "string"}]}"#, + #"PATCH /collections/books\#n{"fields": [{"name": "title", "drop": true}]}"#, + #"PUT /aliases/a\#n{"collection_name": "books"}"#, + #"PUT /collections/books/synonyms/s\#n{"synonyms": ["a", "b"]}"#, + #"POST /config\#n{"log-slow-requests-time-ms": 1}"#, + "POST /operations/cache/clear", + #"POST /conversations/models\#n{"model_name": "openai/gpt-4o", "api_key": "k", "history_collection": "h"}"#, + #"POST /proxy\#n{"url": "http://169.254.169.254/", "method": "GET"}"#, + #"POST /analytics/events\#n{"type": "click", "name": "e", "data": {}}"#, + "POST /collections/books/documents/../../../config\n{}", + ]) + func typesenseRefusesTheRest(sql: String) { + #expect(refusal(sql, on: .typesense) == .denied(ExternalStatementGate.dataStatementRefusal)) + } + + @Test("Edit runs Typesense reads and document changes", arguments: [ + #"POST /collections/books/documents\#n{"id": "1", "title": "x"}"#, + #"POST /collections/books/documents?action=upsert\#n{"id": "1", "title": "y"}"#, + #"POST /collections/books/documents/import?action=upsert\#n{"id": "1", "title": "a"}\#n{"id": "2", "title": "b"}"#, + #"PATCH /collections/books/documents/1\#n{"title": "z"}"#, + #"PATCH /collections/books/documents?filter_by=year:<2000\#n{"old": true}"#, + "GET /collections/books/documents/search?q=*&query_by=title", + #"POST /multi_search\#n{"searches": [{"collection": "books", "q": "*"}]}"#, + ]) + func typesenseRunsDataRequests(sql: String) { + #expect(refusal(sql, on: .typesense) == nil) + } + + @Test("Edit refuses a Qdrant request that is not a read or a point change", arguments: [ + #"PUT /collections/docs\#n{"vectors": {"size": 4, "distance": "Cosine"}}"#, + #"PATCH /collections/docs\#n{"optimizers_config": {"indexing_threshold": 0}}"#, + #"PUT /collections/docs/index\#n{"field_name": "city", "field_schema": "keyword"}"#, + #"PUT /collections/docs/vectors/v2\#n{"size": 4, "distance": "Dot"}"#, + #"PUT /collections/docs/shards\#n{"shard_key": "k"}"#, + "PUT /quotas\n{}", + #"POST /locks\#n{"write": true, "error_message": "x"}"#, + ]) + func qdrantRefusesTheRest(sql: String) { + #expect(refusal(sql, on: .qdrant) == .denied(ExternalStatementGate.dataStatementRefusal)) + } + + @Test("Edit runs Qdrant reads and point changes", arguments: [ + #"PUT /collections/docs/points?wait=true\#n{"points": [{"id": 1, "vector": [0.1, 0.2, 0.3, 0.4], "payload": {"city": "Hanoi"}}]}"#, + #"POST /collections/docs/points/payload\#n{"payload": {"a": 1}, "points": [1]}"#, + #"PUT /collections/docs/points/vectors\#n{"points": [{"id": 1, "vector": [0.4, 0.3, 0.2, 0.1]}]}"#, + #"POST /collections/docs/points/query\#n{"query": [0.1, 0.2, 0.3, 0.4], "limit": 3}"#, + "GET /collections/docs", + ]) + func qdrantRunsDataRequests(sql: String) { + #expect(refusal(sql, on: .qdrant) == nil) + } + + @Test("Edit refuses a Weaviate request that is not a read or an object change", arguments: [ + #"POST /v1/schema\#n{"class": "Article"}"#, + #"POST /v1/schema/Article/properties\#n{"name": "x", "dataType": ["text"]}"#, + #"POST /v1/schema/Article/tenants\#n[{"name": "t1"}]"#, + "POST /v1/users/db/bob", + #"POST /v1/authz/users/bob/assign\#n{"roles": ["admin"]}"#, + #"POST /v1/backups/filesystem\#n{"id": "b1"}"#, + #"POST /v1/mcp\#n{"jsonrpc": "2.0", "id": 1, "method": "tools/list"}"#, + #"POST /v1/classifications/\#n{"class": "Article", "classifyProperties": ["cat"], "basedOnProperties": ["title"], "type": "knn"}"#, + #"PUT /v1/objects/../schema/Article\#n{"class": "Article"}"#, + ]) + func weaviateRefusesTheRest(sql: String) { + #expect(refusal(sql, on: .weaviate) == .denied(ExternalStatementGate.dataStatementRefusal)) + } + + @Test("Edit runs Weaviate reads and object changes", arguments: [ + #"POST /v1/objects\#n{"class": "Article", "properties": {"title": "x"}}"#, + #"PATCH /v1/objects/Article/36ddd591-2dee-4e7e-a3cc-eb86d30a4303\#n{"properties": {"title": "y"}}"#, + #"POST /v1/batch/objects\#n{"objects": [{"class": "Article", "properties": {"title": "a"}}]}"#, + #"POST /v1/batch/references\#n[{"from": "weaviate://localhost/Article/1/author", "to": "weaviate://localhost/Author/2"}]"#, + "{ Get { Article { title } } }", + #"POST /v1/graphql\#n{"query": "{ Get { Article { title } } }"}"#, + "GET /v1/objects?class=Article&limit=10", + ]) + func weaviateRunsDataRequests(sql: String) { + #expect(refusal(sql, on: .weaviate) == nil) + } + + @Test("Edit refuses a Realtime Database write into the server's own nodes", arguments: [ + #"PUT .settings/defaultWriteSizeLimit "unlimited""#, + "PUT .settings/strictTriggerValidation false", + #"PATCH / {".settings/defaultWriteSizeLimit": "unlimited"}"#, + ]) + func realtimeDatabaseRefusesServerNodes(sql: String) { + #expect(refusal(sql, on: .firebaseRealtimeDatabase) == .denied(ExternalStatementGate.dataStatementRefusal)) + } + + @Test("Edit runs Realtime Database reads and node changes", arguments: [ + #"PUT users/alice {"age": 31}"#, + #"PATCH users/alice {"age": 32}"#, + #"PATCH / {"users/alice/age": 33}"#, + #"POST logs {"msg": "hi"}"#, + #"GET users?orderBy="$key"&limitToFirst=5"#, + ]) + func realtimeDatabaseRunsDataRequests(sql: String) { + #expect(refusal(sql, on: .firebaseRealtimeDatabase) == nil) + } + + @Test("Edit refuses a Firestore write through a dot segment, and the PUT Firestore lacks", arguments: [ + #"PATCH ./../x/y {"a": 1}"#, + #"POST ./../x {"a": 1}"#, + #"PUT users/alice {"age": 31}"#, + ]) + func firestoreRefusesTheRest(sql: String) { + #expect(refusal(sql, on: .firestore) == .denied(ExternalStatementGate.dataStatementRefusal)) + } + + @Test("Edit runs Firestore reads and document changes", arguments: [ + #"POST users?documentId=alice {"age": 31}"#, + #"PATCH users/alice?updateMask=age {"age": 32}"#, + #"QUERY {"structuredQuery": {"from": [{"collectionId": "users"}]}}"#, + "GET users/alice", + ]) + func firestoreRunsDataRequests(sql: String) { + #expect(refusal(sql, on: .firestore) == nil) + } + + @Test("Edit refuses a Bigtable request the driver cannot read", arguments: ["CREATE events", "DROP events"]) + func bigtableRefusesTheRest(sql: String) { + #expect(refusal(sql, on: .bigtable) == .denied(ExternalStatementGate.dataStatementRefusal)) + } + + @Test("Edit runs Bigtable reads and row changes", arguments: [ + #"INSERT events/user#42 {"profile:name": "Ann"}"#, + #"PUT events/user#42 {"profile:name": "Bo"}"#, + "GET events/user#42", + "SCAN events/user#?limit=20", + #"READ events {"rowsLimit": "10"}"#, + ]) + func bigtableRunsDataRequests(sql: String) { + #expect(refusal(sql, on: .bigtable) == nil) + } + + // Every HTTP DELETE tiers destructive, so the destructive rule refuses it before the data rule reads the list. + @Test("A data delete is on its engine's list, and Edit still refuses it as destructive", arguments: [ + (DatabaseType.elasticsearch, "DELETE /orders/_doc/1"), + (.typesense, "DELETE /collections/books/documents/1"), + (.qdrant, #"POST /collections/docs/points/delete\#n{"points": [1]}"#), + (.weaviate, "DELETE /v1/objects/Article/36ddd591-2dee-4e7e-a3cc-eb86d30a4303"), + (.firestore, "DELETE users/alice"), + (.firebaseRealtimeDatabase, "DELETE users/alice"), + (.bigtable, "DELETE events/user#42"), + ]) + func dataDeleteIsListedButDestructive(type: DatabaseType, sql: String) { + #expect(QueryClassifier.isDataStatement(sql, databaseType: type)) + #expect(refusal(sql, on: type) == .denied(ExternalStatementGate.destructiveRefusal)) + } + + @Test("A write into the engine's own containers is not data, even where another rule refuses it first", arguments: [ + (DatabaseType.firebaseRealtimeDatabase, #"PUT .settings/rules {"rules": {".read": true, ".write": true}}"#), + (.firebaseRealtimeDatabase, #"PUT %2Esettings/rules {"rules": {".read": true}}"#), + (.firestore, "DELETE ./../collectionGroups/users/indexes/abc"), + (.elasticsearch, "DELETE /.security-7/_doc/x"), + ]) + func systemWriteIsNotData(type: DatabaseType, sql: String) { + #expect(!QueryClassifier.isDataStatement(sql, databaseType: type)) + #expect(refusal(sql, on: type) != nil) + } +} diff --git a/apple/TableProTests/Core/Execution/ExternalStatementGateTests.swift b/apple/TableProTests/Core/Execution/ExternalStatementGateTests.swift index 68cdca381e..5f2566a69f 100644 --- a/apple/TableProTests/Core/Execution/ExternalStatementGateTests.swift +++ b/apple/TableProTests/Core/Execution/ExternalStatementGateTests.swift @@ -68,16 +68,26 @@ struct ExternalStatementGateTests { } } - // A query language of the engine's own has no data statement to tell from a definition, so the flag leaves it to - // the tier rules. - @Test("A caller held to data statements keeps the tier rules on an engine with its own query language", arguments: [ - (DatabaseType.mongodb, "db.orders.insertOne({a: 1})"), - (.kafka, "PRODUCE INTO \"orders\" KEY \"k\" VALUE \"v\""), - (.surrealdb, "DEFINE TABLE person SCHEMAFULL"), + // An engine with a query language of its own is read by its own reader against its curated list. + @Test("A caller held to data statements runs data and refuses a definition on an engine with its own language", arguments: [ + (DatabaseType.mongodb, "db.orders.insertOne({a: 1})", true), + (.kafka, "PRODUCE INTO \"orders\" KEY \"k\" VALUE \"v\"", true), + (.surrealdb, "CREATE person:one SET name = 'a'", true), + (.surrealdb, "DEFINE TABLE person SCHEMAFULL", false), + (.surrealdb, "SELECT fn::mk() FROM ONLY person:one", false), ]) - func dataStatementsOnlyLeavesOtherLanguages(type: DatabaseType, sql: String) { + func dataStatementsOnlyReadsOtherLanguages(type: DatabaseType, sql: String, runs: Bool) { #expect(QueryClassifier.isSQLDataStatement(sql, databaseType: type) == nil) - #expect(refusal(statement(sql, databaseType: type, allowsOnlyDataStatements: true)) == nil) + let refused = refusal(statement(sql, databaseType: type, allowsOnlyDataStatements: true)) + #expect(refused == (runs ? nil : .denied(ExternalStatementGate.dataStatementRefusal))) + } + + @Test("A write into a keyspace or database the engine keeps its own state in is a system scope") + func systemScopesOfOtherLanguages() { + #expect(QueryClassifier.writesSystemScope(database: "system_auth", schema: nil, databaseType: .cassandra)) + #expect(QueryClassifier.writesSystemScope(database: "admin", schema: nil, databaseType: .mongodb)) + #expect(!QueryClassifier.writesSystemScope(database: "shop", schema: nil, databaseType: .mongodb)) + #expect(!QueryClassifier.writesSystemScope(database: "admin", schema: nil, databaseType: .postgresql)) } @Test("A MongoDB read passes a connection that is read only for external clients", arguments: [ diff --git a/apple/TableProTests/Core/Services/Execution/BareFunctionCatalogReadTests.swift b/apple/TableProTests/Core/Services/Execution/BareFunctionCatalogReadTests.swift new file mode 100644 index 0000000000..3bc5e9cd23 --- /dev/null +++ b/apple/TableProTests/Core/Services/Execution/BareFunctionCatalogReadTests.swift @@ -0,0 +1,130 @@ +// +// BareFunctionCatalogReadTests.swift +// TableProTests +// + +import Foundation +@testable import TablePro +import TableProPluginKit +import TableProSQLGrammar +import Testing + +// Answers the catalog the way Oracle 23ai answered for a schema with function PURGE, table ORDERS(ID, PURGE) and DUAL. +private final class CatalogDriver: PluginDriverStub, @unchecked Sendable { + private let fails: Bool + + init(fails: Bool) { + self.fails = fails + } + + func execute(query: String) async throws -> PluginQueryResult { + if fails { throw DatabaseError.notConnected } + let rows: [[PluginCellValue]] + if query == SQLCallableCatalog.query { + rows = [[.text("F"), .text("PURGE")]] + } else if query.contains("'DUAL'") { + rows = [[.text("0"), .text("3"), .null, .text("DUMMY")]] + } else { + rows = [[.text("0"), .text("1"), .null, .text("ID")], [.text("0"), .text("1"), .null, .text("PURGE")]] + } + return PluginQueryResult( + columns: rows.first.map { $0.indices.map { "C\($0)" } } ?? [], + columnTypeNames: rows.first.map { $0.map { _ in "VARCHAR2" } } ?? [], + rows: rows, + rowsAffected: 0, + executionTime: 0 + ) + } +} + +@MainActor +private final class CatalogProvider: ScopedMetadataProviding { + private let driver: DatabaseDriver + private let browse: DatabaseScope? + private(set) var scopes: [DatabaseScope] = [] + + init(fails: Bool = false, browse: DatabaseScope?) { + let connection = DatabaseConnection(name: "Probe", database: "FREEPDB1", type: .oracle) + driver = PluginDriverAdapter(connection: connection, pluginDriver: CatalogDriver(fails: fails)) + self.browse = browse + } + + func withMetadataDriver( + scope: DatabaseScope, + workload: MetadataConnectionPool.Workload, + _ body: @Sendable @escaping (DatabaseDriver) async throws -> T + ) async throws -> T { + scopes.append(scope) + return try await body(driver) + } + + func browseScope(for connectionId: UUID) -> DatabaseScope? { browse } +} + +@MainActor +struct BareFunctionCatalogReadTests { + private static func reading(_ sql: String) -> SQLBareNameReading { + SQLBareNameCalls.read(sql, databaseTypeId: "Oracle", readings: DatabaseType.oracle.lexicalReadings) + } + + private static func request(_ sql: String, connectionId: UUID, runsIn: DatabaseScope? = nil) -> OperationRequest { + OperationRequest( + connectionId: connectionId, + databaseType: .oracle, + sql: sql, + kind: .readQuery, + caller: .userInterface, + capabilities: .interactiveUser, + operationDescription: "Execute Query", + runsIn: runsIn + ) + } + + @Test("The session's catalog tells a function named bare from a column, in the schema the tab runs in") + func readsTheCatalogInTheTabScope() async { + let connectionId = UUID() + let browse = DatabaseScope(connectionId: connectionId, database: "FREEPDB1", schema: "APP") + let tab = DatabaseScope(connectionId: connectionId, database: "FREEPDB1", schema: "HR") + let provider = CatalogProvider(browse: browse) + + let call = await ExecutionGateProvider.callsBareFunction( + Self.request("SELECT purge FROM dual", connectionId: connectionId, runsIn: tab), + reading: Self.reading("SELECT purge FROM dual"), + catalog: SQLCallableCatalogCache(), + metadata: provider + ) + let column = await ExecutionGateProvider.callsBareFunction( + Self.request("SELECT purge FROM orders", connectionId: connectionId), + reading: Self.reading("SELECT purge FROM orders"), + catalog: SQLCallableCatalogCache(), + metadata: provider + ) + + #expect(call) + #expect(!column) + #expect(provider.scopes.first == tab) + #expect(provider.scopes.last == browse) + } + + @Test("A catalog that cannot be read, or no scope to read it in, counts as a call") + func unreadableCatalogRefuses() async { + let connectionId = UUID() + let browse = DatabaseScope(connectionId: connectionId, database: "FREEPDB1", schema: nil) + + let failed = await ExecutionGateProvider.callsBareFunction( + Self.request("SELECT id FROM orders", connectionId: connectionId), + reading: Self.reading("SELECT id FROM orders"), + catalog: SQLCallableCatalogCache(), + metadata: CatalogProvider(fails: true, browse: browse) + ) + let unscoped = await ExecutionGateProvider.callsBareFunction( + Self.request("SELECT id FROM orders", connectionId: connectionId), + reading: Self.reading("SELECT id FROM orders"), + catalog: SQLCallableCatalogCache(), + metadata: CatalogProvider(browse: nil) + ) + + #expect(failed) + #expect(unscoped) + } +} diff --git a/apple/TableProTests/Core/Services/Execution/ExecutionGateTests.swift b/apple/TableProTests/Core/Services/Execution/ExecutionGateTests.swift index 77f1f2668e..c2112f40d1 100644 --- a/apple/TableProTests/Core/Services/Execution/ExecutionGateTests.swift +++ b/apple/TableProTests/Core/Services/Execution/ExecutionGateTests.swift @@ -954,6 +954,61 @@ struct ExecutionGateTests { } } + // Oracle and Dameng call a function named bare; the session's catalog tells it from a column. + private actor ResolverCalls { + private(set) var count = 0 + func record() { count += 1 } + } + + private func makeBareNameGate(calls: ResolverCalls, answer: Bool) -> DefaultExecutionGate { + let policy = Self.writableFunctions + return DefaultExecutionGate( + confirming: StubConfirming(answer: true), + authenticating: StubAuthenticating(answer: true), + safeModeLevelResolver: { _ in .readOnly }, + forcesWriteResolver: { _ in false }, + readOnlyFunctionPolicyResolver: { _, _ in policy }, + bareNameCallResolver: { _, _ in + await calls.record() + return answer + } + ) + } + + @Test("Read-Only on Oracle and Dameng refuses a name the catalog resolves to a function", arguments: [ + DatabaseType.oracle, .dameng, + ]) + func readOnlyRefusesABareFunctionName(type: DatabaseType) async { + let calls = ResolverCalls() + let gate = makeBareNameGate(calls: calls, answer: true) + + let decision = await gate.authorize(makeRequest(sql: "SELECT purge_orders FROM dual", kind: .readQuery, databaseType: type)) + + #expect(decision.deniedReason?.contains("calls a function that can change data") == true) + #expect(await calls.count == 1) + } + + @Test("A name the catalog reads as a column runs, and a query naming nothing never asks the catalog") + func bareNameAsksOnlyWhenItMust() async { + let calls = ResolverCalls() + let gate = makeBareNameGate(calls: calls, answer: false) + + #expect(await gate.authorize(makeRequest(sql: "SELECT id FROM orders", kind: .readQuery, databaseType: .oracle)).isAuthorized) + #expect(await calls.count == 1) + #expect(await gate.authorize(makeRequest(sql: "SELECT 1 FROM dual", kind: .readQuery, databaseType: .oracle)).isAuthorized) + #expect(await gate.authorize(makeRequest(sql: "SELECT id FROM orders", kind: .readQuery, databaseType: .mysql)).isAuthorized) + #expect(await calls.count == 1) + } + + @Test("A package function named bare is refused without the catalog") + func dottedBareCallRefusedByDefault() async { + let gate = makeReadOnlyGate(policy: Self.writableFunctions, confirm: StubConfirming(answer: true)) + + let decision = await gate.authorize(makeRequest(sql: "SELECT pkg.purge FROM dual", kind: .readQuery, databaseType: .oracle)) + + #expect(!decision.isAuthorized) + } + @Test("A caller that cannot prompt gets the same refusal") func readOnlyFunctionCallDeniedWithoutPrompt() async { let confirm = StubConfirming(answer: true) diff --git a/apple/TableProTests/Views/Main/ReadOnlySafeModeEntryPointTests.swift b/apple/TableProTests/Views/Main/ReadOnlySafeModeEntryPointTests.swift index c7529f4654..0032a8c583 100644 --- a/apple/TableProTests/Views/Main/ReadOnlySafeModeEntryPointTests.swift +++ b/apple/TableProTests/Views/Main/ReadOnlySafeModeEntryPointTests.swift @@ -307,6 +307,19 @@ struct ReadOnlySafeModeEntryPointTests { #expect(browses.count == 2) } + // Oracle and Dameng call a function named bare, which only the catalog the gate reads can tell from a column. + @Test("On Oracle and Dameng every raw filter goes to the gate at Read-Only, parentheses or not", arguments: [ + DatabaseType.oracle, .dameng, + ]) + func bareNameRawFilterGoesToTheGate(type: DatabaseType) { + let (fixture, _) = makeFixture(.readOnly, type: type, enforcesReadOnlyMode: false) + let (postgres, _) = makeFixture(.readOnly, enforcesReadOnlyMode: false) + let filters = [TableFilter.rawCondition("purge_orders IS NULL")] + + #expect(fixture.coordinator.rawFilterNeedsReadOnlyGate(filters)) + #expect(!postgres.coordinator.rawFilterNeedsReadOnlyGate(filters)) + } + // 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 { diff --git a/apple/project.yml b/apple/project.yml index 345f475d4a..c5848b3bd9 100644 --- a/apple/project.yml +++ b/apple/project.yml @@ -322,6 +322,9 @@ targets: # The etcd plugin's own command parser, so the app tiers an etcd statement by the operation # the driver parses it into. - Plugins/EtcdDriverPlugin/EtcdCommandParser.swift + # The Kafka plugin's KafkaQL parser, so the app reads a KafkaQL statement as the driver does. + - Packages/TableProCore/Sources/TableProKafkaCore/KafkaQL.swift + - Packages/TableProCore/Sources/TableProKafkaCore/KafkaStatement.swift # The MySQL plugin's socket key and path rules, so the app writes and checks the socket path # exactly as the driver reads it. - Plugins/MySQLDriverPlugin/MySQLLocalSocket.swift @@ -416,7 +419,7 @@ targets: - package: Yams - package: PostHog - package: TableProCore - products: [TableProAutocomplete, TableProConnectionLibrary, TableProCoreTypes, TableProEtcdCore, TableProFirebaseCore, TableProGeometry, TableProGoogleCloud, TableProGzip, TableProHTTP, TableProImport, TableProInfluxDBCore, TableProLogRedaction, TableProNumberFormatting, TableProProcess, TableProQdrantCore, TableProRedisCore, TableProSQLGrammar, TableProSQLQuoting, TableProSQLVocabulary, TableProSSHTransport, TableProSyncTransport, TableProTLSClientIdentity, TableProTabular, TableProTabularIO, TableProTheme, TableProWeaviateCore] + products: [TableProAutocomplete, TableProConnectionLibrary, TableProCoreTypes, TableProEtcdCore, TableProFirebaseCore, TableProGeometry, TableProGoogleCloud, TableProGzip, TableProHTTP, TableProImport, TableProInfluxDBCore, TableProLogRedaction, TableProMemcachedCore, TableProNumberFormatting, TableProProcess, TableProQdrantCore, TableProRedisCore, TableProSQLGrammar, TableProSQLQuoting, TableProSQLVocabulary, TableProSSHTransport, TableProSyncTransport, TableProTLSClientIdentity, TableProTabular, TableProTabularIO, TableProTheme, TableProWeaviateCore] - package: TableProMacCore products: [TableProCloudAPI, TableProMCP, TableProMCPClients, TableProSSHConfig, TableProUtilities] # Writes nothing for a version CHANGELOG.md has no section for, so a 0.0.0-dev build ships no notes. diff --git a/apple/scripts/probes/check-bare-function-names.sh b/apple/scripts/probes/check-bare-function-names.sh new file mode 100755 index 0000000000..0f4cf05036 --- /dev/null +++ b/apple/scripts/probes/check-bare-function-names.sh @@ -0,0 +1,77 @@ +#!/usr/bin/env bash +# +# Check the name resolution SQLBareNameCalls hard-codes against a real Oracle and DM8 server. +# +# Oracle and Dameng run a function named without parentheses once the name is not a column of a table in scope. +# Packages/TableProCore/Sources/TableProSQLGrammar/SQLBareNameCalls+Parser.swift reads which tables a name can see +# (correlated subqueries see the outer query, inline views do not, ORDER BY sees select-list aliases) and treats a +# dotted name no table starts as a call. This runs each shape and reports whether the function ran. +# +# Usage: +# scripts/probes/check-bare-function-names.sh oracle # gvenzl/oracle-free:23-slim-faststart, APP_USER probe +# scripts/probes/check-bare-function-names.sh dameng # a DM8 image, SYSDBA password in DM8_PASSWORD +# +# Creates PURGE, WIPE, package PK, tables ORDERS and AUDIT_LOG in the probe schema. Each line prints the query and +# `calls` or `runs` (the function did not run); compare with SQLBareNameCallsTests. + +set -uo pipefail + +ENGINE="${1:?oracle or dameng}" +CONTAINER="${2:?container name}" + +case "$ENGINE" in + oracle) + run_sql() { docker exec -i "$CONTAINER" sqlplus -s probe/probe_pw@FREEPDB1; } + ;; + dameng) + PASSWORD="${DM8_PASSWORD:?set DM8_PASSWORD}" + run_sql() { + docker exec -i "$CONTAINER" bash -c "cat > /tmp/tp-probe.sql && cd /home/dmdba/dmdbms/bin \ + && ./disql -S SYSDBA/$PASSWORD@localhost:5236 \\\`/tmp/tp-probe.sql" + } + ;; + *) + echo "unknown engine: $ENGINE" >&2 + exit 3 + ;; +esac + +run_sql > /dev/null <<'SQL' +CREATE TABLE orders (id NUMBER, purge NUMBER); +INSERT INTO orders VALUES (1, 100); +CREATE TABLE audit_log (n NUMBER); +COMMIT; +CREATE OR REPLACE FUNCTION purge RETURN NUMBER IS PRAGMA AUTONOMOUS_TRANSACTION; BEGIN INSERT INTO audit_log VALUES (1); COMMIT; RETURN 42; END; +/ +CREATE OR REPLACE FUNCTION wipe RETURN NUMBER IS PRAGMA AUTONOMOUS_TRANSACTION; BEGIN INSERT INTO audit_log VALUES (2); COMMIT; RETURN 7; END; +/ +CREATE OR REPLACE PACKAGE pk AS FUNCTION f RETURN NUMBER; END; +/ +CREATE OR REPLACE PACKAGE BODY pk AS FUNCTION f RETURN NUMBER IS PRAGMA AUTONOMOUS_TRANSACTION; BEGIN INSERT INTO audit_log VALUES (3); COMMIT; RETURN 3; END; END; +/ +EXIT +SQL + +while IFS= read -r query; do + out="$(printf 'DELETE FROM audit_log;\nCOMMIT;\n%s\nSELECT '"'"'audit='"'"' || COUNT(*) FROM audit_log;\nEXIT\n' "$query" | run_sql 2>&1)" + if printf '%s' "$out" | grep -q 'audit=0'; then verdict=runs; else verdict=calls; fi + printf '%-6s %s\n' "$verdict" "$query" +done <<'QUERIES' +SELECT purge FROM orders; +SELECT purge FROM dual; +SELECT id FROM orders WHERE EXISTS (SELECT 1 FROM dual WHERE purge = 100); +SELECT * FROM orders, (SELECT purge FROM dual); +SELECT 1 AS wipe FROM dual ORDER BY wipe; +SELECT 1 AS wipe FROM dual WHERE wipe = 7; +SELECT 1 wipe FROM dual GROUP BY wipe; +SELECT pk.f FROM dual; +SELECT pk.f FROM orders pk; +WITH c AS (SELECT 1 wipe FROM dual) SELECT wipe FROM c; +SELECT purge FROM (SELECT * FROM orders); +SELECT "WIPE" FROM dual; +SELECT id, purge FROM orders UNION ALL SELECT 1, purge FROM dual; +SELECT * FROM (VALUES (wipe)) t (a); +SELECT * FROM orders, (VALUES (purge)) v (a); +SELECT * FROM orders MATCH_RECOGNIZE (ORDER BY id ALL ROWS PER MATCH PATTERN (x) DEFINE x AS wipe = 7); +SELECT * FROM orders MATCH_RECOGNIZE (ORDER BY id ALL ROWS PER MATCH PATTERN (x) DEFINE x AS purge = 100); +QUERIES From 84d99a8b06e30f4003b856109bd316cf79b47554 Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Fri, 13 Nov 2026 08:50:33 +0700 Subject: [PATCH 2/5] docs(changelog): add d-safety2 fragments --- apple/changelog.d/4260.changed.md | 1 + apple/changelog.d/4260.security.1.md | 1 + apple/changelog.d/4260.security.md | 1 + 3 files changed, 3 insertions(+) create mode 100644 apple/changelog.d/4260.changed.md create mode 100644 apple/changelog.d/4260.security.1.md create mode 100644 apple/changelog.d/4260.security.md diff --git a/apple/changelog.d/4260.changed.md b/apple/changelog.d/4260.changed.md new file mode 100644 index 0000000000..8872a8fe57 --- /dev/null +++ b/apple/changelog.d/4260.changed.md @@ -0,0 +1 @@ +Read-Only and Edit mode treat Redis module and new data-type reads, and Elasticsearch POST reads, as reads. diff --git a/apple/changelog.d/4260.security.1.md b/apple/changelog.d/4260.security.1.md new file mode 100644 index 0000000000..438ef2350d --- /dev/null +++ b/apple/changelog.d/4260.security.1.md @@ -0,0 +1 @@ +Read-Only blocks an Oracle or Dameng function called without parentheses, and a SurrealDB fn:: call. diff --git a/apple/changelog.d/4260.security.md b/apple/changelog.d/4260.security.md new file mode 100644 index 0000000000..e133908a67 --- /dev/null +++ b/apple/changelog.d/4260.security.md @@ -0,0 +1 @@ +AI Edit mode refuses schema, privilege and session commands on MongoDB, Redis, Neo4j and every other engine. From 14f8b74892567343f22143f7bab9c7a58220c2d8 Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Fri, 13 Nov 2026 09:22:26 +0700 Subject: [PATCH 3/5] fix(macos): read the Oracle catalog in the schema an actual plan or a dashboard tile runs in --- .../SQLBareNameCalls+Parser.swift | 25 ++++++++++++ .../TableProSQLGrammar/SQLBareNameCalls.swift | 8 ++++ .../SQLBareNameCallsTests.swift | 2 + .../ViewModels/DashboardTileBoard.swift | 38 +++++++++++++++---- .../MainContentCoordinator+Explain.swift | 8 ++-- .../ViewModels/DashboardTileBoardTests.swift | 30 ++++++++++++++- 6 files changed, 99 insertions(+), 12 deletions(-) diff --git a/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLBareNameCalls+Parser.swift b/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLBareNameCalls+Parser.swift index 497359755d..dc36c771ff 100644 --- a/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLBareNameCalls+Parser.swift +++ b/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLBareNameCalls+Parser.swift @@ -420,6 +420,12 @@ extension SQLBareNameCalls { index += 1 } if index < end, tokens[index].kind == .open, let close = closing[index] { + // A row pattern variable qualifies the reshaped table's columns: `up.price`. + if word == "MATCH_RECOGNIZE", let input = blocks[body].sources.first { + for variable in patternVariables((index + 1)..) -> [String] { + var names: [String] = [] + var index = range.lowerBound + while index < range.upperBound { + if word(at: index) == "PATTERN", index + 1 < range.upperBound, tokens[index + 1].kind == .open, + let close = closing[index + 1] { + names += ((index + 2).. Source { + switch self { + case .table(let table, _): return .table(table, alias: alias) + case .derived(let block, let columns, _): return .derived(block: block, columns: columns, alias: alias) + case .opaque: return .opaque(alias: alias) + } + } + // The name a qualifier can use: the alias, or else the table's own name. func answers(to qualifier: String) -> Bool { if let alias { return alias == qualifier } diff --git a/apple/Packages/TableProCore/Tests/TableProSQLGrammarTests/SQLBareNameCallsTests.swift b/apple/Packages/TableProCore/Tests/TableProSQLGrammarTests/SQLBareNameCallsTests.swift index 7635d9b244..364d605e0b 100644 --- a/apple/Packages/TableProCore/Tests/TableProSQLGrammarTests/SQLBareNameCallsTests.swift +++ b/apple/Packages/TableProCore/Tests/TableProSQLGrammarTests/SQLBareNameCallsTests.swift @@ -104,6 +104,8 @@ struct SQLBareNameCallsTests { #expect(!Self.calls("SELECT a FROM (VALUES (1)) t (a)")) #expect(Self.calls("SELECT * FROM orders MATCH_RECOGNIZE (PATTERN (x) DEFINE x AS wipe = 1)")) #expect(!Self.calls("SELECT * FROM orders MATCH_RECOGNIZE (PATTERN (x) DEFINE x AS purge = 100)")) + #expect(!Self.calls("SELECT * FROM orders MATCH_RECOGNIZE (PATTERN (up+) DEFINE up AS up.purge > 1)")) + #expect(Self.calls("SELECT * FROM orders MATCH_RECOGNIZE (PATTERN (up+) DEFINE up AS other.purge > 1)")) #expect(Self.calls("SELECT * FROM orders, dual PIVOT (MAX(purge) FOR dummy IN ('X' AS x))")) #expect(!Self.calls("SELECT * FROM orders, dual PIVOT (MAX(dummy) FOR dummy IN ('X' AS x)) WHERE purge = 1")) #expect(Self.calls("SELECT * FROM orders PARTITION FOR (wipe)")) diff --git a/apple/TablePro/ViewModels/DashboardTileBoard.swift b/apple/TablePro/ViewModels/DashboardTileBoard.swift index a4af9501be..b158da1104 100644 --- a/apple/TablePro/ViewModels/DashboardTileBoard.swift +++ b/apple/TablePro/ViewModels/DashboardTileBoard.swift @@ -7,6 +7,7 @@ import Combine import Foundation import os import TableProPluginKit +import TableProSQLGrammar @MainActor final class DashboardTileBoard: ObservableObject { @@ -32,6 +33,8 @@ final class DashboardTileBoard: ObservableObject { var nullDisplay: @MainActor () -> String var storage: ServerDashboardTileStorage var now: @MainActor () -> Date = Date.init + // Where a tile's query runs, so Read-Only reads an Oracle or Dameng catalog in that schema. + var runsIn: @MainActor (UUID) -> DatabaseScope? = { _ in nil } @MainActor static func live(services: AppServices) -> Environment { @@ -55,7 +58,13 @@ final class DashboardTileBoard: ObservableObject { }, chartsAvailable: { services.featureAccess.isAvailable(.resultCharts) }, nullDisplay: { services.appSettings.dataGrid.nullDisplay }, - storage: ServerDashboardTileStorage() + storage: ServerDashboardTileStorage(), + // A tile runs on the session itself, in whichever schema it is on. + runsIn: { connectionId in + guard let browse = services.databaseManager.browseScope(for: connectionId) else { return nil } + let schema = services.databaseManager.driver(for: connectionId)?.currentSchema ?? browse.schema + return DatabaseScope(connectionId: connectionId, database: browse.database, schema: schema) + } ) } } @@ -77,11 +86,17 @@ final class DashboardTileBoard: ObservableObject { private struct GateDecision { let sql: String let safeModeLevel: SafeModeLevel + let runsIn: DatabaseScope? + let decidedAt: Date let denial: String? } - /// One gate decision per tile, statement and Safe Mode level, until Refresh Now. The gate logs - /// every decision, and a tile on a one second cycle must not append one each second. + // Oracle and Dameng read a bare name against a catalog that changes, so their decision is asked again after the + // catalog's own lifetime. + private static let catalogDecisionLifetime: TimeInterval = 60 + + // One gate decision per tile, statement, Safe Mode level and schema, until Refresh Now, or a minute on Oracle and + // Dameng. The gate logs every decision, and a tile on a one second cycle must not append one each second. private var gateDecisions: [UUID: GateDecision] = [:] private var cancellables: Set = [] @@ -244,16 +259,22 @@ final class DashboardTileBoard: ObservableObject { let denial: String? let safeModeLevel = environment.safeModeLevel(connectionId) - if let cached = gateDecisions[tileId], cached.sql == sql, cached.safeModeLevel == safeModeLevel { + let runsIn = environment.runsIn(connectionId) + let readsCatalog = SQLBareNameCalls.engines.contains(databaseType.rawValue) + if let cached = gateDecisions[tileId], cached.sql == sql, cached.safeModeLevel == safeModeLevel, + cached.runsIn == runsIn, + !readsCatalog || (0.. OperationRequest { + private func request(for sql: String, runsIn: DatabaseScope?) -> OperationRequest { OperationRequest( connectionId: connectionId, databaseType: databaseType, @@ -322,7 +343,8 @@ final class DashboardTileBoard: ObservableObject { kind: .readQuery, caller: .userInterface, capabilities: [.cannotPrompt], - operationDescription: String(localized: "Run a saved query on the Server Dashboard") + operationDescription: String(localized: "Run a saved query on the Server Dashboard"), + runsIn: runsIn ) } diff --git a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Explain.swift b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Explain.swift index b767f87f62..6ee104d4ba 100644 --- a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Explain.swift +++ b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Explain.swift @@ -50,7 +50,7 @@ extension MainContentCoordinator { isShowingSafeModePrompt = true let tabId = tab.id - let authorization = authorizationRequest(for: request) + let authorization = authorizationRequest(for: request, runsIn: scope(for: tab)) return Task { defer { isShowingSafeModePrompt = false } switch await executionGate.authorize(authorization) { @@ -66,7 +66,8 @@ extension MainContentCoordinator { // MARK: - Request - private func authorizationRequest(for request: ExplainRequest) -> OperationRequest { + // An Oracle actual plan runs its statement in the tab's schema, where a bare name resolves. + private func authorizationRequest(for request: ExplainRequest, runsIn: DatabaseScope?) -> OperationRequest { OperationRequest( connectionId: connectionId, databaseType: connection.type, @@ -74,7 +75,8 @@ extension MainContentCoordinator { kind: request.operationKind(databaseType: connection.type), caller: .userInterface, capabilities: .interactiveUser, - operationDescription: String(localized: "Execute Query") + operationDescription: String(localized: "Execute Query"), + runsIn: runsIn ) } diff --git a/apple/TableProTests/ViewModels/DashboardTileBoardTests.swift b/apple/TableProTests/ViewModels/DashboardTileBoardTests.swift index 9534866282..43904ad2fd 100644 --- a/apple/TableProTests/ViewModels/DashboardTileBoardTests.swift +++ b/apple/TableProTests/ViewModels/DashboardTileBoardTests.swift @@ -27,6 +27,7 @@ struct DashboardTileBoardTests { var holdExecution: AsyncGate? var loads = 0 var safeModeLevel = SafeModeLevel.silent + var runsIn: DatabaseScope? init(favorites: [SQLFavorite]) throws { suite = try EphemeralDefaults() @@ -51,7 +52,8 @@ struct DashboardTileBoardTests { chartsAvailable: { chartsAvailable }, nullDisplay: { "NULL" }, storage: storage, - now: { self.clock } + now: { self.clock }, + runsIn: { _ in self.runsIn } ) return DashboardTileBoard( connectionId: connectionId, @@ -177,6 +179,32 @@ struct DashboardTileBoardTests { #expect(await gate.requests.count == 3) } + @Test("On Oracle a tile is asked again when the session's schema moves, and once its catalog read is a minute old") + func oracleTileAsksAgainForItsSchema() async throws { + let saved = favorite() + let fixture = try Fixture(favorites: [saved]) + let gate = CountingGate() + fixture.gate = gate + fixture.runsIn = DatabaseScope(connectionId: fixture.connectionId, database: "FREEPDB1", schema: "HR") + let board = fixture.makeBoard(databaseType: .oracle) + board.add(favoriteId: saved.id) + let tile = try #require(board.tiles.first) + try await waitUntil { board.state(for: tile.id).display != nil } + + await board.refresh(force: false) + #expect(await gate.requests.count == 1) + #expect(await gate.requests.first?.runsIn?.schema == "HR") + + fixture.runsIn = DatabaseScope(connectionId: fixture.connectionId, database: "FREEPDB1", schema: "APP") + await board.refresh(force: false) + #expect(await gate.requests.count == 2) + #expect(await gate.requests.last?.runsIn?.schema == "APP") + + fixture.clock.addTimeInterval(61) + await board.refresh(force: false) + #expect(await gate.requests.count == 3) + } + @Test("A slower tile waits for its interval, and Refresh Now runs it anyway") func slowerIntervalWaits() async throws { let saved = favorite() From 6b257daff6f97eff7c3c97de999e8a8d825d28ff Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Fri, 13 Nov 2026 09:37:47 +0700 Subject: [PATCH 4/5] fix(macos): read a dashboard tile's schema from a driver that switches schema --- apple/TablePro/ViewModels/DashboardTileBoard.swift | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/apple/TablePro/ViewModels/DashboardTileBoard.swift b/apple/TablePro/ViewModels/DashboardTileBoard.swift index b158da1104..a3786e63d8 100644 --- a/apple/TablePro/ViewModels/DashboardTileBoard.swift +++ b/apple/TablePro/ViewModels/DashboardTileBoard.swift @@ -62,7 +62,8 @@ final class DashboardTileBoard: ObservableObject { // A tile runs on the session itself, in whichever schema it is on. runsIn: { connectionId in guard let browse = services.databaseManager.browseScope(for: connectionId) else { return nil } - let schema = services.databaseManager.driver(for: connectionId)?.currentSchema ?? browse.schema + let driver = services.databaseManager.driver(for: connectionId) as? SchemaSwitchable + let schema = driver?.currentSchema ?? browse.schema return DatabaseScope(connectionId: connectionId, database: browse.database, schema: schema) } ) From 2ea1a952f036ba39b28e8084abafe3ca35a61cc0 Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Fri, 13 Nov 2026 11:16:43 +0700 Subject: [PATCH 5/5] test(ios): answer DUAL's own columns in the bare function name guard test --- .../Helpers/ReadOnlyBareNameGuardTests.swift | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/apple/TableProMobile/TableProMobileTests/Helpers/ReadOnlyBareNameGuardTests.swift b/apple/TableProMobile/TableProMobileTests/Helpers/ReadOnlyBareNameGuardTests.swift index 4f5c29c6e9..c5e839916a 100644 --- a/apple/TableProMobile/TableProMobileTests/Helpers/ReadOnlyBareNameGuardTests.swift +++ b/apple/TableProMobile/TableProMobileTests/Helpers/ReadOnlyBareNameGuardTests.swift @@ -7,9 +7,10 @@ import Testing struct ReadOnlyBareNameGuardTests { private struct Unreachable: Error {} - // The catalog Oracle 23ai answered for a schema with function PURGE and table ORDERS(ID, PURGE). + // The catalog Oracle 23ai answered for a schema with function PURGE, table ORDERS(ID, PURGE) and DUAL. private static let catalog: SQLCallableCatalogCache.Reader = { query in if query == SQLCallableCatalog.query { return [["F", "PURGE"]] } + if query.contains("'DUAL'") { return [["0", "3", nil, "DUMMY"]] } return [["0", "1", nil, "ID"], ["0", "1", nil, "PURGE"]] }