use of org.wso2.carbon.apimgt.ballerina.threatprotection.configurations.JSONConfig in project carbon-apimgt by wso2.
the class JSONAnalyzerTestCase method testMaxStringLengthInsideAnArrayFail.
@Test(expectedExceptions = APIMThreatAnalyzerException.class)
public void testMaxStringLengthInsideAnArrayFail() throws Exception {
JSONConfig config = Mockito.mock(JSONConfig.class);
Mockito.when(config.getMaxStringLength()).thenReturn(5);
JSONAnalyzer analyzer = new JSONAnalyzer();
analyzer.configure(config);
String jsonString = "{\"abcdef\": [1, \"123456\", 3, 4, 5, 6]}";
analyzer.analyze(jsonString, "/foo");
}
use of org.wso2.carbon.apimgt.ballerina.threatprotection.configurations.JSONConfig in project carbon-apimgt by wso2.
the class XMLAnalyzerTestCase method testConfigureAnalyzerException.
@Test(expectedExceptions = UnsupportedOperationException.class)
public void testConfigureAnalyzerException() throws Exception {
XMLAnalyzer analyzer = new XMLAnalyzer();
JSONConfig config = new JSONConfig();
analyzer.configure(config);
}
use of org.wso2.carbon.apimgt.ballerina.threatprotection.configurations.JSONConfig in project carbon-apimgt by wso2.
the class ConfigureJsonAnalyzer method execute.
@Override
public BValue[] execute(Context context) {
String event = getStringArgument(context, 0);
// configure json analyzer
BStruct jsonInfo = ((BStruct) getRefArgument(context, 0));
if (jsonInfo != null) {
String jsonPolicyId = jsonInfo.getStringField(0);
switch(event) {
case THREAT_PROTECTION_POLICY_ADD:
case THREAT_PROTECTION_POLICY_UPDATE:
String name = jsonInfo.getStringField(1);
int propertyCount = (int) jsonInfo.getIntField(0);
int stringLength = (int) jsonInfo.getIntField(1);
int arrayElementCount = (int) jsonInfo.getIntField(2);
int keyLength = (int) jsonInfo.getIntField(3);
int maxJSONDepth = (int) jsonInfo.getIntField(4);
JSONConfig jsonConfig = new JSONConfig();
jsonConfig.setName(name);
jsonConfig.setMaxPropertyCount(propertyCount);
jsonConfig.setMaxStringLength(stringLength);
jsonConfig.setMaxArrayElementCount(arrayElementCount);
jsonConfig.setMaxKeyLength(keyLength);
jsonConfig.setMaxJsonDepth(maxJSONDepth);
// put into ConfigurationHolder
ConfigurationHolder.addJsonConfig(jsonPolicyId, jsonConfig);
break;
case THREAT_PROTECTION_POLICY_DELETE:
ConfigurationHolder.removeJsonConfig(jsonPolicyId);
break;
default:
log.warn("Unknown event type for Threat Protection Policy. Event: " + event);
break;
}
}
return getBValues(new BBoolean(true));
}
use of org.wso2.carbon.apimgt.ballerina.threatprotection.configurations.JSONConfig in project carbon-apimgt by wso2.
the class JSONAnalyzerTestCase method testMaxFieldLengthPass.
@Test
public void testMaxFieldLengthPass() throws Exception {
JSONConfig config = Mockito.mock(JSONConfig.class);
Mockito.when(config.getMaxKeyLength()).thenReturn(5);
JSONAnalyzer analyzer = new JSONAnalyzer();
analyzer.configure(config);
String jsonString = "{\"abcde\": [1, 2, 3, 4, 5]}";
analyzer.analyze(jsonString, "/foo");
}
use of org.wso2.carbon.apimgt.ballerina.threatprotection.configurations.JSONConfig in project carbon-apimgt by wso2.
the class JSONAnalyzerTestCase method testMaxArrayElementCountFail.
@Test(expectedExceptions = APIMThreatAnalyzerException.class)
public void testMaxArrayElementCountFail() throws Exception {
JSONConfig config = Mockito.mock(JSONConfig.class);
Mockito.when(config.getMaxArrayElementCount()).thenReturn(5);
JSONAnalyzer analyzer = new JSONAnalyzer();
analyzer.configure(config);
String jsonString = "{\"a\": [1, 2, 3, 4, 5, 6]}";
analyzer.analyze(jsonString, "/foo");
}
Aggregations