Pārlūkot izejas kodu

more filename checks&fix promise flow in route handler

Benoît Hubert 8 gadi atpakaļ
vecāks
revīzija
78110434b9
2 mainītis faili ar 25 papildinājumiem un 6 dzēšanām
  1. 6 1
      js/ws-forms.js
  2. 19 5
      sandboxApp.js

+ 6 - 1
js/ws-forms.js

@@ -139,10 +139,15 @@
           else {
             var ext = (bits[lastIdx]).toLowerCase();
             if( ['html', 'js', 'css'].indexOf( ext ) === -1 ) {
+              this.cantSubmit = 'Impossible de valider: extension ' + ext + ' invalide (autorisées: .html/.js/.css)';
+            }
+            else if(bits[0] === '') {
+              this.cantSubmit = "Le premier caractère du nom de fichier doit être autre chose qu'un point";
+            }
+            if( this.cantSubmit ) {
               this.$input
               .removeClass('input-success input-warning')
               .addClass('input-error');
-              this.cantSubmit = 'Impossible de valider: extension ' + ext + ' invalide (autorisées: .html/.js/.css)';
               return;
             }
           }

+ 19 - 5
sandboxApp.js

@@ -94,6 +94,13 @@ function checkBodyPropsExist(props) {
   };
 }
 
+function checkFilename(filename) {
+  const base = path.basename(filename);
+  let ext    = path.extname(filename);
+  ext = ext ? ext.toLowerCase().substr(1) : ext;
+  return base && ['html', 'js', 'css'].indexOf( ext ) > -1;
+}
+
 /**
  * Get repo parts
  */
@@ -131,11 +138,11 @@ app.get('/:repoSlug/:exampleSlug', getIndexExample);
 
 function checkRepoExists(req, res, next) {
   const { repoSlug } = req.params;
-console.log('### checkRepoExists', repoSlug);
+  console.log('### checkRepoExists', repoSlug);
   // Get repo from store
   req.repo = exStore.getRepo(repoSlug);
   if(! req.repo) {
-    res.status(404).send("Repo " + repoSlug + "not found");
+    return res.status(404).send("Repo " + repoSlug + "not found");
   }
   next();
 }
@@ -181,19 +188,26 @@ app.post('/:repoSlug/examples/:exampleSlug/file',
     if(! re.test(name)) {
       return res.status(400).json('Le paramètre `name` est incorrect: caractères autorisés: lettres, chiffres, _, - et .' );
     }
+    if( ! checkFilename(name)) {
+      return res.status(400).json("Le paramètre `name` est incorrect: il doit comporter un nom suivi d'une extension (.html, .js ou .css)" );
+    }
     fs.statAsync(fullPath)
     // Invert the usual flow of a Promise. fs.stat() fails if file does not exist (which is what we want)
     // Hence .catch() is a success handler and .then() an error handler (has to rethrow)
+    .then(stats => {
+      throw new Error('Le fichier `' + name + '` existe déjà !');
+    })
     .catch(err => {
+      // Rethrow error if it is not a "file not found" thrown by fs.stat()
+      if( ! err.message.startsWith('ENOENT') ) {
+        throw err;
+      }
       return fs.writeFileAsync(fullPath, '')
       .then(() => res.json({
         name,
         path: fullPath
       }));
     })
-    .then(stats => {
-      throw new Error('Le fichier `' + name + '` existe déjà !');
-    })
     .catch(err => {
       const statusCode = err.message.startsWith('Le fichier') ? 409 : 500;
       return res.status(statusCode).send(err.message);