Skip to content

Cannot read properties of undefined if the logger is an object that references other members #7297

@rix0rrr

Description

@rix0rrr

Checkboxes for prior research

Describe the bug

In the fromHttp credential provider, the function options.logger.warn is copied into a local variable, and that function is subsequently called:

const warn: (warning: string) => void =
options.logger?.constructor?.name === "NoOpLogger" || !options.logger ? console.warn : options.logger.warn;
if (relative && full) {
warn(
"@aws-sdk/credential-provider-http: " +
"you have set both awsContainerCredentialsRelativeUri and awsContainerCredentialsFullUri."
);
warn("awsContainerCredentialsFullUri will take precedence.");
}

If options.logger happens to point to a class, that might be implemented with a reference to this, then the reference to this is lost and an error will be thrown.

class SomeLogger {
  public warn(...args: string[]) {
     this.helper(...args);     // 💥 'this' is not bound to anything because the function was not called on an object
     // Cannot read properties of undefined
  }

  private helper(...args:string[]) {
    // whatever
  }
}

The warn function should retain a reference to the original object by using bind(), otherwise it is not possible to use classes as loggers which is very unexpected behavior.

const warn: (warning: string) => void =
    options.logger?.constructor?.name === "NoOpLogger" || !options.logger ? console.warn : options.logger.warn.bind(options.logger);

This pattern might occur in more places than just the place we ran into it.

Regression Issue

  • Select this option if this issue appears to be a regression.

SDK version number

Latest

Which JavaScript Runtime is this issue in?

Node.js

Details of the browser/Node.js/ReactNative version

All nodes

Reproduction Steps

See above.

Observed Behavior

See above.

Expected Behavior

See above.

Possible Solution

No response

Additional Information/Context

No response

Metadata

Metadata

Assignees

Labels

bugThis issue is a bug.needs-triageThis issue or PR still needs to be triaged.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions