-
Notifications
You must be signed in to change notification settings - Fork 351
fix(device-agent): correct macos password policy check to parse modern account policy #3535
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
123 changes: 123 additions & 0 deletions
123
packages/device-agent/src/checks/macos/password-policy.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| const execSyncMock = vi.fn(); | ||
|
|
||
| vi.mock('node:child_process', () => ({ | ||
| execSync: (command: string) => execSyncMock(command), | ||
| })); | ||
|
|
||
| import { MacOSPasswordPolicyCheck } from './password-policy'; | ||
|
|
||
| /** | ||
| * Captured `pwpolicy getaccountpolicies` output of a stock, unmanaged Mac (macOS 26.2), | ||
| * with the non-English `policyContentDescription` localizations trimmed. | ||
| */ | ||
| const ACCOUNT_POLICIES_MIN_4 = `Getting global account policies | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | ||
| <plist version="1.0"> | ||
| <dict> | ||
| <key>policyCategoryPasswordContent</key> | ||
| <array> | ||
| <dict> | ||
| <key>policyContent</key> | ||
| <string>policyAttributePassword matches '.{4,}+'</string> | ||
| <key>policyContentDescription</key> | ||
| <dict> | ||
| <key>en</key> | ||
| <string>Enter a password that is four characters or more.</string> | ||
| </dict> | ||
| <key>policyIdentifier</key> | ||
| <string>com.apple.defaultpasswordpolicy.fde</string> | ||
| </dict> | ||
| </array> | ||
| </dict> | ||
| </plist> | ||
| `; | ||
|
|
||
| /** The same output with the content policy's quantifier raised to 8 characters. */ | ||
| const ACCOUNT_POLICIES_MIN_8 = ACCOUNT_POLICIES_MIN_4.replace("'.{4,}+'", "'.{8,}+'"); | ||
|
|
||
| /** `pwpolicy getaccountpolicies` when no account policy is set at all. */ | ||
| const NO_ACCOUNT_POLICIES = `Getting global account policies | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | ||
| <plist version="1.0"> | ||
| <dict/> | ||
| </plist> | ||
| `; | ||
|
|
||
| /** Representative `system_profiler SPConfigurationProfileDataType` output of a managed Mac. */ | ||
| const MDM_PROFILE_MIN_8 = `Configuration Profiles: | ||
|
|
||
| ProfileItems: | ||
|
|
||
| PayloadType: com.apple.mobiledevice.passwordpolicy | ||
| minLength: 8 | ||
| `; | ||
|
|
||
| function mockCommands({ | ||
| accountPolicies, | ||
| globalPolicy = '', | ||
| configurationProfiles = '', | ||
| }: { | ||
| accountPolicies: string; | ||
| globalPolicy?: string; | ||
| configurationProfiles?: string; | ||
| }) { | ||
| execSyncMock.mockImplementation((command: string) => { | ||
| if (command.includes('getaccountpolicies')) return accountPolicies; | ||
| if (command.includes('getglobalpolicy')) return globalPolicy; | ||
| if (command.includes('system_profiler')) return configurationProfiles; | ||
| throw new Error(`unexpected command: ${command}`); | ||
| }); | ||
| } | ||
|
|
||
| describe('MacOSPasswordPolicyCheck', () => { | ||
| beforeEach(() => { | ||
| execSyncMock.mockReset(); | ||
| }); | ||
|
|
||
| it('fails when the effective account policy only requires 4 characters', async () => { | ||
| // `minChars` in the deprecated global store is what our own remediation used to write; | ||
| // macOS does not enforce it, so it must not make this check pass. | ||
| mockCommands({ accountPolicies: ACCOUNT_POLICIES_MIN_4, globalPolicy: 'minChars=8\n' }); | ||
|
|
||
| const result = await new MacOSPasswordPolicyCheck().run(); | ||
|
|
||
| expect(result.passed).toBe(false); | ||
| expect(result.details.message).toContain('only 4 characters'); | ||
| expect(execSyncMock.mock.calls.flat().join('\n')).not.toContain('getglobalpolicy'); | ||
| }); | ||
|
|
||
| it('fails when nothing constrains the password length', async () => { | ||
| mockCommands({ accountPolicies: NO_ACCOUNT_POLICIES }); | ||
|
|
||
| const result = await new MacOSPasswordPolicyCheck().run(); | ||
|
|
||
| expect(result.passed).toBe(false); | ||
| expect(result.details.message).toContain('No minimum password length policy detected'); | ||
| }); | ||
|
|
||
| it('passes when the account policy content requires 8 characters', async () => { | ||
| mockCommands({ accountPolicies: ACCOUNT_POLICIES_MIN_8 }); | ||
|
|
||
| const result = await new MacOSPasswordPolicyCheck().run(); | ||
|
|
||
| expect(result.passed).toBe(true); | ||
| expect(result.details.message).toContain('minimum 8 characters'); | ||
| }); | ||
|
|
||
| it('passes when an MDM passcode payload raises the minimum above the account policy', async () => { | ||
| // A managed Mac keeps the built-in 4-character policy; the profile is enforced on top of it. | ||
| mockCommands({ | ||
| accountPolicies: ACCOUNT_POLICIES_MIN_4, | ||
| configurationProfiles: MDM_PROFILE_MIN_8, | ||
| }); | ||
|
|
||
| const result = await new MacOSPasswordPolicyCheck().run(); | ||
|
|
||
| expect(result.passed).toBe(true); | ||
| expect(result.details.message).toContain('minimum 8 characters'); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P2: MDM-only Macs can be reported as non-compliant without checking their Passcode profile: if
pwpolicy getaccountpoliciesthrows, the outer catch returns beforegetProfileMinLength()runs. Keeping the account-policy and profile reads independently guarded would preserve the existing fallback and allow a readable MDM policy to pass even when the account-policy store is inaccessible.Prompt for AI agents