I am sending a POST request from an iOS client
-(void)loadFavorite:(NSArray*)favorites{
//data and url preparation
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"https://example.com" forHTTPHeaderField: @"Referer"];
[request setValue:[NSString stringWithFormat:@"%tu", [requestData length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody: requestData];
if ([Tools isNetWorkConnectionAvailable]) {
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
//response handle
}
}
Here is the response :
<div id="summary">
<h1>Forbidden <span>(403)</span></h1>
<p>CSRF verification failed. Request aborted.</p>
</div>
I'm using Flask framework and pythonanywhere for hosting.
It works fine when I reload the python script but after few hours/days the CSRF verification failed error reappear.
Even if I try to disable the CSRF verification in my app.py with :
app.config['WTF_CSRF_CHECK_DEFAULT'] = False
App.py script :
//some import error handlers ...
app = Flask(__name__)
app.config['WTF_CSRF_CHECK_DEFAULT'] = False
@app.route('/api/favorites', methods=['POST'])
def get_favorites_beaches():
if not request.json or not 'favorite' in request.json:
abort(400)
//data process
if __name__ == '__main__':
app.run(host='0.0.0.0',debug=True)
How can I implement the CSRF verification correctly or how to disable it ?
you have a line in your code that is
[request setValue:@"https://example.com" forHTTPHeaderField: @"Referer"];
did you not set it to the correct url? A wrong referer is one way you would get a cross site error.
Conrad