Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
kulgg committed Mar 11, 2024
1 parent b99ea2a commit 03fbc69
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 17 deletions.
57 changes: 42 additions & 15 deletions src/app/core/services/http-params-mapper.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ export class HttpRequestParamsMapper {
value: ContractAgreementTransferDialogFormValue,
): Record<string, string> {
const method = value.httpProxiedMethod?.trim() ?? '';
const {url: pathSegments, queryParams} = this.getUrlAndQueryParams(
const pathSegments = this.getUrl(value.httpProxiedPath);
const queryParams = this.getQueryParams(
value.httpProxiedPath,
value.httpProxiedQueryParams,
);
Expand Down Expand Up @@ -98,7 +99,8 @@ export class HttpRequestParamsMapper {
method = null;
}

const {url: baseUrl, queryParams} = this.getUrlAndQueryParams(
const baseUrl = this.getUrl(formValue?.httpUrl);
const queryParams = this.getRawQueryParams(
formValue?.httpUrl,
formValue?.httpQueryParams,
);
Expand Down Expand Up @@ -141,34 +143,59 @@ export class HttpRequestParamsMapper {
return {authHeaderName, authHeaderValue, authHeaderSecretName};
}

getUrlAndQueryParams(
getUrl(rawUrl: string | null | undefined): string | null {
return everythingBefore('?', this.trimOrEmpty(rawUrl));
}

getQueryParams(
rawUrl: string | null | undefined,
rawQueryParams: HttpDatasourceQueryParamFormValue[] | null | undefined,
): {
url: string | null;
queryParams: string | null;
} {
let rawUrlTrimmed = rawUrl?.trim() ?? '';
): string | null {
const queryParamSegments = (rawQueryParams ?? []).map((param) =>
this.encodeQueryParam(param),
);
const queryParams = [
everythingAfter('?', this.trimOrEmpty(rawUrl)),
...queryParamSegments,
]
.filter((it) => !!it)
.join('&');

const url = everythingBefore('?', rawUrlTrimmed);
return queryParams;
}

getRawQueryParams(
rawUrl: string | null | undefined,
rawQueryParams: HttpDatasourceQueryParamFormValue[] | null | undefined,
): string | null {
const queryParamSegments = (rawQueryParams ?? []).map((param) =>
this.encodeQueryParam(param),
this.buildQueryParam(
this.trimOrEmpty(param.paramName),
this.trimOrEmpty(param.paramValue),
),
);
const queryParams = [
everythingAfter('?', rawUrlTrimmed),
everythingAfter('?', this.trimOrEmpty(rawUrl)),
...queryParamSegments,
]
.filter((it) => !!it)
.join('&');

return {url: url || null, queryParams: queryParams || null};
return queryParams;
}

private encodeQueryParam(param: HttpDatasourceQueryParamFormValue): string {
const k = param.paramName?.trim() ?? '';
const v = param.paramValue?.trim() ?? '';
return `${encodeURIComponent(k)}=${encodeURIComponent(v)}`;
const k = encodeURIComponent(this.trimOrEmpty(param.paramName));
const v = encodeURIComponent(this.trimOrEmpty(param.paramValue));
return this.buildQueryParam(k, v);
}

private trimOrEmpty(s: string | null | undefined): string {
return s?.trim() ?? '';
}

private buildQueryParam(name: string, value: string) {
return `${name}=${value}`;
}

private buildHttpHeaders(
Expand Down
18 changes: 18 additions & 0 deletions src/app/core/validators/valid-query-param.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {AbstractControl, ValidationErrors, ValidatorFn} from '@angular/forms';

/**
* Validates that param string does not contain "=" or "&" characters
* Temporary solution until EDC double encoding issue is resolved
* See https://github.com/sovity/edc-extensions/issues/582
* @param control control
*/
export const validQueryParam: ValidatorFn = (
control: AbstractControl,
): ValidationErrors | null => {
const value: string = control.value;
if (value?.includes('=') || value?.includes('&')) {
return {invalidQueryParam: true};
}

return null;
};
1 change: 1 addition & 0 deletions src/app/core/validators/validation-messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ export class ValidationMessages {
`${field} must start with "${prefix}".`;
invalidDateRangeMessage = 'Need valid date range.';
idExistsErrorMessage = 'ID already exists.';
invalidQueryParam = "Must not contain '=' or '&' characters.";
}
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,14 @@ <h1 mat-dialog-title>
required
autocomplete="new-query-param-name"
[formControl]="header.controls.paramName" />
{{ header.errors }}
<mat-error
*ngIf="
header.controls.paramName.invalid &&
header.controls.paramName.errors?.invalidQueryParam
"
>{{ validationMessages.invalidQueryParam }}</mat-error
>
</mat-form-field>

<!-- Query Param Value -->
Expand All @@ -500,6 +508,13 @@ <h1 mat-dialog-title>
placeholder="..."
autocomplete="new-query-param-value"
[formControl]="header.controls.paramValue" />
<mat-error
*ngIf="
header.controls.paramValue.invalid &&
header.controls.paramValue.errors?.invalidQueryParam
"
>{{ validationMessages.invalidQueryParam }}</mat-error
>
</mat-form-field>

<!-- Query Param Delete Button -->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {Injectable} from '@angular/core';
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
import {validQueryParam} from 'src/app/core/validators/valid-query-param';
import {switchDisabledControls} from '../../../../../core/utils/form-group-utils';
import {jsonValidator} from '../../../../../core/validators/json-validator';
import {urlValidator} from '../../../../../core/validators/url-validator';
Expand Down Expand Up @@ -120,8 +121,8 @@ export class AssetDatasourceFormBuilder {
initial: HttpDatasourceQueryParamFormValue,
): FormGroup<HttpDatasourceQueryParamFormModel> {
return this.formBuilder.nonNullable.group({
paramName: [initial.paramName!, Validators.required],
paramValue: [initial.paramValue!],
paramName: [initial.paramName!, [Validators.required, validQueryParam]],
paramValue: [initial.paramValue!, [validQueryParam]],
});
}
}

0 comments on commit 03fbc69

Please sign in to comment.