diff --git a/.circleci/config.yml b/.circleci/config.yml index 621db6412..6383e3c7c 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -360,6 +360,7 @@ workflows: - develop - TOP-1390 - PM-191-2 + - pm-199 # This is alternate dev env for parallel testing # Deprecate this workflow due to beta env shutdown # https://topcoder.atlassian.net/browse/CORE-251 diff --git a/src/server/index.js b/src/server/index.js index 393a72b9a..4ea3d46c0 100644 --- a/src/server/index.js +++ b/src/server/index.js @@ -42,9 +42,40 @@ global.atob = atob; const CMS_BASE_URL = `https://app.contentful.com/spaces/${config.SECRET.CONTENTFUL.SPACE_ID}`; -let ts = path.resolve(__dirname, '../../.build-info'); -ts = JSON.parse(fs.readFileSync(ts)); -ts = moment(ts.timestamp).valueOf(); +const getTimestamp = async () => { + let timestamp; + try { + const filePath = path.resolve(__dirname, '../../.build-info'); + if (!filePath.startsWith(path.resolve(__dirname, '../../'))) { + throw new Error('Invalid file path detected'); + } + + const MAX_FILE_SIZE = 10 * 1024; // 10 KB max file size + const stats = await fs.promises.stat(filePath); + if (stats.size > MAX_FILE_SIZE) { + throw new Error('File is too large and may cause DoS issues'); + } + + const fileContent = await fs.promises.readFile(filePath, 'utf-8'); + + let tsData; + try { + tsData = JSON.parse(fileContent); + } catch (parseErr) { + throw new Error('Invalid JSON format in file'); + } + + if (!tsData || !tsData.timestamp) { + throw new Error('Timestamp is missing in the JSON file'); + } + + timestamp = moment(tsData.timestamp).valueOf(); + } catch (err) { + console.error('Error:', err.message); + } + + return timestamp; +}; const sw = `sw.js${process.env.NODE_ENV === 'production' ? '' : '?debug'}`; const swScope = '/challenges'; // we are currently only interested in improving challenges pages @@ -52,7 +83,7 @@ const swScope = '/challenges'; // we are currently only interested in improving const tcoPattern = new RegExp(/^tco\d{2}\.topcoder(?:-dev)?\.com$/i); const universalNavUrl = config.UNIVERSAL_NAV_URL; -const EXTRA_SCRIPTS = [ +const getExtraScripts = ts => [ `