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

SNOW-1825789 Secure token cache #1012

Open
wants to merge 48 commits into
base: master
Choose a base branch
from

Conversation

sfc-gh-astachowski
Copy link

No description provided.

@sfc-gh-astachowski sfc-gh-astachowski requested a review from a team as a code owner February 18, 2025 08:22
return false;
}
const cacheStat = await fs.lstat(cacheDir).catch(() => {});
if (!Util.exists(cacheStat)) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

In this logic, the empty cacheStats is the correct equivalent for the directory that doesn't exist. We also return empty even for any cached error.

Copy link
Author

Choose a reason for hiding this comment

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

Looking back at it, I should probably check if the error is ENOENT and only ignore that one.

if (process.platform !== 'win32') {
options.mode = 0o700;
}
await fs.mkdir(cacheDir, options);
Copy link
Collaborator

@sfc-gh-pmotacki sfc-gh-pmotacki Mar 7, 2025

Choose a reason for hiding this comment

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

According to the previous comment, we can skip verification checking the directory exists?

Copy link
Author

Choose a reason for hiding this comment

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

With the fix mentioned above, we should enter this branch iff the directory doesn't exist

if ((cacheStat.mode & 0o777) === 0o700) {
return true;
}
await fs.chmod(cacheDir, 0o700);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why is the permission changing for the existing directory? For me it is unsafe ...

Copy link
Author

@sfc-gh-astachowski sfc-gh-astachowski Mar 7, 2025

Choose a reason for hiding this comment

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

};


this.withFileLocked = async function (fun) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should we treat fun as a callback?

Copy link
Author

Choose a reason for hiding this comment

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

Well, I'm not sure how you define callback. It is called within the function, but not at the end of the function, as we need to cleanup after

const { getSecureHandle } = require('../../file_util');

function JsonCredentialManager(credentialCacheDir, timeoutMs = 60000) {
const topLevelKey = 'tokens';
Copy link
Collaborator

Choose a reason for hiding this comment

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

It would be more readable for me if the name indicated its relation to the token.

lib/file_util.js Outdated
@@ -164,7 +164,9 @@ exports.validateOnlyUserReadWritePermissionAndOwner = async function (filePath,
if (octalPermissions === '600') {
Logger.getInstance().debug(`Validated that the user has only read and write permission for file: ${filePath}, Permission: ${permission}`);
} else {
throw new Error(`Invalid file permissions (${octalPermissions} for file ${filePath}). Make sure you have read and write permissions and other users do not have access to it. Please remove the file and re-run the driver.`);
await fsp.chmod(filePath, 0o600).catch(() => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why are we changing permission for existing file? Potentially, it could be modified by an attacker.

Copy link
Author

Choose a reason for hiding this comment

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

From the design doc: "If cache file doesn’t have exactly 600 permissions: all operations should attempt to change the permissions to 600 or fail if not possible"

That being said, as I ended up creating a new function based on this one and no longer use it, I can revert this change

const permission = mode & 0o777;

//This should be 600 permission, which means the file permission has not been changed by others.
const octalPermissions = permission.toString(8);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Isn't it duplication of validateOnlyUserReadWritePermissionAndOwner method?

Copy link
Author

Choose a reason for hiding this comment

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

It is very similar, but operates on file handles in order to mitigate any manipulations between our checks and operations. validateOnlyUserReadWritePermissionAndOwner should probably be replaced with this later on, but I didn't want to do it in this PR. See https://snowflakecomputing.atlassian.net/browse/SNOW-1944224

@@ -21,16 +21,6 @@ describe('Validate cache permissions test', async function () {
await fs.unlink(validPermissionsFilePath);
});

it('should return error on insecure permissions', async function () {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should we remove this test? why?

Copy link
Author

Choose a reason for hiding this comment

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

This was removed because of the change to the tested function that attempted to change the permissions. I'll restore it together with reverting the mentioned change

const pathFromHome = function () {
switch (process.platform) {
case 'win32':
return ['AppData', 'Local', 'Snowflake', 'Caches'];
Copy link
Collaborator

Choose a reason for hiding this comment

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

We use the paths in JsonCredentialManager. What du you thin about extracting subfolders to some variables?

Copy link
Author

Choose a reason for hiding this comment

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

Good call, done

Copy link

codecov bot commented Mar 7, 2025

Codecov Report

Attention: Patch coverage is 83.33333% with 26 lines in your changes missing coverage. Please review.

Project coverage is 88.88%. Comparing base (556e783) to head (3514cd7).

Files with missing lines Patch % Lines
...tication/secure_storage/json_credential_manager.js 85.60% 19 Missing ⚠️
lib/file_util.js 68.18% 7 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1012      +/-   ##
==========================================
- Coverage   88.91%   88.88%   -0.04%     
==========================================
  Files          72       72              
  Lines        7003     7124     +121     
==========================================
+ Hits         6227     6332     +105     
- Misses        776      792      +16     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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.

2 participants