Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(NODE-3487): check for nullish aws mechanism property #2951

Merged
merged 3 commits into from
Aug 27, 2021

Conversation

nbbeeken
Copy link
Contributor

Checks for nullish aws mechanism property instead of falsy, but usage in the AWS mechanism propery checks for falsiness, this allows users to pass an empty string to avoid using the environment variable.

(needs 3.x port)

{
accessKeyId: username,
secretAccessKey: password,
token
Copy link
Contributor Author

@nbbeeken nbbeeken Aug 25, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should've been sessionToken, I moved the whole object declaration above.

a: options.headers.Authorization,
d: options.headers['X-Amz-Date']
};
if (sessionToken != null && sessionToken !== '') {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this just stay false-y?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I think falsey-ness works best here, anything except a non-empty string should probably be ignored.

Copy link
Contributor Author

@nbbeeken nbbeeken Aug 26, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SGTM or I can also do typeof sessionToken === 'string' && sessionToken != ''
Explicit permitting of non-empty string typed variables would be the strictest

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we only ever read the credentials in from the env variable, there is no chance of it being anything other than a string, so I think the if (sessionToken) check would be perfectly fine, and if we read it in from anywhere else, we should really be doing this validation at that entry point, not here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indeed, and we do validate this in MongoCredentials so I put this back to a simple faleyness check

'X-Amz-Date': string;
};
};
}
Copy link
Contributor Author

@nbbeeken nbbeeken Aug 25, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I inlined our own definitions of the types to hold us strictly to our usage, also this catches the mistake of passing in token instead of sessionToken

@emadum emadum self-requested a review August 26, 2021 15:25
a: options.headers.Authorization,
d: options.headers['X-Amz-Date']
};
if (sessionToken != null && sessionToken !== '') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I think falsey-ness works best here, anything except a non-empty string should probably be ignored.

@emadum emadum requested a review from dariakp August 26, 2021 17:46
@emadum emadum added the Team Review Needs review from team label Aug 26, 2021

// If all three defined, include sessionToken, else include username and pass, else no credentials
const awsCredentials =
accessKeyId != null && secretAccessKey != null && sessionToken != null
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so it's ok to have an empty string sessionToken in the options here but not in L146? what are the rules precisely?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think based on your reply to my other comment, the type validation should be done in the MongoCredentials class. Here we should rely on falseness for all 3 properties.

The empty string filtering below was meant to handle the case where its being set to an empty string by the user to explicitly not make use of the environment variable. But being empty we don't actually want to send it in the auth process. It probably would be harmless to send it.. maybe we're doing too much special casing.

Should I just follow the spec precisely?
Which would be: If the token is set by the user in the options you MUST use it, regardless of whether it is an empty string?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I am still a bit lost here. My understanding is that we do want the user to be able to override the env variable by passing in any non-null value, that's not in question. But it was also my understanding that if sessionToken === '' then we don't want to sent it to aws, but by setting it here and passing it in L139, we are in fact passing the empty string, no? I was comparing L139 to L146 where we explicitly omit it in L146 but not in L139. What am I missing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are not missing anything, I am, 😅 fixed the code here, I was getting mixed up in the assertions. We just confirm non-falsy-ness here since type validation has already been done in the MongoCredentials, and we want to skip using the sessionToken if it is an empty string.

a: options.headers.Authorization,
d: options.headers['X-Amz-Date']
};
if (sessionToken != null && sessionToken !== '') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we only ever read the credentials in from the env variable, there is no chance of it being anything other than a string, so I think the if (sessionToken) check would be perfectly fine, and if we read it in from anywhere else, we should really be doing this validation at that entry point, not here

@emadum emadum merged commit 78ec0dd into 4.1 Aug 27, 2021
@emadum emadum deleted the NODE-3487/aws-session-env branch August 27, 2021 15:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Team Review Needs review from team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants