Skip to content

Bug: AWS_SDK_UA_APP_ID can exceed 50-character limit during initialisation #5082

@akee01

Description

@akee01

Expected Behavior

The AWS_SDK_UA_APP_ID should remain within the 50-character limit and should not contain redundant or duplicated Powertools version strings, ensuring compatibility with the AWS SDK.

Why this matters

As shown in the logic flow of how User Agents are constructed, the App ID is a suffix that helps AWS identify the origin of the request. If it is malformed or too long, it risks breaking the integration with AWS services that enforce strict header validation.

Current Behavior

In packages/commons/src/index.ts, the logic used to append Powertools metadata to the AWS_SDK_UA_APP_ID environment variable does not account for the AWS SDK's 50-character limit or the possibility of multiple initializations (e.g., when multiple versions of commons exist in the dependency tree).

The current implementation blindly appends strings:

if (process.env.AWS_SDK_UA_APP_ID) {
  process.env.AWS_SDK_UA_APP_ID = `${process.env.AWS_SDK_UA_APP_ID}/PT/NO-OP/${PT_VERSION}/PTEnv/${env}`;
} else {
  process.env.AWS_SDK_UA_APP_ID = `PT/NO-OP/${PT_VERSION}/PTEnv/${env}`;
}

If a user already has an App ID defined, or if two different versions of the library are loaded (common in monorepos or nested dependencies), the string can quickly look like this:
MyCustomAppID/PT/NO-OP/2.0.0/PTEnv/prod/PT/NO-OP/2.1.0/PTEnv/prod

This exceeds the 50-character limit defined in the AWS SDK Shared Configuration documentation, which may lead to SDK initialization errors or truncated User-Agent headers.

Code snippet

/**
 * Reproduction Script: AWS SDK UA App ID Overflow
 * This simulates how 'packages/commons/src/index.ts' handles the environment variable.
 */

const PT_VERSION = "2.0.4";
const env = "production";
const LIMIT = 50;

function simulateCommonsInit() {
    // This is the exact logic currently in Powertools commons
    if (process.env.AWS_SDK_UA_APP_ID) {
        process.env.AWS_SDK_UA_APP_ID = `${process.env.AWS_SDK_UA_APP_ID}/PT/NO-OP/${PT_VERSION}/PTEnv/${env}`;
    } else {
        process.env.AWS_SDK_UA_APP_ID = `PT/NO-OP/${PT_VERSION}/PTEnv/${env}`;
    }
}

// --- SCENARIO 1: User already has a custom App ID ---
process.env.AWS_SDK_UA_APP_ID = "MyCompany-ECommerce-Ordering-Service"; // 36 chars
console.log(`Initial Length: ${process.env.AWS_SDK_UA_APP_ID.length}`);

simulateCommonsInit();

console.log(`\n--- After 1st Init ---`);
console.log(`Value:  ${process.env.AWS_SDK_UA_APP_ID}`);
console.log(`Length: ${process.env.AWS_SDK_UA_APP_ID.length}`);
console.log(`Status: ${process.env.AWS_SDK_UA_APP_ID.length > LIMIT ? '❌ FAILED (Too Long)' : '✅ OK'}`);

// --- SCENARIO 2: Multiple versions of Powertools loaded ---
// (Simulating a second 'commons' package from a different version/location being loaded)
simulateCommonsInit();

console.log(`\n--- After 2nd Init (Multiple Versions) ---`);
console.log(`Value:  ${process.env.AWS_SDK_UA_APP_ID}`);
console.log(`Length: ${process.env.AWS_SDK_UA_APP_ID.length}`);
console.log(`Status: ${process.env.AWS_SDK_UA_APP_ID.length > LIMIT ? '❌ FAILED (Too Long)' : '✅ OK'}`);

Steps to Reproduce

  1. Set an initial environment variable: export AWS_SDK_UA_APP_ID="MyExistingApplicationID" (24 chars)
  2. Import any Powertools utility that triggers the commons index initialization
  3. Observe that the new value is ~55+ characters
  4. If a second version of commons is initialised, the string grows indefinitely

Possible Solution

  1. Idempotency: Check if the string already contains PT/NO-OP before appending
  2. Bounds Checking: Ensure the final string is sliced to a maximum of 50 characters

Powertools for AWS Lambda (TypeScript) version

latest

AWS Lambda function runtime

24.x

Packaging format used

npm

Execution logs

WARN The provided userAgentAppId exceeds the maximum length of 50 characters.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingneed-responseThis item requires a response from a customer and will considered stale after 2 weeks

    Type

    No type

    Projects

    Status

    Pending customer

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions