Skip to content

Commit 9f3d003

Browse files
authored
OAK-11683: Optionally disallow registration of invalid namespace URIs (#2258)
This feature can be enabled by setting framework/system property "oak.allowInvalidNamespaceUris" to "false".
1 parent 63eb1ba commit 9f3d003

File tree

2 files changed

+45
-16
lines changed

2 files changed

+45
-16
lines changed

oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/name/ReadWriteNamespaceRegistry.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.apache.jackrabbit.oak.api.PropertyState;
2626
import org.apache.jackrabbit.oak.api.Root;
2727
import org.apache.jackrabbit.oak.api.Tree;
28+
import org.apache.jackrabbit.oak.commons.properties.SystemPropertySupplier;
2829
import org.apache.jackrabbit.oak.spi.namespace.NamespaceConstants;
2930
import org.slf4j.Logger;
3031
import org.slf4j.LoggerFactory;
@@ -38,7 +39,15 @@ public abstract class ReadWriteNamespaceRegistry
3839

3940
private static final Logger LOG = LoggerFactory.getLogger(ReadWriteNamespaceRegistry.class);
4041

41-
public ReadWriteNamespaceRegistry(Root root) {
42+
/**
43+
* Feature flag to allow registering invalid namespace URIs (without a colon).
44+
* Set the system property {@code oak.allowInvalidNamespaceUris} to {@code false} to disable this feature.
45+
* Cannot be static in order to allow testing with different values.
46+
*/
47+
private final boolean allowInvalidNamespaceUris = SystemPropertySupplier.create("oak.allowInvalidNamespaceUris", true)
48+
.loggingTo(LOG).get();;
49+
50+
protected ReadWriteNamespaceRegistry(Root root) {
4251
super(root);
4352
}
4453

@@ -71,11 +80,13 @@ public void registerNamespace(String prefix, String uri)
7180

7281
// sanity check for legal namespace names (excluding the "internal"
7382
// namespace, see OAK-74)
74-
if (!NamespaceConstants.NAMESPACE_REP.equals(uri)) {
75-
if (!uri.contains(":")) {
83+
if (!NamespaceConstants.NAMESPACE_REP.equals(uri) && !uri.contains(":")) {
84+
if (allowInvalidNamespaceUris) {
7685
LOG.error("Registering invalid namespace name '" + uri + "' for prefix '" + prefix
77-
+ "', please see https://developer.adobe.com/experience-manager/reference-materials/spec/jcr/2.0/3_Repository_Model.html#3.2.1%20Namespaces",
78-
new Exception("call stack"));
86+
+ "', please see https://s.apache.org/jcr-2.0-spec/3_Repository_Model.html#3.2.1%20Namespaces",
87+
new Exception("call stack"));
88+
} else {
89+
throw new NamespaceException("Invalid namespace URI given: " + uri + ". It must contain a colon.");
7990
}
8091
}
8192

oak-it/src/test/java/org/apache/jackrabbit/oak/plugins/name/ReadWriteNamespaceRegistryTest.java

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.apache.jackrabbit.oak.plugins.name;
1818

1919
import static org.junit.Assert.assertEquals;
20+
import static org.junit.Assert.assertThrows;
2021
import static org.junit.Assert.assertTrue;
2122
import static org.junit.Assert.fail;
2223

@@ -68,13 +69,13 @@ public void testMappings() throws Exception {
6869
assertEquals("mix", r.getPrefix("http://www.jcp.org/jcr/mix/1.0"));
6970
assertEquals("xml", r.getPrefix("http://www.w3.org/XML/1998/namespace"));
7071

71-
r.registerNamespace("p", "n");
72-
assertEquals(r.getURI("p"), "n");
73-
assertEquals(r.getPrefix("n"), "p");
72+
r.registerNamespace("p", "myscheme:n");
73+
assertEquals(r.getURI("p"), "myscheme:n");
74+
assertEquals(r.getPrefix("myscheme:n"), "p");
7475

75-
r.registerNamespace("p2", "n2");
76-
assertEquals(r.getURI("p2"), "n2");
77-
assertEquals(r.getPrefix("n2"), "p2");
76+
r.registerNamespace("p2", "myscheme:n2");
77+
assertEquals(r.getURI("p2"), "myscheme:n2");
78+
assertEquals(r.getPrefix("myscheme:n2"), "p2");
7879

7980
// xml namespace check
8081
assertTrue(SetUtils.toSet(r.getPrefixes()).contains("xml"));
@@ -87,13 +88,12 @@ public void testMappings() throws Exception {
8788
}
8889

8990
@Test
90-
public void testInvalidNamespace() throws Exception {
91-
final ContentSession session = createContentSession();
92-
final Root root = session.getLatestRoot();
93-
NamespaceRegistry r = getNamespaceRegistry(session, root);
94-
91+
public void testInvalidNamespaceInDefaultMode() throws Exception {
9592
LogCustomizer customLogs = LogCustomizer.forLogger("org.apache.jackrabbit.oak.plugins.name.ReadWriteNamespaceRegistry").enable(Level.ERROR).create();
9693
try {
94+
final ContentSession session = createContentSession();
95+
final Root root = session.getLatestRoot();
96+
NamespaceRegistry r = getNamespaceRegistry(session, root);
9797
customLogs.starting();
9898
r.registerNamespace("foo", "example.com");
9999
r.unregisterNamespace("foo");
@@ -103,6 +103,24 @@ public void testInvalidNamespace() throws Exception {
103103
}
104104
finally {
105105
customLogs.finished();
106+
107+
}
108+
}
109+
110+
@Test
111+
public void testInvalidNamespaceInStrictMode() {
112+
String oldValue = System.setProperty("oak.allowInvalidNamespaceUris", "true");
113+
try {
114+
final ContentSession session = createContentSession();
115+
final Root root = session.getLatestRoot();
116+
NamespaceRegistry r = getNamespaceRegistry(session, root);
117+
assertThrows(NamespaceException.class, () -> r.registerNamespace("foo", "example.com"));
118+
} finally {
119+
if (oldValue != null) {
120+
System.setProperty("oak.allowInvalidNamespaceUris", oldValue);
121+
} else {
122+
System.clearProperty("oak.allowInvalidNamespaceUris");
123+
}
106124
}
107125
}
108126

0 commit comments

Comments
 (0)