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

NAS-133377 / 25.04 / Add max file validation in kerberos keytabs #11276

Merged
merged 5 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { FormControl, ValidatorFn } from '@angular/forms';
import { createServiceFactory, SpectatorService } from '@ngneat/spectator/jest';
import { MiB } from 'app/constants/bytes.constant';
import { fakeFile } from 'app/core/testing/utils/fake-file.uitls';
import { ixManualValidateError } from 'app/modules/forms/ix-forms/components/ix-errors/ix-errors.component';
import { FileValidatorService } from 'app/modules/forms/ix-forms/validators/file-validator/file-validator.service';

describe('FileValidatorService', () => {
let spectator: SpectatorService<FileValidatorService>;
const maxSizeInBytes = 10 * MiB;
let validatorFn: ValidatorFn;

const createService = createServiceFactory({
service: FileValidatorService,
});
beforeEach(() => {
spectator = createService();

validatorFn = spectator.service.maxSize(maxSizeInBytes);
});

it('should return null if value is null', () => {
const control = new FormControl(null as File[] | null);
expect(validatorFn(control)).toBeNull();
});

it('should return null if there are no files in the array', () => {
const control = new FormControl([] as File[]);
expect(validatorFn(control)).toBeNull();
});

it('should return null if all files are within size limit', () => {
const file1 = fakeFile('file1.txt', 2 * MiB);

const control = new FormControl([file1]);
expect(validatorFn(control)).toBeNull();
});

it('should return an error object if any file exceeds the size limit', () => {
const file1 = fakeFile('file1.txt', 11 * MiB);

const control = new FormControl([file1]);
expect(validatorFn(control)).toEqual({
[ixManualValidateError]: {
message: 'Maximum file size is limited to 10 MiB.',
},
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Injectable } from '@angular/core';
import { FormControl, ValidationErrors } from '@angular/forms';
import { TranslateService } from '@ngx-translate/core';
import { buildNormalizedFileSize } from 'app/helpers/file-size.utils';
import { ixManualValidateError } from 'app/modules/forms/ix-forms/components/ix-errors/ix-errors.component';

@Injectable({
providedIn: 'root',
})
export class FileValidatorService {
constructor(
private translate: TranslateService,
) {}

maxSize(maxSizeInBytes: number) {
return (control: FormControl<File[] | null>): ValidationErrors | null => {
const files = control.value;
if (!files?.length) {
return null;
}

for (const file of files) {
if (file.size > maxSizeInBytes) {
return {
[ixManualValidateError]: {
message: this.translate.instant(
'Maximum file size is limited to {maxSize}.',
{ maxSize: buildNormalizedFileSize(maxSizeInBytes) },
),
},
};
}
}
return null;
};
}
}
43 changes: 1 addition & 42 deletions src/app/modules/forms/ix-forms/validators/validators.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {
FormControl, FormGroup, UntypedFormControl, ValidationErrors, ValidatorFn,
FormGroup, UntypedFormControl, ValidationErrors, ValidatorFn,
} from '@angular/forms';
import { isEmpty, isNumber, toNumber } from 'lodash-es';

Expand Down Expand Up @@ -50,44 +50,3 @@ export function greaterThanFg(
return null;
};
}

export function rangeValidator(min: number, max?: number): ValidatorFn {
let thisControl: FormControl<string>;

return function rangeValidate(control: FormControl<string>) {
let regex;
if (min === 0) {
regex = /^(0|[1-9]\d*)$/;
} else {
regex = /^[1-9]\d*$/;
}

if (!control.parent) {
return null;
}

// Initializing the validator.
if (!thisControl) {
thisControl = control;
}

if (!thisControl.value) {
return null;
}

if (regex.test(thisControl.value)) {
const num = Number(thisControl.value);
if (num >= min) {
if (max) {
if (num <= max) {
return null;
}
} else {
return null;
}
}
}

return { range: true, rangeValue: { min, max } };
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ import { IxSelectComponent } from 'app/modules/forms/ix-forms/components/ix-sele
import { WithManageCertificatesLinkComponent } from 'app/modules/forms/ix-forms/components/with-manage-certificates-link/with-manage-certificates-link.component';
import { FormErrorHandlerService } from 'app/modules/forms/ix-forms/services/form-error-handler.service';
import { IxValidatorsService } from 'app/modules/forms/ix-forms/services/ix-validators.service';
import { greaterThanFg, rangeValidator } from 'app/modules/forms/ix-forms/validators/validators';
import { rangeValidator } from 'app/modules/forms/ix-forms/validators/range-validation/range-validation';
import { greaterThanFg } from 'app/modules/forms/ix-forms/validators/validators';
import { ModalHeaderComponent } from 'app/modules/slide-ins/components/modal-header/modal-header.component';
import { SlideInRef } from 'app/modules/slide-ins/slide-in-ref';
import { SnackbarService } from 'app/modules/snackbar/services/snackbar.service';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { MatCard, MatCardContent } from '@angular/material/card';
import { UntilDestroy, untilDestroyed } from '@ngneat/until-destroy';
import { TranslateService, TranslateModule } from '@ngx-translate/core';
import { Observable } from 'rxjs';
import { KiB } from 'app/constants/bytes.constant';
import { RequiresRolesDirective } from 'app/directives/requires-roles/requires-roles.directive';
import { Role } from 'app/enums/role.enum';
import { helptextKerberosKeytabs } from 'app/helptext/directory-service/kerberos-keytabs-form-list';
Expand All @@ -16,6 +17,7 @@ import { IxFieldsetComponent } from 'app/modules/forms/ix-forms/components/ix-fi
import { IxFileInputComponent } from 'app/modules/forms/ix-forms/components/ix-file-input/ix-file-input.component';
import { IxInputComponent } from 'app/modules/forms/ix-forms/components/ix-input/ix-input.component';
import { FormErrorHandlerService } from 'app/modules/forms/ix-forms/services/form-error-handler.service';
import { FileValidatorService } from 'app/modules/forms/ix-forms/validators/file-validator/file-validator.service';
import { ModalHeaderComponent } from 'app/modules/slide-ins/components/modal-header/modal-header.component';
import { SlideInRef } from 'app/modules/slide-ins/slide-in-ref';
import { TestDirective } from 'app/modules/test-id/test.directive';
Expand Down Expand Up @@ -58,7 +60,10 @@ export class KerberosKeytabsFormComponent implements OnInit {

form = this.formBuilder.nonNullable.group({
name: ['', Validators.required],
file: [null as File[] | null, Validators.required],
file: [null as File[] | null, Validators.compose([
Validators.required,
this.fileValidator.maxSize(40 * KiB),
])],
});

isLoading = false;
Expand All @@ -72,6 +77,7 @@ export class KerberosKeytabsFormComponent implements OnInit {
private cdr: ChangeDetectorRef,
private api: ApiService,
public slideInRef: SlideInRef<KerberosKeytab | undefined, boolean>,
private fileValidator: FileValidatorService,
) {
this.editingKerberosKeytab = slideInRef.getData();
}
Expand Down
1 change: 1 addition & 0 deletions src/assets/i18n/af.json
Original file line number Diff line number Diff line change
Expand Up @@ -2665,6 +2665,7 @@
"Maximum Passive Port": "",
"Maximum Transmission Unit, the largest protocol data unit that can be communicated. The largest workable MTU size varies with network interfaces and equipment. <i>1500</i> and <i>9000</i> are standard Ethernet MTU sizes. Leaving blank restores the field to the default value of <i>1500</i>.": "",
"Maximum Upload Parts": "",
"Maximum file size is limited to {maxSize}.": "",
"Maximum number of replication tasks being executed simultaneously.": "",
"Maximum number of seconds a client is allowed to spend connected, after\nauthentication, without issuing a command which results in creating an active or passive data connection\n(i.e. sending/receiving a file, or receiving a directory listing).": "",
"Maximum number of seconds that proftpd will allow clients to stay connected without receiving\nany data on either the control or data connection.": "",
Expand Down
1 change: 1 addition & 0 deletions src/assets/i18n/ar.json
Original file line number Diff line number Diff line change
Expand Up @@ -2665,6 +2665,7 @@
"Maximum Passive Port": "",
"Maximum Transmission Unit, the largest protocol data unit that can be communicated. The largest workable MTU size varies with network interfaces and equipment. <i>1500</i> and <i>9000</i> are standard Ethernet MTU sizes. Leaving blank restores the field to the default value of <i>1500</i>.": "",
"Maximum Upload Parts": "",
"Maximum file size is limited to {maxSize}.": "",
"Maximum number of replication tasks being executed simultaneously.": "",
"Maximum number of seconds a client is allowed to spend connected, after\nauthentication, without issuing a command which results in creating an active or passive data connection\n(i.e. sending/receiving a file, or receiving a directory listing).": "",
"Maximum number of seconds that proftpd will allow clients to stay connected without receiving\nany data on either the control or data connection.": "",
Expand Down
1 change: 1 addition & 0 deletions src/assets/i18n/ast.json
Original file line number Diff line number Diff line change
Expand Up @@ -2665,6 +2665,7 @@
"Maximum Passive Port": "",
"Maximum Transmission Unit, the largest protocol data unit that can be communicated. The largest workable MTU size varies with network interfaces and equipment. <i>1500</i> and <i>9000</i> are standard Ethernet MTU sizes. Leaving blank restores the field to the default value of <i>1500</i>.": "",
"Maximum Upload Parts": "",
"Maximum file size is limited to {maxSize}.": "",
"Maximum number of replication tasks being executed simultaneously.": "",
"Maximum number of seconds a client is allowed to spend connected, after\nauthentication, without issuing a command which results in creating an active or passive data connection\n(i.e. sending/receiving a file, or receiving a directory listing).": "",
"Maximum number of seconds that proftpd will allow clients to stay connected without receiving\nany data on either the control or data connection.": "",
Expand Down
1 change: 1 addition & 0 deletions src/assets/i18n/az.json
Original file line number Diff line number Diff line change
Expand Up @@ -2665,6 +2665,7 @@
"Maximum Passive Port": "",
"Maximum Transmission Unit, the largest protocol data unit that can be communicated. The largest workable MTU size varies with network interfaces and equipment. <i>1500</i> and <i>9000</i> are standard Ethernet MTU sizes. Leaving blank restores the field to the default value of <i>1500</i>.": "",
"Maximum Upload Parts": "",
"Maximum file size is limited to {maxSize}.": "",
"Maximum number of replication tasks being executed simultaneously.": "",
"Maximum number of seconds a client is allowed to spend connected, after\nauthentication, without issuing a command which results in creating an active or passive data connection\n(i.e. sending/receiving a file, or receiving a directory listing).": "",
"Maximum number of seconds that proftpd will allow clients to stay connected without receiving\nany data on either the control or data connection.": "",
Expand Down
1 change: 1 addition & 0 deletions src/assets/i18n/be.json
Original file line number Diff line number Diff line change
Expand Up @@ -2665,6 +2665,7 @@
"Maximum Passive Port": "",
"Maximum Transmission Unit, the largest protocol data unit that can be communicated. The largest workable MTU size varies with network interfaces and equipment. <i>1500</i> and <i>9000</i> are standard Ethernet MTU sizes. Leaving blank restores the field to the default value of <i>1500</i>.": "",
"Maximum Upload Parts": "",
"Maximum file size is limited to {maxSize}.": "",
"Maximum number of replication tasks being executed simultaneously.": "",
"Maximum number of seconds a client is allowed to spend connected, after\nauthentication, without issuing a command which results in creating an active or passive data connection\n(i.e. sending/receiving a file, or receiving a directory listing).": "",
"Maximum number of seconds that proftpd will allow clients to stay connected without receiving\nany data on either the control or data connection.": "",
Expand Down
1 change: 1 addition & 0 deletions src/assets/i18n/bg.json
Original file line number Diff line number Diff line change
Expand Up @@ -2665,6 +2665,7 @@
"Maximum Passive Port": "",
"Maximum Transmission Unit, the largest protocol data unit that can be communicated. The largest workable MTU size varies with network interfaces and equipment. <i>1500</i> and <i>9000</i> are standard Ethernet MTU sizes. Leaving blank restores the field to the default value of <i>1500</i>.": "",
"Maximum Upload Parts": "",
"Maximum file size is limited to {maxSize}.": "",
"Maximum number of replication tasks being executed simultaneously.": "",
"Maximum number of seconds a client is allowed to spend connected, after\nauthentication, without issuing a command which results in creating an active or passive data connection\n(i.e. sending/receiving a file, or receiving a directory listing).": "",
"Maximum number of seconds that proftpd will allow clients to stay connected without receiving\nany data on either the control or data connection.": "",
Expand Down
1 change: 1 addition & 0 deletions src/assets/i18n/bn.json
Original file line number Diff line number Diff line change
Expand Up @@ -2665,6 +2665,7 @@
"Maximum Passive Port": "",
"Maximum Transmission Unit, the largest protocol data unit that can be communicated. The largest workable MTU size varies with network interfaces and equipment. <i>1500</i> and <i>9000</i> are standard Ethernet MTU sizes. Leaving blank restores the field to the default value of <i>1500</i>.": "",
"Maximum Upload Parts": "",
"Maximum file size is limited to {maxSize}.": "",
"Maximum number of replication tasks being executed simultaneously.": "",
"Maximum number of seconds a client is allowed to spend connected, after\nauthentication, without issuing a command which results in creating an active or passive data connection\n(i.e. sending/receiving a file, or receiving a directory listing).": "",
"Maximum number of seconds that proftpd will allow clients to stay connected without receiving\nany data on either the control or data connection.": "",
Expand Down
1 change: 1 addition & 0 deletions src/assets/i18n/br.json
Original file line number Diff line number Diff line change
Expand Up @@ -2665,6 +2665,7 @@
"Maximum Passive Port": "",
"Maximum Transmission Unit, the largest protocol data unit that can be communicated. The largest workable MTU size varies with network interfaces and equipment. <i>1500</i> and <i>9000</i> are standard Ethernet MTU sizes. Leaving blank restores the field to the default value of <i>1500</i>.": "",
"Maximum Upload Parts": "",
"Maximum file size is limited to {maxSize}.": "",
"Maximum number of replication tasks being executed simultaneously.": "",
"Maximum number of seconds a client is allowed to spend connected, after\nauthentication, without issuing a command which results in creating an active or passive data connection\n(i.e. sending/receiving a file, or receiving a directory listing).": "",
"Maximum number of seconds that proftpd will allow clients to stay connected without receiving\nany data on either the control or data connection.": "",
Expand Down
1 change: 1 addition & 0 deletions src/assets/i18n/bs.json
Original file line number Diff line number Diff line change
Expand Up @@ -2665,6 +2665,7 @@
"Maximum Passive Port": "",
"Maximum Transmission Unit, the largest protocol data unit that can be communicated. The largest workable MTU size varies with network interfaces and equipment. <i>1500</i> and <i>9000</i> are standard Ethernet MTU sizes. Leaving blank restores the field to the default value of <i>1500</i>.": "",
"Maximum Upload Parts": "",
"Maximum file size is limited to {maxSize}.": "",
"Maximum number of replication tasks being executed simultaneously.": "",
"Maximum number of seconds a client is allowed to spend connected, after\nauthentication, without issuing a command which results in creating an active or passive data connection\n(i.e. sending/receiving a file, or receiving a directory listing).": "",
"Maximum number of seconds that proftpd will allow clients to stay connected without receiving\nany data on either the control or data connection.": "",
Expand Down
1 change: 1 addition & 0 deletions src/assets/i18n/ca.json
Original file line number Diff line number Diff line change
Expand Up @@ -2665,6 +2665,7 @@
"Maximum Passive Port": "",
"Maximum Transmission Unit, the largest protocol data unit that can be communicated. The largest workable MTU size varies with network interfaces and equipment. <i>1500</i> and <i>9000</i> are standard Ethernet MTU sizes. Leaving blank restores the field to the default value of <i>1500</i>.": "",
"Maximum Upload Parts": "",
"Maximum file size is limited to {maxSize}.": "",
"Maximum number of replication tasks being executed simultaneously.": "",
"Maximum number of seconds a client is allowed to spend connected, after\nauthentication, without issuing a command which results in creating an active or passive data connection\n(i.e. sending/receiving a file, or receiving a directory listing).": "",
"Maximum number of seconds that proftpd will allow clients to stay connected without receiving\nany data on either the control or data connection.": "",
Expand Down
1 change: 1 addition & 0 deletions src/assets/i18n/cs.json
Original file line number Diff line number Diff line change
Expand Up @@ -2151,6 +2151,7 @@
"Maximum Passive Port": "",
"Maximum Transmission Unit, the largest protocol data unit that can be communicated. The largest workable MTU size varies with network interfaces and equipment. <i>1500</i> and <i>9000</i> are standard Ethernet MTU sizes. Leaving blank restores the field to the default value of <i>1500</i>.": "",
"Maximum Upload Parts": "",
"Maximum file size is limited to {maxSize}.": "",
"Maximum number of replication tasks being executed simultaneously.": "",
"Maximum number of seconds a client is allowed to spend connected, after\nauthentication, without issuing a command which results in creating an active or passive data connection\n(i.e. sending/receiving a file, or receiving a directory listing).": "",
"Maximum number of seconds that proftpd will allow clients to stay connected without receiving\nany data on either the control or data connection.": "",
Expand Down
1 change: 1 addition & 0 deletions src/assets/i18n/cy.json
Original file line number Diff line number Diff line change
Expand Up @@ -2665,6 +2665,7 @@
"Maximum Passive Port": "",
"Maximum Transmission Unit, the largest protocol data unit that can be communicated. The largest workable MTU size varies with network interfaces and equipment. <i>1500</i> and <i>9000</i> are standard Ethernet MTU sizes. Leaving blank restores the field to the default value of <i>1500</i>.": "",
"Maximum Upload Parts": "",
"Maximum file size is limited to {maxSize}.": "",
"Maximum number of replication tasks being executed simultaneously.": "",
"Maximum number of seconds a client is allowed to spend connected, after\nauthentication, without issuing a command which results in creating an active or passive data connection\n(i.e. sending/receiving a file, or receiving a directory listing).": "",
"Maximum number of seconds that proftpd will allow clients to stay connected without receiving\nany data on either the control or data connection.": "",
Expand Down
1 change: 1 addition & 0 deletions src/assets/i18n/da.json
Original file line number Diff line number Diff line change
Expand Up @@ -2665,6 +2665,7 @@
"Maximum Passive Port": "",
"Maximum Transmission Unit, the largest protocol data unit that can be communicated. The largest workable MTU size varies with network interfaces and equipment. <i>1500</i> and <i>9000</i> are standard Ethernet MTU sizes. Leaving blank restores the field to the default value of <i>1500</i>.": "",
"Maximum Upload Parts": "",
"Maximum file size is limited to {maxSize}.": "",
"Maximum number of replication tasks being executed simultaneously.": "",
"Maximum number of seconds a client is allowed to spend connected, after\nauthentication, without issuing a command which results in creating an active or passive data connection\n(i.e. sending/receiving a file, or receiving a directory listing).": "",
"Maximum number of seconds that proftpd will allow clients to stay connected without receiving\nany data on either the control or data connection.": "",
Expand Down
1 change: 1 addition & 0 deletions src/assets/i18n/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -1927,6 +1927,7 @@
"Maximum Passive Port": "",
"Maximum Transmission Unit, the largest protocol data unit that can be communicated. The largest workable MTU size varies with network interfaces and equipment. <i>1500</i> and <i>9000</i> are standard Ethernet MTU sizes. Leaving blank restores the field to the default value of <i>1500</i>.": "",
"Maximum Upload Parts": "",
"Maximum file size is limited to {maxSize}.": "",
"Maximum number of replication tasks being executed simultaneously.": "",
"Maximum number of seconds a client is allowed to spend connected, after\nauthentication, without issuing a command which results in creating an active or passive data connection\n(i.e. sending/receiving a file, or receiving a directory listing).": "",
"Maximum number of seconds that proftpd will allow clients to stay connected without receiving\nany data on either the control or data connection.": "",
Expand Down
Loading
Loading