1
0
Bläddra i källkod

Merge pull request #758 from tomrake/base-path-validation

FileServer: added validation of --base-path; only existing directories should be allowed.
Nicolas Petton 11 år sedan
förälder
incheckning
2e94920ee8
3 ändrade filer med 140 tillägg och 12 borttagningar
  1. 63 5
      cli/js/AmberCli.js
  2. 14 2
      cli/st/AmberCli.st
  3. 63 5
      cli/support/amber-cli.js

+ 63 - 5
cli/js/AmberCli.js

@@ -216,15 +216,15 @@ return smalltalk.withContext(function($ctx1) {
 var $2,$1;
 $2=self["@basePath"];
 if(($receiver = $2) == nil || $receiver == null){
-$1="./";
+$1=_st(self._class())._defaultBasePath();
 } else {
 $1=$2;
 };
 return $1;
 }, function($ctx1) {$ctx1.fill(self,"basePath",{},smalltalk.FileServer)})},
 args: [],
-source: "basePath\x0a\x09^basePath ifNil: ['./']",
-messageSends: ["ifNil:"],
+source: "basePath\x0a\x09^basePath ifNil: [self class defaultBasePath]",
+messageSends: ["ifNil:", "defaultBasePath", "class"],
 referencedClasses: []
 }),
 smalltalk.FileServer);
@@ -237,10 +237,11 @@ fn: function (aString){
 var self=this;
 return smalltalk.withContext(function($ctx1) { 
 self["@basePath"]=aString;
+self._validateBasePath();
 return self}, function($ctx1) {$ctx1.fill(self,"basePath:",{aString:aString},smalltalk.FileServer)})},
 args: ["aString"],
-source: "basePath: aString\x0a\x09basePath := aString",
-messageSends: [],
+source: "basePath: aString\x0a\x09basePath := aString.\x0a\x09self validateBasePath.",
+messageSends: ["validateBasePath"],
 referencedClasses: []
 }),
 smalltalk.FileServer);
@@ -928,6 +929,47 @@ referencedClasses: []
 }),
 smalltalk.FileServer);
 
+smalltalk.addMethod(
+smalltalk.method({
+selector: "validateBasePath",
+protocol: 'private',
+fn: function (){
+var self=this;
+return smalltalk.withContext(function($ctx1) { 
+var $1,$2,$3,$4,$7,$6,$5,$8,$9;
+$1=self["@fs"];
+$2=self._basePath();
+$ctx1.sendIdx["basePath"]=1;
+_st($1)._stat_then_($2,(function(err,stat){
+return smalltalk.withContext(function($ctx2) {
+if(($receiver = err) == nil || $receiver == null){
+$3=_st(stat)._isDirectory();
+if(! smalltalk.assert($3)){
+$4=console;
+$7=self._basePath();
+$ctx2.sendIdx["basePath"]=2;
+$6="Warning: --base-path parameter ".__comma($7);
+$ctx2.sendIdx[","]=2;
+$5=_st($6).__comma(" is not a directory.");
+$ctx2.sendIdx[","]=1;
+return _st($4)._warn_($5);
+$ctx2.sendIdx["warn:"]=1;
+};
+} else {
+$8=console;
+$9=_st("Warning: path at --base-path parameter ".__comma(self._basePath())).__comma(" does not exist.");
+$ctx2.sendIdx[","]=3;
+return _st($8)._warn_($9);
+};
+}, function($ctx2) {$ctx2.fillBlock({err:err,stat:stat},$ctx1,1)})}));
+return self}, function($ctx1) {$ctx1.fill(self,"validateBasePath",{},smalltalk.FileServer)})},
+args: [],
+source: "validateBasePath\x0a\x09\x22The basePath must be an existing directory. \x22\x0a\x09fs stat: self basePath then: [ :err :stat | err\x0a\x09\x09ifNil: [ stat isDirectory ifFalse: [ console warn: 'Warning: --base-path parameter ' , self basePath , ' is not a directory.' ]]\x0a\x09\x09ifNotNil: [ console warn: 'Warning: path at --base-path parameter ' , self basePath , ' does not exist.'  ]].",
+messageSends: ["stat:then:", "basePath", "ifNil:ifNotNil:", "ifFalse:", "isDirectory", "warn:", ","],
+referencedClasses: []
+}),
+smalltalk.FileServer);
+
 smalltalk.addMethod(
 smalltalk.method({
 selector: "withBasePath:",
@@ -1076,6 +1118,22 @@ referencedClasses: ["Array"]
 }),
 smalltalk.FileServer.klass);
 
