Skip to content

fix: set restrictive file permissions in FilePersistence#8623

Open
tnorling wants to merge 5 commits into
devfrom
fix/file-persistence-permissions
Open

fix: set restrictive file permissions in FilePersistence#8623
tnorling wants to merge 5 commits into
devfrom
fix/file-persistence-permissions

Conversation

@tnorling
Copy link
Copy Markdown
Collaborator

@tnorling tnorling commented Jun 2, 2026

Cache files created by FilePersistence use Node.js defaults (0644), making them world-readable. This sets mode 0o600 for files and 0o700 for directories, with chmod after writes to harden existing files.

Changes:

  • Added FILE_MODE = 0o600 and DIR_MODE = 0o700 constants
  • Applied restrictive mode to save(), saveBuffer(), createCacheFile(), and createFileDirectory()
  • Added chmod after writes to tighten permissions on existing files

Impact:

  • Linux plaintext fallback: Fixes world-readable token cache files (CWE-276)
  • Windows (DPAPI): No-op (Windows ignores POSIX file modes)
  • Mac/Linux (Keychain/LibSecret): Hardens mtime tracking files (contain only dummy data)

Fixes ADO #3607064

tnorling and others added 4 commits June 2, 2026 12:59
Cache files created by FilePersistence use Node.js defaults (0644),
making them world-readable. Set mode 0o600 for files and 0o700 for
directories, with chmod after writes to harden existing files.

Fixes SPADE CWE-276 vulnerability for Linux plaintext fallback path.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Verify that create(), save(), and saveBuffer() set 0o600 permissions,
and that save() tightens permissions on pre-existing permissive files.
Tests only run on POSIX platforms (Mac/Linux) where file modes apply.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@tnorling tnorling marked this pull request as ready for review June 2, 2026 20:42
@tnorling tnorling requested a review from a team as a code owner June 2, 2026 20:42
Copilot AI review requested due to automatic review settings June 2, 2026 20:42
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR hardens FilePersistence in @azure/msal-node-extensions by ensuring cache files are created/maintained with restrictive POSIX permissions, reducing the risk of token cache disclosure on Unix-like systems.

Changes:

  • Enforced 0o600 file permissions and 0o700 directory permissions during cache file/directory creation.
  • Added chmod after writes to tighten permissions on pre-existing permissive cache files.
  • Added non-Windows unit tests to validate file permission behavior.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
extensions/msal-node-extensions/src/persistence/FilePersistence.ts Applies restrictive file/directory modes and post-write chmod hardening.
extensions/msal-node-extensions/test/persistence/FilePersistence.spec.ts Adds permission-focused tests on non-Windows platforms.
change/@azure-msal-node-extensions-c75a531e-3e89-4fe2-86d8-d551e766c890.json Adds a changefile for the patch release.

Comment thread extensions/msal-node-extensions/src/persistence/FilePersistence.ts
Comment thread change/@azure-msal-node-extensions-c75a531e-3e89-4fe2-86d8-d551e766c890.json Outdated
… changefile link format

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

Comment on lines +53 to +57
await fs.writeFile(this.getFilePath(), contents, {
encoding: "utf-8",
mode: FILE_MODE,
});
await fs.chmod(this.getFilePath(), FILE_MODE);
Comment on lines 71 to +75
try {
await fs.writeFile(this.getFilePath(), contents);
await fs.writeFile(this.getFilePath(), contents, {
mode: FILE_MODE,
});
await fs.chmod(this.getFilePath(), FILE_MODE);
Comment on lines +201 to +204
await fs.mkdir(dirname(this.filePath), {
recursive: true,
mode: DIR_MODE,
});
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