Skip to content

Commit

Permalink
fix: allow relative URLs in backend_url, add other checks for valid …
Browse files Browse the repository at this point in the history
…URLs (ushahidi#682)

* fix: allow relative URLs in backend_url, add other checks for valid URLs

* Update environment.ts

* Update environment.ts

---------

Co-authored-by: Wisdom Ebong <30173059+webong@users.noreply.github.com>
  • Loading branch information
tuxpiper and webong committed Oct 31, 2023
1 parent 34d54bb commit 3f7708a
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions apps/web-mzima-client/src/app/core/helpers/environment.ts
@@ -1,11 +1,28 @@
export const checkBackendURL = (backendUrl: string) => {
if (backendUrl.indexOf('http') === -1) {
backendUrl = `https://${backendUrl}`;
const parseURL = (url: string) => {
try {
return new URL(url);
} catch (e) {
// Attempt to build based on the current document
if (e instanceof TypeError) {
return new URL(url, document.baseURI);
} else {
throw e;
}
}
};

const result = parseURL(backendUrl);

// Query string or hash not allowed
if (result.search !== '' || result.hash !== '') {
throw new Error('Invalid backend URL');
}

if (backendUrl.slice(-1) !== '/') {
backendUrl = `${backendUrl}/`;
// Ensure the path ends in /
if (result.pathname.slice(-1) !== '/') {
result.pathname += '/';
}

return backendUrl;
return result.href;
};

0 comments on commit 3f7708a

Please sign in to comment.