@@ -51,18 +51,34 @@ module.exports = function resolve(x, options, callback) {
5151
5252 opts . paths = opts . paths || [ ] ;
5353
54- if ( opts . basedir ) {
55- var basedirError = new TypeError ( 'Provided basedir "' + opts . basedir + '" is not a directory' ) ;
56- isDirectory ( opts . basedir , function ( err , result ) {
57- if ( err ) return cb ( err ) ;
58- if ( ! result ) { return cb ( basedirError ) ; }
59- validBasedir ( ) ;
54+ // ensure that `basedir` is an absolute path at this point, resolving against the process' current working directory
55+ var absoluteStart = path . resolve ( basedir ) ;
56+
57+ if ( ! opts || ! opts . preserveSymlinks ) {
58+ fs . realpath ( absoluteStart , function ( realPathErr , realStart ) {
59+ if ( realPathErr && realPathErr . code !== 'ENOENT' ) cb ( err ) ;
60+ else validateBasedir ( realPathErr ? absoluteStart : realStart ) ;
6061 } ) ;
6162 } else {
62- validBasedir ( ) ;
63+ validateBasedir ( absoluteStart ) ;
64+ }
65+
66+ function validateBasedir ( basedir ) {
67+ if ( opts . basedir ) {
68+ var dirError = new TypeError ( 'Provided basedir "' + basedir + '" is not a directory' + ( opts . preserveSymlinks ? '' : ', or a symlink to a directory' ) ) ;
69+ dirError . code = 'INVALID_BASEDIR' ;
70+ isDirectory ( basedir , function ( err , result ) {
71+ if ( err ) return cb ( err ) ;
72+ if ( ! result ) { return cb ( dirError ) ; }
73+ validBasedir ( basedir ) ;
74+ } ) ;
75+ } else {
76+ validBasedir ( basedir ) ;
77+ }
6378 }
79+
6480 var res ;
65- function validBasedir ( ) {
81+ function validBasedir ( basedir ) {
6682 if ( ( / ^ (?: \. \. ? (?: \/ | $ ) | \/ | ( [ A - Z a - z ] : ) ? [ / \\ ] ) / ) . test ( x ) ) {
6783 res = path . resolve ( basedir , x ) ;
6884 if ( x === '..' || x . slice ( - 1 ) === '/' ) res += '/' ;
0 commit comments