-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(deno): support for custom
allow
permissions (#284)
* feat(deno): support for custom `allow` permissions * docs: add `deno.allow` documentation
- Loading branch information
1 parent
8245cf4
commit 4e551f6
Showing
7 changed files
with
271 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,116 @@ | ||
/* c8 ignore start */ | ||
import process from 'node:process'; | ||
|
||
const [, , ...args] = process.argv; | ||
const [, , ...processArgs] = process.argv; | ||
|
||
export const getArg = (arg: string): string | undefined => { | ||
const getArg = args.find((a) => a.startsWith(`--${arg}=`)); | ||
if (getArg) return getArg.split('=')?.[1] || undefined; | ||
/** | ||
* Gets the value of an argument. | ||
* | ||
* --- | ||
* | ||
* CLI arguments examples: | ||
* | ||
* ```sh | ||
* command --arg=some # 'some' | ||
* command --arg="" # '' | ||
* command --arg # undefined | ||
* ``` | ||
*/ | ||
export const getArg = (arg: string, prefix = '--'): string | undefined => { | ||
const mountArg = processArgs.find((a) => a.startsWith(`${prefix}${arg}=`)); | ||
if (!mountArg) return undefined; | ||
|
||
return undefined; | ||
return mountArg.split('=')?.[1].replace(/''|""/, ''); | ||
}; | ||
|
||
export const hasArg = (arg: string): boolean => | ||
args.some((a) => a.startsWith(`--${arg}`)); | ||
/** | ||
* Parses all arguments of an argument value. | ||
* | ||
* --- | ||
* | ||
* CLI arguments examples: | ||
* | ||
* ```sh | ||
* command --arg='--sub=some' # ['--sub=some'] | ||
* command --arg='--sub=some, --sub2' # ['--sub=some', '--sub2'] | ||
* ``` | ||
*/ | ||
export const getSubArg = (arg: string, prefix = '--') => { | ||
if (hasArg(arg) && !getArg(arg)?.[1]) return []; | ||
|
||
export const getLastParam = (): string => { | ||
return args[args.length - 1]; | ||
return processArgs | ||
.find((a) => a.startsWith(`${prefix}${arg}=`)) | ||
?.split(`--${arg}=`)[1] | ||
.split(',') | ||
.map((a) => a.trim()) | ||
.filter((a) => a && !/''|""/.test(a)); | ||
}; | ||
|
||
/** | ||
* Checks if an argument exists. | ||
* | ||
* --- | ||
* | ||
* CLI arguments examples: | ||
* | ||
* ```sh | ||
* command --arg # true | ||
* command # false | ||
* ``` | ||
*/ | ||
export const hasArg = (arg: string, prefix = '--'): boolean => | ||
processArgs.some((a) => a.startsWith(`${prefix}${arg}`)); | ||
|
||
/** | ||
* Gets the last param/value. | ||
* | ||
* CLI arguments examples: | ||
* | ||
* ```sh | ||
* command --arg --arg2=some value # 'value' | ||
* command value # 'value' | ||
* command # undefined | ||
* command --arg # undefined | ||
* ``` | ||
*/ | ||
export const getLastParam = (prefix = '--'): string | undefined => { | ||
const lastArg = processArgs[processArgs.length - 1]; | ||
|
||
if (!lastArg || lastArg.startsWith(prefix)) return undefined; | ||
|
||
return lastArg; | ||
}; | ||
|
||
// TODO (Custom Args) | ||
// export const getAllArgs = (arg: string, prefix = '--'): string[] => { | ||
// return processArgs | ||
// .filter((a) => a.startsWith(`${prefix}${arg}=`) || a === `${prefix}${arg}`) | ||
// .map((a) => { | ||
// const [key, ...value] = a.split('='); | ||
// return value.length > 0 ? value.join('=') : key; | ||
// }); | ||
// }; | ||
|
||
// TODO (Custom Args) | ||
// export const setArgs = ( | ||
// args: (string | Record<string, string>)[], | ||
// options?: { prefix: string } | ||
// ): string[] => { | ||
// const customArgs: string[] = []; | ||
// const prefix = options?.prefix || ''; | ||
|
||
// args.forEach((arg) => { | ||
// if (!Array.isArray(arg) && typeof arg === 'object') { | ||
// for (const key in arg) { | ||
// customArgs.push(`${prefix}${key}=${arg[key]}`); | ||
// } | ||
|
||
// return; | ||
// } | ||
|
||
// customArgs.push(`${prefix}${arg}`); | ||
// }); | ||
|
||
// return customArgs; | ||
// }; | ||
/* c8 ignore stop */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { assert, describe, test } from '../../../src/index.js'; | ||
import { runner } from '../../../src/helpers/runner.js'; | ||
|
||
describe('Deno Security Arguments', { background: false, icon: '🔬' }); | ||
|
||
test(() => { | ||
assert.deepStrictEqual( | ||
runner('', { | ||
platform: 'deno', | ||
}), | ||
[ | ||
'deno', | ||
'run', | ||
'--allow-read', | ||
'--allow-env', | ||
'--allow-run', | ||
'--allow-net', | ||
], | ||
'Default Permissions' | ||
); | ||
|
||
assert.deepStrictEqual( | ||
runner('', { | ||
platform: 'deno', | ||
deno: { | ||
allow: ['read'], | ||
}, | ||
}), | ||
['deno', 'run', '--allow-read'], | ||
'Custom Permission' | ||
); | ||
|
||
assert.deepStrictEqual( | ||
runner('', { | ||
platform: 'deno', | ||
deno: { | ||
allow: ['read', 'env'], | ||
}, | ||
}), | ||
['deno', 'run', '--allow-read', '--allow-env'], | ||
'Custom Permissions' | ||
); | ||
|
||
assert.deepStrictEqual( | ||
runner('', { | ||
platform: 'deno', | ||
deno: { | ||
allow: ['read="file.js"', 'env'], | ||
}, | ||
}), | ||
['deno', 'run', '--allow-read="file.js"', '--allow-env'], | ||
'Custom Permissions per Files' | ||
); | ||
|
||
assert.deepStrictEqual( | ||
runner('', { | ||
platform: 'deno', | ||
deno: { | ||
allow: [], | ||
}, | ||
}), | ||
['deno', 'run'], | ||
'No Permissions' | ||
); | ||
}); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
--- | ||
sidebar_position: 98 | ||
--- | ||
|
||
# `deno` | ||
|
||
## `allow` | ||
|
||
> `poku(targetPaths: string | string[], configs?: Configs)` | ||
> | ||
> `allow: string[]` | ||
Change permissions for **Deno**. | ||
|
||
By default **Poku** uses `--allow-run`, `--allow-env`, `--allow-read` and `--allow-net`. | ||
|
||
### API (_in-code_) | ||
|
||
```ts | ||
poku(['...'], { | ||
deno: { | ||
allow: ['read', 'run' /* ... */], | ||
}, | ||
}); | ||
``` | ||
|
||
```ts | ||
poku(['...'], { | ||
deno: { | ||
allow: ['read=file.js', 'run' /* ... */], | ||
}, | ||
}); | ||
``` | ||
|
||
Clear all permissions: | ||
|
||
```ts | ||
poku(['...'], { | ||
deno: { | ||
allow: [], | ||
}, | ||
}); | ||
``` | ||
|
||
### CLI | ||
|
||
```bash | ||
npx poku --deno-allow='read, run' ./test | ||
``` | ||
|
||
```bash | ||
npx poku --deno-allow='read=file.js, run' ./test | ||
``` | ||
|
||
Clear all permissions: | ||
|
||
```bash | ||
npx poku --deno-allow='' ./test | ||
``` |