-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert the fs module and its example(s) to use the recently-introduced core module.
- Loading branch information
1 parent
d8c7c6d
commit 99cc59d
Showing
5 changed files
with
106 additions
and
39 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { visit } from '@slangroom/fs/visitor'; | ||
import { AfterPlugin } from '@slangroom/core/plugin'; | ||
import * as path from 'node:path'; | ||
import * as fs from 'node:fs/promises'; | ||
|
||
/** | ||
* A directory that is safe to write to and read from. | ||
* | ||
* Care must be taken to not allow writes and reads outside of this directory. | ||
*/ | ||
export const SandboxDir = '/tmp/slangroom'; | ||
|
||
export const ThenISaveStringIntoTheFile = new AfterPlugin(async ({ statement, result }) => { | ||
const ast = visit(statement); | ||
// TODO: if `visit()` fails, exit (return) | ||
|
||
const filename = result[ast.filename] as string; | ||
const content = result[ast.content] as string; | ||
const normalized = path.normalize(filename); | ||
// "/" and ".." prevents directory traversal | ||
const does_directory_traversal = normalized.startsWith('/') || normalized.startsWith('..'); | ||
// "." ensures that "foo/bar" or "./foo" is valid, while "." isn't | ||
// (that is, no "real" filepath is provided) | ||
const doesnt_provide_file = normalized.startsWith('.'); | ||
if (does_directory_traversal || doesnt_provide_file) return; // TODO: instead of ignoring, do we wanna error out? | ||
// here onward, we're sure `filepath` is under `SandboxDir` | ||
const filepath = path.join(SandboxDir, normalized); | ||
|
||
// these following two lines allow subdirs to be created if | ||
// `normalized` contains any (or `SandboxDir` to be created if it | ||
// doesn't exist already) | ||
const dirname = path.dirname(filepath); | ||
await fs.mkdir(dirname, { recursive: true }); | ||
|
||
const fhandle = await fs.open(filepath, 'wx'); | ||
await fhandle.write(content); | ||
}); | ||
|
||
export const allPlugins = new Set([ThenISaveStringIntoTheFile]); |
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,53 @@ | ||
import test from 'ava'; | ||
import { allPlugins, SandboxDir } from '@slangroom/fs/plugins'; | ||
import { Slangroom } from '@slangroom/core'; | ||
import * as fs from 'node:fs/promises'; | ||
import * as path from 'node:path'; | ||
|
||
test('requires a real path', async (t) => { | ||
const slang = new Slangroom(allPlugins); | ||
const contract = `Rule unknown ignore | ||
Given I have a 'string' named 'nameOfTheFile' | ||
When I create the random object of '64' bits | ||
When I rename the 'random_object' to 'stringToWrite' | ||
Then I save the 'stringToWrite' into the file 'nameOfTheFile' | ||
Then I print the 'stringToWrite' | ||
Then I print the 'nameOfTheFile' | ||
`; | ||
{ | ||
const params = { data: { nameOfTheFile: '../trying/to/h4ck' } }; | ||
const zout = await slang.execute(contract, params); | ||
const nameOfTheFile = zout.result['nameOfTheFile'] as string; | ||
await t.throwsAsync(fs.readFile(path.resolve(SandboxDir, nameOfTheFile))); | ||
} | ||
|
||
{ | ||
const params = { data: { nameOfTheFile: '/trying/to/h4ck' } }; | ||
const zout = await slang.execute(contract, params); | ||
const nameOfTheFile = zout.result['nameOfTheFile'] as string; | ||
await t.throwsAsync(fs.readFile(path.resolve(SandboxDir, nameOfTheFile))); | ||
} | ||
}); | ||
|
||
test('keyholder works', async (t) => { | ||
const slang = new Slangroom(allPlugins); | ||
const contract = `Rule unknown ignore | ||
Given I have a 'string' named 'nameOfTheFile' | ||
When I create the random object of '64' bits | ||
When I rename the 'random_object' to 'stringToWrite' | ||
Then I save the 'stringToWrite' into the file 'nameOfTheFile' | ||
Then I print the 'stringToWrite' | ||
Then I print the 'nameOfTheFile' | ||
`; | ||
const randomStr = (Math.random() + 1).toString(36).substring(7); | ||
const params = { data: { nameOfTheFile: randomStr } }; | ||
const zout = await slang.execute(contract, params); | ||
const nameOfTheFile = zout.result['nameOfTheFile'] as string; | ||
const stringToWrite = zout.result['stringToWrite'] as string; | ||
const buf = await fs.readFile(path.resolve(SandboxDir, nameOfTheFile)); | ||
t.is(buf.toString(), stringToWrite); | ||
}); |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.