# Improper Neutralization of Special Elements in Data Query Logic (CWE-943) The product generates a query intended to access or manipulate data in a data store, but it does not neutralize or incorrectly neutralizes special elements that can modify the intended logic of the query. - Prevalence: 高 频繁被利用 - Impact: 高 3 条严重级别为高的规则 - Prevention: 已记录 3 个修复示例 **OWASP:** Injection (A03:2021-Injection) - #3 ## Description This covers injection attacks against NoSQL databases, ORM frameworks, and other data query mechanisms that differ from traditional SQL injection. ## Prevention 基于 3 条 Shoulder 检测规则的 NoSQL Injection 预防策略。 ### Go Use typed structs or explicit operators, validate all user input ### JavaScript Validate input types and sanitize MongoDB operators ### Python Validate input types and use ObjectId for ID fields ## Warning Signs - [HIGH] user input flowing to MongoDB or Redis queries without proper validation - [HIGH] user input flowing into NoSQL database queries without validation - [HIGH] untrusted user input being used in NoSQL queries without proper validation ## Consequences - 读取应用程序数据 - 修改应用程序数据 - 绕过保护机制 ## Mitigations - 对所有数据存储使用参数化查询 - 在用于查询前对输入进行验证和净化 - 对数据库账户应用最小权限原则 ## Detection - Total rules: 3 - Languages: go, javascript, typescript, python ## Rules by Language ### Go (1 rules) - **NoSQL Injection** [HIGH]: Detects user input flowing to MongoDB or Redis queries without proper validation. - Remediation: Use typed structs or explicit $eq operators for MongoDB, validate Redis keys with regex. ```go filter := bson.M{"username": bson.M{"$eq": username}} ``` Learn more: https://shoulder.dev/learn/go/cwe-943/nosql-injection ### Javascript (1 rules) - **NoSQL Injection via MongoDB Queries** [HIGH]: Detects user input flowing into NoSQL database queries without validation. - Remediation: Validate input types and use mongo-sanitize to remove operators from user input. ```javascript const sanitized = mongoSanitize(req.body.query); const user = await User.findOne(sanitized); ``` Learn more: https://shoulder.dev/learn/javascript/cwe-943/nosql-injection ### Typescript (1 rules) - **NoSQL Injection via MongoDB Queries** [HIGH]: Detects user input flowing into NoSQL database queries without validation. - Remediation: Validate input types and use mongo-sanitize to remove operators from user input. ```javascript const sanitized = mongoSanitize(req.body.query); const user = await User.findOne(sanitized); ``` Learn more: https://shoulder.dev/learn/javascript/cwe-943/nosql-injection ### Python (1 rules) - **NoSQL Injection** [HIGH]: Detects untrusted user input being used in NoSQL queries without proper validation. - Remediation: Validate and type-check user input before using in queries. ```python from bson import ObjectId object_id = ObjectId(user_id) # Validates format ``` Learn more: https://shoulder.dev/learn/python/cwe-943/nosql-injection