marschall / memoryfilesystem

An in memory implementation of a JSR-203 file system
282 stars 36 forks source link

MemoryFileSystem::newOutputFileStream does not check the path it opens for write permissions #138

Closed stwZiemer closed 1 year ago

stwZiemer commented 1 year ago

I think that MemoryFileSystem::newOutputFileStream does not check the path it opens for write permissions. I can reproduce the issue in the test class below. Is there something I'm missing?

I used the most current version (2.4.0)

package com.github.marschall.memoryfilesystem;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.nio.file.AccessDeniedException;
import java.nio.file.FileSystem;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.PosixFileAttributeView;
import java.util.Collections;

class WritePermissionTest {
    @Test
    void outputStreamChecksPermissions() throws IOException {
        try (FileSystem fileSystem = MemoryFileSystemBuilder.newLinux().build()) {
            final Path path = fileSystem.getPath("/a.csv");
            Files.createFile(path);

            PosixFileAttributeView fileAttributeView = fileSystem.provider().getFileAttributeView(path, PosixFileAttributeView.class);
            fileAttributeView.setPermissions(Collections.emptySet());

            // double check that writing not allowed
            // check passed
            Assertions.assertFalse(Files.isWritable(path));

            // should throw in my opinion but doesn't
            Assertions.assertThrows(IOException.class, () -> Files.newOutputStream(path));
        }
    }
}
marschall commented 1 year ago

You are correct, it should throw AccessDeniedException

marschall commented 1 year ago

I just released 2.5.0 which contains a fix.

stwZiemer commented 1 year ago

Thank you very much! We’ve found many bugs thanks to your memory file system.

Von: Philippe Marschall @.> Gesendet: Dienstag, 7. Februar 2023 07:09 An: marschall/memoryfilesystem @.> Cc: Weidmann Stefano @.>; Author @.> Betreff: Re: [marschall/memoryfilesystem] MemoryFileSystem::newOutputFileStream does not check the path it opens for write permissions (Issue #138)

I just released 2.5.0 which contains a fix.

— Reply to this email directly, view it on GitHubhttps://github.com/marschall/memoryfilesystem/issues/138#issuecomment-1420250015, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AQ3Y5KF524IT4TDEWO4JE3LWWHRJTANCNFSM6AAAAAAS46XWI4. You are receiving this because you authored the thread.Message ID: @.***>

marschall commented 1 year ago

That's great to hear.