Hi All, attached code is not working. It's going into if loop even query Param's not present in the request. Please suggest anyone knows.
Solved! Go to Solution.
Glad to hear you resolved the issue using request.queryparams.count - nice work! 🎉
On your point about logging query parameters: while it's helpful to store them in a single variable, it's worth being cautious with this approach. Some query parameters, like access_token or api_key, might contain sensitive data or PII. Logging them directly could expose this information, which might lead to compliance and security issues.
Here’s an approach that handles this safely:
Sensitive Data Masking:
Handling Duplicate Parameters:
Here’s an example of how the code works:
var queryParamCount = context.getVariable("request.queryparams.count");
if (queryParamCount > 0) {
var queryParamNamesString = context.getVariable("request.queryparams.names.string"); // Comma-separated string of query parameter names
var sensitiveQueryParamNames = context.getVariable("sensitiveQueryParamNames") || "access_token,api_key"; // Comma-separated sensitive parameter names
var sensitiveParams = sensitiveQueryParamNames.split(",").map(function (param) {
return param.trim().toLowerCase(); // Normalize to lowercase for case-insensitive comparison
});
var queryParamObject = {};
// Split the comma-separated list of parameter names
var queryParamNamesArray = queryParamNamesString.split(",");
for (var i = 0; i < queryParamNamesArray.length; i++) {
var paramName = queryParamNamesArray[i].trim();
var paramNameLower = paramName.toLowerCase(); // Normalize to lowercase
if (sensitiveParams.includes(paramNameLower)) {
// Mask sensitive parameter values
queryParamObject[paramName] = "***";
} else {
// Collect all values for the parameter
var paramValuesCount = context.getVariable("request.queryparam." + paramName + ".values.count");
var paramValuesArray = [];
for (var j = 1; j <= paramValuesCount; j++) {
var value = context.getVariable("request.queryparam." + paramName + "." + j);
paramValuesArray.push(value);
}
// Join multiple values into a single string
queryParamObject[paramName] = paramValuesArray.join(",");
}
}
context.setVariable("ProxyReqQueryParams", JSON.stringify(queryParamObject));
}
For a query string like:
param1=value1¶m1=value2&ACCESS_TOKEN=12345, and with default sensitiveQueryParamNames, this would log: { "param1": "value1,value2", "ACCESS_TOKEN": "***" }
This way, you can log parameters safely while avoiding sensitive data exposure!
For more details on securing Apigee and best practices, you might find my blog helpful: Apigee Best Security Practices.
User | Count |
---|---|
1 | |
1 | |
1 | |
1 | |
1 |