Skip to content

fix: prevent unhandled credential prefetch rejections - #900

Open
tamazonian wants to merge 3 commits into
aws-observability:mainfrom
tamazonian:fix/handle-credential-prefetch-rejection
Open

tamazonian wants to merge 3 commits into
aws-observability:mainfrom
tamazonian:fix/handle-credential-prefetch-rejection

Conversation

@tamazonian

Copy link
Copy Markdown

Why this change

I saw InvalidTokenError in a web application's error logs. I traced it to the function RUM uses to get AWS credentials, rather than the application's own code.

RUM calls that function early, in setAwsCredentials(), so credentials are ready before the page closes. But it ignores the returned promise. If the request fails, the browser reports an unhandled error that other error loggers can pick up.

I reproduced this in aws-rum-web 3.2.1 using a function that always fails. The problem also happens with a plain Error, not just InvalidTokenError.

What the fix does

Catch the failed early request and log a warning when debug logging is on:

Could not get AWS credentials. RUM may be unable to send monitoring data.

The warning does not include the original error or credential details. RUM still uses the same credential function for later requests. This does not fix AWS access or recover lost monitoring data.

Promise.resolve keeps existing JavaScript callers working when they return credentials directly or an object with a then method. Errors thrown immediately by the function are unchanged.

Try it

Both pages run the same test. The first uses the released SDK; the second uses this proposed fix. No monitoring data is sent.

import { AwsRum } from 'aws-rum-web';

const rum = new AwsRum(
    '00000000-0000-4000-8000-000000000000', // Dummy monitor ID
    '1.0.0',
    'us-east-1',
    {
        telemetries: [],
        allowCookies: false,
        enableRumClient: false, // Do not send monitoring data
        debug: true
    }
);

rum.setAwsCredentials(() =>
    Promise.reject(new Error('Example credential failure'))
);

Tests

  • Full build and 756 unit tests passed.
  • Both live demos and 12 browser checks passed in Chrome, including blocked scripts and checks that prevent a false success.
  • Local tests used Node.js 24. The project's CI uses Node.js 20, which I have not tested locally.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant