Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-3702

printShardingStatus interprets database name as regular expression

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Minor - P4 Minor - P4
    • 2.1.1
    • Affects Version/s: None
    • Component/s: Sharding, Shell
    • Labels:
      None
    • ALL

      When running db.printShardingStatus(), you can get unexpected behaviour if you have database names with characters which are interpreted by regular expressions. This could cause the output to be incorrect or, worse, for the function to not work at all due to an invalid regular expression.

      I think in my case I had a database with an asterisk in it or with square brackets and was getting a regexp error in shell/server.js when I ran the function. I'm a little confused as to exactly what the problematic name was now because I've deleted them.

      I'm not sure whether these database names are valid or not. They don't seem to show up in "show dbs" but they were certainly present in the "config.databases" collection.

      I would suggest that the printShardingStatus() function should be quoting any string it places into a regular expression, regardless of the range of valid values. JavaScript doesn't seem to provide a function to do this, but an example of a function that could be used is here:

      http://80.68.89.23/2006/Jan/20/escape/

      The function itself is as follows:

      RegExp.escape = function(text) {
          return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
      }
      

      Obviously this doesn't have to be stored in the RegExp function itself but it would probably be a good idea for MongoDB to provide something like this.

      If it declared as above then the change in shell/server.js is from:

      configDB.collections.find( { _id : new RegExp( "^" + db._id + "\." ) } ).sort( { _id : 1 } ).forEach(
      

      To:

      configDB.collections.find( { _id : new RegExp( "^" + RegExp.escape(db._id) + "\." ) } ).sort( { _id : 1 } ).forEach(
      

      Or just declaring this function locally:

      var regExpEscape = function(text) {
          return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
      }
      configDB.collections.find( { _id : new RegExp( "^" + regExpEscape(db._id) + "\." ) } ).sort( { _id : 1 } ).forEach(
      

      I have a feeling there is another bug here as well, and that these database names are not valid. If this is the case then they shouldn't be ending up in my config.databases collection. I'm not sure how they were created, however, only that they somehow were.

            Assignee:
            randolph@mongodb.com Randolph Tan
            Reporter:
            jamespharaoh James Pharaoh
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: