Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support directories named __proto__ #945

Merged
merged 3 commits into from
Sep 15, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/__tests__/volume/mkdirSync.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { create } from '../util';
import type Stats from '../../Stats';

describe('mkdirSync', () => {
it('can create a directory', () => {
Expand Down Expand Up @@ -50,4 +51,22 @@ describe('mkdirSync', () => {
expect(error).toBeInstanceOf(Error);
expect(error.message).toMatchSnapshot();
});

/**
* See issue #938
* https://github.com/streamich/memfs/issues/938
*/
it('can create a directory with name "__proto__"', () => {
const vol = create();

vol.mkdirSync('/__proto__');
let isDirectory = false;
try {
isDirectory = vol.statSync('/__proto__').isDirectory();
} catch {
// ignore
}

expect(isDirectory).toBe(true);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be better to just let the error throw, since if this test fails because of that the first thing you're going to want to do is see that error

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was copying the test spec from existing test cases. But yes, throwing the error does make sense.

Copy link
Contributor Author

@SukkaW SukkaW Sep 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 2394a8e (#945).

});
});
20 changes: 9 additions & 11 deletions src/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ export class Link extends EventEmitter {

parent: Link;

children: { [child: string]: Link | undefined } = {};
children = new Map<string, Link | undefined>();

// Path to this node as Array: ['usr', 'bin', 'node'].
private _steps: string[] = [];
Expand All @@ -331,7 +331,7 @@ export class Link extends EventEmitter {
// Recursively sync children steps, e.g. in case of dir rename
set steps(val) {
this._steps = val;
for (const [child, link] of Object.entries(this.children)) {
for (const [child, link] of this.children.entries()) {
if (child === '.' || child === '..') {
continue;
}
Expand Down Expand Up @@ -361,7 +361,7 @@ export class Link extends EventEmitter {
link.setNode(node);

if (node.isDirectory()) {
link.children['.'] = link;
link.children.set('.', link);
link.getNode().nlink++;
}

Expand All @@ -371,13 +371,13 @@ export class Link extends EventEmitter {
}

setChild(name: string, link: Link = new Link(this.vol, this, name)): Link {
this.children[name] = link;
this.children.set(name, link);
link.parent = this;
this.length++;

const node = link.getNode();
if (node.isDirectory()) {
link.children['..'] = this;
link.children.set('..', this);
this.getNode().nlink++;
}

Expand All @@ -390,10 +390,10 @@ export class Link extends EventEmitter {
deleteChild(link: Link) {
const node = link.getNode();
if (node.isDirectory()) {
delete link.children['..'];
link.children.delete('..');
this.getNode().nlink--;
}
delete this.children[link.getName()];
this.children.delete(link.getName());
this.length--;

this.getNode().mtime = new Date();
Expand All @@ -402,9 +402,7 @@ export class Link extends EventEmitter {

getChild(name: string): Link | undefined {
this.getNode().mtime = new Date();
if (Object.hasOwnProperty.call(this.children, name)) {
return this.children[name];
}
return this.children.get(name);
}

getPath(): string {
Expand Down Expand Up @@ -446,7 +444,7 @@ export class Link extends EventEmitter {
return {
steps: this.steps,
ino: this.ino,
children: Object.keys(this.children),
children: Array.from(this.children.keys()),
};
}

Expand Down
24 changes: 13 additions & 11 deletions src/volume.ts
Original file line number Diff line number Diff line change
Expand Up @@ -528,11 +528,13 @@ export class Volume implements FsCallbackApi {
let children = link.children;

if (link.getNode().isFile()) {
children = { [link.getName()]: link.parent.getChild(link.getName()) };
children = new Map([
[link.getName(), link.parent.getChild(link.getName())]
]);
link = link.parent;
}

for (const name in children) {
for (const name of children.keys()) {
if (name === '.' || name === '..') {
continue;
}
Expand Down Expand Up @@ -568,7 +570,7 @@ export class Volume implements FsCallbackApi {
const links: Link[] = [];

if (paths) {
if (!(paths instanceof Array)) paths = [paths];
if (!Array.isArray(paths)) paths = [paths];
for (const path of paths) {
const filename = pathToFilename(path);
const link = this.getResolvedLink(filename);
Expand Down Expand Up @@ -1346,7 +1348,7 @@ export class Volume implements FsCallbackApi {

if (options.withFileTypes) {
const list: Dirent[] = [];
for (const name in link.children) {
for (const name of link.children.keys()) {
const child = link.getChild(name);

if (!child || name === '.' || name === '..') {
Expand All @@ -1365,7 +1367,7 @@ export class Volume implements FsCallbackApi {
}

const list: TDataOut[] = [];
for (const name in link.children) {
for (const name of link.children.keys()) {
if (name === '.' || name === '..') {
continue;
}
Expand Down Expand Up @@ -2406,23 +2408,23 @@ export class FSWatcher extends EventEmitter {
removers.forEach(r => r());
this._listenerRemovers.delete(ino);
}
Object.entries(curLink.children).forEach(([name, childLink]) => {
for (const [name, childLink] of curLink.children.entries()) {
if (childLink && name !== '.' && name !== '..') {
removeLinkNodeListeners(childLink);
}
});
};
};
removeLinkNodeListeners(l);

this.emit('change', 'rename', relative(this._filename, l.getPath()));
};

// children nodes changed
Object.entries(link.children).forEach(([name, childLink]) => {
for (const [name, childLink] of link.children.entries()) {
if (childLink && name !== '.' && name !== '..') {
watchLinkNodeChanged(childLink);
}
});
};
// link children add/remove
link.on('child:add', onLinkChildAdd);
link.on('child:delete', onLinkChildDelete);
Expand All @@ -2434,11 +2436,11 @@ export class FSWatcher extends EventEmitter {
});

if (recursive) {
Object.entries(link.children).forEach(([name, childLink]) => {
for (const [name, childLink] of link.children.entries()) {
if (childLink && name !== '.' && name !== '..') {
watchLinkChildrenChanged(childLink);
}
});
};
}
};
watchLinkNodeChanged(this._link);
Expand Down