From 2ae50bc97393851e5c83184d9766314471e57913 Mon Sep 17 00:00:00 2001 From: Oleksii Kurinnyi Date: Fri, 16 Aug 2024 17:21:46 +0300 Subject: [PATCH] fix: check if ssh key has newline at the end Signed-off-by: Oleksii Kurinnyi --- .../TextFileUpload/__tests__/index.spec.tsx | 6 ++-- .../src/components/TextFileUpload/index.tsx | 7 ++--- .../SshPrivateKey/__tests__/index.spec.tsx | 28 ++++++++++++------- .../AddModal/Form/SshPrivateKey/index.tsx | 8 ++++++ .../SshPublicKey/__tests__/index.spec.tsx | 26 +++++++++++------ .../AddModal/Form/SshPublicKey/index.tsx | 8 ++++++ 6 files changed, 55 insertions(+), 28 deletions(-) diff --git a/packages/dashboard-frontend/src/components/TextFileUpload/__tests__/index.spec.tsx b/packages/dashboard-frontend/src/components/TextFileUpload/__tests__/index.spec.tsx index a6b4550a1..2522bf7e2 100644 --- a/packages/dashboard-frontend/src/components/TextFileUpload/__tests__/index.spec.tsx +++ b/packages/dashboard-frontend/src/components/TextFileUpload/__tests__/index.spec.tsx @@ -82,14 +82,13 @@ describe('TextFileUpload', () => { /* Upload a file */ const fileContent = 'file-content'; - const fileContentBase64 = Buffer.from(fileContent).toString('base64'); const file = new File([fileContent], 'file.txt', { type: 'text/plain' }); const fileUploader = screen.getByTestId(fieldId); const fileUploadInput = fileUploader.querySelector('input[type="file"]'); userEvent.upload(fileUploadInput!, file); - await waitFor(() => expect(mockOnChange).toHaveBeenCalledWith(fileContentBase64, true)); + await waitFor(() => expect(mockOnChange).toHaveBeenCalledWith(fileContent, true)); expect(fileInput).toHaveValue(file.name); expect(clearButton).toBeEnabled(); @@ -118,10 +117,9 @@ describe('TextFileUpload', () => { /* Paste a content */ const content = 'content'; - const contentBase64 = Buffer.from(content).toString('base64'); userEvent.paste(contentInput, content); - await waitFor(() => expect(mockOnChange).toHaveBeenCalledWith(contentBase64, false)); + await waitFor(() => expect(mockOnChange).toHaveBeenCalledWith(content, false)); expect(fileInput).toHaveValue(''); expect(clearButton).toBeEnabled(); diff --git a/packages/dashboard-frontend/src/components/TextFileUpload/index.tsx b/packages/dashboard-frontend/src/components/TextFileUpload/index.tsx index e4aea7ef1..fbc1d337a 100644 --- a/packages/dashboard-frontend/src/components/TextFileUpload/index.tsx +++ b/packages/dashboard-frontend/src/components/TextFileUpload/index.tsx @@ -20,9 +20,6 @@ export type Props = { fileNamePlaceholder?: string; textAreaPlaceholder?: string; validated: ValidatedOptions; - /** - * @param content base64 encoded file content - */ onChange: (content: string, isUpload: boolean) => void; }; @@ -66,7 +63,7 @@ export class TextFileUpload extends React.PureComponent { private handleDataChange(content: string): void { this.setState({ content }); - this.props.onChange(btoa(content), true); + this.props.onChange(content, true); } private handleTextChange(content: string): void { @@ -75,7 +72,7 @@ export class TextFileUpload extends React.PureComponent { file: undefined, filename: undefined, }); - this.props.onChange(btoa(content), false); + this.props.onChange(content, false); } public render(): React.ReactElement { diff --git a/packages/dashboard-frontend/src/pages/UserPreferences/SshKeys/AddModal/Form/SshPrivateKey/__tests__/index.spec.tsx b/packages/dashboard-frontend/src/pages/UserPreferences/SshKeys/AddModal/Form/SshPrivateKey/__tests__/index.spec.tsx index c19d7aa49..b2f6d691d 100644 --- a/packages/dashboard-frontend/src/pages/UserPreferences/SshKeys/AddModal/Form/SshPrivateKey/__tests__/index.spec.tsx +++ b/packages/dashboard-frontend/src/pages/UserPreferences/SshKeys/AddModal/Form/SshPrivateKey/__tests__/index.spec.tsx @@ -45,7 +45,9 @@ describe('SshPrivateKey', () => { const sshPrivateKey = 'ssh-private-key'; userEvent.paste(input, sshPrivateKey); - expect(mockOnChange).toHaveBeenCalledWith(sshPrivateKey, true); + const expectedSshPrivateKey = btoa(sshPrivateKey.trim() + '\n'); + + expect(mockOnChange).toHaveBeenCalledWith(expectedSshPrivateKey, true); expect(screen.queryByText(WRONG_TYPE_ERROR)).toBeFalsy(); expect(screen.queryByText(REQUIRED_ERROR)).toBeFalsy(); expect(screen.queryByText(MAX_LENGTH_ERROR)).toBeFalsy(); @@ -81,7 +83,9 @@ describe('SshPrivateKey', () => { const sshPrivateKey = 'ssh-private-key'.repeat(5000); userEvent.paste(input, sshPrivateKey); - expect(mockOnChange).toHaveBeenCalledWith(sshPrivateKey, false); + const expectedSshPrivateKey = btoa(sshPrivateKey.trim() + '\n'); + + expect(mockOnChange).toHaveBeenCalledWith(expectedSshPrivateKey, false); expect(screen.queryByText(MAX_LENGTH_ERROR)).toBeTruthy(); expect(screen.queryByText(WRONG_TYPE_ERROR)).toBeFalsy(); expect(screen.queryByText(REQUIRED_ERROR)).toBeFalsy(); @@ -97,10 +101,12 @@ describe('SshPrivateKey', () => { const input = screen.getByPlaceholderText('Or paste the PRIVATE key'); // fill the SSH key data field - const sshKeyData = 'ssh-key-data'; - userEvent.paste(input, sshKeyData); + const sshPrivateKey = 'ssh-key-data'; + userEvent.paste(input, sshPrivateKey); - expect(mockOnChange).toHaveBeenCalledWith(sshKeyData, true); + const expectedSshPrivateKey = btoa(sshPrivateKey.trim() + '\n'); + + expect(mockOnChange).toHaveBeenCalledWith(expectedSshPrivateKey, true); expect(screen.queryByText(WRONG_TYPE_ERROR)).toBeFalsy(); expect(screen.queryByText(REQUIRED_ERROR)).toBeFalsy(); expect(screen.queryByText(MAX_LENGTH_ERROR)).toBeFalsy(); @@ -114,8 +120,8 @@ describe('SshPrivateKey', () => { const input = screen.getByPlaceholderText('Or paste the PRIVATE key'); // fill the SSH key data field - const sshKeyData = 'ssh-key-data'; - userEvent.paste(input, sshKeyData); + const sshPrivateKey = 'ssh-key-data'; + userEvent.paste(input, sshPrivateKey); mockOnChange.mockClear(); @@ -137,10 +143,12 @@ describe('SshPrivateKey', () => { const input = screen.getByPlaceholderText('Or paste the PRIVATE key'); // fill the SSH key data field - const sshKeyData = 'ssh-key-data'.repeat(5000); - userEvent.paste(input, sshKeyData); + const sshPrivateKey = 'ssh-key-data'.repeat(5000); + userEvent.paste(input, sshPrivateKey); + + const expectedSshPrivateKey = btoa(sshPrivateKey.trim() + '\n'); - expect(mockOnChange).toHaveBeenCalledWith(sshKeyData, false); + expect(mockOnChange).toHaveBeenCalledWith(expectedSshPrivateKey, false); expect(screen.queryByText(MAX_LENGTH_ERROR)).toBeTruthy(); expect(screen.queryByText(WRONG_TYPE_ERROR)).toBeFalsy(); diff --git a/packages/dashboard-frontend/src/pages/UserPreferences/SshKeys/AddModal/Form/SshPrivateKey/index.tsx b/packages/dashboard-frontend/src/pages/UserPreferences/SshKeys/AddModal/Form/SshPrivateKey/index.tsx index 3814b70d9..c433c84db 100644 --- a/packages/dashboard-frontend/src/pages/UserPreferences/SshKeys/AddModal/Form/SshPrivateKey/index.tsx +++ b/packages/dashboard-frontend/src/pages/UserPreferences/SshKeys/AddModal/Form/SshPrivateKey/index.tsx @@ -50,6 +50,14 @@ export class SshPrivateKey extends React.Component { private onChange(privateKey: string, isUpload: boolean): void { const { onChange } = this.props; + + privateKey = privateKey.trim() + ? btoa( + // expect the only new line at the end + privateKey.trim() + '\n', + ) + : ''; + const validated = this.validate(privateKey); const isValid = validated === ValidatedOptions.success; diff --git a/packages/dashboard-frontend/src/pages/UserPreferences/SshKeys/AddModal/Form/SshPublicKey/__tests__/index.spec.tsx b/packages/dashboard-frontend/src/pages/UserPreferences/SshKeys/AddModal/Form/SshPublicKey/__tests__/index.spec.tsx index 3c6655bf8..c25dc641d 100644 --- a/packages/dashboard-frontend/src/pages/UserPreferences/SshKeys/AddModal/Form/SshPublicKey/__tests__/index.spec.tsx +++ b/packages/dashboard-frontend/src/pages/UserPreferences/SshKeys/AddModal/Form/SshPublicKey/__tests__/index.spec.tsx @@ -45,7 +45,9 @@ describe('SshPublicKey', () => { const sshPublicKey = 'ssh-public-key'; userEvent.paste(input, sshPublicKey); - expect(mockOnChange).toHaveBeenCalledWith(sshPublicKey, true); + const expectedSshPrivateKey = btoa(sshPublicKey.trim() + '\n'); + + expect(mockOnChange).toHaveBeenCalledWith(expectedSshPrivateKey, true); expect(screen.queryByText(WRONG_TYPE_ERROR)).toBeFalsy(); expect(screen.queryByText(REQUIRED_ERROR)).toBeFalsy(); expect(screen.queryByText(MAX_LENGTH_ERROR)).toBeFalsy(); @@ -67,7 +69,7 @@ describe('SshPublicKey', () => { const sshPublicKey = ''; fireEvent.change(input, { target: { value: sshPublicKey } }); - expect(mockOnChange).toHaveBeenCalledWith(sshPublicKey, false); + expect(mockOnChange).toHaveBeenCalledWith('', false); expect(screen.queryByText(WRONG_TYPE_ERROR)).toBeTruthy(); expect(screen.queryByText(REQUIRED_ERROR)).toBeFalsy(); expect(screen.queryByText(MAX_LENGTH_ERROR)).toBeFalsy(); @@ -84,7 +86,9 @@ describe('SshPublicKey', () => { const sshPublicKey = 'ssh-public-key'.repeat(1000); userEvent.paste(input, sshPublicKey); - expect(mockOnChange).toHaveBeenCalledWith(sshPublicKey, false); + const expectedSshPublicKey = btoa(sshPublicKey.trim() + '\n'); + + expect(mockOnChange).toHaveBeenCalledWith(expectedSshPublicKey, false); expect(screen.queryByText(MAX_LENGTH_ERROR)).toBeTruthy(); expect(screen.queryByText(WRONG_TYPE_ERROR)).toBeFalsy(); expect(screen.queryByText(REQUIRED_ERROR)).toBeFalsy(); @@ -99,10 +103,12 @@ describe('SshPublicKey', () => { const input = screen.getByPlaceholderText('Or paste the PUBLIC key'); - const sshKeyData = 'ssh-key-data'; - userEvent.paste(input, sshKeyData); + const sshPublicKey = 'ssh-key-data'; + userEvent.paste(input, sshPublicKey); - expect(mockOnChange).toHaveBeenCalledWith(sshKeyData, true); + const expectedSshPublicKey = btoa(sshPublicKey.trim() + '\n'); + + expect(mockOnChange).toHaveBeenCalledWith(expectedSshPublicKey, true); expect(screen.queryByText(WRONG_TYPE_ERROR)).toBeFalsy(); expect(screen.queryByText(REQUIRED_ERROR)).toBeFalsy(); expect(screen.queryByText(MAX_LENGTH_ERROR)).toBeFalsy(); @@ -137,10 +143,12 @@ describe('SshPublicKey', () => { const input = screen.getByPlaceholderText('Or paste the PUBLIC key'); // fill the SSH key data field - const sshKeyData = 'ssh-key-data'.repeat(1000); - userEvent.paste(input, sshKeyData); + const sshPublicKey = 'ssh-key-data'.repeat(1000); + userEvent.paste(input, sshPublicKey); + + const expectedSshPublicKey = btoa(sshPublicKey.trim() + '\n'); - expect(mockOnChange).toHaveBeenCalledWith(sshKeyData, false); + expect(mockOnChange).toHaveBeenCalledWith(expectedSshPublicKey, false); expect(screen.queryByText(MAX_LENGTH_ERROR)).toBeTruthy(); expect(screen.queryByText(WRONG_TYPE_ERROR)).toBeFalsy(); expect(screen.queryByText(REQUIRED_ERROR)).toBeFalsy(); diff --git a/packages/dashboard-frontend/src/pages/UserPreferences/SshKeys/AddModal/Form/SshPublicKey/index.tsx b/packages/dashboard-frontend/src/pages/UserPreferences/SshKeys/AddModal/Form/SshPublicKey/index.tsx index b85f0573c..1d83e3d27 100644 --- a/packages/dashboard-frontend/src/pages/UserPreferences/SshKeys/AddModal/Form/SshPublicKey/index.tsx +++ b/packages/dashboard-frontend/src/pages/UserPreferences/SshKeys/AddModal/Form/SshPublicKey/index.tsx @@ -50,6 +50,14 @@ export class SshPublicKey extends React.Component { private onChange(publicKey: string, isUpload: boolean): void { const { onChange } = this.props; + + publicKey = publicKey.trim() + ? btoa( + // expect the only new line at the end + publicKey.trim() + '\n', + ) + : ''; + const validated = this.validate(publicKey); const isValid = validated === ValidatedOptions.success;