Skip to content

Commit 832011a

Browse files
authored
fix(macos): hold Read-Only Safe Mode at the server, bound MCP and assistant answers, narrow the assistant's tools and refuse MCP file access (#4219)
1 parent b7cd2d9 commit 832011a

171 files changed

Lines changed: 9912 additions & 806 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLFileNamedAsTable.swift‎

Lines changed: 74 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import Foundation
22

33
// DuckDB reads a file named as a table. A quoted identifier, in `"` or a backtick, counts only with a `/`, a `.` or a
4-
// `\`.
4+
// `\`. So does a dotted name ending in a file extension, and a table function DuckDB does not keep inside the database.
55
public enum SQLFileNamedAsTable {
66
private static let tableIntroducers: Set<String> = [
77
"FROM", "JOIN", "USING", "TABLE", "DESCRIBE", "DESC", "SHOW", "SUMMARIZE", "PIVOT", "UNPIVOT",
@@ -16,6 +16,23 @@ public enum SQLFileNamedAsTable {
1616
// `extract(year FROM '2026-01-01'::DATE)` separates arguments with FROM.
1717
private static let functionsTakingFrom: Set<String> = ["EXTRACT", "TRIM", "SUBSTRING", "OVERLAY"]
1818

19+
// Only a FROM or JOIN list holds table functions; PIVOT's and USING's parentheses hold expressions.
20+
private static let callingIntroducers: Set<String> = ["FROM", "JOIN"]
21+
22+
// An allowlist: an extension DuckDB loads on its own brings table functions no list here knows.
23+
static let tableFunctionsInsideDatabase: Set<String> = [
24+
"RANGE", "GENERATE_SERIES", "UNNEST", "REPEAT", "JSON_EACH", "JSON_TREE", "DUCKDB_COLUMNS",
25+
"DUCKDB_CONSTRAINTS", "DUCKDB_DATABASES", "DUCKDB_DEPENDENCIES", "DUCKDB_FUNCTIONS", "DUCKDB_INDEXES",
26+
"DUCKDB_KEYWORDS", "DUCKDB_SCHEMAS", "DUCKDB_SEQUENCES", "DUCKDB_SETTINGS", "DUCKDB_TABLES", "DUCKDB_TYPES",
27+
"DUCKDB_VARIABLES", "DUCKDB_VIEWS", "PRAGMA_DATABASE_SIZE", "PRAGMA_PLATFORM", "PRAGMA_SHOW",
28+
"PRAGMA_STORAGE_INFO", "PRAGMA_TABLE_INFO", "PRAGMA_VERSION",
29+
]
30+
31+
// DuckDB joins a dotted table name with `.` and reads it as a file in the working folder.
32+
static let fileExtensions: Set<String> = [
33+
"CSV", "TSV", "GZ", "ZST", "PARQUET", "JSON", "JSONL", "NDJSON", "GEOJSON", "GEOJSONL", "DB", "DDB", "DUCKDB",
34+
]
35+
1936
private static let nameContinuations = Set(".:&".utf16)
2037

2138
private static let pathMarks = CharacterSet(charactersIn: "/.\\")
@@ -31,6 +48,8 @@ public enum SQLFileNamedAsTable {
3148
var separatesArgumentsWithFrom = false
3249
var listsTables = false
3350
var expects = Expectation.nothing
51+
var introducer = ""
52+
var nameParts = 0
3453
}
3554

3655
public static func appears(in statement: String, grammar: SQLLexicalGrammar) -> Bool {
@@ -46,32 +65,53 @@ public enum SQLFileNamedAsTable {
4665
break
4766
case .word(let word) where tableIntroducers.contains(word):
4867
levels[top].expects = .table
68+
levels[top].introducer = word
69+
levels[top].nameParts = 0
4970
if word == "FROM" { levels[top].listsTables = true }
5071
case .word(let word) where tableListEnders.contains(word) && expected != .table:
5172
levels[top].listsTables = false
52-
case .word("ONLY") where expected == .table:
73+
case .word("ONLY") where expected == .table, .word("LATERAL") where expected == .table:
5374
levels[top].expects = .table
54-
case .word, .name:
75+
case .word(let word):
76+
guard expected == .table else { break }
77+
levels[top].nameParts += 1
78+
if levels[top].nameParts > 1, fileExtensions.contains(word) { return true }
79+
levels[top].expects = .restOfName
80+
case .name:
5581
if expected == .table { levels[top].expects = .restOfName }
5682
case .quoted:
5783
guard expected == .table else { break }
5884
if namesFile(at: token.location, in: source, grammar: grammar) { return true }
85+
levels[top].nameParts += 1
86+
let quotedName = identifier(at: token.location, in: source, grammar: grammar)?.uppercased()
87+
if levels[top].nameParts > 1, let quotedName, fileExtensions.contains(quotedName) { return true }
5988
levels[top].expects = .restOfName
6089
case .open:
90+
if expected == .restOfName, callingIntroducers.contains(levels[top].introducer),
91+
!callsTableFunctionInsideDatabase(before: index, in: tokens, source: source, grammar: grammar) {
92+
return true
93+
}
6194
// `FROM (notes JOIN 'payroll.csv' USING (id))` groups tables, and any other `(` starts afresh.
6295
levels.append(
6396
Parentheses(
6497
separatesArgumentsWithFrom: callsFunctionTakingFrom(before: index, in: tokens, source: source),
65-
expects: expected == .table ? .table : .nothing
98+
expects: expected == .table ? .table : .nothing,
99+
introducer: expected == .table ? levels[top].introducer : ""
66100
)
67101
)
68102
case .close:
69103
if top > 0 { levels.removeLast() }
70104
case .comma:
71-
if levels[top].listsTables { levels[top].expects = .table }
105+
if levels[top].listsTables {
106+
levels[top].expects = .table
107+
levels[top].nameParts = 0
108+
}
72109
case .other:
73-
let continuesName = nameContinuations.contains(source.character(at: token.location))
74-
if expected == .restOfName, continuesName { levels[top].expects = .table }
110+
let mark = source.character(at: token.location)
111+
if expected == .restOfName, nameContinuations.contains(mark) {
112+
levels[top].expects = .table
113+
if mark != period { levels[top].nameParts = 0 }
114+
}
75115
case .terminator:
76116
break
77117
}
@@ -106,6 +146,33 @@ public enum SQLFileNamedAsTable {
106146
return tableIntroducers.first(where: word.hasSuffix).map { .word($0) } ?? token.kind
107147
}
108148

149+
// A name the scanner cannot read counts as unknown.
150+
private static func callsTableFunctionInsideDatabase(
151+
before index: Int,
152+
in tokens: [TSQLTokens.Token],
153+
source: NSString,
154+
grammar: SQLLexicalGrammar
155+
) -> Bool {
156+
guard index >= 1 else { return false }
157+
let callee = tokens[index - 1]
158+
let name: String? = switch callee.kind {
159+
case .word(let word): word
160+
case .quoted: identifier(at: callee.location, in: source, grammar: grammar)?.uppercased()
161+
default: nil
162+
}
163+
guard let name else { return false }
164+
return name == "VALUES" || tableFunctionsInsideDatabase.contains(name)
165+
}
166+
167+
private static func identifier(at location: Int, in source: NSString, grammar: SQLLexicalGrammar) -> String? {
168+
guard [SqlLexer.doubleQuote, SqlLexer.backtick].contains(source.character(at: location)),
169+
let span = SQLNonCodeSpan.span(at: location, in: source, grammar: grammar)
170+
else {
171+
return nil
172+
}
173+
return source.substring(with: NSRange(location: location + 1, length: max(0, span.contentEnd - location - 1)))
174+
}
175+
109176
private static func namesFile(at location: Int, in source: NSString, grammar: SQLLexicalGrammar) -> Bool {
110177
guard [SqlLexer.doubleQuote, SqlLexer.backtick].contains(source.character(at: location)),
111178
let span = SQLNonCodeSpan.span(at: location, in: source, grammar: grammar)
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
import Foundation
2+
import TableProPluginKit
3+
import Testing
4+
5+
struct PluginReadOnlyModeTests {
6+
@Test("A new context asks for no Read-Only mode and no byte limit")
7+
func contextDefaults() {
8+
let context = PluginStatementContext()
9+
10+
#expect(!context.enforcesReadOnly)
11+
#expect(context.resultByteLimit == nil)
12+
#expect(context == PluginStatementContext(enforcesReadOnly: false, resultByteLimit: nil))
13+
}
14+
15+
@Test("The initializer sets both fields, and equality reads them")
16+
func contextInitializerSetsTheFields() {
17+
let context = PluginStatementContext(enforcesReadOnly: true, resultByteLimit: 4_096)
18+
19+
#expect(context.enforcesReadOnly)
20+
#expect(context.resultByteLimit == 4_096)
21+
#expect(context != PluginStatementContext())
22+
#expect(PluginStatementContext(resultByteLimit: 1) != PluginStatementContext(resultByteLimit: 2))
23+
}
24+
25+
@Test("The Read-Only capability is bit 24 and shares no bit with another capability")
26+
func capabilityBit() {
27+
let others: [PluginCapabilities] = [
28+
.materializedViews, .foreignTables, .storedProcedures, .userFunctions, .alterTableDDL, .foreignKeyToggle,
29+
.truncateTable, .multiSchema, .parameterizedQueries, .cancelQuery, .batchExecute, .transactions,
30+
.userManagement, .schemaCompare, .dataCompare, .resultSetBatches, .cellExpressions, .uuidCellText,
31+
.temporaryTables, .rowWrites, .repeatingPrimaryKey, .readOnlyStatements, .statementPolicy,
32+
.columnDefaultMarks,
33+
]
34+
35+
#expect(PluginCapabilities.enforcesReadOnlyMode.rawValue == 1 << 24)
36+
for other in others {
37+
#expect(other.isDisjoint(with: .enforcesReadOnlyMode), "\(other.rawValue)")
38+
}
39+
}
40+
41+
@Test("The default bounded read with a context runs the context-free one for a plain read")
42+
func defaultForwardsAPlainRead() async throws {
43+
let driver = BoundedReadDriver()
44+
let context = PluginStatementContext(resultByteLimit: 1_024)
45+
46+
let result = try await driver.executeBoundedQuery(query: "SELECT 1", rowCap: 5, context: context)
47+
48+
#expect(result?.rows.count == 1)
49+
#expect(driver.calls == [5])
50+
}
51+
52+
@Test(
53+
"The default bounded read with a context hands back what the context-free one cannot honor",
54+
arguments: Restriction.allCases
55+
)
56+
func defaultRefusesARestrictedRead(restriction: Restriction) async throws {
57+
let driver = BoundedReadDriver()
58+
59+
let result = try await driver.executeBoundedQuery(query: "SELECT 1", rowCap: 5, context: restriction.context)
60+
61+
#expect(result == nil)
62+
#expect(driver.calls.isEmpty)
63+
}
64+
65+
enum Restriction: CaseIterable, Sendable {
66+
case enforcesReadOnly
67+
case readOnly
68+
case readsCatalog
69+
case refusesDestructive
70+
case refusesServerCode
71+
72+
var context: PluginStatementContext {
73+
var context = PluginStatementContext()
74+
switch self {
75+
case .enforcesReadOnly: context.enforcesReadOnly = true
76+
case .readOnly: context.readOnly = true
77+
case .readsCatalog: context.readsCatalog = true
78+
case .refusesDestructive: context.allowsDestructive = false
79+
case .refusesServerCode: context.allowsServerCode = false
80+
}
81+
return context
82+
}
83+
}
84+
}
85+
86+
private final class BoundedReadDriver: PluginDatabaseDriver, @unchecked Sendable {
87+
let supportsSchemas = false
88+
let supportsTransactions = false
89+
let currentSchema: String? = nil
90+
let serverVersion: String? = nil
91+
92+
private let lock = NSLock()
93+
private var rowCaps: [Int] = []
94+
95+
var calls: [Int] {
96+
lock.withLock { rowCaps }
97+
}
98+
99+
func connect() async throws {}
100+
func disconnect() {}
101+
func ping() async throws {}
102+
103+
func execute(query: String) async throws -> PluginQueryResult {
104+
PluginQueryResult(columns: [], columnTypeNames: [], rows: [], rowsAffected: 0, executionTime: 0)
105+
}
106+
107+
func executeBoundedQuery(query: String, rowCap: Int) async throws -> PluginQueryResult? {
108+
lock.withLock { rowCaps.append(rowCap) }
109+
return PluginQueryResult(
110+
columns: ["one"],
111+
columnTypeNames: ["INTEGER"],
112+
rows: [[.text("1")]],
113+
rowsAffected: 0,
114+
executionTime: 0
115+
)
116+
}
117+
118+
func fetchTables(schema: String?) async throws -> [PluginTableInfo] { [] }
119+
func fetchColumns(table: String, schema: String?) async throws -> [PluginColumnInfo] { [] }
120+
func fetchIndexes(table: String, schema: String?) async throws -> [PluginIndexInfo] { [] }
121+
func fetchForeignKeys(table: String, schema: String?) async throws -> [PluginForeignKeyInfo] { [] }
122+
func fetchTableDDL(table: String, schema: String?) async throws -> String { "" }
123+
func fetchViewDefinition(view: String, schema: String?) async throws -> String { "" }
124+
func fetchTableMetadata(table: String, schema: String?) async throws -> PluginTableMetadata {
125+
PluginTableMetadata(tableName: table)
126+
}
127+
func fetchDatabases() async throws -> [String] { [] }
128+
func fetchDatabaseMetadata(_ database: String) async throws -> PluginDatabaseMetadata {
129+
PluginDatabaseMetadata(name: database)
130+
}
131+
}

‎apple/Packages/TableProCore/Tests/TableProSQLGrammarTests/SQLFileNamedAsTableTests.swift‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,30 @@ struct SQLFileNamedAsTableTests {
6666
#expect(namesFile(sql))
6767
}
6868

69+
@Test("A table function DuckDB does not keep inside the database counts as one", arguments: [
70+
"SELECT * FROM quack_serve('x')",
71+
"SELECT * FROM odbc_query('DSN=x', 'DROP TABLE t')",
72+
"SELECT * FROM notes, cloudformation_create_stack('x')",
73+
"SELECT * FROM notes JOIN avro_metadata('x') ON true",
74+
"SELECT * FROM notes, LATERAL quack_query('x')",
75+
"SELECT * FROM (FROM quack_serve('x'))",
76+
"SELECT * FROM system.main.quack_serve('x')",
77+
"SELECT * FROM \"quack_serve\"('x')",
78+
])
79+
func unknownTableFunctions(sql: String) {
80+
#expect(namesFile(sql))
81+
}
82+
83+
@Test("A dotted name ending in a file extension is a file in the working folder", arguments: [
84+
"SELECT * FROM finances.csv",
85+
"SELECT * FROM notes.\"csv\"",
86+
"SELECT * FROM backup.duckdb",
87+
"SELECT * FROM notes, data.parquet",
88+
])
89+
func dottedFileNames(sql: String) {
90+
#expect(namesFile(sql))
91+
}
92+
6993
@Test("A FROM that runs on from a number or an operator still lists tables", arguments: [
7094
"SELECT 1FROM 'payroll.csv'",
7195
"SELECT 1e5FROM 'payroll.csv'",
@@ -122,6 +146,11 @@ struct SQLFileNamedAsTableTests {
122146
"COMMENT ON TABLE notes IS 'a.csv'",
123147
"CREATE TABLE notes (path VARCHAR DEFAULT 'a.csv')",
124148
"SELECT _from, _desc, 'a.csv' FROM notes",
149+
"SELECT * FROM main.range(1, 10)",
150+
"SELECT * FROM duckdb_tables()",
151+
"PIVOT notes ON year USING sum(salary)",
152+
"SELECT * FROM notes JOIN tags ON f(tags.id) = notes.id",
153+
"SELECT * FROM notes.csv_rows",
125154
])
126155
func valuesAndTableNames(sql: String) {
127156
#expect(!namesFile(sql))
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import Foundation
2+
@testable import TableProSQLGrammar
3+
import Testing
4+
5+
// The core keeps its own copy of these names for its MCP server, and both test suites compare theirs with one file.
6+
struct SQLOutsideReachNamesTests {
7+
private static let sharedNames = URL(fileURLWithPath: #filePath)
8+
.deletingLastPathComponent()
9+
.deletingLastPathComponent()
10+
.deletingLastPathComponent()
11+
.deletingLastPathComponent()
12+
.deletingLastPathComponent()
13+
.deletingLastPathComponent()
14+
.appendingPathComponent("core/crates/core/tests/fixtures/outside_reach.json")
15+
16+
@Test("Each list names what the core lists")
17+
func listsMatchTheSharedNames() throws {
18+
let data = try Data(contentsOf: Self.sharedNames)
19+
let shared = try JSONDecoder().decode([String: [String]].self, from: data)
20+
.mapValues { Set($0.map { $0.uppercased() }) }
21+
let duckDB = SQLStatementTiering.duckDB
22+
let clickHouse = SQLStatementTiering.clickHouse
23+
let listed: [String: Set<String>] = [
24+
"filesystem_word_runs": Set(SQLStatementTiering.filesystemWordRuns.map { $0.joined(separator: " ") }),
25+
"filesystem_names": SQLStatementTiering.filesystemNames,
26+
"filesystem_or_code_keywords": SQLStatementTiering.filesystemOrCodeKeywords,
27+
"filesystem_functions": SQLStatementTiering.filesystemFunctions,
28+
"duckdb_functions": SQLStatementTiering.functionsReachingOutside[duckDB] ?? [],
29+
"duckdb_families": Set(SQLStatementTiering.functionFamiliesReachingOutside[duckDB] ?? []),
30+
"clickhouse_functions": SQLStatementTiering.functionsReachingOutside[clickHouse] ?? [],
31+
"clickhouse_families": Set(SQLStatementTiering.functionFamiliesReachingOutside[clickHouse] ?? []),
32+
]
33+
#expect(listed == shared)
34+
}
35+
}

‎apple/Packages/TableProMacCore/Sources/TableProMCP/Auth/MCPPrincipal.swift‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,23 @@ public struct MCPPrincipal: Sendable, Equatable, Hashable {
2828
public let scopes: Set<MCPScope>
2929
public let connectionAccess: ConnectionAccess
3030
public let metadata: MCPPrincipalMetadata
31+
// Nil means every tool the scopes allow.
32+
public let toolAllowlist: Set<String>?
3133

3234
public init(
3335
tokenFingerprint: String,
3436
tokenId: UUID,
3537
scopes: Set<MCPScope>,
3638
connectionAccess: ConnectionAccess = .all,
37-
metadata: MCPPrincipalMetadata
39+
metadata: MCPPrincipalMetadata,
40+
toolAllowlist: Set<String>? = nil
3841
) {
3942
self.tokenFingerprint = tokenFingerprint
4043
self.tokenId = tokenId
4144
self.scopes = scopes
4245
self.connectionAccess = connectionAccess
4346
self.metadata = metadata
47+
self.toolAllowlist = toolAllowlist
4448
}
4549

4650
public static let inAppAssistantTokenId = UUID(uuid: (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xA1, 0x1A))
@@ -69,6 +73,10 @@ public struct MCPPrincipal: Sendable, Equatable, Hashable {
6973
scopes.contains(scope)
7074
}
7175

76+
public func allowsTool(_ name: String) -> Bool {
77+
toolAllowlist?.contains(name) ?? true
78+
}
79+
7280
public func missingScopes(from required: Set<MCPScope>) -> Set<MCPScope> {
7381
required.subtracting(scopes)
7482
}
@@ -86,6 +94,7 @@ public struct MCPPrincipal: Sendable, Equatable, Hashable {
8694
&& lhs.scopes == rhs.scopes
8795
&& lhs.connectionAccess == rhs.connectionAccess
8896
&& lhs.metadata == rhs.metadata
97+
&& lhs.toolAllowlist == rhs.toolAllowlist
8998
}
9099

91100
public func hash(into hasher: inout Hasher) {

0 commit comments

Comments
 (0)