# Unsanitized user input in AWS query

## Overview

* **Rule ID**: `java_third_parties_aws_query_injection`
* **Applicable Languages**: Java
* **Weakness ID**: CWE-943

## Description

Including unsanitized data, such as user input or request data, in raw queries makes your application vulnerable to injection attacks.

## Remediation Guidelines

* **Always sanitize user input**, especially if it will be used in database queries. Sanitization should include the removal of special characters (such as ' or ") that could be used to manipulate the query's semantics.
* **Validate** user input to ensure it meets the expected format and length wherever possible.
* **Use parameterized queries** instead of concatenating user input directly into the query string. This separates query logic from user input, which is best practice and, in the case of AWS SimpleDB, allows for internal parameterization and sanitization through `SelectRequest`.

  ```java
  // query logic
  public static SelectResult executeQuery(String query, String itemName) {
      AmazonSimpleDB simpleDBClient = AmazonSimpleDBClientBuilder.defaultClient();
      SelectRequest selectRequest = new SelectRequest(query, true).withNextToken(itemName);

      return simpleDBClient.select(selectRequest);
    }

  public static void selectItem(String itemName) { // itemName is dynamic and could be malicious
    // parameterized query string
    String query = "select * from items where itemName = ?";

    SelectResult result = executeQuery(query, itemName);
    ...
   } 
  ```

## References

* [**AWS SimpleDB docs**](https://docs.aws.amazon.com/AmazonSimpleDB/latest/DeveloperGuide/Welcome.html)
* [**CWE-943: Improper Neutralization of Special Elements in Data Query Logic**](https://cwe.mitre.org/data/definitions/943.html)

## Configuration

To omit this rule during a scan, and to provide you with continuous 24/7 code-level scanning, you can employ our [**SAST TOOL**](https://scopy.sec1.io/login)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.sec1.io/user-docs/4-sast/2-java/unsanitized-user-input-in-aws-query.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
