diff --git a/lib/httputils.js b/lib/httputils.js index 2491e5ee34a978b30c91aa53c4b69d772f89887d..671fa5a16554c0e36b46519611c4e774be7c9917 100644 --- a/lib/httputils.js +++ b/lib/httputils.js @@ -42,6 +42,16 @@ exports.forbidden = function(resp, reason) resp.end(); }; +exports.throttled = function(resp, reason) +{ + resp.writeHead(429, {"Content-Type": "text/plain"}); + resp.write("Too Many Requests"); + if (reason) { + resp.write(": " + reason); + } + resp.end(); +}; + exports.jsonResponse = function(resp, obj) { resp.writeHead(200, {"Content-Type": "application/json"}); diff --git a/lib/wsapi/stage_email.js b/lib/wsapi/stage_email.js index c5b562304f4ca262e5eff7b2e66e025bb435b783..9b4061bc8cba06187a672636e37eaaf85bf4dcdc 100644 --- a/lib/wsapi/stage_email.js +++ b/lib/wsapi/stage_email.js @@ -26,7 +26,7 @@ exports.process = function(req, res) { if (last && (new Date() - last) < config.get('min_time_between_emails_ms')) { logger.warn('throttling request to stage email address ' + req.body.email + ', only ' + ((new Date() - last) / 1000.0) + "s elapsed"); - return httputils.forbidden(res, "throttling. try again later."); + return httputils.throttled(res, "Too many emails sent to that address, try again later."); } try { diff --git a/lib/wsapi/stage_user.js b/lib/wsapi/stage_user.js index dc5f0aef04cb392a3e02de3f31de20f450bf2242..580838037c2d40eee2c74117eb1f93830fed5f9d 100644 --- a/lib/wsapi/stage_user.js +++ b/lib/wsapi/stage_user.js @@ -31,7 +31,7 @@ exports.process = function(req, resp) { if (last && (new Date() - last) < config.get('min_time_between_emails_ms')) { logger.warn('throttling request to stage email address ' + req.body.email + ', only ' + ((new Date() - last) / 1000.0) + "s elapsed"); - return httputils.forbidden(resp, "throttling. try again later."); + return httputils.throttled(resp, "Too many emails sent to that address, try again later."); } try { diff --git a/resources/static/shared/network.js b/resources/static/shared/network.js index a16b38df9b737888eb61b435ac5e0df12f4a7931..ce84f5616a89d131fbfccd70e9a04056af01f12e 100644 --- a/resources/static/shared/network.js +++ b/resources/static/shared/network.js @@ -186,8 +186,8 @@ BrowserID.Network = (function() { complete(onComplete, status.success); }, error: function(info) { - // 403 is throttling. - if (info.network.status === 403) { + // 429 is throttling. + if (info.network.status === 429) { complete(onComplete, false); } else complete(onFailure, info); @@ -392,8 +392,8 @@ BrowserID.Network = (function() { complete(onComplete, response.success); }, error: function(info) { - // 403 is throttling. - if (info.network.status === 403) { + // 429 is throttling. + if (info.network.status === 429) { complete(onComplete, false); } else complete(onFailure, info); diff --git a/resources/static/test/mocks/xhr.js b/resources/static/test/mocks/xhr.js index e74cccc1a24659f5dd930d61bbb5cee7bb8c0db2..98428bd107d5d68827c12efea6746c28f5133c76 100644 --- a/resources/static/test/mocks/xhr.js +++ b/resources/static/test/mocks/xhr.js @@ -52,7 +52,7 @@ BrowserID.Mocks.xhr = (function() { "post /wsapi/stage_user unknown_secondary": { success: true }, "post /wsapi/stage_user valid": { success: true }, "post /wsapi/stage_user invalid": { success: false }, - "post /wsapi/stage_user throttle": 403, + "post /wsapi/stage_user throttle": 429, "post /wsapi/stage_user ajaxError": undefined, "get /wsapi/user_creation_status?email=registered%40testuser.com pending": { status: "pending" }, "get /wsapi/user_creation_status?email=registered%40testuser.com complete": { status: "complete" }, @@ -79,7 +79,7 @@ BrowserID.Mocks.xhr = (function() { "post /wsapi/stage_email unknown_secondary": { success: true }, "post /wsapi/stage_email known_secondary": { success: true }, "post /wsapi/stage_email invalid": { success: false }, - "post /wsapi/stage_email throttle": 403, + "post /wsapi/stage_email throttle": 429, "post /wsapi/stage_email ajaxError": undefined, "post /wsapi/cert_key ajaxError": undefined, "get /wsapi/email_addition_status?email=registered%40testuser.com pending": { status: "pending" }, diff --git a/tests/email-throttling-test.js b/tests/email-throttling-test.js index db6a7d1a38193463945238af2bcc1bd0626bf402..ba807a7829aa692ca705946e767ec9c461a7a7de 100755 --- a/tests/email-throttling-test.js +++ b/tests/email-throttling-test.js @@ -52,7 +52,7 @@ suite.addBatch({ site:'fakesite.com' }), "is throttled": function(err, r) { - assert.strictEqual(r.code, 403); + assert.strictEqual(r.code, 429); } } }); @@ -101,8 +101,8 @@ suite.addBatch({ email: 'second@fakeemail.com', site:'fakesite.com' }), - "is throttled with a 403": function(err, r) { - assert.strictEqual(r.code, 403); + "is throttled with a 429": function(err, r) { + assert.strictEqual(r.code, 429); } } });