Node.js based forum software built for the modern web
feat: server-side rate limiting in intents.query
Julian Lamec232d328c7b
diff --git a/public/src/modules/intents.js b/public/src/modules/intents.jsindex bf8422bd64..76c8ad98a7 100644--- a/public/src/modules/intents.js+++ b/public/src/modules/intents.js@@ -82,14 +82,10 @@ export async function refresh(handle) {}handle = handle.trim().replace(/^@/, '');- try {- const result = await get(`/api/v3/intents/query/${handle}`);- if (result && result.intents && typeof result.intents === 'object') {- save(handle, result.intents);- return { intents: Object.keys(result.intents) };- }- } catch (e) {- // Network or server error — handle may not support intents+ const result = await get(`/api/v3/intents/query/${handle}`);+ if (result && result.intents && typeof result.intents === 'object') {+ save(handle, result.intents);+ return { intents: Object.keys(result.intents) };}return null;}@@ -138,7 +134,7 @@ export function register() {const html = await app.parseAndTranslate('modals/intents/register', 'handles', { handles });modal.find('#intents-registered-list').html(html);} catch (e) {- alerts.error('[[intents:register-error]]');+ alerts.error(e.message);} finally {handleInput.val('');submitBtn.prop('disabled', false);@@ -186,6 +182,8 @@ export function trigger(intent, parameters) {const handle = $(this).attr('data-handle');const intents = map.get(handle);const template = intents && intents[requiredIntent];++ // todo: replace placeholders in the template with data from parameters. The properties and placeholders are identically named.if (template) {window.location.href = template;}diff --git a/src/controllers/write/intents.js b/src/controllers/write/intents.jsindex ab3fec5d26..a17728b1d2 100644--- a/src/controllers/write/intents.js+++ b/src/controllers/write/intents.js@@ -1,12 +1,29 @@'use strict';+const db = require('../../database');+const Intents = module.exports;const activitypub = require('../../activitypub');const helpers = require('../helpers');+const RATE_LIMIT_WINDOW = 5000; // 5 seconds++async function checkRateLimit(ip) {+ const key = `locks:intents:query:${ip}`;+ const exists = await db.exists(key);+ if (exists) {+ throw new Error('[[error:api.429]]');+ }+ await db.set(key, '1');+ await db.pexpire(key, RATE_LIMIT_WINDOW);+}+Intents.query = async (req, res) => {+ const ip = req.ip || req.connection.remoteAddress;+ await checkRateLimit(ip);+let { handle } = req.params;handle = handle.trim().replace(/^@/, '');const webfinger = await activitypub.helpers.query(handle);