Custom SQL Query Validation (WinForms)
- 3 minutes to read
This tutorial describes best practices to validate custom SQL queries.
Important
The use of custom SQL queries can lead to inadvertent or unauthorized modifications to your data/database structure. The default validation mechanism only allows custom queries that contain SELECT
statements (except for SELECT INTO
clauses) and blocks any SQL keywords that can potentially be used for data modification (like REPLACE
, UPDATE
, INSERT
, DELETE
, and other SQL statements). Despite this precaution, this validation is not considered safe as it does not prevent the execution of potentially harmful requests.
We strongly recommend that you implement additional custom SQL query verification. However, do not use it as the only security precaution. Ensure that you follow best practices and implement the appropriate user read/write privileges at the database level.
#Where a User Can Edit Custom SQL Queries
Refer to the following help topic for information on where a user can edit custom SQL queries and how to enable this functionality: Custom SQL Query in Report Designer.
#Manage Access Control at the Database Level
Do not use custom SQL query verification as the only security precaution. We recommend that you manage database security at the database level through appropriate access configuration and related options. By setting permissions within the database, you ensure that only authorized users and processes can access or modify data.
#Manage Custom Query Execution with Properties
- SqlWizardSettings.EnableCustomSql
This property is used in both WinForms and WPF Report Designers. If the
EnableCustomSql
property at the level of a particular control is set tofalse
, users cannot enter and execute custom SQL queries in the Data Source Wizard for that control.Do not set the
EnableCustomSql
property totrue
unless you understand the expected results and consequences, and have considered all possible security-related conditions within your organization. Remember, unrestricted execution of custom queries allows end users to modify the connected database.- SqlDataSource.AllowCustomSqlQueries
When the
AllowCustomSqlQueries
property isfalse
, an application cannot use custom SQL queries to fill the SqlDataSource instance with data at runtime. Disabling this property (false
) helps prevent execution of potentially harmful user-entered SQL, even if the UI allows it. If you are certain you want to allow custom SQL execution, set this static property totrue
:
#Validate Custom Queries with Events
- XRDesignMdiController.ValidateCustomSql
- This event is raised when a user enters a custom SQL query in the Query Builder UI of the Report Designer. Use this event when you want to control/validate custom SQL queries entered by the user.
- SqlDataSource.ValidateCustomSqlQuery
- Use this event to validate custom SQL on a specific
SqlDataSource
. Handle this event if you want per-instance control over query validation. - SqlDataSource.ValidateCustomSqlQueryGlobal
- This event allows global-level validation of all custom SQL queries. Use this event if you want to enforce consistent validation across your entire app (including Report Designers, custom wizards, and runtime-generated reports).
#Remove Default Restrictions for Custom SQL Queries
If you use custom SQL queries with restricted statements (including DELETE
, INSERT
, PROCEDURE
, UPDATE
, and other SQL statements), you receive a validation error:
A custom SQL Query should contain only SELECT Statements.
To enable unrestricted execution of all custom queries, set the static SqlDataSource.DisableCustomQueryValidation property to true
. However, we do not recommend it due to security risks.