-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
fix(sharing): Prevent empty password when checkbox is enabled #58226
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
Open
nfebe
wants to merge
1
commit into
master
Choose a base branch
from
fix/password-state-management
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+336
−1
Open
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
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,331 @@ | ||
| /** | ||
| * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| import { beforeEach, describe, expect, it, vi } from 'vitest' | ||
|
|
||
| vi.mock('../services/ConfigService.ts', () => ({ | ||
| default: vi.fn().mockImplementation(() => ({ | ||
| enableLinkPasswordByDefault: false, | ||
| enforcePasswordForPublicLink: false, | ||
| isPublicUploadEnabled: true, | ||
| isDefaultExpireDateEnabled: false, | ||
| isDefaultInternalExpireDateEnabled: false, | ||
| isDefaultRemoteExpireDateEnabled: false, | ||
| defaultExpirationDate: null, | ||
| defaultInternalExpirationDate: null, | ||
| defaultRemoteExpirationDateString: null, | ||
| isResharingAllowed: true, | ||
| excludeReshareFromEdit: false, | ||
| showFederatedSharesAsInternal: false, | ||
| defaultPermissions: 31, | ||
| })), | ||
| })) | ||
|
|
||
| vi.mock('../utils/GeneratePassword.ts', () => ({ | ||
| default: vi.fn().mockResolvedValue('generated-password-123'), | ||
| })) | ||
|
|
||
| describe('SharingDetailsTab - Password State Management Logic', () => { | ||
| describe('isPasswordProtected getter logic', () => { | ||
| it('returns true when passwordProtectedState is explicitly true', () => { | ||
| const passwordProtectedState: boolean | undefined = true | ||
| const enforcePasswordForPublicLink = false | ||
| const newPassword: string | undefined = undefined | ||
| const password: string | undefined = undefined | ||
|
|
||
| const isPasswordProtected = (() => { | ||
| if (enforcePasswordForPublicLink) { | ||
| return true | ||
| } | ||
| if (passwordProtectedState !== undefined) { | ||
| return passwordProtectedState | ||
| } | ||
| return typeof newPassword === 'string' | ||
| || typeof password === 'string' | ||
| })() | ||
|
|
||
| expect(isPasswordProtected).toBe(true) | ||
| }) | ||
|
|
||
| it('returns false when passwordProtectedState is explicitly false', () => { | ||
| const passwordProtectedState: boolean | undefined = false | ||
| const enforcePasswordForPublicLink = false | ||
| const newPassword: string | undefined = 'some-password' | ||
| const password: string | undefined = undefined | ||
|
|
||
| const isPasswordProtected = (() => { | ||
| if (enforcePasswordForPublicLink) { | ||
| return true | ||
| } | ||
| if (passwordProtectedState !== undefined) { | ||
| return passwordProtectedState | ||
| } | ||
| return typeof newPassword === 'string' | ||
| || typeof password === 'string' | ||
| })() | ||
|
|
||
| expect(isPasswordProtected).toBe(false) | ||
| }) | ||
|
|
||
| it('returns true when enforcePasswordForPublicLink is true regardless of other state', () => { | ||
| const passwordProtectedState: boolean | undefined = false | ||
| const enforcePasswordForPublicLink = true | ||
| const newPassword: string | undefined = undefined | ||
| const password: string | undefined = undefined | ||
|
|
||
| const isPasswordProtected = (() => { | ||
| if (enforcePasswordForPublicLink) { | ||
| return true | ||
| } | ||
| if (passwordProtectedState !== undefined) { | ||
| return passwordProtectedState | ||
| } | ||
| return typeof newPassword === 'string' | ||
| || typeof password === 'string' | ||
| })() | ||
|
|
||
| expect(isPasswordProtected).toBe(true) | ||
| }) | ||
|
|
||
| it('falls back to inferring from password when passwordProtectedState is undefined', () => { | ||
| const passwordProtectedState: boolean | undefined = undefined | ||
| const enforcePasswordForPublicLink = false | ||
| const newPassword: string | undefined = 'some-password' | ||
| const password: string | undefined = undefined | ||
|
|
||
| const isPasswordProtected = (() => { | ||
| if (enforcePasswordForPublicLink) { | ||
| return true | ||
| } | ||
| if (passwordProtectedState !== undefined) { | ||
| return passwordProtectedState | ||
| } | ||
| return typeof newPassword === 'string' | ||
| || typeof password === 'string' | ||
| })() | ||
|
|
||
| expect(isPasswordProtected).toBe(true) | ||
| }) | ||
|
|
||
| it('returns false when passwordProtectedState is undefined and no passwords exist', () => { | ||
| const passwordProtectedState: boolean | undefined = undefined | ||
| const enforcePasswordForPublicLink = false | ||
| const newPassword: string | undefined = undefined | ||
| const password: string | undefined = undefined | ||
|
|
||
| const isPasswordProtected = (() => { | ||
| if (enforcePasswordForPublicLink) { | ||
| return true | ||
| } | ||
| if (passwordProtectedState !== undefined) { | ||
| return passwordProtectedState | ||
| } | ||
| return typeof newPassword === 'string' | ||
| || typeof password === 'string' | ||
| })() | ||
|
|
||
| expect(isPasswordProtected).toBe(false) | ||
| }) | ||
| }) | ||
|
|
||
| describe('initializeAttributes sets passwordProtectedState', () => { | ||
| it('should set passwordProtectedState to true when enableLinkPasswordByDefault is true', async () => { | ||
| const config = { | ||
| enableLinkPasswordByDefault: true, | ||
| enforcePasswordForPublicLink: false, | ||
| } | ||
| const isNewShare = true | ||
| const isPublicShare = true | ||
| let passwordProtectedState: boolean | undefined | ||
|
|
||
| if (isNewShare) { | ||
| if ((config.enableLinkPasswordByDefault || config.enforcePasswordForPublicLink) && isPublicShare) { | ||
| passwordProtectedState = true | ||
| } | ||
| } | ||
|
|
||
| expect(passwordProtectedState).toBe(true) | ||
| }) | ||
|
|
||
| it('should set passwordProtectedState to true when isPasswordEnforced is true', async () => { | ||
| const config = { | ||
| enableLinkPasswordByDefault: false, | ||
| enforcePasswordForPublicLink: true, | ||
| } | ||
| const isNewShare = true | ||
| const isPublicShare = true | ||
| let passwordProtectedState: boolean | undefined | ||
|
|
||
| if (isNewShare) { | ||
| if ((config.enableLinkPasswordByDefault || config.enforcePasswordForPublicLink) && isPublicShare) { | ||
| passwordProtectedState = true | ||
| } | ||
| } | ||
|
|
||
| expect(passwordProtectedState).toBe(true) | ||
| }) | ||
|
|
||
| it('should not set passwordProtectedState for non-public shares', async () => { | ||
| const config = { | ||
| enableLinkPasswordByDefault: true, | ||
| enforcePasswordForPublicLink: false, | ||
| } | ||
| const isNewShare = true | ||
| const isPublicShare = false | ||
| let passwordProtectedState: boolean | undefined | ||
|
|
||
| if (isNewShare) { | ||
| if ((config.enableLinkPasswordByDefault || config.enforcePasswordForPublicLink) && isPublicShare) { | ||
| passwordProtectedState = true | ||
| } | ||
| } | ||
|
|
||
| expect(passwordProtectedState).toBe(undefined) | ||
| }) | ||
|
|
||
| it('should not set passwordProtectedState for existing shares', async () => { | ||
| const config = { | ||
| enableLinkPasswordByDefault: true, | ||
| enforcePasswordForPublicLink: false, | ||
| } | ||
| const isNewShare = false | ||
| const isPublicShare = true | ||
| let passwordProtectedState: boolean | undefined | ||
|
|
||
| if (isNewShare) { | ||
| if ((config.enableLinkPasswordByDefault || config.enforcePasswordForPublicLink) && isPublicShare) { | ||
| passwordProtectedState = true | ||
| } | ||
| } | ||
|
|
||
| expect(passwordProtectedState).toBe(undefined) | ||
| }) | ||
| }) | ||
|
|
||
| describe('saveShare validation blocks empty password', () => { | ||
| const isValidShareAttribute = (attr: unknown) => { | ||
| return typeof attr === 'string' && attr.length > 0 | ||
| } | ||
|
|
||
| it('should set passwordError when isPasswordProtected but newPassword is empty for new share', () => { | ||
| const isPasswordProtected = true | ||
| const isNewShare = true | ||
| const newPassword = '' | ||
| let passwordError = false | ||
|
|
||
| if (isPasswordProtected) { | ||
| if (isNewShare && !isValidShareAttribute(newPassword)) { | ||
| passwordError = true | ||
| } | ||
| } | ||
|
|
||
| expect(passwordError).toBe(true) | ||
| }) | ||
|
|
||
| it('should set passwordError when isPasswordProtected but newPassword is undefined for new share', () => { | ||
| const isPasswordProtected = true | ||
| const isNewShare = true | ||
| const newPassword = undefined | ||
| let passwordError = false | ||
|
|
||
| if (isPasswordProtected) { | ||
| if (isNewShare && !isValidShareAttribute(newPassword)) { | ||
| passwordError = true | ||
| } | ||
| } | ||
|
|
||
| expect(passwordError).toBe(true) | ||
| }) | ||
|
|
||
| it('should not set passwordError when password is valid for new share', () => { | ||
| const isPasswordProtected = true | ||
| const isNewShare = true | ||
| const newPassword = 'valid-password-123' | ||
| let passwordError = false | ||
|
|
||
| if (isPasswordProtected) { | ||
| if (isNewShare && !isValidShareAttribute(newPassword)) { | ||
| passwordError = true | ||
| } | ||
| } | ||
|
|
||
| expect(passwordError).toBe(false) | ||
| }) | ||
|
|
||
| it('should not set passwordError when isPasswordProtected is false', () => { | ||
| const isPasswordProtected = false | ||
| const isNewShare = true | ||
| const newPassword = '' | ||
| let passwordError = false | ||
|
|
||
| if (isPasswordProtected) { | ||
| if (isNewShare && !isValidShareAttribute(newPassword)) { | ||
| passwordError = true | ||
| } | ||
| } | ||
|
|
||
| expect(passwordError).toBe(false) | ||
| }) | ||
|
|
||
| it('should not validate password for existing shares', () => { | ||
| const isPasswordProtected = true | ||
| const isNewShare = false | ||
| const newPassword = '' | ||
| let passwordError = false | ||
|
|
||
| if (isPasswordProtected) { | ||
| if (isNewShare && !isValidShareAttribute(newPassword)) { | ||
| passwordError = true | ||
| } | ||
| } | ||
|
|
||
| expect(passwordError).toBe(false) | ||
| }) | ||
| }) | ||
|
|
||
| describe('checkbox persistence after clearing password', () => { | ||
| it('checkbox remains checked when passwordProtectedState is explicitly true even if password is cleared', () => { | ||
| let passwordProtectedState: boolean | undefined = true | ||
| const enforcePasswordForPublicLink = false | ||
| let newPassword: string | undefined = 'initial-password' | ||
|
|
||
| newPassword = '' | ||
|
|
||
| const isPasswordProtected = (() => { | ||
| if (enforcePasswordForPublicLink) { | ||
| return true | ||
| } | ||
| if (passwordProtectedState !== undefined) { | ||
| return passwordProtectedState | ||
| } | ||
| return typeof newPassword === 'string' | ||
| || false | ||
| })() | ||
|
|
||
| expect(isPasswordProtected).toBe(true) | ||
| }) | ||
|
|
||
| it('checkbox unchecks incorrectly if passwordProtectedState was never set (bug scenario)', () => { | ||
| let passwordProtectedState: boolean | undefined = undefined | ||
| const enforcePasswordForPublicLink = false | ||
| let newPassword: string | undefined = 'initial-password' | ||
|
|
||
| newPassword = undefined | ||
|
|
||
| const isPasswordProtected = (() => { | ||
| if (enforcePasswordForPublicLink) { | ||
| return true | ||
| } | ||
| if (passwordProtectedState !== undefined) { | ||
| return passwordProtectedState | ||
| } | ||
| return typeof newPassword === 'string' | ||
| || false | ||
| })() | ||
|
|
||
| expect(isPasswordProtected).toBe(false) | ||
| }) | ||
| }) | ||
| }) |
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
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.
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.
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.
?
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.
Prevent 1 exit when not tests around found in a linked app.
https://vitest.dev/config/passwithnotests.html
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.
But in which case this should happen?
If it happens then this points to a real issue of that app.
Except you mean like external other apps you have linked only locally.
But in that case you should not put them into
appsbut rather something likeapps-extraor similar.