Skip to content

Commit

Permalink
fix(ui): Apply url encode and decode to a ProcessURL. Fixes #9791 (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewChubatiuk committed Oct 29, 2022
1 parent d612d5d commit 23e3d4d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
12 changes: 12 additions & 0 deletions ui/src/app/shared/components/links.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@ describe('process URL', () => {
expect(ProcessURL('https://logging?from=${status.startedAt}&to=${status.finishedAt}', object)).toBe('https://logging?from=2021-01-01T10:30:00Z&to=2021-01-01T10:30:00Z');
});

test('url encoded string', () => {
const object = {
metadata: {
name: 'test'
},
status: {
startedAt: '2021-01-01T10:30:00Z'
}
};
expect(ProcessURL('https://logging/$%7Bmetadata.name%7D', object)).toBe('https://logging/test');
});

test('epoch timestamp', () => {
const object = {
status: {
Expand Down
20 changes: 12 additions & 8 deletions ui/src/app/shared/components/links.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,20 @@ const addEpochTimestamp = (jsonObject: {metadata: ObjectMeta; workflow?: Workflo
};

export const ProcessURL = (url: string, jsonObject: any) => {
/* decode url string to apply templating */
const decodedUrl = decodeURI(url);
addEpochTimestamp(jsonObject);
/* replace ${} from input url with corresponding elements from object
return null if element is not found*/
return url.replace(/\${[^}]*}/g, x => {
const res = x
.replace(/[${}]+/g, '')
.split('.')
.reduce((p: any, c: string) => (p && p[c]) || null, jsonObject);
return res;
});
return encodeURI(
decodedUrl.replace(/\${[^}]*}/g, x => {
const res = x
.replace(/[${}]+/g, '')
.split('.')
.reduce((p: any, c: string) => (p && p[c]) || null, jsonObject);
return res;
})
);
};

export const Links = ({scope, object, button}: {scope: string; object: {metadata: ObjectMeta; workflow?: Workflow; status?: any}; button?: boolean}) => {
Expand All @@ -47,7 +51,7 @@ export const Links = ({scope, object, button}: {scope: string; object: {metadata
}, []);

const formatUrl = (url: string) => {
return encodeURI(ProcessURL(decodeURI(url), object));
return ProcessURL(url, object);
};

const openLink = (url: string) => {
Expand Down

0 comments on commit 23e3d4d

Please sign in to comment.