Permalink
Browse files

Filesystem API: Read a file's contents.

  • Loading branch information...
chriseppstein committed Oct 23, 2015
1 parent 893d965 commit 4ebc47cd309720510917a791936241698f7eec7c
Showing with 74 additions and 0 deletions.
  1. +7 −0 docs/filesystem/index.md
  2. +15 −0 lib/functions/fs.js
  3. +4 −0 sass/fs.scss
  4. +1 −0 test/fixtures/filesystem/a.txt
  5. +47 −0 test/test_fs.js
@@ -146,6 +146,13 @@ following keys:
This function will fail with an error if sandboxing is enabled (See
below) and the path is outside the sandbox.
#### `fs-read-file($filename)`
Returns the contents of a file as a string.
This function will fail with an error if sandboxing is enabled (See
below) and the path is outside the sandbox.
### Security
By default, sass code is running locally on a dev machine running code
@@ -159,6 +159,21 @@ module.exports = function(eyeglass, sass) {
} else {
done(sass.types.Error("Security violation: Cannot access " + filename));
}
},
"eyeglass-fs-read-file($filename)": function(sassFilename, done) {
var filename = sassFilename.getValue();
if (inSandbox(filename)) {
fs.readFile(filename, function(err, contents) {
if (err) {
done(sass.types.Error(err.message));
} else {
done(sassUtils.castToSass(contents.toString()));
}
});
} else {
done(sass.types.Error("Security violation: Cannot access " + filename));
}
}
};
};
@@ -38,3 +38,7 @@ $fs-path-separator: eyeglass-fs-path-separator();
@function fs-info($filename) {
@return eyeglass-fs-info($filename);
}
@function fs-read-file($filename) {
@return eyeglass-fs-read-file($filename);
}
@@ -0,0 +1 @@
contents of a.txt
@@ -354,4 +354,51 @@ describe("fs", function () {
testutils.assertCompilationError(eg, expectedError, done);
});
it("can read a file", function (done) {
var rootDir = testutils.fixtureDirectory("filesystem");
var input = "@import 'fs(root)';" +
"$contents: fs-read-file(fs-absolute-path(root, 'a.txt'));" +
"fs {\n" +
" contents: $contents;\n" +
"}";
var file = path.join(rootDir, "a.txt");
var contents = fs.readFileSync(file);
var expected = "fs {\n" +
" contents: " + contents.toString() + "; }\n";
var eg = new Eyeglass({
root: rootDir,
data: input,
file: path.join(rootDir, "uses_mod_1.scss"),
fsSandbox: true
}, sass);
testutils.assertCompiles(eg, expected, done);
});
it("cannot get info about a path that's outside the sandbox", function (done) {
var rootDir = testutils.fixtureDirectory("filesystem");
var input = "@import 'fs(root)';" +
"fs {" +
" error: fs-info(fs-absolute-path(root, '..', '..', 'foo.txt'));" +
"}";
var expectedError = {
message: "Security violation: Cannot access " + path.resolve(rootDir, "..", "..", "foo.txt")
};
var eg = new Eyeglass({
root: rootDir,
data: input,
file: path.join(rootDir, "haserror.scss"),
fsSandbox: true
}, sass);
testutils.assertCompilationError(eg, expectedError, done);
});
});

0 comments on commit 4ebc47c

Please sign in to comment.