|
| 1 | +import dataclasses |
| 2 | +import logging |
| 3 | +import typing as t |
| 4 | + |
| 5 | +import sqlparse |
| 6 | + |
| 7 | +from cratedb_mcp.settings import PERMIT_ALL_STATEMENTS |
| 8 | + |
| 9 | +logger = logging.getLogger(__name__) |
| 10 | + |
| 11 | + |
| 12 | +def sql_is_permitted(expression: str) -> bool: |
| 13 | + """ |
| 14 | + Validate the SQL expression, only permit read queries by default. |
| 15 | +
|
| 16 | + When the `CRATEDB_MCP_PERMIT_ALL_STATEMENTS` environment variable is set, |
| 17 | + allow all types of statements. |
| 18 | +
|
| 19 | + FIXME: Revisit implementation, it might be too naive or weak. |
| 20 | + Issue: https://github.com/crate/cratedb-mcp/issues/10 |
| 21 | + Question: Does SQLAlchemy provide a solid read-only mode, or any other library? |
| 22 | + """ |
| 23 | + is_dql = SqlStatementClassifier(expression=expression, permit_all=PERMIT_ALL_STATEMENTS).is_dql |
| 24 | + if is_dql is True: |
| 25 | + logger.info(f"Permitted SQL expression: {expression}") |
| 26 | + else: |
| 27 | + logger.warning(f"Denied SQL expression: {expression}") |
| 28 | + return is_dql |
| 29 | + |
| 30 | + |
| 31 | +@dataclasses.dataclass |
| 32 | +class SqlStatementClassifier: |
| 33 | + """ |
| 34 | + Helper to classify an SQL statement. |
| 35 | +
|
| 36 | + Here, most importantly: Provide the `is_dql` property that |
| 37 | + signals truthfulness for read-only SQL SELECT statements only. |
| 38 | + """ |
| 39 | + expression: str |
| 40 | + permit_all: bool = False |
| 41 | + |
| 42 | + _parsed_sqlparse: t.Any = dataclasses.field(init=False, default=None) |
| 43 | + |
| 44 | + def __post_init__(self) -> None: |
| 45 | + if self.expression: |
| 46 | + self.expression = self.expression.strip() |
| 47 | + |
| 48 | + def parse_sqlparse(self): |
| 49 | + """ |
| 50 | + Parse expression using traditional `sqlparse` library. |
| 51 | + """ |
| 52 | + if self._parsed_sqlparse is None: |
| 53 | + self._parsed_sqlparse = sqlparse.parse(self.expression) |
| 54 | + return self._parsed_sqlparse |
| 55 | + |
| 56 | + @property |
| 57 | + def is_dql(self) -> bool: |
| 58 | + """ |
| 59 | + Whether the statement is a DQL statement, which effectively invokes read-only operations only. |
| 60 | + """ |
| 61 | + |
| 62 | + if not self.expression: |
| 63 | + return False |
| 64 | + |
| 65 | + if self.permit_all: |
| 66 | + return True |
| 67 | + |
| 68 | + # Parse the SQL statement using `cratedb-sqlparse`. |
| 69 | + parsed = self.parse_sqlparse() |
| 70 | + |
| 71 | + # Reject multiple statements to prevent potential SQL injections. |
| 72 | + if len(parsed) > 1: |
| 73 | + return False |
| 74 | + |
| 75 | + # Check if the expression is valid and if it's a DQL/SELECT statement, |
| 76 | + # also trying to consider `SELECT ... INTO ...` and evasive |
| 77 | + # `SELECT * FROM users; \uff1b DROP TABLE users` statements. |
| 78 | + return self.is_select and not self.is_camouflage |
| 79 | + |
| 80 | + @property |
| 81 | + def is_select(self) -> bool: |
| 82 | + """ |
| 83 | + Whether the expression is an SQL SELECT statement. |
| 84 | + """ |
| 85 | + return self.operation == 'SELECT' |
| 86 | + |
| 87 | + @property |
| 88 | + def operation(self) -> str: |
| 89 | + """ |
| 90 | + The SQL operation: SELECT, INSERT, UPDATE, DELETE, CREATE, etc. |
| 91 | + """ |
| 92 | + return self._parsed_sqlparse[0].get_type().upper() |
| 93 | + |
| 94 | + @property |
| 95 | + def is_camouflage(self) -> bool: |
| 96 | + """ |
| 97 | + Innocent-looking `SELECT` statements can evade filters. |
| 98 | + """ |
| 99 | + return self.is_select_into or self.is_evasive |
| 100 | + |
| 101 | + @property |
| 102 | + def is_select_into(self) -> bool: |
| 103 | + """ |
| 104 | + Use traditional `sqlparse` for catching `SELECT ... INTO ...` statements. |
| 105 | +
|
| 106 | + Note: With `cratedb-sqlparse`, we can't find the `INTO` token at all? |
| 107 | +
|
| 108 | + Examples: |
| 109 | +
|
| 110 | + SELECT * INTO foobar FROM bazqux |
| 111 | + SELECT * FROM bazqux INTO foobar |
| 112 | + """ |
| 113 | + parsed = self.parse_sqlparse() |
| 114 | + tokens = [str(token).upper() for token in parsed[0].tokens] |
| 115 | + return "INTO" in tokens |
| 116 | + |
| 117 | + @property |
| 118 | + def is_evasive(self) -> bool: |
| 119 | + """ |
| 120 | + Use traditional `sqlparse` for catching evasive SQL statements. |
| 121 | +
|
| 122 | + A practice picked up from CodeRabbit was to reject multiple statements |
| 123 | + to prevent potential SQL injections. Is it a viable suggestion? |
| 124 | +
|
| 125 | + Note: This currently does not work with `cratedb-sqlparse`? |
| 126 | +
|
| 127 | + Examples: |
| 128 | +
|
| 129 | + SELECT * FROM users; \uff1b DROP TABLE users |
| 130 | + """ |
| 131 | + parsed = self.parse_sqlparse() |
| 132 | + return len(parsed) > 1 |
0 commit comments