+smalltalk.addMethod(
+smalltalk.method({
+selector: "defaultBasePath",
+protocol: 'accessing',
+fn: function (){
+var self=this;
+return smalltalk.withContext(function($ctx1) { 
+return "./";
+}, function($ctx1) {$ctx1.fill(self,"defaultBasePath",{},smalltalk.FileServer.klass)})},
+args: [],
+source: "defaultBasePath\x0a\x09^ './'",
+messageSends: [],
+referencedClasses: []
+}),
+smalltalk.FileServer.klass);
+
 smalltalk.addMethod(
 smalltalk.method({
 selector: "defaultHost",

+ 14 - 2
cli/st/AmberCli.st

@@ -126,11 +126,12 @@ Adding new commandline parameters to `FileServer` is as easy as adding a new sin
 !FileServer methodsFor: 'accessing'!
 
 basePath
-	^basePath ifNil: ['./']
+	^basePath ifNil: [self class defaultBasePath]
 !
 
 basePath: aString
-	basePath := aString
+	basePath := aString.
+	self validateBasePath.
 !
 
 fallbackPage
@@ -229,6 +230,13 @@ require: aModuleString
 	^require value: aModuleString
 !
 
+validateBasePath
+	"The basePath must be an existing directory. "
+	fs stat: self basePath then: [ :err :stat | err
+		ifNil: [ stat isDirectory ifFalse: [ console warn: 'Warning: --base-path parameter ' , self basePath , ' is not a directory.' ]]
+		ifNotNil: [ console warn: 'Warning: path at --base-path parameter ' , self basePath , ' does not exist.'  ]].
+!
+
 withBasePath: aBaseRelativePath
 	"return a file path which is relative to the basePath."
 	^	path join: self basePath with: aBaseRelativePath
@@ -399,6 +407,10 @@ commandLineSwitches
 	^switches
 !
 
+defaultBasePath
+	^ './'
+!
+
 defaultHost
 	^'127.0.0.1'
 !

+ 63 - 5
cli/support/amber-cli.js

@@ -38839,15 +38839,15 @@ return smalltalk.withContext(function($ctx1) {
 var $2,$1;
 $2=self["@basePath"];
 if(($receiver = $2) == nil || $receiver == null){
-$1="./";
+$1=_st(self._class())._defaultBasePath();
 } else {
 $1=$2;
 };
 return $1;
 }, function($ctx1) {$ctx1.fill(self,"basePath",{},smalltalk.FileServer)})},
 args: [],
-source: "basePath\x0a\x09^basePath ifNil: ['./']",
-messageSends: ["ifNil:"],
+source: "basePath\x0a\x09^basePath ifNil: [self class defaultBasePath]",
+messageSends: ["ifNil:", "defaultBasePath", "class"],
 referencedClasses: []
 }),
 smalltalk.FileServer);
@@ -38860,10 +38860,11 @@ fn: function (aString){
 var self=this;
 return smalltalk.withContext(function($ctx1) { 
 self["@basePath"]=aString;
+self._validateBasePath();
 return self}, function($ctx1) {$ctx1.fill(self,"basePath:",{aString:aString},smalltalk.FileServer)})},
 args: ["aString"],
-source: "basePath: aString\x0a\x09basePath := aString",
-messageSends: [],
+source: "basePath: aString\x0a\x09basePath := aString.\x0a\x09self validateBasePath.",
+messageSends: ["validateBasePath"],
 referencedClasses: []
 }),
 smalltalk.FileServer);
@@ -39551,6 +39552,47 @@ referencedClasses: []
 }),
 smalltalk.FileServer);
 
+smalltalk.addMethod(
+smalltalk.method({
+selector: "validateBasePath",
+protocol: 'private',
+fn: function (){
+var self=this;
+return smalltalk.withContext(function($ctx1) { 
+var $1,$2,$3,$4,$7,$6,$5,$8,$9;
+$1=self["@fs"];
+$2=self._basePath();
+$ctx1.sendIdx["basePath"]=1;
+_st($1)._stat_then_($2,(function(err,stat){
+return smalltalk.withContext(function($ctx2) {
+if(($receiver = err) == nil || $receiver == null){
+$3=_st(stat)._isDirectory();
+if(! smalltalk.assert($3)){
+$4=console;
+$7=self._basePath();
+$ctx2.sendIdx["basePath"]=2;
+$6="Warning: --base-path parameter ".__comma($7);
+$ctx2.sendIdx[","]=2;
+$5=_st($6).__comma(" is not a directory.");
+$ctx2.sendIdx[","]=1;
+return _st($4)._warn_($5);
+$ctx2.sendIdx["warn:"]=1;
+};
+} else {
+$8=console;
+$9=_st("Warning: path at --base-path parameter ".__comma(self._basePath())).__comma(" does not exist.");
+$ctx2.sendIdx[","]=3;
+return _st($8)._warn_($9);
+};
+}, function($ctx2) {$ctx2.fillBlock({err:err,stat:stat},$ctx1,1)})}));
+return self}, function($ctx1) {$ctx1.fill(self,"validateBasePath",{},smalltalk.FileServer)})},
+args: [],
+source: "validateBasePath\x0a\x09\x22The basePath must be an existing directory. \x22\x0a\x09fs stat: self basePath then: [ :err :stat | err\x0a\x09\x09ifNil: [ stat isDirectory ifFalse: [ console warn: 'Warning: --base-path parameter ' , self basePath , ' is not a directory.' ]]\x0a\x09\x09ifNotNil: [ console warn: 'Warning: path at --base-path parameter ' , self basePath , ' does not exist.'  ]].",
+messageSends: ["stat:then:", "basePath", "ifNil:ifNotNil:", "ifFalse:", "isDirectory", "warn:", ","],
+referencedClasses: []
+}),
+smalltalk.FileServer);
+
 smalltalk.addMethod(
 smalltalk.method({
 selector: "withBasePath:",
@@ -39699,6 +39741,22 @@ referencedClasses: ["Array"]
 }),
 smalltalk.FileServer.klass);
 
+smalltalk.addMethod(
+smalltalk.method({
+selector: "defaultBasePath",
+protocol: 'accessing',
+fn: function (){
+var self=this;
+return smalltalk.withContext(function($ctx1) { 
+return "./";
+}, function($ctx1) {$ctx1.fill(self,"defaultBasePath",{},smalltalk.FileServer.klass)})},
+args: [],
+source: "defaultBasePath\x0a\x09^ './'",
+messageSends: [],
+referencedClasses: []
+}),
+smalltalk.FileServer.klass);
+
 smalltalk.addMethod(
 smalltalk.method({
 selector: "defaultHost",