Skip to content

Commit

Permalink
fix(Variables): Ensure no same object instances are shared across config
Browse files Browse the repository at this point in the history
Fixes #7098
  • Loading branch information
medikoo committed Dec 19, 2019
1 parent 7bee36e commit 4893f7d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
15 changes: 14 additions & 1 deletion lib/classes/Variables.js
Expand Up @@ -10,6 +10,8 @@ const fse = require('../utils/fs/fse');
const logWarning = require('./Error').logWarning;
const PromiseTracker = require('./PromiseTracker');

const resolvedValuesWeak = new WeakSet();

/**
* Maintainer's notes:
*
Expand Down Expand Up @@ -576,7 +578,18 @@ class Variables {
}
ret = this.tracker.add(variableString, ret, propertyString);
}
return ret;
return ret.then(resolvedValue => {
if (!Array.isArray(resolvedValue) && !_.isPlainObject(resolvedValue)) return resolvedValue;
if (resolvedValuesWeak.has(resolvedValue)) {
try {
return _.cloneDeep(resolvedValue);
} catch (error) {
return resolvedValue;
}
}
resolvedValuesWeak.add(resolvedValue);
return resolvedValue;
});
}

getValueFromSls(variableString) {
Expand Down
27 changes: 27 additions & 0 deletions lib/classes/Variables.test.js
Expand Up @@ -226,6 +226,33 @@ describe('Variables', () => {
});
});

describe('ensure unique instances', () => {
it('should not produce same instances for same variable patters used more than once', () => {
serverless.variables.service.custom = {
settings1: '${file(~/config.yml)}',
settings2: '${file(~/config.yml)}',
};

const fileExistsStub = sinon.stub(serverless.utils, 'fileExistsSync').returns(true);
const realpathSync = sinon.stub(fse, 'realpathSync').returns(`${os.homedir()}/config.yml`);
const readFileSyncStub = sinon.stub(serverless.utils, 'readFileSync').returns({
test: 1,
test2: 'test2',
});

return serverless.variables
.populateService()
.should.be.fulfilled.then(result => {
expect(result.custom.settings1).to.not.equal(result.custom.settings2);
})
.finally(() => {
fileExistsStub.restore();
realpathSync.restore();
readFileSyncStub.restore();
});
});
});

describe('aws-specific syntax', () => {
let awsProvider;
let requestStub;
Expand Down

0 comments on commit 4893f7d

Please sign in to comment.