Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
47 changes: 33 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,9 @@
"showdown": "^1.8.6",
"slick-carousel": "^1.8.1",
"supertest": "^3.1.0",
"tc-core-library-js": "github:appirio-tech/tc-core-library-js#v2.6.3.1",
"tc-core-library-js": "github:topcoder-platform/tc-core-library-js#master",
"tc-ui": "^1.0.12",
"topcoder-react-lib": "github:topcoder-platform/topcoder-react-lib#auth0",
"topcoder-react-lib": "github:topcoder-platform/topcoder-react-lib#develop",
"topcoder-react-ui-kit": "2.0.1",
"topcoder-react-utils": "github:topcoder-platform/topcoder-react-utils#v6",
"turndown": "^4.0.2",
Expand Down
7 changes: 6 additions & 1 deletion src/shared/containers/challenge-detail/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import { getService } from 'services/contentful';
import { getSubmissionArtifacts as getSubmissionArtifactsService } from 'services/submissions';
import getReviewSummationsService from 'services/reviewSummations';
import { buildMmSubmissionData, buildStatisticsData } from 'utils/mm-review-summations';
import { appendUtmParamsToUrl } from 'utils/utm';
// import {
// getDisplayRecommendedChallenges,
// getRecommendedTags,
Expand Down Expand Up @@ -349,7 +350,11 @@ class ChallengeDetailPageContainer extends React.Component {
} = this.props;
if (!auth.tokenV3) {
const utmSource = communityId || 'community-app-main';
window.location.href = `${config.URL.AUTH}/member?retUrl=${encodeURIComponent(`${window.location.origin}${window.location.pathname}`)}&utm_source=${utmSource}&regSource=challenges`;
window.location.href = appendUtmParamsToUrl(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[❗❗ security]
The appendUtmParamsToUrl function is used to construct the URL with UTM parameters. Ensure that this function properly encodes all URL components to prevent any potential URL injection vulnerabilities.

`${config.URL.AUTH}/member?retUrl=${encodeURIComponent(`${window.location.origin}${window.location.pathname}`)}&regSource=challenges`, {
utm_source: utmSource,
},
);
} else {
// Show security reminder to all registrants
this.setState({
Expand Down
69 changes: 69 additions & 0 deletions src/shared/utils/utm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// UTM cookie configuration constants
const TC_UTM_COOKIE_NAME = 'tc_utm';

/**
* Retrieves and parses the tc_utm cookie
* @returns Parsed UTM parameters or null if cookie doesn't exist
*/
export function getUtmCookie() {
try {
const cookies = document.cookie.split(';');
const cookieStr = cookies.find(cookie => cookie.trim().startsWith(`${TC_UTM_COOKIE_NAME}=`));

if (!cookieStr) {
return null;
}

// handle values that might contain '='
const cookieValue = decodeURIComponent(cookieStr.split('=').slice(1).join('='));
return JSON.parse(cookieValue);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[⚠️ maintainability]
Consider logging the error or providing more context in the catch block to aid in debugging if JSON parsing fails.

} catch (error) {
return null;
}
}

/**
* Appends UTM parameters from the tc_utm cookie to a given URL
* Only appends parameters that exist in the cookie
* @param url - The base URL to append parameters to
* @returns URL with UTM parameters appended, or original URL if no cookie exists
*/
export function appendUtmParamsToUrl(url, defaultParams = {}) {
if (!url) {
return url;
}

const utmParams = getUtmCookie();

// If there are no cookie params and no defaults, nothing to do
if (
(!utmParams || Object.keys(utmParams).length === 0)
&& (!defaultParams || Object.keys(defaultParams).length === 0)
) {
return url;
}

try {
const urlObj = new URL(url, window.location.origin);
const paramNames = ['utm_source', 'utm_medium', 'utm_campaign'];

paramNames.forEach((param) => {
const cookieVal = utmParams && utmParams[param];
const defaultVal = defaultParams && defaultParams[param];

// Cookie takes precedence and will overwrite existing query param
if (cookieVal) {
urlObj.searchParams.set(param, cookieVal);
} else if (defaultVal) {
// Only apply default if the URL does not already have the param
if (!urlObj.searchParams.has(param)) {
urlObj.searchParams.set(param, defaultVal);
}
}
});

return urlObj.toString();
} catch (error) {
return url;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[⚠️ maintainability]
Consider logging the error or providing more context in the catch block to aid in debugging if URL manipulation fails.

}
}
Loading