Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 395274 | Differences between
and this patch

Collapse All | Expand All

(-)a/bundles/org.eclipse.osgi.tests/src/org/eclipse/osgi/tests/bundles/BundleResourceTests.java (+18 lines)
Lines 15-20 import java.util.Enumeration; Link Here
15
import junit.framework.Test;
15
import junit.framework.Test;
16
import junit.framework.TestSuite;
16
import junit.framework.TestSuite;
17
import org.eclipse.core.tests.harness.CoreTest;
17
import org.eclipse.core.tests.harness.CoreTest;
18
import org.eclipse.osgi.framework.internal.core.FrameworkProperties;
18
import org.eclipse.osgi.tests.OSGiTestsActivator;
19
import org.eclipse.osgi.tests.OSGiTestsActivator;
19
import org.osgi.framework.*;
20
import org.osgi.framework.*;
20
21
Lines 65-70 public class BundleResourceTests extends CoreTest { Link Here
65
		assertNotNull("Did not find resource!", paths);
66
		assertNotNull("Did not find resource!", paths);
66
	}
67
	}
67
68
69
	public void testBug395274() throws Exception {
70
		FrameworkProperties.setProperty("osgi.strictBundleEntryPath", "true");
71
		Bundle bundle = installer.installBundle("test"); //$NON-NLS-1$
72
		URL path = bundle.getEntry("META-INF./MANIFEST.MF");
73
		assertNull("found resource!", path);
74
		path = bundle.getEntry("META-INF/MANIFEST.MF");
75
		assertNotNull("Did not find resource!", path);
76
		path = bundle.getEntry("folder/file1.TXT");
77
		assertNull("found resource!", path);
78
		path = bundle.getEntry("folder/file1.txt");
79
		assertNotNull("Did not find resource!", path);
80
	}
