Skip to content

Commit

Permalink
chore: update formatting for dataplane url
Browse files Browse the repository at this point in the history
  • Loading branch information
yashasvibajpai committed Sep 20, 2024
1 parent 2481c59 commit e623370
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ const ensureHttpsPrefix = (url) => {
return url;
};

const isValidDataPlaneURL = (dataPlaneUrl) => {
const formatDataPlaneURL = (dataPlaneUrl) => {
// TODO :: Sanitize dataplane url with basic checks before prefixing with https
const newDataPlaneUrl = ensureHttpsPrefix(dataPlaneUrl);
try {
new URL(newDataPlaneUrl); // This will throw if the URL is invalid
return true;
return newDataPlaneUrl;
} catch {
return false;
return undefined;
}
};
const isValidWriteKey = (writeKey) => /^[A-Za-z0-9_]{5,}$/.test(writeKey);
Expand Down Expand Up @@ -52,16 +53,18 @@ router.get('/load', async (ctx) => {
const { writeKey, dataPlaneUrl } = ctx.request.query;
console.log('writeKey', writeKey);
console.log('dataplaneUrl', dataPlaneUrl);
if (!isValidDataPlaneURL(dataPlaneUrl) || !isValidWriteKey(writeKey)) {
if (formatDataPlaneURL(dataPlaneUrl) === undefined || !isValidWriteKey(writeKey)) {
ctx.response.body = {
error: 'writeKey or dataPlaneUrl is invalid or missing',
};
ctx.status = 400;
return ctx;
}
const formattedDataPlaneUrl = formatDataPlaneURL(dataPlaneUrl);
console.log('formattedDataPlaneUrl', formattedDataPlaneUrl);

d = d.replace('writeKey', writeKey);
d = d.replace('dataPlaneUrl', dataPlaneUrl);
d = d.replace('dataPlaneUrl', formattedDataPlaneUrl);
d = d.replace('configBackendUrl', configUrl);

const pollTimeForSessionIdentifierCheck =
Expand All @@ -70,7 +73,7 @@ router.get('/load', async (ctx) => {
/sessionIdentifierPollTime_placeHolder/g,
pollTimeForSessionIdentifierCheck,
);
deviceModeInit = deviceModeInit.replace(/dataplaneUrl_placeHolder/g, dataPlaneUrl);
deviceModeInit = deviceModeInit.replace(/dataplaneUrl_placeHolder/g, formattedDataPlaneUrl);
deviceModeInit = deviceModeInit.replace(/writeKey_placeHolder/g, writeKey);
deviceModeInit = deviceModeInit.replace(/configUrl_placeholder/g, configUrl);

Expand Down

0 comments on commit e623370

Please sign in to comment.