Permalink
Browse files

don't accept RegExp grammar in vhost's hostname

  • Loading branch information...
1 parent 9da8ac9 commit 82a7ab7db4c41d2999f26929ba60a326751e89aa Tomasz Elendt committed Feb 11, 2013
Showing with 16 additions and 4 deletions.
  1. +3 −3 lib/middleware/vhost.js
  2. +13 −1 test/vhost.js
View
@@ -8,7 +8,7 @@
/**
* Vhost:
- *
+ *
* Setup vhost for the given `hostname` and `server`.
*
* connect()
@@ -17,7 +17,7 @@
* .use(connect.vhost('*.com', mainApp))
*
* The `server` may be a Connect server or
- * a regular Node `http.Server`.
+ * a regular Node `http.Server`.
*
* @param {String} hostname
* @param {Server} server
@@ -28,7 +28,7 @@
module.exports = function vhost(hostname, server){
if (!hostname) throw new Error('vhost hostname required');
if (!server) throw new Error('vhost server required');
- var regexp = new RegExp('^' + hostname.replace(/[*]/g, '(.*?)') + '$', 'i');
+ var regexp = new RegExp('^' + hostname.replace(/[^*\w]/g, '\\$&').replace(/[*]/g, '(?:.*?)') + '$', 'i');
if (server.onvhost) server.onvhost(hostname);
return function vhost(req, res, next){
if (!req.headers.host) return next();
View
@@ -61,4 +61,16 @@ describe('connect.vhost()', function(){
.set('Host', 'ferrets.com')
.expect(404, done);
})
-})
+
+ it('should treat dot as a dot', function(done){
+ var app = connect()
+ , tobi = http.createServer(function(req, res){ res.end('tobi') })
+
+ app.use(connect.vhost('a.b.com', tobi));
+
+ app.request()
+ .get('/')
+ .set('Host', 'aXb.com')
+ .expect(404, done);
+ })
+})

0 comments on commit 82a7ab7

Please sign in to comment.