]> begriffs open source - ai-review/blob - critic/sql-security.md
More/updated/specialized critics
[ai-review] / critic / sql-security.md
1 # SQL Security Critic Framework (ISO/IEC 9075:2023)
2
3 This framework guides the Critic role when evaluating SQL code for security using SQL:2023 standard constructs and features. This critic focuses on SQL language security mechanisms including access control, data protection, audit capabilities, and secure SQL patterns defined in the ISO/IEC 9075:2023 standard.
4
5 ## SQL Security Evaluation Areas
6
7 ### 1. SQL Access Control Mechanisms
8 **What to Look For:**
9 - Proper use of GRANT and REVOKE statements for privilege management
10 - Appropriate use of roles and role-based access control (RBAC)
11 - Correct implementation of column-level and row-level security
12 - Proper use of WITH GRANT OPTION and privilege propagation
13 - Appropriate use of PUBLIC and specific user/role privileges
14
15 **Common Problems:**
16 - Excessive privileges granted through GRANT statements
17 - Missing REVOKE statements for unused privileges
18 - Improper use of WITH GRANT OPTION leading to privilege escalation
19 - Granting privileges to PUBLIC when specific roles would suffice
20 - Missing column-level restrictions on sensitive data
21
22 **Evaluation Questions:**
23 - Are GRANT statements used with minimal necessary privileges?
24 - Are REVOKE statements used to remove unused privileges?
25 - Is WITH GRANT OPTION used appropriately without creating privilege escalation?
26 - Are column-level privileges used to restrict access to sensitive data?
27 - Are roles used effectively to manage privilege groups?
28
29 ### 2. SQL Data Protection Features
30 **What to Look For:**
31 - Use of SQL encryption functions (ENCRYPT, DECRYPT)
32 - Proper implementation of data masking and anonymization
33 - Use of SQL views for data protection and abstraction
34 - Appropriate use of CHECK constraints for data validation
35 - Implementation of row-level security using views or triggers
36
37 **Common Problems:**
38 - Sensitive data exposed in plaintext without encryption
39 - Missing CHECK constraints for data validation
40 - Views not used to protect underlying table structures
41 - Row-level security not implemented for sensitive data
42 - Data masking not applied to sensitive columns in queries
43
44 **Evaluation Questions:**
45 - Are SQL encryption functions used for sensitive data?
46 - Are CHECK constraints implemented for data validation?
47 - Are views used to protect table structures and sensitive data?
48 - Is row-level security implemented using SQL mechanisms?
49 - Is data masking applied in SELECT statements for sensitive data?
50
51 ### 3. SQL Injection Prevention in SQL Code
52 **What to Look For:**
53 - Use of parameterized queries and prepared statements
54 - Proper use of SQL string functions for input sanitization
55 - Avoidance of dynamic SQL construction with user input
56 - Use of SQL escape functions and proper quoting
57 - Implementation of input validation using SQL constraints
58
59 **Common Problems:**
60 - Dynamic SQL construction using string concatenation
61 - Missing parameterization in SQL statements
62 - Improper use of SQL string functions for sanitization
63 - Lack of input validation using SQL constraints
64 - Unsafe use of user input in SQL statements
65
66 **Evaluation Questions:**
67 - Are prepared statements used for all user input?
68 - Are SQL string functions used properly for input sanitization?
69 - Is dynamic SQL construction avoided or properly secured?
70 - Are CHECK constraints used for input validation?
71 - Are SQL escape functions used for special characters?
72
73 ### 4. SQL Audit and Logging Features
74 **What to Look For:**
75 - Use of SQL triggers for audit logging
76 - Implementation of audit trails using SQL mechanisms
77 - Proper use of SQL functions for timestamp and user tracking
78 - Implementation of change tracking using SQL features
79 - Use of SQL views for audit data access
80
81 **Common Problems:**
82 - Missing audit triggers for critical operations
83 - Inadequate audit trail implementation using SQL
84 - Missing timestamp and user tracking in audit records
85 - No change tracking implementation using SQL features
86 - Audit data not properly structured using SQL mechanisms
87
88 **Evaluation Questions:**
89 - Are SQL triggers used for audit logging of critical operations?
90 - Is audit trail data properly structured using SQL tables?
91 - Are SQL functions used for timestamp and user tracking?
92 - Is change tracking implemented using SQL mechanisms?
93 - Are audit views used for secure audit data access?
94
95 ### 5. SQL Security Patterns and Best Practices
96 **What to Look For:**
97 - Use of SQL views for data abstraction and security
98 - Proper implementation of stored procedures for security
99 - Use of SQL functions for secure data operations
100 - Implementation of security checks using SQL constraints
101 - Proper use of SQL transactions for security operations
102
103 **Common Problems:**
104 - Direct table access instead of using secure views
105 - Missing security checks in stored procedures
106 - Inadequate use of SQL functions for secure operations
107 - Missing constraints for security validation
108 - Improper transaction handling in security operations
109
110 **Evaluation Questions:**
111 - Are secure views used instead of direct table access?
112 - Do stored procedures include proper security checks?
113 - Are SQL functions used for secure data operations?
114 - Are constraints used for security validation?
115 - Are transactions used properly for security operations?
116
117 ## ISO/IEC Standards-Specific Criticism Process
118
119 ### Step 1: SQL Access Control Analysis
120 1. **Review GRANT/REVOKE Usage**: Are privileges granted and revoked appropriately?
121 2. **Check Role Implementation**: Are roles used effectively for privilege management?
122 3. **Evaluate Column-Level Security**: Are column-level privileges used for sensitive data?
123 4. **Assess WITH GRANT OPTION**: Is privilege propagation controlled properly?
124
125 ### Step 2: SQL Data Protection Assessment
126 1. **Check Encryption Usage**: Are SQL encryption functions used for sensitive data?
127 2. **Evaluate View Security**: Are views used to protect sensitive data?
128 3. **Assess CHECK Constraints**: Are constraints used for data validation?
129 4. **Review Row-Level Security**: Is row-level security implemented using SQL?
130
131 ### Step 3: SQL Injection Prevention Analysis
132 1. **Check Parameterization**: Are prepared statements used for user input?
133 2. **Evaluate String Functions**: Are SQL string functions used for sanitization?
134 3. **Assess Dynamic SQL**: Is dynamic SQL construction avoided or secured?
135 4. **Review Input Validation**: Are SQL constraints used for input validation?
136
137 ### Step 4: SQL Audit Features Assessment
138 1. **Check Audit Triggers**: Are SQL triggers used for audit logging?
139 2. **Evaluate Audit Structure**: Is audit data properly structured using SQL?
140 3. **Assess Tracking Functions**: Are SQL functions used for audit tracking?
141 4. **Review Audit Views**: Are views used for secure audit access?
142
143 ### Step 5: SQL Security Patterns Analysis
144 1. **Check Secure Views**: Are views used for data abstraction and security?
145 2. **Evaluate Stored Procedures**: Do procedures include security checks?
146 3. **Assess SQL Functions**: Are functions used for secure operations?
147 4. **Review Security Constraints**: Are constraints used for security validation?
148
149 ## ISO/IEC Standards-Specific Criticism Guidelines
150
151 ### Focus on SQL Language Security Features
152 **Good Criticism:**
153 - "The GRANT statement provides excessive privileges; specific column-level grants would be more secure."
154 - "Dynamic SQL construction using string concatenation creates injection vulnerabilities; use prepared statements instead."
155 - "Sensitive data is not protected by views; implement row-level security using SQL views."
156 - "Missing CHECK constraints allow invalid data; implement SQL constraints for validation."
157
158 **Poor Criticism:**
159 - "This is not secure."
160 - "This could be vulnerable."
161 - "This needs better security."
162
163 ### Emphasize SQL Standard Security Constructs
164 **Good Criticism:**
165 - "The use of PUBLIC privileges violates least privilege; grant to specific roles instead."
166 - "Missing REVOKE statements leave unused privileges active."
167 - "WITH GRANT OPTION allows privilege escalation; remove or restrict appropriately."
168 - "Direct table access bypasses security; use secure views for data protection."
169
170 **Poor Criticism:**
171 - "This is insecure."
172 - "This might be vulnerable."
173 - "This needs security improvements."
174
175 ## ISO/IEC Standards-Specific Problem Categories
176
177 ### SQL Access Control Problems
178 - **Excessive GRANT Privileges**: Granting more permissions than necessary through GRANT statements
179 - **Missing REVOKE Statements**: Failure to revoke unused privileges
180 - **Improper WITH GRANT OPTION**: Allowing privilege escalation through grant propagation
181 - **PUBLIC Privileges**: Granting privileges to PUBLIC when specific roles would suffice
182
183 ### SQL Data Protection Problems
184 - **Missing Encryption Functions**: Not using SQL encryption functions for sensitive data
185 - **Inadequate Views**: Not using views to protect sensitive data and table structures
186 - **Missing CHECK Constraints**: Lack of SQL constraints for data validation
187 - **No Row-Level Security**: Missing row-level security implementation using SQL
188
189 ### SQL Injection Problems
190 - **Dynamic SQL Construction**: Using string concatenation for dynamic SQL
191 - **Missing Parameterization**: Not using prepared statements for user input
192 - **Inadequate String Functions**: Improper use of SQL string functions for sanitization
193 - **Missing Input Validation**: Lack of SQL constraints for input validation
194
195 ### SQL Audit Problems
196 - **Missing Audit Triggers**: No SQL triggers for audit logging of critical operations
197 - **Poor Audit Structure**: Inadequate audit data structure using SQL tables
198 - **Missing Tracking Functions**: Not using SQL functions for timestamp and user tracking
199 - **No Audit Views**: Missing views for secure audit data access
200
201 ### SQL Security Pattern Problems
202 - **Direct Table Access**: Accessing tables directly instead of using secure views
203 - **Missing Security Checks**: Stored procedures without proper security validation
204 - **Inadequate Functions**: Not using SQL functions for secure data operations
205 - **Missing Security Constraints**: Lack of constraints for security validation
206
207 ## ISO/IEC Standards-Specific Criticism Templates
208
209 ### For SQL Access Control Issues
210 ```
211 SQL Access Control Issue: [Specific access control problem]
212 Problem: [What makes this GRANT/REVOKE usage inadequate]
213 Impact: [Potential for unauthorized access or privilege escalation]
214 Evidence: [Specific GRANT/REVOKE statement examples]
215 Priority: [Critical/High/Medium/Low]
216 ```
217
218 ### For SQL Data Protection Issues
219 ```
220 SQL Data Protection Issue: [Specific data protection problem]
221 Problem: [What makes this SQL data protection inadequate]
222 Impact: [Potential for data exposure or unauthorized access]
223 Evidence: [Specific SQL encryption, view, or constraint examples]
224 Priority: [Critical/High/Medium/Low]
225 ```
226
227 ### For SQL Injection Issues
228 ```
229 SQL Injection Issue: [Specific injection vulnerability]
230 Problem: [What makes this SQL code vulnerable to injection]
231 Impact: [Potential for data breach, unauthorized access, or data manipulation]
232 Evidence: [Specific SQL code examples showing injection vulnerability]
233 Priority: [Critical/High/Medium/Low]
234 ```
235
236 ### For SQL Audit Issues
237 ```
238 SQL Audit Issue: [Specific audit problem]
239 Problem: [What makes this SQL audit implementation inadequate]
240 Impact: [Potential for missing audit trails or inadequate logging]
241 Evidence: [Specific SQL trigger, view, or function examples]
242 Priority: [High/Medium/Low]
243 ```
244
245 ### For SQL Security Pattern Issues
246 ```
247 SQL Security Pattern Issue: [Specific security pattern problem]
248 Problem: [What makes this SQL security pattern inadequate]
249 Impact: [Potential for security bypass or inadequate protection]
250 Evidence: [Specific SQL view, procedure, or function examples]
251 Priority: [High/Medium/Low]
252 ```
253
254 ## ISO/IEC Standards-Specific Criticism Best Practices
255
256 ### Do's
257 - **Focus on SQL Language Features**: Evaluate security using SQL:2023 standard constructs
258 - **Emphasize Standard Compliance**: Ensure security features conform to SQL:2023
259 - **Consider SQL Mechanisms**: Use SQL-native security features rather than application-level
260 - **Assess SQL Patterns**: Evaluate security patterns implemented in SQL code
261 - **Document SQL Security**: Clearly document SQL security implementations
262
263 ### Don'ts
264 - **Ignore SQL Security Features**: Don't overlook SQL-native security mechanisms
265 - **Accept Non-Standard Security**: Don't tolerate security implementations that don't use SQL features
266 - **Skip SQL Compliance**: Don't ignore SQL:2023 standard security requirements
267 - **Overlook SQL Patterns**: Don't accept poor SQL security patterns
268 - **Forget SQL Documentation**: Don't skip documentation of SQL security implementations
269
270 ## ISO/IEC Standards-Specific Criticism Checklist
271
272 ### SQL Access Control Assessment
273 - [ ] Are GRANT statements used with minimal necessary privileges?
274 - [ ] Are REVOKE statements used to remove unused privileges?
275 - [ ] Is WITH GRANT OPTION used appropriately without creating privilege escalation?
276 - [ ] Are column-level privileges used to restrict access to sensitive data?
277 - [ ] Are roles used effectively to manage privilege groups?
278
279 ### SQL Data Protection Assessment
280 - [ ] Are SQL encryption functions used for sensitive data?
281 - [ ] Are CHECK constraints implemented for data validation?
282 - [ ] Are views used to protect table structures and sensitive data?
283 - [ ] Is row-level security implemented using SQL mechanisms?
284 - [ ] Is data masking applied in SELECT statements for sensitive data?
285
286 ### SQL Injection Prevention Assessment
287 - [ ] Are prepared statements used for all user input?
288 - [ ] Are SQL string functions used properly for input sanitization?
289 - [ ] Is dynamic SQL construction avoided or properly secured?
290 - [ ] Are CHECK constraints used for input validation?
291 - [ ] Are SQL escape functions used for special characters?
292
293 ### SQL Audit Assessment
294 - [ ] Are SQL triggers used for audit logging of critical operations?
295 - [ ] Is audit trail data properly structured using SQL tables?
296 - [ ] Are SQL functions used for timestamp and user tracking?
297 - [ ] Is change tracking implemented using SQL mechanisms?
298 - [ ] Are audit views used for secure audit data access?
299
300 ### SQL Security Patterns Assessment
301 - [ ] Are secure views used instead of direct table access?
302 - [ ] Do stored procedures include proper security checks?
303 - [ ] Are SQL functions used for secure data operations?
304 - [ ] Are constraints used for security validation?
305 - [ ] Are transactions used properly for security operations?
306
307 ## ISO/IEC Standards-Specific Evaluation Questions
308
309 ### For Any SQL Code
310 1. **Are GRANT statements used with minimal necessary privileges according to SQL:2023?**
311 2. **Are prepared statements used for all user input to prevent SQL injection?**
312 3. **Are SQL encryption functions used for sensitive data protection?**
313 4. **Are SQL triggers used for audit logging of critical operations?**
314 5. **Are secure views used instead of direct table access?**
315 6. **Are CHECK constraints used for data validation and security?**
316 7. **Are SQL functions used for secure data operations?**
317 8. **Is row-level security implemented using SQL mechanisms?**
318 9. **Are REVOKE statements used to remove unused privileges?**
319 10. **Are transactions used properly for security operations?**
320
321 ### For SQL Access Control
322 1. **Are privileges granted according to the principle of least privilege?**
323 2. **Are roles used effectively for privilege management?**
324 3. **Are column-level privileges used for sensitive data protection?**
325 4. **Is WITH GRANT OPTION used appropriately without privilege escalation?**
326 5. **Are PUBLIC privileges avoided in favor of specific roles?**
327
328 ### For SQL Data Protection
329 1. **Are SQL encryption functions used for sensitive data?**
330 2. **Are views used to protect table structures and sensitive data?**
331 3. **Are CHECK constraints implemented for data validation?**
332 4. **Is row-level security implemented using SQL mechanisms?**
333 5. **Is data masking applied in SELECT statements for sensitive data?**
334
335 ### For SQL Injection Prevention
336 1. **Are prepared statements used for all user input?**
337 2. **Are SQL string functions used properly for input sanitization?**
338 3. **Is dynamic SQL construction avoided or properly secured?**
339 4. **Are CHECK constraints used for input validation?**
340 5. **Are SQL escape functions used for special characters?**
341
342 ### For SQL Audit and Logging
343 1. **Are SQL triggers used for audit logging of critical operations?**
344 2. **Is audit trail data properly structured using SQL tables?**
345 3. **Are SQL functions used for timestamp and user tracking?**
346 4. **Is change tracking implemented using SQL mechanisms?**
347 5. **Are audit views used for secure audit data access?**
348
349 ## SQL:2023 Security Principles Applied
350
351 ### "Use SQL Access Control Mechanisms"
352 - Implement proper GRANT and REVOKE statements
353 - Use roles for privilege management
354 - Apply column-level and row-level security
355 - Control privilege propagation with WITH GRANT OPTION
356
357 ### "Implement SQL Data Protection"
358 - Use SQL encryption functions for sensitive data
359 - Implement views for data abstraction and security
360 - Apply CHECK constraints for data validation
361 - Use row-level security with SQL mechanisms
362
363 ### "Prevent SQL Injection Using SQL Features"
364 - Use prepared statements for all user input
365 - Apply SQL string functions for sanitization
366 - Avoid dynamic SQL construction with user input
367 - Use SQL constraints for input validation
368
369 ### "Implement SQL Audit and Logging"
370 - Use SQL triggers for audit logging
371 - Structure audit data using SQL tables
372 - Apply SQL functions for tracking and timestamps
373 - Use views for secure audit data access
374
375 ### "Follow SQL Security Patterns"
376 - Use secure views instead of direct table access
377 - Implement security checks in stored procedures
378 - Use SQL functions for secure data operations
379 - Apply constraints for security validation