-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: readonly permissions with disk FS provider (#12354)
Signed-off-by: Akos Kitta <a.kitta@arduino.cc>
- Loading branch information
Showing
4 changed files
with
120 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
packages/filesystem/src/node/disk-file-system-provider.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
// ***************************************************************************** | ||
// Copyright (C) 2023 Arduino SA and others. | ||
// | ||
// This program and the accompanying materials are made available under the | ||
// terms of the Eclipse Public License v. 2.0 which is available at | ||
// http://www.eclipse.org/legal/epl-2.0. | ||
// | ||
// This Source Code may also be made available under the following Secondary | ||
// Licenses when the conditions for such availability set forth in the Eclipse | ||
// Public License v. 2.0 are satisfied: GNU General Public License, version 2 | ||
// with the GNU Classpath Exception which is available at | ||
// https://www.gnu.org/software/classpath/license.html. | ||
// | ||
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 | ||
// ***************************************************************************** | ||
|
||
import { Disposable, DisposableCollection } from '@theia/core/lib/common/disposable'; | ||
import { EncodingService } from '@theia/core/lib/common/encoding-service'; | ||
import { ILogger } from '@theia/core/lib/common/logger'; | ||
import { MockLogger } from '@theia/core/lib/common/test/mock-logger'; | ||
import { FileUri } from '@theia/core/lib/node/file-uri'; | ||
import { IPCConnectionProvider } from '@theia/core/lib/node/messaging/ipc-connection-provider'; | ||
import { Container, ContainerModule } from '@theia/core/shared/inversify'; | ||
import { equal, fail } from 'assert'; | ||
import { promises as fs } from 'fs'; | ||
import { join } from 'path'; | ||
import * as temp from 'temp'; | ||
import { v4 } from 'uuid'; | ||
import { FilePermission, FileSystemProviderError, FileSystemProviderErrorCode } from '../common/files'; | ||
import { DiskFileSystemProvider } from './disk-file-system-provider'; | ||
import { bindFileSystemWatcherServer } from './filesystem-backend-module'; | ||
|
||
const tracked = temp.track(); | ||
|
||
describe('disk-file-system-provider', () => { | ||
let toDisposeAfter: DisposableCollection; | ||
let fsProvider: DiskFileSystemProvider; | ||
|
||
before(() => { | ||
fsProvider = createContainer().get<DiskFileSystemProvider>( | ||
DiskFileSystemProvider | ||
); | ||
toDisposeAfter = new DisposableCollection( | ||
fsProvider, | ||
Disposable.create(() => tracked.cleanupSync()) | ||
); | ||
}); | ||
|
||
after(() => { | ||
toDisposeAfter.dispose(); | ||
}); | ||
|
||
describe('stat', () => { | ||
it("should omit the 'permissions' property of the stat if the file can be both read and write", async () => { | ||
const tempDirPath = tracked.mkdirSync(); | ||
const tempFilePath = join(tempDirPath, `${v4()}.txt`); | ||
await fs.writeFile(tempFilePath, 'some content', { encoding: 'utf8' }); | ||
|
||
let content = await fs.readFile(tempFilePath, { encoding: 'utf8' }); | ||
equal(content, 'some content'); | ||
|
||
await fs.writeFile(tempFilePath, 'other content', { encoding: 'utf8' }); | ||
|
||
content = await fs.readFile(tempFilePath, { encoding: 'utf8' }); | ||
equal(content, 'other content'); | ||
|
||
const stat = await fsProvider.stat(FileUri.create(tempFilePath)); | ||
equal(stat.permissions, undefined); | ||
}); | ||
|
||
it("should set the 'permissions' property to `Readonly` if the file can be read but not write", async () => { | ||
const tempDirPath = tracked.mkdirSync(); | ||
const tempFilePath = join(tempDirPath, `${v4()}.txt`); | ||
await fs.writeFile(tempFilePath, 'readonly content', { | ||
encoding: 'utf8', | ||
}); | ||
|
||
await fs.chmod(tempFilePath, '444'); // read-only for owner/group/world | ||
|
||
try { | ||
await fsProvider.writeFile(FileUri.create(tempFilePath), new Uint8Array(), { create: false, overwrite: true }); | ||
fail('Expected an EACCES error for readonly (chmod 444) files'); | ||
} catch (err) { | ||
equal(err instanceof FileSystemProviderError, true); | ||
equal((<FileSystemProviderError>err).code, FileSystemProviderErrorCode.NoPermissions); | ||
} | ||
|
||
const content = await fs.readFile(tempFilePath, { encoding: 'utf8' }); | ||
equal(content, 'readonly content'); | ||
|
||
const stat = await fsProvider.stat(FileUri.create(tempFilePath)); | ||
equal(stat.permissions, FilePermission.Readonly); | ||
}); | ||
}); | ||
|
||
function createContainer(): Container { | ||
const container = new Container({ defaultScope: 'Singleton' }); | ||
const module = new ContainerModule(bind => { | ||
bind(DiskFileSystemProvider).toSelf().inSingletonScope(); | ||
bind(EncodingService).toSelf().inSingletonScope(); | ||
bindFileSystemWatcherServer(bind); | ||
bind(MockLogger).toSelf().inSingletonScope(); | ||
bind(ILogger).toService(MockLogger); | ||
bind(IPCConnectionProvider).toSelf().inSingletonScope(); | ||
}); | ||
container.load(module); | ||
return container; | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters