Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: dataplane sanitise logic #166

Merged
merged 2 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: 6 additions & 23 deletions src/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,9 @@ 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 +37,17 @@ 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)) {
console.log(`writeKey:${writeKey} or dataPlaneUrl:${dataPlaneUrl} is invalid or missing`);
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 +56,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
Loading