Skip to content

Commit

Permalink
fix: dataplane sanitise logic
Browse files Browse the repository at this point in the history
  • Loading branch information
krishna2020 committed Sep 20, 2024
1 parent b8bff3e commit fb2b740
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/deviceModeInit.js
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ let _rudderTracking = (function () {

// common function for sending anonymousId and sessionId Identifier
function sendToRudderWebhook(data, type, updateTypeCookieFunction, retryAttempt = 0) {
const webhookUrl = 'dataplaneUrl_placeHolder/v1/webhook?writeKey=writeKey_placeHolder';
const webhookUrl = 'https://dataplaneUrl_placeHolder/v1/webhook?writeKey=writeKey_placeHolder';
const timeToRetry = 1000; // 1 second
const maxRetries = 3;
if (maxRetries > retryAttempt) {
Expand Down
2 changes: 1 addition & 1 deletion src/loadingCode.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
};
})(method);
}
rudderanalytics.load('writeKey', 'dataPlaneUrl', {
rudderanalytics.load('writeKey', 'https://dataPlaneUrl', {
configUrl: 'configBackendUrl',
logLevel: 'DEBUG',
});
Expand Down
29 changes: 5 additions & 24 deletions src/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,8 @@ const configUrl = process.env.CONFIG_BACKEND_URL || 'https://api.rudderstack.com
const jsSdkCdnUrl =
process.env.JS_SDK_CDN || 'https://cdn.rudderlabs.com/v1.1/rudder-analytics.min.js';

const ensureHttpsPrefix = (url) => {
// Check if the URL starts with http:// or https://
if (!/^https?:\/\//i.test(url)) {
return `https://${url}`;
}
return url;
};

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 newDataPlaneUrl;
} catch {
return undefined;
}
};
const isValidWriteKey = (writeKey) => /^[A-Za-z0-9_]{5,}$/.test(writeKey);
const isValidDataPlaneURL = (dataPlaneUrl) => /^(?!:\/\/)([a-zA-Z0-9-_]{1,63}\.)+[a-zA-Z]{2,6}$/.test(dataPlaneUrl);

router.get('/load', async (ctx) => {
// only takes in writeKey and DataPlane Url
Expand All @@ -53,18 +36,16 @@ router.get('/load', async (ctx) => {
const { writeKey, dataPlaneUrl } = ctx.request.query;
console.log('writeKey', writeKey);
console.log('dataplaneUrl', dataPlaneUrl);
if (formatDataPlaneURL(dataPlaneUrl) === undefined || !isValidWriteKey(writeKey)) {
if (!isValidDataPlaneURL(dataPlaneUrl) || !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', formattedDataPlaneUrl);
d = d.replace('dataPlaneUrl', dataPlaneUrl);
d = d.replace('configBackendUrl', configUrl);

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

Expand Down

0 comments on commit fb2b740

Please sign in to comment.