-
Notifications
You must be signed in to change notification settings - Fork 54
[feature] set up allowed class-permissions to JEXL constructor #154
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Milchreis
wants to merge
9
commits into
apache:master
Choose a base branch
from
Milchreis:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c52ece7
[feature] set up allowed class-permissions to JEXL constructor, to us…
Milchreis 47336f2
Adds license
Milchreis 2075653
Fixes formatting
Milchreis cea5499
Adds a test for a custom JexlEvaluator created by JexlEvaluatorBuilder
Milchreis 92bfb09
Contributor added
Milchreis 7332e42
Renaming variable
Milchreis 23d4f3b
Builder for JexlEvaluator added
Milchreis b8eefd6
Merge branch 'apache:master' into master
Milchreis 90c19b3
JavaDoc updated
Milchreis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
74 changes: 74 additions & 0 deletions
74
src/main/java/org/apache/commons/scxml2/env/jexl/JexlEvaluatorBuilder.java
This file contains hidden or 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,74 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You 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 org.apache.commons.scxml2.env.jexl; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class JexlEvaluatorBuilder { | ||
|
|
||
| private final List<String> allowedPackages = new ArrayList<>(); | ||
| private final List<String> deniedClasses = new ArrayList<>(); | ||
|
|
||
| /** | ||
| * Adds a whole package to the JEXL permissions and allows to use this classes in JEXL expressions. | ||
| * | ||
| * @param fullQualifiedPackageName expects the complete path f.e. "org.apache.commons.logging" | ||
| */ | ||
| public JexlEvaluatorBuilder addAllowedPackage(String fullQualifiedPackageName) { | ||
| allowedPackages.add(fullQualifiedPackageName); | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Adds a specific class, which is not allowed to be used in JEXL expressions. | ||
| * | ||
| * @param fullQualifiedClassName expects a complete path f.e. "org.apache.commons.logging.Logger" | ||
| */ | ||
| public JexlEvaluatorBuilder addDeniedClass(String fullQualifiedClassName) { | ||
| deniedClasses.add(fullQualifiedClassName); | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Creates a JexlEvaluator by the defined options. | ||
| */ | ||
| public JexlEvaluator build() { | ||
| return new JexlEvaluator(this); | ||
| } | ||
|
|
||
| /** | ||
| * Package-private: converts the user given information to JEXL understandable src-list | ||
| */ | ||
| String[] getJexlPermissions() { | ||
| List<String> permissions = new ArrayList<>(); | ||
|
|
||
| for (String allowedPackage : allowedPackages) { | ||
| permissions.add(allowedPackage.replace(".*", "") + ".*"); | ||
| } | ||
|
|
||
| for (String deniedClass : deniedClasses) { | ||
| final int lastDot = deniedClass.lastIndexOf('.'); | ||
| String packageName = deniedClass.substring(0, lastDot); | ||
| String className = deniedClass.substring(lastDot + 1); | ||
| permissions.add(packageName + " { " + className + " {} }"); | ||
| } | ||
|
|
||
| return permissions.toArray(new String[]{}); | ||
| } | ||
| } | ||
|
|
This file contains hidden or 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,36 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You 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.custom; | ||
|
|
||
| public class Payload { | ||
|
|
||
| private final int id; | ||
| private final String someString; | ||
|
|
||
| public Payload(int id, String someString) { | ||
| this.id = id; | ||
| this.someString = someString; | ||
| } | ||
|
|
||
| public int getId() { | ||
| return id; | ||
| } | ||
|
|
||
| public String getSomeString() { | ||
| return someString; | ||
| } | ||
| } | ||
This file contains hidden or 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
35 changes: 35 additions & 0 deletions
35
src/test/java/org/apache/commons/scxml2/env/jexl/JexlEvaluatorBuilderTest.java
This file contains hidden or 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,35 @@ | ||
| package org.apache.commons.scxml2.env.jexl; | ||
|
|
||
| import org.junit.jupiter.api.Assertions; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| class JexlEvaluatorBuilderTest { | ||
|
|
||
| @Test | ||
| public void testAddAllowedPackages() { | ||
|
|
||
| final String[] jexlPermissions = new JexlEvaluatorBuilder() | ||
| .addAllowedPackage("org.apache.commons.scxml2") | ||
| .addAllowedPackage("org.apache.commons.logging") | ||
| .getJexlPermissions(); | ||
|
|
||
| Assertions.assertEquals(2, jexlPermissions.length); | ||
| Assertions.assertEquals("org.apache.commons.scxml2.*", jexlPermissions[0]); | ||
| Assertions.assertEquals("org.apache.commons.logging.*", jexlPermissions[1]); | ||
| } | ||
|
|
||
| @Test | ||
| public void testAllowedPackagesAndDeniedClasses() { | ||
|
|
||
| final String[] jexlPermissions = new JexlEvaluatorBuilder() | ||
| .addAllowedPackage("org.apache.commons.scxml2") | ||
| .addDeniedClass("org.apache.commons.scxml2.Builtin") | ||
| .addDeniedClass("org.apache.commons.logging.Logger") | ||
| .getJexlPermissions(); | ||
|
|
||
| Assertions.assertEquals(3, jexlPermissions.length); | ||
| Assertions.assertEquals("org.apache.commons.scxml2.*", jexlPermissions[0]); | ||
| Assertions.assertEquals("org.apache.commons.scxml2 { Builtin {} }", jexlPermissions[1]); | ||
| Assertions.assertEquals("org.apache.commons.logging { Logger {} }", jexlPermissions[2]); | ||
| } | ||
| } |
This file contains hidden or 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
40 changes: 40 additions & 0 deletions
40
src/test/java/org/apache/commons/scxml2/external-payload.xml
This file contains hidden or 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,40 @@ | ||
| <?xml version="1.0"?> | ||
| <!-- | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You 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. | ||
| --> | ||
| <!-- Used for comparison with custom-hello-world.xml by | ||
| CustomActionTest.java in model package --> | ||
| <scxml xmlns="http://www.w3.org/2005/07/scxml" | ||
| version="1.0" | ||
| datamodel="jexl" | ||
| initial="start"> | ||
|
|
||
| <datamodel> | ||
| <data id="idFromEventPayload" /> | ||
| </datamodel> | ||
|
|
||
| <state id="start"> | ||
| <transition event="done" target="end"/> | ||
| </state> | ||
|
|
||
| <final id="end"> | ||
| <onentry> | ||
| <assign location="idFromEventPayload" expr="_event.data.id" /> | ||
| </onentry> | ||
| </final> | ||
|
|
||
| </scxml> | ||
|
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.