Skip to content

Commit

Permalink
[INTERNAL] Adapters: Fix if-clause checking for matching base path
Browse files Browse the repository at this point in the history
AbstractAdapter#_resolveVirtualPathToBase might return an empty string
in case the root path of the adapter is matched. Therefore, adapters
must only reject the path if the method returned 'null'.
  • Loading branch information
RandomByte committed Feb 3, 2023
1 parent d65f5ce commit 12a47f8
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 4 deletions.
6 changes: 4 additions & 2 deletions lib/adapters/FileSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class FileSystem extends AbstractAdapter {
promises.push(new Promise((resolve, reject) => {
const virPath = (this._virBasePath + matches[i]);
const relPath = this._resolveVirtualPathToBase(virPath);
if (!relPath) {
if (relPath === null) {
// Match is likely outside adapter base path
log.verbose(
`Failed to resolve virtual path of glob match '${virPath}': Path must start with ` +
Expand Down Expand Up @@ -154,9 +154,11 @@ class FileSystem extends AbstractAdapter {
async _byPath(virPath, options, trace) {
const relPath = this._resolveVirtualPathToBase(virPath);

if (!relPath) {
if (relPath === null) {
// Neither starts with basePath, nor equals baseDirectory
if (!options.nodir && this._virBasePath.startsWith(virPath)) {
// Create virtual directories for the virtual base path (which has to exist)
// TODO: Maybe improve this by actually matching the base paths segments to the virPath
return this._createResource({
project: this._project,
statInfo: { // TODO: make closer to fs stat info
Expand Down
2 changes: 1 addition & 1 deletion lib/adapters/Memory.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ class Memory extends AbstractAdapter {
*/
async _byPath(virPath, options, trace) {
const relPath = this._resolveVirtualPathToBase(virPath);
if (!relPath) {
if (relPath === null) {
return null;
}

Expand Down
54 changes: 53 additions & 1 deletion test/lib/adapters/FileSystem_read.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ test("glob virtual directory above virtual base path (path traversal)", async (t
t.is(res.length, 0, "Returned no resources");
});


test("byPath", async (t) => {
const readerWriter = createAdapter({
fsBasePath: "./test/fixtures/application.a/webapp",
Expand All @@ -134,6 +133,59 @@ test("byPath virtual directory above base path (path traversal)", async (t) => {
t.is(resource, null, "Found no resource");
});

test("byPath: Root dir w/ trailing slash", async (t) => {
const readerWriter = createAdapter({
fsBasePath: "./test/fixtures/application.a/webapp",
virBasePath: "/resources/app/"
});

const resource = await readerWriter.byPath("/resources/app/", {nodir: false});
t.truthy(resource, "Found one resource");
});

test("byPath: Root dir w/o trailing slash", async (t) => {
const readerWriter = createAdapter({
fsBasePath: "./test/fixtures/application.a/webapp",
virBasePath: "/resources/app/"
});

const resource = await readerWriter.byPath("/resources/app", {nodir: false});
t.truthy(resource, "Found one resource");
});

test("byPath: Virtual directory w/ trailing slash", async (t) => {
const readerWriter = createAdapter({
fsBasePath: "./test/fixtures/application.a/webapp",
virBasePath: "/resources/app/"
});

const resource = await readerWriter.byPath("/resources/", {nodir: false});
t.truthy(resource, "Found one resource");
});

test("byPath: Virtual directory w/o trailing slash", async (t) => {
const readerWriter = createAdapter({
fsBasePath: "./test/fixtures/application.a/webapp",
virBasePath: "/resources/app/"
});

const resource = await readerWriter.byPath("/resources", {nodir: false});
t.truthy(resource, "Found one resource");
});

test("byPath: Incorrect virtual directory w/o trailing slash", async (t) => {
// Adding a trailing slash would make the path not match the base path, which is already
// tested elsewhere
const readerWriter = createAdapter({
fsBasePath: "./test/fixtures/application.a/webapp",
virBasePath: "/resources/app/"
});

// TODO: This should actually not match
const resource = await readerWriter.byPath("/resour", {nodir: false});
t.truthy(resource, "Found one resource");
});

function getPathFromResource(resource) {
return resource.getPath();
}
Expand Down

0 comments on commit 12a47f8

Please sign in to comment.