Mitigation Strategy: Use Placeholders and Dataset Methods
Description:
- Identify all user inputs: Determine all points where data from users (or external sources) is used in database queries constructed using Sequel.
- Replace direct string concatenation: Wherever user input is currently being directly inserted into SQL strings within Sequel code (e.g., within
where
,select
,order
, etc.), replace it with Sequel's placeholder syntax. - Prefer symbolic placeholders: Use the
column: value
syntax within Sequel's dataset methods (e.g.,dataset.where(username: params[:username])
). - Use
?
placeholders when necessary: If symbolic placeholders aren't suitable, use the?
placeholder syntax (e.g.,dataset.where("username = ?", params[:username])
). - Avoid
Sequel.lit
: MinimizeSequel.lit
. If unavoidable, ensure rigorous input validation before using it, and document the reason clearly within the Sequel-related code. - Review all Sequel query construction: Conduct a thorough code review to ensure that all Sequel-based database queries are using placeholders or Sequel's safe dataset methods.
- Use Virtual Row Blocks with Placeholders: When using Sequel's virtual row blocks, ensure user-supplied values are passed as placeholder arguments to the block, not directly embedded.
Threats Mitigated:
- SQL Injection (Severity: Critical): Attackers can inject malicious SQL code via Sequel if input is not handled correctly within Sequel's API.
- Data Disclosure (Severity: High): Improperly constructed Sequel queries might unintentionally expose sensitive data.
- Data Modification (Severity: High): Attackers could alter or delete data without authorization through manipulated Sequel queries.
- Denial of Service (Severity: Medium): Malicious Sequel queries could overload the database.
Impact:
- SQL Injection: Risk reduced to near zero if implemented correctly within Sequel.
- Data Disclosure: Significantly reduced risk.
- Data Modification: Significantly reduced risk.
- Denial of Service: Some reduction in risk.
Currently Implemented:
- Example:
- "User authentication queries in
models/user.rb
use Sequel placeholders." - "Product search in
controllers/products_controller.rb
uses Sequel'sfilter
with symbolic placeholders."
- "User authentication queries in
Missing Implementation:
- Example:
- "Legacy code in
lib/legacy_reports.rb
uses string concatenation within Sequel calls. Refactor needed." - "
controllers/admin/users_controller.rb
usesSequel.lit
with insufficient validation within a Sequel query. Review and rewrite."
- "Legacy code in
Mitigation Strategy: Whitelist Dynamic Table/Column Names (within Sequel)
Description:
- Identify dynamic identifiers in Sequel calls: Locate all instances where table or column names are determined by user input and are used within Sequel's API.
- Create whitelists: For each instance, create a hardcoded list of allowed table and column names.
- Validate input before Sequel usage: Before passing the user-provided table/column name to any Sequel method, check if it exists in the whitelist.
- Handle invalid input: If the input is not in the whitelist, reject the request. Do not pass the invalid input to Sequel.
- Prefer symbols with Sequel: Use symbols (e.g.,
:users
,:name
) for table and column names within Sequel calls whenever possible. - Review and document: Document the whitelisting logic related to Sequel calls.
Threats Mitigated:
- SQL Injection (Severity: Critical): Prevents attackers from accessing arbitrary tables/columns through Sequel.
- Data Disclosure (Severity: High): Limits access to only authorized tables/columns via Sequel.
- Data Modification (Severity: High): Prevents unauthorized modification via Sequel.
Impact:
- SQL Injection: Significantly reduces risk related to dynamic identifiers used with Sequel.
- Data Disclosure/Modification: Greatly reduces risk.
Currently Implemented:
- Example:
- "The reporting module (
modules/reporting.rb
) uses a whitelist for allowed report types before passing them to Sequel." - "Dynamic sorting in
controllers/products_controller.rb
validates the sort column before using it in a Sequelorder
call."
- "The reporting module (
Missing Implementation:
- Example:
- "User profile editing (
controllers/users_controller.rb
) allows specifying fields to update, but this is not validated before being used in a Sequelupdate
call."
- "User profile editing (
Mitigation Strategy: Avoid/Sanitize Raw SQL within Sequel.[]
Description:
- Minimize raw SQL within Sequel: Prioritize Sequel's dataset methods. Raw SQL within
Sequel.[]
orDB.fetch
should be a last resort. - Justify and document: If raw SQL is unavoidable within Sequel, clearly document the reason.
- Use parameterized queries (within raw SQL): Even within
Sequel.[]
, always use parameterized queries (placeholders) to handle user input. The syntax depends on the database adapter, but Sequel passes this through. - Code review: Any code using
Sequel.[]
orDB.fetch
should be rigorously reviewed, focusing on potential SQL injection within the raw SQL string. - Consider Sequel alternatives: Explore all Sequel features before resorting to raw SQL within
Sequel.[]
.
Threats Mitigated:
- SQL Injection (Severity: Critical): Directly addresses the highest risk associated with using raw SQL through Sequel.
- Data Disclosure (Severity: High): Reduces risk.
- Data Modification (Severity: High): Reduces risk.
- Denial of Service (Severity: Medium): Reduces risk.
Impact:
- SQL Injection: Significantly reduces risk if parameterized queries are used correctly within the raw SQL passed to Sequel. Eliminates risk if raw SQL within Sequel is avoided.
- Data Disclosure/Modification/DoS: Similar impact.
Currently Implemented:
- Example:
- "The core application logic avoids raw SQL within Sequel calls."
- "A database-specific optimization in
lib/performance_tweaks.rb
uses parameterized raw SQL withinDB.fetch
, and the reason is documented."
Missing Implementation:
- Example:
- "Older parts of the application, in
lib/legacy
, use raw SQL withinSequel.[]
without parameterization. Refactor needed." - "No formal policy specifically addresses the use of raw SQL within Sequel calls."
- "Older parts of the application, in
Mitigation Strategy: Secure Deserialization (Sequel Plugins)
Description:
- Identify Sequel plugin deserialization: Determine all places where Sequel plugins are used to deserialize data (e.g., JSON, YAML, or custom serialization plugins).
- Use safe libraries (within the plugin context): Ensure that the Sequel plugins themselves, or the libraries they rely on, use secure deserialization methods. If you're using a custom plugin, ensure it uses safe methods.
- Validate after deserialization (by Sequel): After Sequel (or its plugin) deserializes data, rigorously validate the resulting data structure and contents.
- Consider alternatives to Sequel-based serialization: If possible, avoid storing complex serialized data that needs to be handled by Sequel.
- Keep Sequel and plugins updated: Regularly update Sequel and all related plugins to patch vulnerabilities.
Threats Mitigated:
- Remote Code Execution (Severity: Critical): Unsafe deserialization by Sequel or its plugins can lead to RCE.
- Data Tampering (Severity: High): Attackers could modify serialized data.
Impact:
- Remote Code Execution: Significantly reduces risk if safe deserialization practices are followed within the Sequel plugin context.
- Data Tampering: Reduces risk through validation after Sequel deserializes.
Currently Implemented:
- Example:
- "The application uses the
pg_json
Sequel extension, and we validate the data after retrieval." - "Custom Sequel plugins are reviewed for safe deserialization practices."
- "The application uses the
Missing Implementation:
- Example:
- "The
legacy_data
column uses a custom Sequel plugin for YAML deserialization, and the plugin's safety is not verified." - "There is no comprehensive validation of data deserialized by Sequel plugins in all parts of the application."
- "The