@faable/faable 4.40.0 → 4.41.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/commands/deploy/waf/add_rule.js +27 -2
- package/dist/commands/deploy/waf/format.js +28 -3
- package/dist/commands/deploy/waf/index.js +4 -2
- package/dist/commands/deploy/waf/limit.js +104 -0
- package/dist/commands/deploy/waf/rate.js +41 -0
- package/dist/commands/deploy/waf/rm.js +1 -1
- package/package.json +1 -1
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { log } from '../../../log.js';
|
|
2
|
+
import { limit_label, example_path } from './format.js';
|
|
2
3
|
|
|
3
4
|
/** How a rule reads in a sentence, for the confirmation the user gets back. */
|
|
4
5
|
const describe_selector = (s) => {
|
|
@@ -25,7 +26,7 @@ const rule_matches_request = (rule, want, action) => rule.action === action &&
|
|
|
25
26
|
* verify, explain, tell the user how to check) lives here once.
|
|
26
27
|
*/
|
|
27
28
|
const add_rule = async (opts) => {
|
|
28
|
-
const { api, app_id, app_name, app_url, pattern, user_agent, query, action, description, force, json } = opts;
|
|
29
|
+
const { api, app_id, app_name, app_url, pattern, user_agent, query, action, description, force, json, rate, per } = opts;
|
|
29
30
|
const want = { pattern, user_agent, query };
|
|
30
31
|
const waf = await api.addAppWafRule(app_id, {
|
|
31
32
|
pattern,
|
|
@@ -33,7 +34,8 @@ const add_rule = async (opts) => {
|
|
|
33
34
|
query,
|
|
34
35
|
action,
|
|
35
36
|
description,
|
|
36
|
-
force
|
|
37
|
+
force,
|
|
38
|
+
...(action === 'limit' ? { rate, per } : {})
|
|
37
39
|
});
|
|
38
40
|
// Read the rule back before telling the user it worked.
|
|
39
41
|
//
|
|
@@ -62,6 +64,29 @@ const add_rule = async (opts) => {
|
|
|
62
64
|
}
|
|
63
65
|
if (json)
|
|
64
66
|
return waf;
|
|
67
|
+
if (action === 'limit') {
|
|
68
|
+
const stored = waf.rules.find(r => rule_matches_request(r, want, 'limit'));
|
|
69
|
+
const label = stored?.rate
|
|
70
|
+
? limit_label(stored.rate, stored.per)
|
|
71
|
+
: 'rate limited';
|
|
72
|
+
log.info(`⏱️ ${describe_selector(want)} → ${label}`);
|
|
73
|
+
log.info(` for ${app_name} (${app_id}).`);
|
|
74
|
+
log.info(``);
|
|
75
|
+
log.info(`Over the limit, Faable answers 429 at the edge: those requests do not reach or wake your app.`);
|
|
76
|
+
if (stored?.per === 'rule') {
|
|
77
|
+
log.info(`The count is shared by every client of each of the app's domains, not kept per IP.`);
|
|
78
|
+
}
|
|
79
|
+
if (user_agent && !pattern) {
|
|
80
|
+
log.info(``);
|
|
81
|
+
log.info(`⚠️ A user-agent rule has no path restriction: it counts every request from that client.`);
|
|
82
|
+
}
|
|
83
|
+
log.info(``);
|
|
84
|
+
log.info(`It takes about 20s to reach the edge. Then check it with:`);
|
|
85
|
+
log.info(` for i in $(seq 1 ${(stored?.rate?.burst ?? 10) + 5}); do curl -s -o /dev/null -w '%{http_code} ' ${user_agent ? `-A '${user_agent}' ` : ''}'https://${app_url}${example_path(pattern, query)}'; done; echo`);
|
|
86
|
+
log.info(``);
|
|
87
|
+
log.info(`Undo: faable deploy waf rm ${undo_args(want)} --action limit -a ${app_id}`);
|
|
88
|
+
return waf;
|
|
89
|
+
}
|
|
65
90
|
if (action === 'allow') {
|
|
66
91
|
// The one rule that opens instead of closing: say exactly what it opens.
|
|
67
92
|
log.info(`🔓 ${describe_selector(want)} → reaches your app`);
|
|
@@ -9,7 +9,9 @@ const action_label = (action) => action === 'sink'
|
|
|
9
9
|
? 'allowed (reaches your app where Faable blocks it for every app)'
|
|
10
10
|
: action === 'monitor'
|
|
11
11
|
? 'monitor only (nothing blocked; the Firewall tab shows what it would stop)'
|
|
12
|
-
: action
|
|
12
|
+
: action === 'limit'
|
|
13
|
+
? 'rate limited (429 at the edge over the limit)'
|
|
14
|
+
: action;
|
|
13
15
|
/**
|
|
14
16
|
* Render the effective WAF of an app.
|
|
15
17
|
*
|
|
@@ -51,7 +53,7 @@ const format_waf = (waf) => {
|
|
|
51
53
|
out.push(' (none)');
|
|
52
54
|
}
|
|
53
55
|
for (const r of waf.rules) {
|
|
54
|
-
out.push(` • ${rule_subject(r)} → ${action_label(r.action)}`);
|
|
56
|
+
out.push(` • ${rule_subject(r)} → ${r.action === 'limit' && r.rate ? limit_label(r.rate, r.per) : action_label(r.action)}`);
|
|
55
57
|
if (r.description)
|
|
56
58
|
out.push(` ${r.description}`);
|
|
57
59
|
}
|
|
@@ -78,5 +80,28 @@ const rule_subject = (r) => {
|
|
|
78
80
|
return `?${r.query} (any path)`;
|
|
79
81
|
return r.pattern ?? '(unknown)';
|
|
80
82
|
};
|
|
83
|
+
/** `10/min per IP (burst 20), then 429` — what a limit rule lets through. */
|
|
84
|
+
const limit_label = (rate, per) => {
|
|
85
|
+
const unit = rate.period === '1s' ? 's' : 'min';
|
|
86
|
+
const who = per === 'rule' ? 'shared by all clients' : 'per IP';
|
|
87
|
+
const burst = rate.burst !== rate.average ? ` (burst ${rate.burst})` : '';
|
|
88
|
+
return `${rate.average}/${unit} ${who}${burst}, then 429`;
|
|
89
|
+
};
|
|
90
|
+
/**
|
|
91
|
+
* A request path a rule matches, for the example `curl`: `^/api/login$` →
|
|
92
|
+
* `/api/login`. Only a plain literal path qualifies (escaped dots allowed);
|
|
93
|
+
* anything with regex syntax left gives `<path>`, the user's to fill in.
|
|
94
|
+
* A query rule gets its parameter, so the example actually matches.
|
|
95
|
+
*/
|
|
96
|
+
const example_path = (pattern, query) => {
|
|
97
|
+
const qs = query ? `?${query}=1` : '';
|
|
98
|
+
if (!pattern)
|
|
99
|
+
return `/${qs}`;
|
|
100
|
+
const literal = pattern
|
|
101
|
+
.replace(/^\^/, '')
|
|
102
|
+
.replace(/\$$/, '')
|
|
103
|
+
.replace(/\\\./g, '.');
|
|
104
|
+
return /^\/[A-Za-z0-9/_.~-]*$/.test(literal) ? `${literal}${qs}` : '<path>';
|
|
105
|
+
};
|
|
81
106
|
|
|
82
|
-
export { action_label, format_waf, rule_subject };
|
|
107
|
+
export { action_label, example_path, format_waf, limit_label, rule_subject };
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { waf_allow } from './allow.js';
|
|
2
2
|
import { waf_block } from './block.js';
|
|
3
|
+
import { waf_limit } from './limit.js';
|
|
3
4
|
import { waf_list } from './list.js';
|
|
4
5
|
import { waf_monitor } from './monitor.js';
|
|
5
6
|
import { waf_rm } from './rm.js';
|
|
@@ -7,15 +8,16 @@ import { waf_sink } from './sink.js';
|
|
|
7
8
|
|
|
8
9
|
const waf = {
|
|
9
10
|
command: 'waf <command>',
|
|
10
|
-
describe: 'Block or
|
|
11
|
+
describe: 'Block, silence or rate-limit requests at the edge, before the app wakes',
|
|
11
12
|
builder: yargs => yargs
|
|
12
13
|
.command(waf_list)
|
|
13
14
|
.command(waf_block)
|
|
14
15
|
.command(waf_sink)
|
|
15
16
|
.command(waf_monitor)
|
|
16
17
|
.command(waf_allow)
|
|
18
|
+
.command(waf_limit)
|
|
17
19
|
.command(waf_rm)
|
|
18
|
-
.demandCommand(1, 'Specify a waf command: list, block, sink, monitor, allow or rm'),
|
|
20
|
+
.demandCommand(1, 'Specify a waf command: list, block, sink, monitor, allow, limit or rm'),
|
|
19
21
|
handler: () => {
|
|
20
22
|
// Unreachable: demandCommand(1) either routes to a subcommand or fails
|
|
21
23
|
// through the global .fail() in src/index.ts.
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { requireApi } from '../../../api/context.js';
|
|
2
|
+
import { print_json, json_option } from '../../../lib/listing.js';
|
|
3
|
+
import { resolve_app_id } from '../resolve_app_id.js';
|
|
4
|
+
import { add_rule } from './add_rule.js';
|
|
5
|
+
import { parse_rate } from './rate.js';
|
|
6
|
+
|
|
7
|
+
const waf_limit = {
|
|
8
|
+
command: 'limit [pattern]',
|
|
9
|
+
describe: 'Rate-limit matching requests: over the limit they get a 429 at the edge',
|
|
10
|
+
builder: yargs => json_option(yargs)
|
|
11
|
+
.positional('pattern', {
|
|
12
|
+
type: 'string',
|
|
13
|
+
description: 'Anchored path regex, e.g. ^/api/login$ (quote it in your shell)'
|
|
14
|
+
})
|
|
15
|
+
.option('rate', {
|
|
16
|
+
alias: 'r',
|
|
17
|
+
type: 'string',
|
|
18
|
+
demandOption: true,
|
|
19
|
+
description: 'Requests allowed per second or per minute, e.g. 10/m or 5/s'
|
|
20
|
+
})
|
|
21
|
+
.option('burst', {
|
|
22
|
+
alias: 'b',
|
|
23
|
+
type: 'number',
|
|
24
|
+
description: 'Requests allowed at once before the rate applies (defaults to the rate)'
|
|
25
|
+
})
|
|
26
|
+
.option('per', {
|
|
27
|
+
choices: ['ip', 'rule'],
|
|
28
|
+
default: 'ip',
|
|
29
|
+
description: 'ip: each client IP has its own count. rule: one count shared by every client (per domain of the app).'
|
|
30
|
+
})
|
|
31
|
+
.option('user-agent', {
|
|
32
|
+
alias: 'u',
|
|
33
|
+
type: 'string',
|
|
34
|
+
description: 'RE2 fragment matched against User-Agent, e.g. GPTBot. With a path, BOTH must match.'
|
|
35
|
+
})
|
|
36
|
+
.option('query', {
|
|
37
|
+
alias: 'q',
|
|
38
|
+
type: 'string',
|
|
39
|
+
description: 'Literal query parameter name, e.g. s (not a regex). With a path, BOTH must match.'
|
|
40
|
+
})
|
|
41
|
+
.option('app', {
|
|
42
|
+
alias: 'a',
|
|
43
|
+
type: 'string',
|
|
44
|
+
description: 'App id, name or slug (defaults to the linked app)'
|
|
45
|
+
})
|
|
46
|
+
.option('description', {
|
|
47
|
+
alias: 'd',
|
|
48
|
+
type: 'string',
|
|
49
|
+
description: 'Why this rule exists (shown in `waf list`)'
|
|
50
|
+
})
|
|
51
|
+
.option('force', {
|
|
52
|
+
type: 'boolean',
|
|
53
|
+
default: false,
|
|
54
|
+
description: 'Accept a user-agent that also matches a known search/AI crawler or uptime monitor, or a query parameter that carries an auth flow'
|
|
55
|
+
})
|
|
56
|
+
.check(({ pattern, userAgent, query, rate, burst }) => {
|
|
57
|
+
if (userAgent && query) {
|
|
58
|
+
throw new Error('A rule can combine a path with --user-agent OR with --query, not both.');
|
|
59
|
+
}
|
|
60
|
+
if (!pattern && !userAgent && !query) {
|
|
61
|
+
throw new Error('Give a path pattern, --user-agent, --query, or a path with one of them.\n' +
|
|
62
|
+
" e.g. faable deploy waf limit '^/api/login$' --rate 10/m\n" +
|
|
63
|
+
' faable deploy waf limit --user-agent GPTBot --rate 60/m --per rule');
|
|
64
|
+
}
|
|
65
|
+
parse_rate(rate);
|
|
66
|
+
if (burst !== undefined && (!(burst >= 1) || burst > 20000)) {
|
|
67
|
+
throw new Error('--burst must be between 1 and 20000.');
|
|
68
|
+
}
|
|
69
|
+
return true;
|
|
70
|
+
})
|
|
71
|
+
.example("$0 deploy waf limit '^/api/login$' --rate 10/m", '10 login attempts per minute per IP; the 11th gets a 429')
|
|
72
|
+
.example("$0 deploy waf limit '^/search' --rate 5/s --burst 20", 'Searches at 5 per second per IP, with room for a burst of 20')
|
|
73
|
+
.example('$0 deploy waf limit --user-agent GPTBot --rate 60/m --per rule --force', 'Slow a crawler down to 60 requests a minute in total, instead of blocking it')
|
|
74
|
+
.epilogue('Requests within the limit reach your app as usual. Over it, Faable ' +
|
|
75
|
+
'answers 429 at the edge, so they do not wake a sleeping app. Your ' +
|
|
76
|
+
'own block and sink rules still win over a limit. Change a limit by ' +
|
|
77
|
+
'running the same command with a new --rate.')
|
|
78
|
+
.showHelpOnFail(false),
|
|
79
|
+
handler: async (args) => {
|
|
80
|
+
const ctx = await requireApi();
|
|
81
|
+
const app_id = await resolve_app_id(args.app, ctx.appId, ctx.api);
|
|
82
|
+
const app = await ctx.api.getApp(app_id);
|
|
83
|
+
const rate = parse_rate(args.rate);
|
|
84
|
+
const waf = await add_rule({
|
|
85
|
+
api: ctx.api,
|
|
86
|
+
app_id,
|
|
87
|
+
app_name: app.name,
|
|
88
|
+
app_url: app.url,
|
|
89
|
+
pattern: args.pattern,
|
|
90
|
+
user_agent: args.userAgent,
|
|
91
|
+
query: args.query,
|
|
92
|
+
action: 'limit',
|
|
93
|
+
rate: { ...rate, ...(args.burst ? { burst: args.burst } : {}) },
|
|
94
|
+
per: args.per,
|
|
95
|
+
description: args.description,
|
|
96
|
+
force: args.force,
|
|
97
|
+
json: args.json
|
|
98
|
+
});
|
|
99
|
+
if (args.json)
|
|
100
|
+
print_json(waf);
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
export { waf_limit };
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
const UNITS = {
|
|
2
|
+
s: '1s',
|
|
3
|
+
sec: '1s',
|
|
4
|
+
second: '1s',
|
|
5
|
+
seconds: '1s',
|
|
6
|
+
m: '1m',
|
|
7
|
+
min: '1m',
|
|
8
|
+
minute: '1m',
|
|
9
|
+
minutes: '1m'
|
|
10
|
+
};
|
|
11
|
+
const LONG_UNITS = new Set(['h', 'hr', 'hour', 'hours', 'd', 'day', 'days']);
|
|
12
|
+
/**
|
|
13
|
+
* `10/m` → `{ average: 10, period: '1m' }`.
|
|
14
|
+
*
|
|
15
|
+
* Per second and per minute only, and an hour is refused with the reason
|
|
16
|
+
* rather than as an unknown unit: the edge refills its counters several times
|
|
17
|
+
* an hour, so an hourly limit would not hold (the api says the same).
|
|
18
|
+
*/
|
|
19
|
+
const parse_rate = (raw) => {
|
|
20
|
+
const m = /^\s*(\d+)\s*\/\s*([a-z]+)\s*$/i.exec(String(raw));
|
|
21
|
+
if (!m) {
|
|
22
|
+
throw new Error(`Can't read --rate "${raw}". Use requests/unit, e.g. --rate 10/m (10 per minute) or --rate 5/s.`);
|
|
23
|
+
}
|
|
24
|
+
const average = Number(m[1]);
|
|
25
|
+
const unit = m[2].toLowerCase();
|
|
26
|
+
if (LONG_UNITS.has(unit)) {
|
|
27
|
+
throw new Error('A limit counts per second or per minute, not per hour or day: the edge ' +
|
|
28
|
+
'refills its counters several times an hour, so it would not hold.\n' +
|
|
29
|
+
' Use a per-minute rate, e.g. --rate 2/m instead of 100/h.');
|
|
30
|
+
}
|
|
31
|
+
const period = UNITS[unit];
|
|
32
|
+
if (!period) {
|
|
33
|
+
throw new Error(`Unknown unit "${m[2]}" in --rate. Use /s (per second) or /m (per minute).`);
|
|
34
|
+
}
|
|
35
|
+
if (average < 1 || average > 10000) {
|
|
36
|
+
throw new Error('--rate must be between 1 and 10000 requests per unit.');
|
|
37
|
+
}
|
|
38
|
+
return { average, period };
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
export { parse_rate };
|
|
@@ -27,7 +27,7 @@ const waf_rm = {
|
|
|
27
27
|
})
|
|
28
28
|
.option('action', {
|
|
29
29
|
type: 'string',
|
|
30
|
-
choices: ['deny', 'sink', 'allow', 'monitor'],
|
|
30
|
+
choices: ['deny', 'sink', 'allow', 'monitor', 'limit'],
|
|
31
31
|
description: 'Only needed when the same rule exists as both'
|
|
32
32
|
})
|
|
33
33
|
.option('app', {
|