Skip to content

Commit

Permalink
Fix for multiple = char in env variables
Browse files Browse the repository at this point in the history
  • Loading branch information
Arvind Agarwal authored and Arvind Agarwal committed Jun 26, 2017
1 parent 2578175 commit 407f9a0
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 22 deletions.
12 changes: 1 addition & 11 deletions bin/forever-service
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,7 @@ platforms.get(function(err, platform){
if(options.envVars) ctx.envVars = options.envVars;
if(ctx.envVars){
//Split at space, but ignore space inside quotes..
ctx.envVarsArray = ctx.envVars.match(/(?:[^\s"']+|["'][^"']*["'])+/g);
ctx.envVarsNameValueArray=[];
for(var evi in ctx.envVarsArray){
var ev = ctx.envVarsArray[evi];
var evp = ev.split("=");
if(evp.length != 2){
console.log("Invalid env variable "+ev);
process.exit(1);
}
ctx.envVarsNameValueArray.push(evp);
}
ctx.envVarsNameValueArray = installer.splitEnvVariables(ctx.envVars);
}
if(options.scriptOptions) ctx.scriptOptions = options.scriptOptions;
if(options.minUptime) ctx.minUptime = options.minUptime;
Expand Down
20 changes: 19 additions & 1 deletion lib/installer.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,23 @@ exports.delete = function(ctx, callback){

exports.splitEnvVariables=function(envVars){
//Split at space, but ignore space inside quotes..
return envVars.match(/(?:[^\s"']+|["'][^"']*["'])+/g);
envVarsArray = envVars.match(/(?:[^\s"']+|["'][^"']*["'])+/g);

envVarsNameValueArray=[];
for(var evi in envVarsArray){
var ev = envVarsArray[evi];

//Look for first = sign to know the variable name, rest of value may contain = sign but will be ignored
var eqlPos = ev.indexOf("=");
if(eqlPos < 0) { //if no equal sign is found it is a problem since env variable is there without = so it will be an error
console.log("Invalid env variable "+ev);
process.exit(1);
}
var evp = [
ev.substring(0, eqlPos),
ev.substring(eqlPos+1)
];
envVarsNameValueArray.push(evp);
}
return envVarsNameValueArray;
}
55 changes: 45 additions & 10 deletions test/scriptBuilderTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,40 +59,75 @@ describe("Check Environment Variable splitting", function(){
var e = installer.splitEnvVariables('HOME=/xyz/');
e.should.be.an.Array;
e.should.have.length(1);
e.should.containEql('HOME=/xyz/');
e.should.containEql(['HOME','/xyz/']);
});


it("Should split env variables by space", function(){
var e = installer.splitEnvVariables('z=10 x=test HOME=/xyz/');
e.should.be.an.Array;
e.should.have.length(3);
e.should.containEql('z=10');
e.should.containEql('x=test');
e.should.containEql('HOME=/xyz/');
e.should.containEql(['z','10']);
e.should.containEql(['x','test']);
e.should.containEql(['z','10']);
});


it("Should split env variables by space but avoid space splitting inside quote", function(){
var e = installer.splitEnvVariables('z=10 x="test testing" HOME=/xyz/');
e.should.be.an.Array;
e.should.have.length(3);
e.should.containEql('z=10');
e.should.containEql('x="test testing"');
e.should.containEql('HOME=/xyz/');

e.should.containEql(['z','10']);
e.should.containEql(['x','"test testing"']);
e.should.containEql(['z','10']);
});


it("Should split env variables by space but avoid space splitting inside single quote", function(){
var e = installer.splitEnvVariables("z=10 x='test testing' HOME=/xyz/");
e.should.be.an.Array;
e.should.have.length(3);
e.should.containEql('z=10');
e.should.containEql("x='test testing'");
e.should.containEql('HOME=/xyz/');
e.should.containEql(['z','10']);
e.should.containEql(['x',"'test testing'"]);
e.should.containEql(['HOME','/xyz/']);
});


it("Should split env variables by space but avoid space splitting inside quote, and ignore = inside quote", function(){
var e = installer.splitEnvVariables('z=10 x="test testing" HOME=/xyz/ MONGO_URI="mongodb://user:password@host1:15145,host2:15145/db-name?ssl=true" ');
e.should.be.an.Array;
e.should.have.length(4);
e.should.containEql(['z','10']);
e.should.containEql(['x','"test testing"']);
e.should.containEql(['HOME','/xyz/']);
e.should.containEql(['MONGO_URI','"mongodb://user:password@host1:15145,host2:15145/db-name?ssl=true"']);
});


it("Should split empty value env variables also", function(){
var e = installer.splitEnvVariables('z=10 x="test testing" Q= HOME=/xyz/ MONGO_URI="mongodb://user:password@host1:15145,host2:15145/db-name?ssl=true" ');
e.should.be.an.Array;
e.should.have.length(5);
e.should.containEql(['z','10']);
e.should.containEql(['x','"test testing"']);
e.should.containEql(['Q','']);
e.should.containEql(['HOME','/xyz/']);
e.should.containEql(['MONGO_URI','"mongodb://user:password@host1:15145,host2:15145/db-name?ssl=true"']);
});

it("Should split empty value env variables also at end", function(){
var e = installer.splitEnvVariables('z=10 x="test testing" Q= HOME=/xyz/ MONGO_URI="mongodb://user:password@host1:15145,host2:15145/db-name?ssl=true" QQ=');
e.should.be.an.Array;
e.should.have.length(6);
e.should.containEql(['z','10']);
e.should.containEql(['x','"test testing"']);
e.should.containEql(['Q','']);
e.should.containEql(['HOME','/xyz/']);
e.should.containEql(['MONGO_URI','"mongodb://user:password@host1:15145,host2:15145/db-name?ssl=true"']);
e.should.containEql(['QQ','']);
});

});


0 comments on commit 407f9a0

Please sign in to comment.