Skip to content
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

feat(agama): reject usage of repeated input names #1484

Merged
merged 2 commits into from
May 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ private void makeCrashException(Exception e) throws FlowCrashException {
* @return
* @throws JsonProcessingException
*/
public FlowResult flowResultFrom(NativeObject result) throws JsonProcessingException {
private FlowResult flowResultFrom(NativeObject result) throws JsonProcessingException {
return mapper.convertValue(result, FlowResult.class);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

FlowStatus fstatus = flowService.getRunningFlowStatus();
String path = request.getServletPath();

if (fstatus == null || fstatus.getStartedAt() == FlowStatus.FINISHED) {
sendPageMismatch(response, NO_ACTIVE_FLOW, null);
return;
Expand All @@ -67,8 +65,9 @@ public void doGet(HttpServletRequest request, HttpServletResponse response)
}

} else {
String path = request.getServletPath();
if (processCallback(response, fstatus, path)) return;

String expectedUrl = getExpectedUrl(fstatus);

if (path.equals(expectedUrl)) {
Expand All @@ -89,13 +88,12 @@ public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

FlowStatus fstatus = flowService.getRunningFlowStatus();
String path = request.getServletPath();

if (fstatus == null || fstatus.getStartedAt() == FlowStatus.FINISHED) {
sendPageMismatch(response, NO_ACTIVE_FLOW, null);
return;
}

String path = request.getServletPath();
if (processCallback(response, fstatus, path)) return;

String expectedUrl = getExpectedUrl(fstatus);
Expand Down
37 changes: 26 additions & 11 deletions agama/transpiler/src/main/java/io/jans/agama/dsl/Transpiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,7 @@ private void loadFreeMarkerTemplate() throws TranspilerException {
Configuration fmConfig = new Configuration(Configuration.VERSION_2_3_31);
fmConfig.setClassLoaderForTemplateLoading(CLS_LOADER, "/");
fmConfig.setDefaultEncoding(UTF_8.toString());
//TODO: ?
//fmConfig.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
fmConfig.setTemplateExceptionHandler(TemplateExceptionHandler.DEBUG_HANDLER);
fmConfig.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
fmConfig.setLogTemplateExceptions(false);
fmConfig.setWrapUncheckedExceptions(true);
fmConfig.setFallbackOnNullLoopVariable(false);
Expand Down Expand Up @@ -179,15 +177,15 @@ public XdmNode asXML(String DSLCode) throws SyntaxException, TranspilerException

public List<String> getInputs(XdmNode node) throws SaxonApiException {

return xpathCompiler.evaluate("/flow/header/inputs/short_var/text()", node)
return xpathCompiler.evaluate(Visitor.INPUTS_XPATH_EXPR, node)
.stream().map(XdmItem::getStringValue).collect(Collectors.toList());

}

public Integer getTimeout(XdmNode node) throws SaxonApiException {

return Optional.ofNullable(
xpathCompiler.evaluateSingle("/flow/header/timeout/UINT/text()", node))
xpathCompiler.evaluateSingle(Visitor.TIMEOUT_XPATH_EXPR, node))
.map(XdmItem::getStringValue).map(Integer::valueOf).orElse(null);

}
Expand All @@ -212,19 +210,19 @@ private void applyValidations(SaplingDocument doc) throws TranspilerException {
XdmNode node = doc.toXdmNode(processor);

//Ensure only existing flows are referenced
checkUnknownInvocation(Visitor.FLOWCALL_XPATH_EXPR, flowNames, node);

checkUnknownInvocation(flowNames, node);
checkInputsUniqueness(node);
} catch (SaxonApiException se) {
throw new TranspilerException("Validation failed", se);
}

}

private void checkUnknownInvocation(String xpathExpr, Set<String> known, XdmNode node)
private void checkUnknownInvocation(Set<String> known, XdmNode node)
throws TranspilerException, SaxonApiException {

if (known != null) {
List<String> invocations = xpathCompiler.evaluate(xpathExpr, node)
List<String> invocations = xpathCompiler.evaluate(Visitor.FLOWCALL_XPATH_EXPR, node)
.stream().map(XdmItem::getStringValue).collect(Collectors.toList());

for (String t : invocations) {
Expand All @@ -237,7 +235,24 @@ private void checkUnknownInvocation(String xpathExpr, Set<String> known, XdmNode
}

}


private void checkInputsUniqueness(XdmNode node) throws TranspilerException, SaxonApiException {

List<String> inputs = getInputs(node);
Set<String> inputsSet = inputs.stream().collect(Collectors.toSet());
String configVar = Optional.ofNullable(
xpathCompiler.evaluateSingle(Visitor.CONFIG_XPATH_EXPR, node))
.map(XdmItem::getStringValue).orElse(null);

if (inputsSet.size() < inputs.size())
throw new TranspilerException("One or more input variable names are duplicated");

if (configVar != null && inputsSet.contains(configVar))
throw new TranspilerException("Configuration variable '" + configVar +
"' cannot be used as an input variable");

}

private void logXml(XdmNode node) {
logger.debug("\n{}", node.toString());
//System.out.println("\n" + node.toString());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.jans.agama.dsl;

import io.jans.agama.antlr.AuthnFlowParser;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
Expand All @@ -15,14 +17,15 @@
import org.antlr.v4.runtime.Token;
import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.TerminalNode;
import io.jans.agama.antlr.AuthnFlowParser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Visitor {

//public static final String FLOWNAME_XPATH_EXPR = "/flow/header/qname/text()";
public static final String FLOWCALL_XPATH_EXPR = "//flow_call/call/call_subject/qname/text()";
public static final String INPUTS_XPATH_EXPR = "/flow/header/inputs/short_var/text()";
public static final String CONFIG_XPATH_EXPR = "/flow/header/configs/short_var/text()";
public static final String TIMEOUT_XPATH_EXPR = "/flow/header/timeout/UINT/text()";

private static final Logger logger = LoggerFactory.getLogger(Visitor.class);
private static final Set<Integer> INCLUDE_SYMBOLS;
Expand Down
18 changes: 0 additions & 18 deletions jans-linux-setup/jans_setup/schema/jans_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -4232,24 +4232,6 @@
"top"
],
"x_origin": "Jans created objectclass"
},
{
"kind": "STRUCTURAL",
"may": [
"ou",
"jansScr"
],
"must": [
"objectclass"
],
"names": [
"agmBasics"
],
"oid": "jansObjClass",
"sup": [
"top"
],
"x_origin": "Jans created objectclass"
}
],
"oidMacros": {
Expand Down