Skip to content

Open-source social media scheduler built for AI agents and the teams behind them. Create, schedule and publish posts to every network through MCP or the REST API, or write them yourself in a composer your team will love.

Stop rate limiting MCP OAuth discovery and trust only real proxies (#432)

Paulo Castellano04a502ddeadb
diff --git a/.env.example b/.env.example
index 06bc0934d..3112b931e 100644
--- a/.env.example
+++ b/.env.example
@@ -8,6 +8,11 @@ APP_URL=http://localhost
# Defaults to APP_URL; set a tunnel URL (e.g. ngrok) for local development.
WEBHOOK_URL=
+# Proxies whose X-Forwarded-* headers are trusted. Defaults to Cloudflare's
+# ranges; behind another proxy, list its IPs or ranges (comma-separated),
+# or * for any proxy (a client can then fake its IP).
+# TRUSTED_PROXIES=
+
# Self-hosted mode (skips payment requirements)
SELF_HOSTED=true
diff --git a/AGENTS.md b/AGENTS.md
index 5f9131726..b9dc8bdf1 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -862,6 +862,22 @@ ChatGPT offers sign-in inside the conversation when a user who never connected (
- **Every tool declares `securitySchemes`** (`oauth2`, `mcp:use`) through `ListToolsWithSecuritySchemes`; ChatGPT offers sign-in only for tools that declare it, other clients ignore it.
- `tests/Feature/Mcp/ChatGptSignInTest.php` covers ChatGPT, Claude and other clients with no token, an invalid token and a valid one, and the cached ranges; every guard there fails its test when removed.
+## MCP OAuth rate limit
+
+All of `Mcp::oauthRoutes()` sits in one `throttle:mcp-oauth` group in `routes/ai.php`, and the `mcp-oauth` limiter (`AppServiceProvider`) limits only what can be abused: `POST /oauth/register` (public dynamic client registration, which writes an `oauth_clients` row), 30 a minute per IP. It never limits ChatGPT and Claude (the egress ranges OpenAI and Anthropic publish: `chatgpt-connectors.json` through `ListChatGptEgressRanges`, and `trypost.mcp_claude_ip_ranges`): each registers for all of its users from a few shared addresses, so a per-IP limit would hold every user to one budget (owner decision October 2026: we trust their ranges).
+
+- The `/.well-known/oauth-protected-resource*` and `/.well-known/oauth-authorization-server*` GETs (and HEADs, `isMethodCacheable()`) are public static JSON and are never limited. Clients fetch them before every connection, even with a stored token (Codex desktop does it for each thread, from the user's own IP), and a 429 there makes the client drop the server, so its tools never appear.
+- The exemption reads `$request->ip()`, so it holds as long as only real proxies are trusted (see "Trusted proxies"). With `TRUSTED_PROXIES=*` a client can fake an OpenAI or Anthropic address with `X-Forwarded-For`.
+- `tests/Feature/Mcp/OAuthRegistrationTest.php` covers the limit, the exemption and the metadata.
+
+## Trusted proxies (`config/trustedproxy.php`)
+
+The client IP (`$request->ip()`, used by every per-IP rate limit and by the ChatGPT/Claude ranges) is only as good as the proxies Laravel trusts. There is one setting, `trustedproxy.proxies`, the key Laravel's `TrustProxies` reads; `bootstrap/app.php` does not call `trustProxies()` (owner decision October 2026).
+
+- It defaults to the ranges Cloudflare publishes (https://www.cloudflare.com/ips-v4 and https://www.cloudflare.com/ips-v6), written in the config file. TryPost Cloud runs on one machine behind Cloudflare, so the IP is the one Cloudflare appended and a client cannot fake it. When Cloudflare changes its lists (rarely), update the config.
+- Another install sets `TRUSTED_PROXIES` to its proxy's IPs or ranges (comma-separated), or `*` to trust any proxy, which lets a client fake its IP with `X-Forwarded-For`. Never `trustProxies(at: '*')` in code: that was the bug.
+- `tests/Feature/TrustedProxiesTest.php` covers Cloudflare (IPv4 and IPv6), a direct request, a listed proxy and `*`.
+
## MCP tool annotations
**Every MCP tool declares all three annotations explicitly, `false` included** (owner decision October 2026): `#[IsReadOnly(bool)]`, `#[IsDestructive(bool)]` and `#[IsOpenWorld(bool)]`, above its `#[Description]`. The OpenAI plugin directory refuses ("Not live") any tool without three explicit booleans, and the Claude directory requires `readOnlyHint` or `destructiveHint`. Annotations only describe the tool to the AI client (when to ask the user for confirmation); they never change what it does and are not rate limits.
diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php
index be65acf6d..d69f640ee 100644
--- a/app/Providers/AppServiceProvider.php
+++ b/app/Providers/AppServiceProvider.php
@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace App\Providers;
+use App\Actions\Mcp\ListChatGptEgressRanges;
use App\Listeners\StripeEventListener;
use App\Models\AccessToken;
use App\Models\Account;
@@ -69,6 +70,7 @@ use SocialiteProviders\LinkedIn\LinkedInExtendSocialite;
use SocialiteProviders\Manager\SocialiteWasCalled;
use SocialiteProviders\Pinterest\PinterestExtendSocialite;
use SocialiteProviders\TikTok\TikTokExtendSocialite;
+use Symfony\Component\HttpFoundation\IpUtils;
class AppServiceProvider extends ServiceProvider
{
@@ -168,8 +170,11 @@ class AppServiceProvider extends ServiceProvider
});
RateLimiter::for(
- 'mcp-oauth-registration',
- fn (Request $request): Limit => Limit::perMinute(30)->by($request->ip()),
+ 'mcp-oauth',
+ fn (Request $request): Limit => $request->isMethodCacheable()
+ || IpUtils::checkIp((string) $request->ip(), [...ListChatGptEgressRanges::execute(), ...config()->array('trypost.mcp_claude_ip_ranges')])
+ ? Limit::none()
+ : Limit::perMinute(30)->by($request->ip()),
);
RateLimiter::for(
diff --git a/bootstrap/app.php b/bootstrap/app.php
index 285a9ffeb..df4c31d6b 100644
--- a/bootstrap/app.php
+++ b/bootstrap/app.php
@@ -24,8 +24,6 @@ return Application::configure(basePath: dirname(__DIR__))
health: '/up',
)
->withMiddleware(function (Middleware $middleware): void {
- $middleware->trustProxies(at: '*');
-
$middleware->encryptCookies(except: ['sidebar_state']);
$middleware->web(append: [
diff --git a/config/trustedproxy.php b/config/trustedproxy.php
new file mode 100644
index 000000000..e00a5b4b0
--- /dev/null
+++ b/config/trustedproxy.php
@@ -0,0 +1,46 @@
+<?php
+
+declare(strict_types=1);
+
+return [
+
+ /*
+ |--------------------------------------------------------------------------
+ | Trusted proxies
+ |--------------------------------------------------------------------------
+ |
+ | The proxies whose X-Forwarded-* headers Laravel trusts. By default these
+ | are the ranges Cloudflare publishes (https://www.cloudflare.com/ips-v4
+ | and https://www.cloudflare.com/ips-v6), so behind Cloudflare the client
+ | IP is the one Cloudflare saw and cannot be faked. Behind another proxy,
+ | set TRUSTED_PROXIES to its IPs or ranges (comma-separated), or "*" to
+ | trust any proxy, which also trusts an X-Forwarded-For a client sends.
+ |
+ */
+
+ 'proxies' => env('TRUSTED_PROXIES', [
+ '173.245.48.0/20',
+ '103.21.244.0/22',
+ '103.22.200.0/22',
+ '103.31.4.0/22',
+ '141.101.64.0/18',
+ '108.162.192.0/18',
+ '190.93.240.0/20',
+ '188.114.96.0/20',
+ '197.234.240.0/22',
+ '198.41.128.0/17',
+ '162.158.0.0/15',
+ '104.16.0.0/13',
+ '104.24.0.0/14',
+ '172.64.0.0/13',
+ '131.0.72.0/22',
+ '2400:cb00::/32',
+ '2606:4700::/32',
+ '2803:f800::/32',
+ '2405:b500::/32',
+ '2405:8100::/32',
+ '2a06:98c0::/29',
+ '2c0f:f248::/32',
+ ]),
+
+];
diff --git a/config/trypost.php b/config/trypost.php
index ee9281671..69c3fb12d 100644
--- a/config/trypost.php
+++ b/config/trypost.php
@@ -79,6 +79,19 @@ return [
'mcp_chatgpt_ip_ranges_url' => env('MCP_CHATGPT_IP_RANGES_URL', 'https://openai.com/chatgpt-connectors.json'),
+ /*
+ |--------------------------------------------------------------------------
+ | Claude MCP egress ranges
+ |--------------------------------------------------------------------------
+ |
+ | Claude calls MCP servers from the outbound ranges Anthropic publishes
+ | (https://platform.claude.com/docs/en/api/ip-addresses). Anthropic has
+ | no machine-readable list, so the ranges live here.
+ |
+ */
+
+ 'mcp_claude_ip_ranges' => explode(',', env('MCP_CLAUDE_IP_RANGES', '160.79.104.0/21')),
+
/*
|--------------------------------------------------------------------------
| Meta page walk budget
diff --git a/routes/ai.php b/routes/ai.php
index 8dce487cd..1b4134e9a 100644
--- a/routes/ai.php
+++ b/routes/ai.php
@@ -9,9 +9,7 @@ use App\Mcp\Servers\TryPostServer;
use Illuminate\Support\Facades\Route;
use Laravel\Mcp\Facades\Mcp;
-Route::middleware('throttle:mcp-oauth-registration')->group(function () {
- Mcp::oauthRoutes();
-});
+Route::middleware('throttle:mcp-oauth')->group(fn () => Mcp::oauthRoutes());
Mcp::web('/mcp/trypost', TryPostServer::class)
->middleware([ChallengeChatGptToolCalls::class, AuthenticateMcp::class.':api', LoadMcpWorkspace::class])
diff --git a/tests/Feature/Mcp/OAuthRegistrationTest.php b/tests/Feature/Mcp/OAuthRegistrationTest.php
index a35e8454a..4b20faaf3 100644
--- a/tests/Feature/Mcp/OAuthRegistrationTest.php
+++ b/tests/Feature/Mcp/OAuthRegistrationTest.php
@@ -61,6 +61,38 @@ test('dynamic oauth client registration is rate limited', function () {
$this->postJson('/oauth/register', $payload)->assertTooManyRequests();
});
+test('dynamic oauth client registration is not rate limited from chatgpt or claude', function (string $ip) {
+ fakeChatGptEgressRanges(['203.0.113.0/24']);
+
+ $payload = [
+ 'client_name' => 'MCP Client',
+ 'redirect_uris' => ['https://client.example/callback'],
+ 'grant_types' => ['authorization_code'],
+ 'response_types' => ['code'],
+ 'token_endpoint_auth_method' => 'none',
+ ];
+
+ for ($attempt = 0; $attempt < 31; $attempt++) {
+ $this->withServerVariables(['REMOTE_ADDR' => $ip])
+ ->postJson('/oauth/register', $payload)
+ ->assertSuccessful();
+ }
+})->with([
+ 'chatgpt' => '203.0.113.5',
+ 'claude' => '160.79.104.10',
+]);
+
+test('oauth discovery metadata is never rate limited', function (string $routeName, array $parameters) {
+ for ($attempt = 0; $attempt < 31; $attempt++) {
+ $this->getJson(route($routeName, $parameters))->assertSuccessful();
+ }
+})->with([
+ 'protected resource' => ['mcp.oauth.protected-resource', []],
+ 'nested protected resource' => ['mcp.oauth.protected-resource.nested', ['path' => 'mcp/trypost']],
+ 'authorization server' => ['mcp.oauth.authorization-server', []],
+ 'nested authorization server' => ['mcp.oauth.authorization-server.nested', ['path' => 'mcp/trypost']],
+]);
+
test('mcp custom schemes config lists every supported native agent', function () {
expect(config('mcp.custom_schemes'))->toEqual(mcpNativeAgentSchemes());
});
diff --git a/tests/Feature/TrustedProxiesTest.php b/tests/Feature/TrustedProxiesTest.php
new file mode 100644
index 000000000..160ec0b99
--- /dev/null
+++ b/tests/Feature/TrustedProxiesTest.php
@@ -0,0 +1,54 @@
+<?php
+
+declare(strict_types=1);
+
+use Illuminate\Http\Request;
+use Illuminate\Support\Facades\Route;
+
+const TRUSTED_PROXIES_CLOUDFLARE_IP = '172.68.1.10';
+const TRUSTED_PROXIES_CLIENT_IP = '203.0.113.9';
+const TRUSTED_PROXIES_FAKED_IP = '160.79.104.10';
+
+beforeEach(function () {
+ Route::get('/_test/client-ip', fn (Request $request): string => (string) $request->ip())
+ ->name('test.client-ip');
+
+ Route::getRoutes()->refreshNameLookups();
+});
+
+/**
+ * A proxy keeps the X-Forwarded-For the client sent and appends the address
+ * the request really came from.
+ */
+function trustedProxiesClientIp(object $test, string $remoteAddress): string
+{
+ return $test->withServerVariables(['REMOTE_ADDR' => $remoteAddress])
+ ->withHeader('X-Forwarded-For', TRUSTED_PROXIES_FAKED_IP.', '.TRUSTED_PROXIES_CLIENT_IP)
+ ->get(route('test.client-ip'))
+ ->assertSuccessful()
+ ->getContent();
+}
+
+test('cloudflare gives the client ip it appended, not one the client sent', function (string $cloudflareIp) {
+ expect(trustedProxiesClientIp($this, $cloudflareIp))->toBe(TRUSTED_PROXIES_CLIENT_IP);
+})->with([
+ 'ipv4' => TRUSTED_PROXIES_CLOUDFLARE_IP,
+ 'ipv6' => '2606:4700::1',
+]);
+
+test('a request that does not come from cloudflare keeps its own address', function () {
+ expect(trustedProxiesClientIp($this, '198.51.100.7'))->toBe('198.51.100.7');
+});
+
+test('another proxy is trusted when listed', function () {
+ config(['trustedproxy.proxies' => '10.0.0.0/8,192.168.1.1']);
+
+ expect(trustedProxiesClientIp($this, '10.1.2.3'))->toBe(TRUSTED_PROXIES_CLIENT_IP)
+ ->and(trustedProxiesClientIp($this, TRUSTED_PROXIES_CLOUDFLARE_IP))->toBe(TRUSTED_PROXIES_CLOUDFLARE_IP);
+});
+
+test('every proxy is trusted with a wildcard', function () {
+ config(['trustedproxy.proxies' => '*']);
+
+ expect(trustedProxiesClientIp($this, '198.51.100.7'))->toBe(TRUSTED_PROXIES_FAKED_IP);
+});