Unable to request resources using http.send - open-policy-agent

When I run the following code:
data := response {
response := http.send({
"method" : "GET",
"url": "https://httpbin.org/status/200"
})
}
I get this error:
1 error occurred: policy.rego:4: rego_type_error: unsafe built-in function calls in expression: http.send
Im using the rego play to run that policy. https://play.openpolicyagent.org/p/iqK8Zt5L62

I believe that is specific to the playground, as allowing arbitrary HTTP requests to be sent from there could potentially be misused. Your rule should work fine in any other context.

Related

dart json.encode(data) can not accept other language

I am currently doing web development with dart.
Implemented service with mockclient.
However, the following error occurs.
The implementation code below is an in memory web api service that inherits mockClient.
The code that calls client.send () and returns the result.
test_value is the result of json.encode (data).
var test_value = '{"id": 1, "type": "Appetizer", "name": "한글"}';
     return Response (test_value, 200, headers: {'content-type': 'application / json'});
ERROR
Invalid argument(s): String contains invalid characters.
dart:convert Latin1Codec.encode
package:http/src/response.dart 36:49 new Response
package:basil/common/mock_rest/mock_recipe.dart 40:12 MockRecipe._handler
If you put an English string in the name of the above implementation code, there is no error.
Why do I get an error when I insert a character other than English?
Please let me know if you know!
A dart programmer struggling alone in Korea
The Response class uses Latin-1 encoding for the body unless something else is specified.
This is not documented clearly on the constructor itself, but the documentation on the body getter does suggest this.
Try setting the charset/encoding in the header, e.g., as:
return Response(test_value, 200, headers: {
HttpHeaders.contentTypeHeader: 'application/json; charset=utf-8'
});

Slack API slash command - returns 502_service_error after the webhook is successfully used

I built a slash command that I run, my server responds using the webhook URL the slash command sends out, the message posts to the channel, but then Slack shows a private message saying "Darn - that slash command didn't work (error message: 502_service_error)"
What's going on that's making Slack think my command failed? I tried adding an immediate response and this error still happens.
The code is an AWS Lambda function, the Slash Command is calling an AWS API Gateway to access it.
Here's my Python code which uses requests to return the data -
response = requests.post(
urllib.parse.unquote(response_hook), json={'attachments':[{'text': result, 'color': 'good'}], 'response_type': 'in_channel'},
headers={'Content-Type': 'application/json'}
)
Ended up figuring out the answer after more digging. I needed to add a specific response at the end of my function that let Slack know the message was received successfully. Below is the Python code I used to do that which resolved the issue -
return { "isBase64Encoded": True, "statusCode": 200, "headers": { }, "body": "" }
You may have to enable Lambda Proxy Integration in your API Gateway settings for this to work, that was enabled by default for me though.
The accepted answer is correct, Slack expects a '200' response with a slash command. Using the included 'callback' argument in the handler function of the lambda that receives the slash command works also.
callback(null);
The first argument to the callback is an error, so pass 'null' if ok.
AWS doc
Slack documentation

silex abort() on BootableProviderInterface ignores provided code, response has 200 always

I have a silex BootableProvider to check for some requirements on every request before executing calls to any of my end points. All works good, except that when I use the $app->abort('403', 'forbidden for some reason'); anywhere in the boot() method, the response always returns code 200. The 'forbidden' message is correctly displayed and execution is interrupted as expected fortunately, but not having a meaningful Status code on the response makes it hard/cumbersome to process these failure responses.
I'm using silex 2.0.4.
If i however, execute the $app->abort(...) from any of my endpoints which implement ControllerProviderInterface, in the same request thread, the responses have the correct response codes I specify in the abort, so I'm thinking it's a timing issue.
Any advise is greatly appreciated.
Sample Code:
class BootProvider implements BootableProviderInterface {
function boot(Application $app) {
$app->abort(403, 'not allowed');
}
}
...
$app->register(new My\Api\BootProvider());
I've tried passing headers to override the Status to no avail.
Thanks!
In you case you should get just exception Fatal error: Uncaught exception.... Service providers are booted before kernel handle cycle, so errors are not processed.
Try to add event listener or middleware that will be executed before controllers and will make all checks.
$app->before(function (Request $request, Application $app) {
$app->abort(403, 'not allowed');
});
http://silex.sensiolabs.org/doc/2.0/middlewares.html

Ajax Error callback not firing for iPhone

Error callback does not work if async request is sent but success does! and it works perfectly fine in Android and browser.
In iPhone it works for synchronous request. here is my code.
other apis work perfectly fine.
$.ajax({
type: 'POST',
url: "https://api.cloud.appcelerator.com/v1/users/login.json?key=xxxxxxxxx",
data: {
"login": useremail,
"password": password
},
success: function (resp) {
console.log(resp);
console.log('User logged-in successfully');
},
error: function (e) {
console.log(e)
}
});
API returns status code 200 for correct email and password but 401 for incorrect one so if status code is 200 its works well I get response in success.
This seems to be a very common issue with Cordova + iOS + jQuery combo.
Seems like there are few ways to resolve this 401 error response handling issue. One is to add timeout time out attribute while making AJAX request and handling the error. The other approach is to handle it in the server side by sending request over HTTPS and returning back authentication token with error details in case of 401 error.
Request you to have a look at this post for more info.
Also currently you cannot differentiate these two errors (401 and 408 errors) in iOS as i could see this defect still open in official Apache Cordova Bug Tracking System.Request you to check out this bug

firefox addon-sdk : handle http request timeout

I'm building a firefox add-on using the add-on sdk. I need to make a http request to a certain page and I want to handle the connection timeout but couldn't find anything in the api: https://addons.mozilla.org/en-US/developers/docs/sdk/latest/modules/sdk/request.html
What I'm actually looking is a callback in case the client couldn't connect to the server.
Is there a way to achieve this?
The SDK request will always call onComplete, when the request is considered done for the network. This means that onComplete is called in any case, disregarding if the request returned an error or a success.
In order to detect which error you've got, you need to check the Response object's (the object passed to the onComplete function) property "status" (response.status). It holds the status code for the request. To look up status codes, consider the list on the mozilla developer network. If the response status is 0, the request has failed completely and the user is probably offline, or the target couldn't be reached.
A timeout would either be a status code 504 or 0. The implementation would be similar to this:
var Request = require("sdk/request");
Request({
url: "http://foo.bar/request.target",
onComplete: function(response) {
if(response.status==0||response.status==504) {
// do connection timeout handling
}
// probably check for other status codes
else {
// assume the request went well
}
}
}).get();
I personally use a validation function on the request object, which returns me a number which depends whether I've got a correct response, an error from the web server or a connection issue (4xx and 0 status codes).

Resources