-
Notifications
You must be signed in to change notification settings - Fork 325
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add security considerations and mitigations (#1079)
- Loading branch information
1 parent
bbbbd1c
commit a6f3ae1
Showing
9 changed files
with
318 additions
and
8 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
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
44 changes: 44 additions & 0 deletions
44
src/main/java/com/networknt/schema/regex/AllowRegularExpressionFactory.java
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,44 @@ | ||
/* | ||
* Copyright (c) 2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.networknt.schema.regex; | ||
|
||
import java.util.function.Predicate; | ||
|
||
import com.networknt.schema.InvalidSchemaException; | ||
import com.networknt.schema.ValidationMessage; | ||
|
||
/** | ||
* {@link RegularExpressionFactory} that allows regular expressions to be used. | ||
*/ | ||
public class AllowRegularExpressionFactory implements RegularExpressionFactory { | ||
private final RegularExpressionFactory delegate; | ||
private final Predicate<String> allowed; | ||
|
||
public AllowRegularExpressionFactory(RegularExpressionFactory delegate, Predicate<String> allowed) { | ||
this.delegate = delegate; | ||
this.allowed = allowed; | ||
} | ||
|
||
@Override | ||
public RegularExpression getRegularExpression(String regex) { | ||
if (this.allowed.test(regex)) { | ||
// Allowed to delegate | ||
return this.delegate.getRegularExpression(regex); | ||
} | ||
throw new InvalidSchemaException(ValidationMessage.builder() | ||
.message("Regular expression ''{1}'' is not allowed to be used.").arguments(regex).build()); | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
src/main/java/com/networknt/schema/resource/AllowSchemaLoader.java
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,49 @@ | ||
/* | ||
* Copyright (c) 2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.networknt.schema.resource; | ||
|
||
import java.util.function.Predicate; | ||
|
||
import com.networknt.schema.AbsoluteIri; | ||
import com.networknt.schema.InvalidSchemaException; | ||
import com.networknt.schema.ValidationMessage; | ||
|
||
/** | ||
* {@link SchemaLoader} that allows loading external resources. | ||
*/ | ||
public class AllowSchemaLoader implements SchemaLoader { | ||
private final Predicate<AbsoluteIri> allowed; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param allowed the predicate to determine which external resource is allowed | ||
* to be loaded | ||
*/ | ||
public AllowSchemaLoader(Predicate<AbsoluteIri> allowed) { | ||
this.allowed = allowed; | ||
} | ||
|
||
@Override | ||
public InputStreamSource getSchema(AbsoluteIri absoluteIri) { | ||
if (this.allowed.test(absoluteIri)) { | ||
// Allow to delegate to the next schema loader | ||
return null; | ||
} | ||
throw new InvalidSchemaException(ValidationMessage.builder() | ||
.message("Schema from ''{1}'' is not allowed to be loaded.").arguments(absoluteIri).build()); | ||
} | ||
} |
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
48 changes: 48 additions & 0 deletions
48
src/main/java/com/networknt/schema/resource/DisallowSchemaLoader.java
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,48 @@ | ||
/* | ||
* Copyright (c) 2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.networknt.schema.resource; | ||
|
||
import com.networknt.schema.AbsoluteIri; | ||
import com.networknt.schema.InvalidSchemaException; | ||
import com.networknt.schema.ValidationMessage; | ||
|
||
/** | ||
* {@link SchemaLoader} that disallows loading external resources. | ||
*/ | ||
public class DisallowSchemaLoader implements SchemaLoader { | ||
private static DisallowSchemaLoader INSTANCE = new DisallowSchemaLoader(); | ||
|
||
/** | ||
* Disallows loading schemas from external resources. | ||
* | ||
* @return the disallow schema loader | ||
*/ | ||
public static DisallowSchemaLoader getInstance() { | ||
return INSTANCE; | ||
} | ||
|
||
/** | ||
* Constructor. | ||
*/ | ||
private DisallowSchemaLoader() { | ||
} | ||
|
||
@Override | ||
public InputStreamSource getSchema(AbsoluteIri absoluteIri) { | ||
throw new InvalidSchemaException(ValidationMessage.builder() | ||
.message("Schema from ''{1}'' is not allowed to be loaded.").arguments(absoluteIri).build()); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/test/java/com/networknt/schema/regex/AllowRegularExpressionFactoryTest.java
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,47 @@ | ||
/* | ||
* Copyright (c) 2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.networknt.schema.regex; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import com.networknt.schema.InvalidSchemaException; | ||
|
||
/** | ||
* Test for AllowRegularExpressionFactory. | ||
*/ | ||
class AllowRegularExpressionFactoryTest { | ||
@Test | ||
void getRegularExpression() { | ||
boolean called[] = { false }; | ||
RegularExpressionFactory delegate = (regex) -> { | ||
called[0] = true; | ||
return null; | ||
}; | ||
String allowed = "testing"; | ||
RegularExpressionFactory factory = new AllowRegularExpressionFactory(delegate, allowed::equals); | ||
InvalidSchemaException exception = assertThrows(InvalidSchemaException.class, () -> factory.getRegularExpression("hello")); | ||
assertEquals("hello", exception.getValidationMessage().getArguments()[0]); | ||
|
||
assertDoesNotThrow(() -> factory.getRegularExpression(allowed)); | ||
assertTrue(called[0]); | ||
} | ||
|
||
} |
48 changes: 48 additions & 0 deletions
48
src/test/java/com/networknt/schema/resource/AllowSchemaLoaderTest.java
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,48 @@ | ||
/* | ||
* Copyright (c) 2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.networknt.schema.resource; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import com.networknt.schema.InvalidSchemaException; | ||
import com.networknt.schema.JsonSchema; | ||
import com.networknt.schema.JsonSchemaFactory; | ||
import com.networknt.schema.SchemaLocation; | ||
import com.networknt.schema.SpecVersion.VersionFlag; | ||
|
||
/** | ||
* Test for AllowSchemaLoader. | ||
*/ | ||
class AllowSchemaLoaderTest { | ||
|
||
@Test | ||
void integration() { | ||
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(VersionFlag.V202012, | ||
builder -> builder.schemaLoaders(schemaLoaders -> schemaLoaders | ||
.add(new AllowSchemaLoader(iri -> iri.toString().startsWith("classpath:"))))); | ||
InvalidSchemaException invalidSchemaException = assertThrows(InvalidSchemaException.class, | ||
() -> factory.getSchema(SchemaLocation.of("http://www.example.org/schema"))); | ||
assertEquals("http://www.example.org/schema", | ||
invalidSchemaException.getValidationMessage().getArguments()[0].toString()); | ||
JsonSchema schema = factory.getSchema(SchemaLocation.of("classpath:schema/example-main.json")); | ||
assertNotNull(schema); | ||
} | ||
|
||
} |
43 changes: 43 additions & 0 deletions
43
src/test/java/com/networknt/schema/resource/DisallowSchemaLoaderTest.java
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,43 @@ | ||
/* | ||
* Copyright (c) 2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.networknt.schema.resource; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import com.networknt.schema.InvalidSchemaException; | ||
import com.networknt.schema.JsonSchemaFactory; | ||
import com.networknt.schema.SchemaLocation; | ||
import com.networknt.schema.SpecVersion.VersionFlag; | ||
|
||
/** | ||
* Test for DisallowSchemaLoader. | ||
*/ | ||
class DisallowSchemaLoaderTest { | ||
|
||
@Test | ||
void integration() { | ||
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(VersionFlag.V202012, builder -> builder | ||
.schemaLoaders(schemaLoaders -> schemaLoaders.add(DisallowSchemaLoader.getInstance()))); | ||
InvalidSchemaException invalidSchemaException = assertThrows(InvalidSchemaException.class, | ||
() -> factory.getSchema(SchemaLocation.of("classpath:schema/example-main.json"))); | ||
assertEquals("classpath:schema/example-main.json", | ||
invalidSchemaException.getValidationMessage().getArguments()[0].toString()); | ||
} | ||
|
||
} |