Node.js Getting Started with ASM

You can monitor application security for Node.js apps running in Docker, Kubernetes, AWS ECS, and AWS Fargate.

In general, setting up Application Security Management (ASM) involves:

  1. Identifying services that are vulnerable or are under attack, which would most benefit from ASM. Find them on the Security tab of your Service Catalog.
  2. Updating to the latest Datadog library (the most recent APM tracing library).
  3. Enabling the library to collect the application security data from the services and send it to Datadog.
  4. Triggering security signals in your application and seeing how Datadog displays the resulting information.

Prerequisites

  • The Datadog Agent is installed and configured for your application’s operating system or container, cloud, or virtual environment.
  • Datadog APM is configured for your application or service, and traces are being received by Datadog.
Beta: 1-Click Enablement
If your service is running with an Agent with Remote Configuration enabled and a tracing library version that supports it, hover over the Not Enabled indicator in the ASM Status column and click Enable ASM. There's no need to re-launch the service with the DD_APPSEC_ENABLED=true or --enable-appsec flags.

Get started

  1. Update your Datadog Node.js library package to at least version 2.23.0 (for NodeJS 12+) or 3.10.0 (for NodeJS 14+), by running one of these commands:

    npm install dd-trace@^2.23.0
    npm install dd-trace@^3.10.0
    

    or to update from a previously installed 1.x version:

    npm install dd-trace@2
    

    Use this migration guide to assess any breaking changes if you upgraded your library from 1.x to 2.x.

    Application Security Management is compatible with Express v4+ and NodeJS v12.17.0+. For additional information, see Compatibility.

  2. Where you import and initialize the Node.js library for APM, also enable ASM. This might be either in your code or with environment variables. If you initialized APM in code, add {appsec: true} to your init statement:

    // This line must come before importing any instrumented module.
    const tracer = require('dd-trace').init({
      appsec: true
    })
    

    For TypeScript and bundlers that support EcmaScript Module syntax, initialize the tracer in a separate file in order to maintain correct load order.

    // server.ts
    import './tracer'; // must come before importing any instrumented module.
    
    // tracer.ts
    import tracer from 'dd-trace';
    tracer.init({
      appsec: true
    }); // initialized in a different file to avoid hoisting.
    export default tracer;
    

    If the default config is sufficient, or all configuration is done through environment variables, you can also use dd-trace/init, which loads and initializes in one step.

    import `dd-trace/init`;
    

    Or if you initialize the APM library on the command line using the --require option to Node.js:

    node --require dd-trace/init app.js
    

    Then use environment variables to enable ASM:

    DD_APPSEC_ENABLED=true node app.js
    

    How you do this varies depending on where your service runs:

    Update your configuration container for APM by adding the following argument in your docker run command:

    docker run [...] -e DD_APPSEC_ENABLED=true [...] 
    

    Add the following environment variable value to your container Dockerfile:

    ENV DD_APPSEC_ENABLED=true
    

    Update your configuration yaml file container for APM and add the AppSec env variable:

    spec:
      template:
        spec:
          containers:
            - name: <CONTAINER_NAME>
              image: <CONTAINER_IMAGE>/<TAG>
              env:
                - name: DD_APPSEC_ENABLED
                  value: "true"
    

    Update your ECS task definition JSON file, by adding this in the environment section:

    "environment": [
      ...,
      {
        "name": "DD_APPSEC_ENABLED",
        "value": "true"
      }
    ]
    

    Initialize ASM in your code or set DD_APPSEC_ENABLED environment variable to true in your service invocation:

    DD_APPSEC_ENABLED=true node app.js
    

    After this configuration is complete, the library collects security data from your application and sends it to the Agent, which sends it to Datadog, where out-of-the-box detection rules flag attacker techniques and potential misconfigurations so you can take steps to remediate.

  3. To see Application Security Management threat detection in action, send known attack patterns to your application. For example, trigger the Security Scanner Detected rule by running a file that contains the following curl script:

    for ((i=1;i<=200;i++)); 
    do
    # Target existing service’s routes
    curl https://your-application-url/existing-route -A dd-test-scanner-log;
    # Target non existing service’s routes
    curl https://your-application-url/non-existing-route -A dd-test-scanner-log;
    done

    Note: The dd-test-scanner-log value is supported in the most recent releases.

    A few minutes after you enable your application and exercise it, threat information appears in the Application Signals Explorer and vulnerability information appears in the Vulnerability Explorer.

Further Reading