Skip to content

Commit e8b9816

Browse files
committed
Add API for restricting operations for sensitive files
This change adds the following API: * org.eclipse.core.resources.IWorkspace.isRestrictedContentEnabled() * org.eclipse.core.resources.IWorkspace.setRestrictedContentEnabled(boolean) * org.eclipse.core.resources.IFile.isContentRestricted() * org.eclipse.core.resources.IFile.setContentRestricted(boolean) The goal of this API is to allow Eclipse-based IDEs to restrict certain platform functionality on a per-file basis, for sensitive files. To restrict access to sensitive files, at runtime call: 1. IWorkspace.setRestrictedContentEnabled(true) 2. For each sensitive file, call: IFile.setContentRestricted(true) Neither the IWorkspace nor the IFile flags are persisted, to enable restricted handling these flags must be set for each session. The restrictions will apply to following functionality: * File search * Storing file history Potentially more platform functionality will be restricted, if the workspace has restricted handling enabled. See: #2588
1 parent e02781c commit e8b9816

File tree

6 files changed

+145
-1
lines changed

6 files changed

+145
-1
lines changed

resources/bundles/org.eclipse.core.resources/META-INF/MANIFEST.MF

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Manifest-Version: 1.0
22
Bundle-ManifestVersion: 2
33
Bundle-Name: %pluginName
44
Bundle-SymbolicName: org.eclipse.core.resources; singleton:=true
5-
Bundle-Version: 3.23.300.qualifier
5+
Bundle-Version: 3.24.0.qualifier
66
Bundle-Activator: org.eclipse.core.resources.ResourcesPlugin
77
Bundle-Vendor: %providerName
88
Bundle-Localization: plugin

resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/Workspace.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,8 @@ public class Workspace extends PlatformObject implements IWorkspace, ICoreConsta
271271
*/
272272
protected IFileModificationValidator validator = null;
273273

274+
private boolean enableRestrictedContent;
275+
274276
/**
275277
* Data structure for holding the multi-part outcome of
276278
* <code>IWorkspace.computeProjectBuildConfigOrder</code>.
@@ -2945,4 +2947,14 @@ private void createMultiple(ConcurrentMap<File, byte[]> filesToCreate, int updat
29452947
subMonitor.done();
29462948
}
29472949
}
2950+
2951+
@Override
2952+
public boolean isRestrictedContentEnabled() {
2953+
return enableRestrictedContent;
2954+
}
2955+
2956+
@Override
2957+
public void setRestrictedContentEnabled(boolean enabled) {
2958+
this.enableRestrictedContent = enabled;
2959+
}
29482960
}

resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/resources/IFile.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@
5353
* @noextend This interface is not intended to be extended by clients.
5454
*/
5555
public interface IFile extends IResource, IEncodedStorage, IAdaptable {
56+
57+
/**
58+
* Session property used to mark a file as containing restricted content
59+
*
60+
* @since 3.24
61+
*/
62+
QualifiedName RESTRICTED_CONTENT = new QualifiedName(ResourcesPlugin.PI_RESOURCES, "restrictedContent"); //$NON-NLS-1$
63+
5664
/**
5765
* Character encoding constant (value 0) which identifies
5866
* files that have an unknown character encoding scheme.
@@ -1414,4 +1422,36 @@ public default String readString() throws CoreException {
14141422
public default String getLineSeparator(boolean checkParent) throws CoreException {
14151423
return getProject().getDefaultLineSeparator();
14161424
}
1425+
1426+
/**
1427+
* Returns whether the current file is marked as containing restricted
1428+
* (sensitive) content, where some IDE functionality related to the file content
1429+
* might be limited.
1430+
*
1431+
* @return whether the current file is marked as containing sensitive content.
1432+
* This flag is not persisted and is {@code false} by default.
1433+
* @throws CoreException if the session properties of the file cannot be read
1434+
* @since 3.24
1435+
*/
1436+
default boolean isContentRestricted() throws CoreException {
1437+
IWorkspace workspace = getWorkspace();
1438+
return workspace != null && workspace.isRestrictedContentEnabled()
1439+
&& getSessionProperty(RESTRICTED_CONTENT) != null;
1440+
}
1441+
1442+
/**
1443+
* Marks the current file as containing restricted (sensitive) content. Some IDE
1444+
* functionality related to the file content might be limited as long as the
1445+
* file is marked as restricted. This flag is not persisted and is {@code false}
1446+
* by default.
1447+
*
1448+
* @param restricted <code>true</code> if the file should be a marked
1449+
* restricted, <code>false</code> if the file should be
1450+
* unmarked
1451+
* @throws CoreException if the session properties of the file cannot be read
1452+
* @since 3.24
1453+
*/
1454+
default void setContentRestricted(boolean restricted) throws CoreException {
1455+
setSessionProperty(RESTRICTED_CONTENT, restricted ? Boolean.TRUE : null);
1456+
}
14171457
}

resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/resources/IWorkspace.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1861,4 +1861,37 @@ public default void write(Map<IFile, byte[]> contentMap, boolean force, boolean
18611861
e.getKey().write(e.getValue(), force, derived, keepHistory, subMon.split(1));
18621862
}
18631863
}
1864+
1865+
/**
1866+
* Returns whether certain IDE functionality should be disabled for restricted
1867+
* (sensitive) files. Examples are file history and search. Default is
1868+
* <code>false</code>.
1869+
* <p>
1870+
* Restricted files are marked with: {@link IFile#setContentRestricted(boolean)}
1871+
* </p>
1872+
*
1873+
* @return <code>true</code> if restricted (sensitive) files handling is enabled
1874+
* by the IDE, <code>false</code> otherwise
1875+
* @see IFile#setContentRestricted(boolean)
1876+
* @see IFile#isContentRestricted()
1877+
* @since 3.24
1878+
*/
1879+
boolean isRestrictedContentEnabled();
1880+
1881+
/**
1882+
* Specifies whether certain IDE functionality should be disabled for restricted
1883+
* (sensitive) files. Examples are file history and search. This flag is not
1884+
* persisted and must be set for every new session that requires file
1885+
* restrictions.
1886+
* <p>
1887+
* Restricted files are marked with: {@link IFile#setContentRestricted(boolean)}
1888+
* </p>
1889+
*
1890+
* @param enabled <code>true</code> to enable restricted (sensitive) files
1891+
* handling, <code>false</code> otherwise
1892+
* @see IFile#setContentRestricted(boolean)
1893+
* @see IFile#isContentRestricted()
1894+
* @since 3.24
1895+
*/
1896+
void setRestrictedContentEnabled(boolean enabled);
18641897
}

resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/internal/resources/AllInternalResourcesTests.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
ResourceInfoTest.class, //
3333
WorkspaceConcurrencyTest.class, //
3434
WorkspacePreferencesTest.class, //
35+
RestrictedFileTests.class, //
3536
})
3637
public class AllInternalResourcesTests {
3738
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2026 Simeon Andreev and others.
3+
*
4+
* This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Public License 2.0
6+
* which accompanies this distribution, and is available at
7+
* https://www.eclipse.org/legal/epl-2.0/
8+
*
9+
* SPDX-License-Identifier: EPL-2.0
10+
*
11+
* Contributors:
12+
* Simeon Andreev - initial API and implementation
13+
*******************************************************************************/
14+
package org.eclipse.core.tests.internal.resources;
15+
16+
import static org.junit.jupiter.api.Assertions.assertFalse;
17+
import static org.junit.jupiter.api.Assertions.assertTrue;
18+
19+
import org.eclipse.core.resources.IFile;
20+
import org.eclipse.core.resources.IProject;
21+
import org.eclipse.core.resources.IResource;
22+
import org.eclipse.core.resources.IWorkspace;
23+
import org.eclipse.core.resources.IWorkspaceRoot;
24+
import org.eclipse.core.resources.ResourcesPlugin;
25+
import org.eclipse.core.tests.resources.util.WorkspaceResetExtension;
26+
import org.junit.jupiter.api.Test;
27+
import org.junit.jupiter.api.extension.ExtendWith;
28+
29+
@ExtendWith(WorkspaceResetExtension.class)
30+
public class RestrictedFileTests {
31+
32+
@Test
33+
public void testRestrictedFile() throws Exception {
34+
IWorkspace workspace = ResourcesPlugin.getWorkspace();
35+
IWorkspaceRoot root = workspace.getRoot();
36+
IProject project = root.getProject(RestrictedFileTests.class.getSimpleName());
37+
try {
38+
assertFalse(workspace.isRestrictedContentEnabled(),
39+
"Expected file content to not be restricted by default");
40+
workspace.setRestrictedContentEnabled(true);
41+
42+
project.create(null);
43+
project.open(null);
44+
45+
IFile file = project.getFile("test.txt");
46+
file.create("line 1".getBytes(), IResource.FORCE, null);
47+
48+
assertFalse(file.isContentRestricted(), "Expected file to not be restricted");
49+
file.setContentRestricted(true);
50+
assertTrue(file.isContentRestricted(), "Expected file to be restricted");
51+
workspace.setRestrictedContentEnabled(false);
52+
assertFalse(file.isContentRestricted(), "Expected file to not be restricted");
53+
} finally {
54+
workspace.setRestrictedContentEnabled(false);
55+
project.delete(true, null);
56+
}
57+
}
58+
}

0 commit comments

Comments
 (0)