Amazon DynamoDB · Create

Catch an injection attempt

A form whose fields become attributes, attacked three ways: an expression placeholder as a field name, SQL in a field name, and key separators in a value meant to land on another customer's key.

Set up for this page

The scenarios build on the sample application: SampleContext.cs (the dbContext every sample is given); SampleOutput.cs (Step and Show, which print); SampleContext.Application.cs (dbContext.Service, the application's ARX service); SampleModel.cs (the DynamoDB layout and the schema catalog); SampleContext.Direct.cs (dbContext.Direct); DynamoDirectHelper.cs (the checks behind dbContext.Direct).

dbContext.Direct is the samples' own checking code on the plain driver, not part of ARX: leave it out of your application.

1 A placeholder as a field name

A field named "#pk": ARX1017.

Already in scope: service (ArxDynamoDbService)

C#
var customer = new ArxEntity("customer");
customer["customer_id"] = "C-D601";
customer["#pk"] = "CUSTOMER#someone-else";

try
{
    await service.CreateAsync(customer);
}
catch (ArxValidationException exception) when (exception.Code == ArxErrorCode.SuspectedInjection)
{
    Console.WriteLine(exception.Message);
}

Check it in the database

PartiQL

A PartiQL statement: run it in NoSQL Workbench, or with aws dynamodb execute-statement --statement, after the code has run.

SELECT "PK" FROM "crm" WHERE "PK" = 'CUSTOMER#C-D601'

Expect: no items.

2 SQL in a field name

SQL in a field name: ARX1017 too.

Already in scope: service (ArxDynamoDbService)

C#
var customer = new ArxEntity("customer");
customer["customer_id"] = "C-D602";
customer["name'; DROP TABLE crm; --"] = "x";

try
{
    await service.CreateAsync(customer);
}
catch (ArxValidationException exception) when (exception.Code == ArxErrorCode.SuspectedInjection)
{
    Console.WriteLine(exception.Message);
}

Check it in the database

PartiQL

A PartiQL statement: run it in NoSQL Workbench, or with aws dynamodb execute-statement --statement, after the code has run.

SELECT "PK" FROM "crm" WHERE "PK" = 'CUSTOMER#C-D602'

Expect: no items.

3 A separator in a key value

An order number "x#ORDER#y": escaped in the key, so it stays this customer's order.

Already in scope: service (ArxDynamoDbService)

C#
var order = new ArxEntity("sales_order");
order["customer_id"] = "C-D603";
order["order_number"] = "x#ORDER#y";
order["status"] = "OPEN";
order["ordered_on"] = "2026-11-09";

await service.CreateAsync(order);

Console.WriteLine("stored, its key escaped");

Check it in the database

PartiQL

A PartiQL statement: run it in NoSQL Workbench, or with aws dynamodb execute-statement --statement, after the code has run.

SELECT "SK", "order_number" FROM "crm" WHERE "PK" = 'CUSTOMER#C-D603'

Expect: 1 item, SK "ORDER#x%23ORDER%23y", order_number "x#ORDER#y".

Good to know

Names with #, : or SQL syntax are ARX1017 before anything is sent: names reach DynamoDB only as placeholders. Values in key templates are escaped, so # in a value can't make another entity's key.

See also

Run the complete sample

The code above comes from one program, built in a fresh project and run against the database. To run it yourself, here is every file and step.

The program, its files and how to run it

Run it locally

  1. .NET SDK: .NET SDK 10.0.100 or later 10.0.x (target net10.0).
  2. Create the project:
    Terminal
    dotnet new console -n ArxSample -f net10.0
    cd ArxSample
  3. Install the packages:
    Terminal
    dotnet add package ARX.SDK.DynamoDb --version 1.0.0-rc.1
  4. Database: a Amazon DynamoDB, or DynamoDB Local for development (tested with DynamoDB Local 2.5.2) database with the sample schema.
  5. Configuration: set ARX_SAMPLES_DYNAMODB to its connection string. Nothing is hard-coded.
  6. Source code: add the files of the sample application this sample uses (SampleContext.cs, SampleOutput.cs, SampleContext.Application.cs, SampleModel.cs, SampleContext.Direct.cs, DynamoDirectHelper.cs), then replace Program.cs and add the other file below, with the file names shown.
  7. Run it:
    Terminal
    dotnet run
  8. Expected output: below.

The files

Program.cs

Runs the sample. Generated for this page; it reads the connection string from the environment and first remakes the sample database from sample-schema.json.

using ArxSamples.DynamoDb;
using ArxSamples.DynamoDb.Operations.Create.RealCases;

// The connection string comes from the environment, never from the code (see "Configure the connection").
var connectionString = Environment.GetEnvironmentVariable("ARX_SAMPLES_DYNAMODB")
    ?? throw new InvalidOperationException("Set the ARX_SAMPLES_DYNAMODB environment variable to your Amazon DynamoDB connection string.");

var dbContext = new SampleContext(connectionString);

// Every run starts from the sample data: this deletes and makes again what sample-schema.json lists
// (save the file in the project folder, see Get started).
await dbContext.Direct.ResetAsync();

await new RealCase2_CatchAnInjectionAttempt().RunAsync(dbContext);
RealCase2_CatchAnInjectionAttempt.cs

The sample, as it is in the repository.

using ARX.SDK;
using ARX.SDK.DynamoDb.Services;
using ARX.SDK.Services;

using static ArxSamples.DynamoDb.SampleOutput;

namespace ArxSamples.DynamoDb.Operations.Create.RealCases;

/// <summary>
/// A form whose fields become attributes, attacked three ways: an expression placeholder as a field name, SQL in a
/// field name, and key separators in a value meant to land on another customer's key.
/// </summary>
/// <remarks>
/// Names with <c>#</c>, <c>:</c> or SQL syntax are ARX1017 before anything is sent: names reach DynamoDB only as
/// placeholders. Values in key templates are escaped, so <c>#</c> in a value can't make another entity's key.
/// </remarks>
public sealed class RealCase2_CatchAnInjectionAttempt : ISample
{
    public string Name => "create-real-2-catch-an-injection-attempt";

    public string Title => "Catch an injection attempt";

    public async Task RunAsync(SampleContext dbContext)
    {
        await Scenario1_APlaceholderAsAFieldNameAsync(dbContext.Service);

        await Scenario2_SqlInAFieldNameAsync(dbContext.Service);

        await Scenario3_ASeparatorInAKeyValueAsync(dbContext.Service);
    }

    /// <summary>1. A field named "#pk": ARX1017.</summary>
    private static async Task Scenario1_APlaceholderAsAFieldNameAsync(ArxDynamoDbService service)
    {
        Step("1. A placeholder as a field name");

        var customer = new ArxEntity("customer");
        customer["customer_id"] = "C-D601";
        customer["#pk"] = "CUSTOMER#someone-else";

        try
        {
            await service.CreateAsync(customer);
        }
        catch (ArxValidationException exception) when (exception.Code == ArxErrorCode.SuspectedInjection)
        {
            ShowError(exception);
        }

        /* ===========================================================
         * [DIRECT QUERY CHECK]
         * -----------------------------------------------------------
         * SELECT "PK" FROM "crm" WHERE "PK" = 'CUSTOMER#C-D601'
         * -- Expect: no items.
         * ===========================================================
         */
    }

    /// <summary>2. SQL in a field name: ARX1017 too.</summary>
    private static async Task Scenario2_SqlInAFieldNameAsync(ArxDynamoDbService service)
    {
        Step("2. SQL in a field name");

        var customer = new ArxEntity("customer");
        customer["customer_id"] = "C-D602";
        customer["name'; DROP TABLE crm; --"] = "x";

        try
        {
            await service.CreateAsync(customer);
        }
        catch (ArxValidationException exception) when (exception.Code == ArxErrorCode.SuspectedInjection)
        {
            ShowError(exception);
        }

        /* ===========================================================
         * [DIRECT QUERY CHECK]
         * -----------------------------------------------------------
         * SELECT "PK" FROM "crm" WHERE "PK" = 'CUSTOMER#C-D602'
         * -- Expect: no items.
         * ===========================================================
         */
    }

    /// <summary>3. An order number "x#ORDER#y": escaped in the key, so it stays this customer's order.</summary>
    private static async Task Scenario3_ASeparatorInAKeyValueAsync(ArxDynamoDbService service)
    {
        Step("3. A separator in a key value");

        var order = new ArxEntity("sales_order");
        order["customer_id"] = "C-D603";
        order["order_number"] = "x#ORDER#y";
        order["status"] = "OPEN";
        order["ordered_on"] = "2026-11-09";

        await service.CreateAsync(order);

        Show("stored, its key escaped");

        /* ===========================================================
         * [DIRECT QUERY CHECK]
         * -----------------------------------------------------------
         * SELECT "SK", "order_number" FROM "crm" WHERE "PK" = 'CUSTOMER#C-D603'
         * -- Expect: 1 item, SK "ORDER#x%23ORDER%23y", order_number "x#ORDER#y".
         * ===========================================================
         */
    }
}

Expected output

What dotnet run printed when the sample was verified, on a freshly created sample schema. Ids differ when the tables already hold rows.

Output
  > 1. A placeholder as a field name
      caught ArxValidationException: ARX1017 SuspectedInjection: Attribute name '#pk' looks like an SQL injection attempt (contains '#' or ':' (DynamoDB expression syntax)). ARX refused it before sending anything to the database. Names must come from your code or a fixed list, never from user input.
  > 2. SQL in a field name
      caught ArxValidationException: ARX1017 SuspectedInjection: Attribute name 'name'; DROP TABLE crm; --' looks like an SQL injection attempt (contains '''). ARX refused it before sending anything to the database. Names must come from your code or a fixed list, never from user input.
  > 3. A separator in a key value
      stored, its key escaped