Skip to content

Commit

Permalink
add realpath (#319)
Browse files Browse the repository at this point in the history
* add realpath

* add test case for symbols

* check range is correct as well
  • Loading branch information
CraigglesO committed Aug 11, 2022
1 parent 8c96e46 commit 810ceda
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/storage-file/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ export async function readFileRange(
let fd: fs.FileHandle | null = null;
let res: Buffer;
try {
// adjust for symbolic links
filePath = await fs.realpath(filePath);
const { size } = await fs.lstat(filePath);
// build offset and length as necessary
if (suffix !== undefined) {
Expand Down
14 changes: 14 additions & 0 deletions packages/storage-file/test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,20 @@ test("FileStorage: getRangeMaybeExpired: suffix: returns partial values", async
t.is(utf8Decode(getAll?.value), "123456789");
t.deepEqual(getAll?.range, { offset: 0, length: 9 });
});
test("FileStorage: getRangeMaybeExpired: check that symbolic links are resolved appropriately", async (t) => {
const storage = await storageFactory.factory(t, {});
await storage.put("inner/key", { value: utf8Encode("value") });
// create the symbolic link
await fs.symlink(
// @ts-ignore
path.join(storage.root, "inner/key"),
// @ts-ignore
path.join(storage.root, "key")
);
const getResult = await storage.getRangeMaybeExpired("key", { offset: 0 });
t.is(utf8Decode(getResult?.value), "value");
t.deepEqual(getResult?.range, { offset: 0, length: 5 });
});

async function unsanitisedStorageFactory(
t: ExecutionContext
Expand Down

0 comments on commit 810ceda

Please sign in to comment.