81
68
	public void testBug328795() throws BundleException {
82
	public void testBug328795() throws BundleException {
69
		Bundle bundle = installer.installBundle("test"); //$NON-NLS-1$
83
		Bundle bundle = installer.installBundle("test"); //$NON-NLS-1$
70
		checkEntries(bundle, "notFound\\", 0); // this results in invalid syntax exception which is logged because of trailing escape
84
		checkEntries(bundle, "notFound\\", 0); // this results in invalid syntax exception which is logged because of trailing escape
Lines 97-102 public class BundleResourceTests extends CoreTest { Link Here
97
		checkEntries(bundle, "*(*", 2);
111
		checkEntries(bundle, "*(*", 2);
98
		checkEntries(bundle, "*\\)*", 2);
112
		checkEntries(bundle, "*\\)*", 2);
99
		checkEntries(bundle, "*\\(*", 2);
113
		checkEntries(bundle, "*\\(*", 2);
114
		checkEntries(bundle, "/./file1.txt", 1);
115
		checkEntries(bundle, "//file1.txt", 1);
116
		checkEntries(bundle, "/", 1);
117
		checkEntries(bundle, "/.", 1);
100
	}
118
	}
101
119
102
	public void testBug338081() throws BundleException {
120
	public void testBug338081() throws BundleException {
(-)a/bundles/org.eclipse.osgi/defaultAdaptor/src/org/eclipse/osgi/baseadaptor/bundlefile/DirBundleFile.java (-7 / +48 lines)
Lines 15-20 import java.io.File; Link Here
15
import java.io.IOException;
15
import java.io.IOException;
16
import java.util.Enumeration;
16
import java.util.Enumeration;
17
import java.util.NoSuchElementException;
17
import java.util.NoSuchElementException;
18
import org.eclipse.osgi.framework.internal.core.FrameworkProperties;
18
import org.eclipse.osgi.internal.baseadaptor.AdaptorMsg;
19
import org.eclipse.osgi.internal.baseadaptor.AdaptorMsg;
19
import org.eclipse.osgi.util.NLS;
20
import org.eclipse.osgi.util.NLS;
20
21
Lines 24-29 import org.eclipse.osgi.util.NLS; Link Here
24
 */
25
 */
25
public class DirBundleFile extends BundleFile {
26
public class DirBundleFile extends BundleFile {
26
27
28
	private static final String POINTER_SAME_DIRECTORY_1 = "/.";//$NON-NLS-1$
29
	private static final String POINTER_SAME_DIRECTORY_2 = "//";//$NON-NLS-1$
30
	private static final String POINTER_UPPER_DIRECTORY = "..";//$NON-NLS-1$
31
32
	protected static final String PROPERTY_STRICT_BUNDLE_ENTRY_PATH = "osgi.strictBundleEntryPath";//$NON-NLS-1$
33
	protected static final String PROPERTY_STRICT_BUNDLE_ENTRY_PATH_DEFAULT_VALUE = "false";//$NON-NLS-1$
34
35
	private final String baseFileCanonicalPath;
36
	private final boolean enabledStrictBundleEntryPath;
37
27
	/**
38
	/**
28
	 * Constructs a DirBundleFile
39
	 * Constructs a DirBundleFile
29
	 * @param basefile the base file
40
	 * @param basefile the base file
Lines 34-55 public class DirBundleFile extends BundleFile { Link Here
34
		if (!BundleFile.secureAction.exists(basefile) || !BundleFile.secureAction.isDirectory(basefile)) {
45
		if (!BundleFile.secureAction.exists(basefile) || !BundleFile.secureAction.isDirectory(basefile)) {
35
			throw new IOException(NLS.bind(AdaptorMsg.ADAPTOR_DIRECTORY_EXCEPTION, basefile));
46
			throw new IOException(NLS.bind(AdaptorMsg.ADAPTOR_DIRECTORY_EXCEPTION, basefile));
36
		}
47
		}
48
		this.baseFileCanonicalPath = BundleFile.secureAction.getCanonicalPath(basefile);
49
		this.enabledStrictBundleEntryPath = Boolean.parseBoolean(FrameworkProperties.getProperty(PROPERTY_STRICT_BUNDLE_ENTRY_PATH, PROPERTY_STRICT_BUNDLE_ENTRY_PATH_DEFAULT_VALUE));
37
	}
50
	}
38
51
39
	public File getFile(String path, boolean nativeCode) {
52
	public File getFile(String path, boolean nativeCode) {
40
		boolean checkInBundle = path != null && path.indexOf("..") >= 0; //$NON-NLS-1$
53
		File file = new File(this.baseFileCanonicalPath, path);
41
		File file = new File(basefile, path);
42
		if (!BundleFile.secureAction.exists(file)) {
54
		if (!BundleFile.secureAction.exists(file)) {
43
			return null;
55
			return null;
44
		}
56
		}
57
		boolean checkInBundle = false;
58
		boolean normalize = false;
59
		boolean isBundleRoot = false;
60
		if (path != null) {
61
			isBundleRoot = path.equals("/");//$NON-NLS-1$
62
			if (!isBundleRoot) {
63
				checkInBundle = path.indexOf(POINTER_UPPER_DIRECTORY) >= 0;
64
				if (this.enabledStrictBundleEntryPath) {
65
					normalize = checkInBundle || path.indexOf(POINTER_SAME_DIRECTORY_1) >= 0 || path.indexOf(POINTER_SAME_DIRECTORY_2) >= 0;
66
				}
67
			}
68
		}
69
		File canonicalFile;
70
		try {
71
			canonicalFile = BundleFile.secureAction.getCanonicalFile(file);
72
			if (this.enabledStrictBundleEntryPath && !isBundleRoot) {
73
				File absoluteFile = BundleFile.secureAction.getAbsoluteFile(file);
74
				String canonicalPath;
75
				String absolutePath;
76
				if (normalize) {
77
					canonicalPath = canonicalFile.toURI().getPath();
78
					absolutePath = absoluteFile.toURI().normalize().getPath();
79
				} else {
80
					canonicalPath = canonicalFile.getPath();
81
					absolutePath = absoluteFile.getPath();
82
				}
83
				if (!canonicalPath.equals(absolutePath)) {
84
					return null;
85
				}
86
			}
87
		} catch (IOException e) {
88
			return null;
89
		}
45
		// must do an extra check to make sure file is within the bundle (bug 320546)
90
		// must do an extra check to make sure file is within the bundle (bug 320546)
46
		if (checkInBundle) {
91
		if (checkInBundle) {
47
			try {
92
			if (!canonicalFile.getPath().startsWith(this.baseFileCanonicalPath))
48
				if (!BundleFile.secureAction.getCanonicalPath(file).startsWith(BundleFile.secureAction.getCanonicalPath(basefile)))
49
					return null;
50
			} catch (IOException e) {
51
				return null;
93
				return null;
52
			}
53
		}
94
		}
54
		return file;
95
		return file;
55
	}
96
	}
(-)a/bundles/org.eclipse.osgi/supplement/src/org/eclipse/osgi/framework/util/SecureAction.java (+38 lines)
Lines 198-203 public class SecureAction { Link Here
198
	}
198
	}
199
199
200
	/**
200
	/**
201
	 * Returns the absolute file.  Same as calling
202
	 * file.getAbsoluteFile().
203
	 * @param file a file object
204
	 * @return the absolute file.
205
	 */
206
	public File getAbsoluteFile(final File file) {
207
		if (System.getSecurityManager() == null)
208
			return file.getAbsoluteFile();
209
		return AccessController.doPrivileged(new PrivilegedAction<File>() {
210
			public File run() {
211
				return file.getAbsoluteFile();
212
			}
213
		}, controlContext);
214
	}
215
216
	/**
217
	 * Returns the canonical file.  Same as calling
218
	 * file.getCanonicalFile().
219
	 * @param file a file object
220
	 * @return the canonical file.
221
	 */
222
	public File getCanonicalFile(final File file) throws IOException {
223
		if (System.getSecurityManager() == null)
224
			return file.getCanonicalFile();
225
		try {
226
			return AccessController.doPrivileged(new PrivilegedExceptionAction<File>() {
227
				public File run() throws IOException {
228
					return file.getCanonicalFile();
229
				}
230
			}, controlContext);
231
		} catch (PrivilegedActionException e) {
232
			if (e.getException() instanceof IOException)
233
				throw (IOException) e.getException();
234
			throw (RuntimeException) e.getException();
235
		}
236
	}
237
238
	/**
201
	 * Returns true if a file exists, otherwise false is returned.  Same as calling
239
	 * Returns true if a file exists, otherwise false is returned.  Same as calling
202
	 * file.exists().
240
	 * file.exists().
203
	 * @param file a file object
241
	 * @param file a file object

Return to bug 395274