レート制限とブルートフォース攻撃対策
レート制限とブルートフォース攻撃対策を実装し、Node.js APIを不正利用、サービス拒否攻撃、認証情報スタッフィング攻撃から守ります。
「レート制限とブルートフォース攻撃対策」はCoddyKit上の無料Node.js Backend Development Bootcampレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはNode.js Backend Development Bootcamp学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Node.js Backend Development Bootcampコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Why Limit Requests?
Without limits, a single client can hammer your API thousands of times per second — scraping data, guessing passwords, or simply overloading the server.
Rate limiting caps how many requests a client may make in a time window.
Attacks Rate Limiting Prevents
Rate limiting is a frontline defense against:
- Brute-force login attempts
- Credential stuffing with leaked passwords
- Denial-of-service floods
- Scraping and API abuse
How Counting Works
A rate limiter tracks a counter per client (usually keyed by IP). Each request increments it; when the count exceeds the limit within the window, further requests are rejected with 429 Too Many Requests.
express-rate-limit
The express-rate-limit package adds rate limiting as middleware in a few lines. Configure the window and max requests.
const rateLimit = require('express-rate-limit');
const limiter = rateLimit({
windowMs: 15 * 60 * 1000,
max: 100
});Applying the Limiter
Apply globally with app.use, or to specific routes. Once over the limit, clients automatically receive a 429 response.
app.use(limiter);
// or just protect one route:
app.use('/api/', limiter);Stricter Limits on Login
Login endpoints are prime brute-force targets, so give them a tighter limit than the rest of your API.
const loginLimiter = rateLimit({
windowMs: 10 * 60 * 1000,
max: 5,
message: 'Too many login attempts'
});
app.post('/login', loginLimiter, handler);Shared Store for Multiple Servers
The default in-memory store does not work when you run multiple instances behind a load balancer — each has its own counter. Use a shared store like Redis so limits apply across all servers.
const RedisStore = require('rate-limit-redis');
const limiter = rateLimit({
store: new RedisStore({ /* client */ }),
max: 100,
windowMs: 60000
});Trusting the Real Client IP
Behind a proxy, every request appears to come from the proxy's IP. Tell Express to trust the proxy so the limiter keys on the real client IP from X-Forwarded-For.
app.set('trust proxy', 1);Account Lockout
Beyond IP limits, track failed logins per account. After several failures, temporarily lock the account or require a CAPTCHA — defeating distributed brute-force from many IPs.
if (user.failedAttempts >= 5) {
return res.status(423).json({ error: 'Account locked' });
}Slowing Down Instead of Blocking
An alternative to hard blocks is progressive delay: each repeated request waits a little longer. The express-slow-down package adds latency rather than rejecting outright.
const slowDown = require('express-slow-down');
const speedLimiter = slowDown({
windowMs: 60000,
delayAfter: 50,
delayMs: () => 500
});Informing Clients
Good limiters send RateLimit headers telling clients their remaining quota and reset time, so well-behaved apps can back off gracefully.
const limiter = rateLimit({
max: 100,
windowMs: 60000,
standardHeaders: true
});Quick Check
Test your rate-limiting knowledge.
Recap
You learned to protect APIs from abuse:
- Rate limiting caps requests per client and returns
429when exceeded express-rate-limitadds it as middleware; use stricter limits on login- Use a Redis store across multiple servers and set
trust proxyfor real IPs - Add account lockout, progressive slow-down, and informative headers
These layers thwart brute-force, scraping, and DoS attacks.
よくある質問
「レート制限とブルートフォース攻撃対策」レッスンは無料ですか?
はい。「レート制限とブルートフォース攻撃対策」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Node.js Backend Development Bootcampコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Node.js Backend Development Bootcampコースには全4レッスンが含まれています。
「レート制限とブルートフォース攻撃対策」で何を学びますか?
レート制限とブルートフォース攻撃対策を実装し、Node.js APIを不正利用、サービス拒否攻撃、認証情報スタッフィング攻撃から守ります。 ブラウザで直接実行するハンズオンコードでNode.js Backend Development Bootcampを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Node.js Backend Development Bootcampを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのNode.js Backend Development Bootcampは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。
「レート制限とブルートフォース攻撃対策」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このNode.js Backend Development Bootcampレッスンでコードを書いて実行できますか?
はい。すべてのNode.js Backend Development Bootcampレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- OWASP Top 10の理解
- Node.jsのセキュアコーディング
- データの暗号化とハッシュ化
- レート制限とブルートフォース攻撃対策