Skip to content

Commit

Permalink
CORS: format errors according to API type
Browse files Browse the repository at this point in the history
JSONRPC and normal REST do not encode the error exactly the same
CORS error were only encoded according to REST format.
  • Loading branch information
tardyp committed Aug 7, 2016
1 parent 4ed5611 commit 814c02c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
6 changes: 6 additions & 0 deletions master/buildbot/test/unit/test_www_rest.py
Expand Up @@ -169,6 +169,12 @@ def test_cors_origin_mismatch(self):
yield self.render_resource(self.rsrc, '/', origin='h://bad')
self.assertNotOk('invalid origin')

@defer.inlineCallbacks
def test_cors_origin_mismatch_post(self):
yield self.render_resource(self.rsrc, '/', method='POST', origin='h://bad')
self.assertRequest(content=json.dumps({'error': {'message': 'invalid origin'}}),
responseCode=400)

@defer.inlineCallbacks
def test_cors_origin_preflight_match_GET(self):
yield self.render_resource(self.rsrc, '/',
Expand Down
6 changes: 5 additions & 1 deletion master/buildbot/www/rest.py
Expand Up @@ -426,7 +426,11 @@ def writeError(msg, errcode=400):
log.msg("HTTP error: %s" % (msg,))
request.setResponseCode(errcode)
request.setHeader('content-type', 'text/plain; charset=utf-8')
request.write(json.dumps(dict(error=msg)))
if request.method == 'POST':
# jsonRPC callers want the error message in error.message
request.write(json.dumps(dict(error=dict(message=msg))))
else:
request.write(json.dumps(dict(error=msg)))
request.finish()
return self.asyncRenderHelper(request, self.asyncRender, writeError)

Expand Down

0 comments on commit 814c02c

Please sign in to comment.