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
- Set an initial environment variable:
export AWS_SDK_UA_APP_ID="MyExistingApplicationID" (24 chars)
- Import any Powertools utility that triggers the
commons index initialization
- Observe that the new value is ~55+ characters
- If a second version of
commons is initialised, the string grows indefinitely
Possible Solution
- Idempotency: Check if the string already contains
PT/NO-OP before appending
- 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.
Expected Behavior
The
AWS_SDK_UA_APP_IDshould 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 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
Steps to Reproduce
export AWS_SDK_UA_APP_ID="MyExistingApplicationID"(24 chars)commonsindex initializationcommonsis initialised, the string grows indefinitelyPossible Solution
PT/NO-OPbefore appendingPowertools for AWS Lambda (TypeScript) version
latest
AWS Lambda function runtime
24.x
Packaging format used
npm
Execution logs