Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add BROWSERSLIST_ROOT_PATH #819

Merged
merged 2 commits into from Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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