From 0e4fde065ad65035d691697ec7d42ed75bd3f4d9 Mon Sep 17 00:00:00 2001 From: Michael Barrett Date: Wed, 11 Dec 2024 10:53:06 +0000 Subject: [PATCH] Use crypto.randomUUID now that the value is memoized --- ghost/core/core/shared/config/helpers.js | 8 ++--- .../test/unit/shared/config/helpers.test.js | 29 ++++++++++++------- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/ghost/core/core/shared/config/helpers.js b/ghost/core/core/shared/config/helpers.js index d74aebece7a1..2644ae4ee399 100644 --- a/ghost/core/core/shared/config/helpers.js +++ b/ghost/core/core/shared/config/helpers.js @@ -74,15 +74,11 @@ let processTmpDirPath = null; * * @returns {string} - tmp dir path for the current process */ -function getProcessTmpDirPath(config) { +function getProcessTmpDirPath() { // Memoize the computed path to avoid re-computing it on each call - The // value should not change during the lifetime of the process. if (processTmpDirPath === null) { - const siteHash = crypto.createHash('md5') - .update(config.getSiteUrl()) - .digest('hex'); - - processTmpDirPath = path.join(os.tmpdir(), `ghost_${siteHash}_${process.pid}`); + processTmpDirPath = path.join(os.tmpdir(), `ghost_${crypto.randomUUID()}`); } return processTmpDirPath; diff --git a/ghost/core/test/unit/shared/config/helpers.test.js b/ghost/core/test/unit/shared/config/helpers.test.js index c48d883697eb..906d2db1152a 100644 --- a/ghost/core/test/unit/shared/config/helpers.test.js +++ b/ghost/core/test/unit/shared/config/helpers.test.js @@ -1,10 +1,10 @@ -const crypto = require('crypto'); const os = require('os'); -const path = require('path'); const should = require('should'); const configUtils = require('../../../utils/configUtils'); +const UUID_REGEX = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/; + describe('vhost utils', function () { beforeEach(function () { configUtils.set('url', 'http://ghost.blog'); @@ -59,17 +59,26 @@ describe('vhost utils', function () { describe('getContentPath', function () { it('should return the correct path for type: public', function () { + const publicPath = configUtils.config.getContentPath('public'); + + // Path should be in the tmpdir const tmpdir = os.tmpdir(); - const siteUrl = configUtils.config.getSiteUrl(); - const siteHash = crypto.createHash('md5') - .update(siteUrl) - .digest('hex'); - const expectedPath = path.join(tmpdir, `ghost_${siteHash}_${process.pid}`, 'public/'); + publicPath.startsWith(tmpdir).should.be.true(); + + // Path should end with /public/ + publicPath.endsWith('/public/').should.be.true(); + + // Path should include ghost_ + publicPath.includes('ghost_').should.be.true(); + + // Path should contain a uuid in the correct location + const publicPathParts = publicPath.split('/'); + const uuidPart = publicPathParts[publicPathParts.length - 3].replace('ghost_', ''); - configUtils.config.getContentPath('public').should.eql(expectedPath); + UUID_REGEX.test(uuidPart).should.be.true(); - // Call again to ensure the path is deterministic - configUtils.config.getContentPath('public').should.eql(expectedPath); + // Path should be memoized + configUtils.config.getContentPath('public').should.eql(publicPath); }); });