Просмотр исходного кода

fix: #153 Bag: Path including round bracket makes something bad

escape round brackets when using RegExp
Yuki Takei 8 лет назад
Родитель
Сommit
8c806e022f
1 измененных файлов с 9 добавлено и 2 удалено
  1. 9 2
      lib/models/page.js

+ 9 - 2
lib/models/page.js

@@ -661,7 +661,7 @@ module.exports = function(crowi) {
     var pathCondition = [];
     var includeDeletedPage = option.includeDeletedPage || false;
 
-    var queryReg = new RegExp('^' + path);
+    var queryReg = new RegExp('^' + escapeRoundBrackets(path));
     pathCondition.push({path: queryReg});
     if (path.match(/\/$/)) {
       debug('Page list by ending with /, so find also upper level page');
@@ -1090,7 +1090,7 @@ module.exports = function(crowi) {
   pageSchema.statics.renameRecursively = function(pageData, newPagePathPrefix, user, options) {
     var Page = this
       , path = pageData.path
-      , pathRegExp = new RegExp('^' + path, 'i');
+      , pathRegExp = new RegExp('^' + escapeRoundBrackets(path), 'i');
 
     return new Promise(function(resolve, reject) {
       Page
@@ -1113,6 +1113,13 @@ module.exports = function(crowi) {
     return;
   };
 
+  function escapeRoundBrackets(path) {
+    // replace
+    //   '(' -> '\('
+    //   ')' -> '\)'
+    return path.replace(/(\(|\))/g, '\\$1');
+  }
+
   pageSchema.statics.GRANT_PUBLIC = GRANT_PUBLIC;
   pageSchema.statics.GRANT_RESTRICTED = GRANT_RESTRICTED;
   pageSchema.statics.GRANT_SPECIFIED = GRANT_SPECIFIED;