Skip to content

Commit

Permalink
feat: add BROWSERSLIST_ROOT_PATH (#819)
Browse files Browse the repository at this point in the history
* feat: add BROWSERSLIST_ROOT_PATH

- check that loc is inside BROWSERSLIST_ROOT_PATH in eachParent
- add tests
- update Readme

* fix linter errors
  • Loading branch information
teleclimber committed Feb 14, 2024
1 parent 8ddc4d8 commit b58ae05
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Expand Up @@ -683,6 +683,12 @@ with [environment variables]:
BROWSERSLIST_DANGEROUS_EXTEND=1 npx webpack
```
* `BROWSERSLIST_ROOT_PATH` to prevent reading files above this path.
```sh
BROWSERSLIST_ROOT_PATH=. npx webpack
```
[environment variables]: https://en.wikipedia.org/wiki/Environment_variable
Expand Down
1 change: 1 addition & 0 deletions index.d.ts
Expand Up @@ -193,6 +193,7 @@ declare global {
BROWSERSLIST_ENV?: string
BROWSERSLIST_IGNORE_OLD_DATA?: string
BROWSERSLIST_STATS?: string
BROWSERSLIST_ROOT_PATH?: string
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions node.js
Expand Up @@ -50,12 +50,22 @@ function eachParent(file, callback) {
var dir = isFile(file) ? path.dirname(file) : file
var loc = path.resolve(dir)
do {
if (!pathInRoot(loc)) break;
var result = callback(loc)
if (typeof result !== 'undefined') return result
} while (loc !== (loc = path.dirname(loc)))
return undefined
}

function pathInRoot(p) {
if (!process.env.BROWSERSLIST_ROOT_PATH) return true
var rootPath = path.resolve(process.env.BROWSERSLIST_ROOT_PATH);
if (path.relative(rootPath, p).substring(0,2) === '..') {
return false;
}
return true
}

function check(section) {
if (Array.isArray(section)) {
for (var i = 0; i < section.length; i++) {
Expand Down
12 changes: 12 additions & 0 deletions test/config.test.js
Expand Up @@ -120,4 +120,16 @@ test('reads config with one string', () => {
equal(browserslist.findConfig(STRING), { defaults: 'ie 9, ie 8' })
})

test('stops at ROOT', () => {
browserslist.clearCaches()
process.env.BROWSERSLIST_ROOT_PATH = join(__dirname, 'fixtures', 'dir')
equal(browserslist.findConfig(FILE), undefined)
})

test('allows up to ROOT', () => {
browserslist.clearCaches()
process.env.BROWSERSLIST_ROOT_PATH = join(__dirname, 'fixtures')
equal(browserslist.findConfig(FILE), { defaults: ['ie 11', 'ie 10'] })
})

test.run()
1 change: 1 addition & 0 deletions test/main.test.js
Expand Up @@ -21,6 +21,7 @@ test.after.each(() => {
delete process.env.BROWSERSLIST
delete process.env.BROWSERSLIST_CONFIG
delete process.env.BROWSERSLIST_ENV
delete process.env.BROWSERSLIST_ROOT_PATH
})

test('accepts array', () => {
Expand Down

0 comments on commit b58ae05

Please sign in to comment.