漏洞卡片#
MCPHub,一个一站式管理多个 MCP 的平台,在 1.0.32 版本之前,其内置的 OAuth 授权服务器在授权码授权流程中,默认情况下攻击者可以通过不提供 client_secret 和 code_verifier 来绕过客户端验证。攻击者如果通过某种方式(比如 CSRF)获取了授权码,则可伪造一个客户端向授权服务器发起访问令牌请求,随后拿得到的令牌去向资源服务器请求受害者资源。
漏洞讲解#
MCPHub 通过 node-oauth2-server 模块实现 OAuth 授权服务器:
import OAuth2Server from '@node-oauth/oauth2-server';OAuth2Server 类的实现:
oauth = new OAuth2Server({
[snip]
// When requireClientSecret is false, allow PKCE without client secret
requireClientAuthentication: oauthConfig.requireClientSecret
? { authorization_code: true, refresh_token: true }
: { authorization_code: false, refresh_token: false },
});这有一段注释信息:如果 requireClientSecret 的值是 false,则对客户端的验证只需要通过 PKCE 而无需再验证 client_secret(当然注释信息只能作为参考,具体实现还得验证)。
PKCE 是授权码授权流程中引入的一种客户端校验机制:
- 客户端在发起授权请求前会生成一个高熵随机字符串
code_verifier作为验证器,并根据某算法(plain/S256)计算得到code_challenge - 随后在授权请求中将
code_challenge和算法(code_challenge_method)发给授权服务器 - 接着在访问令牌请求中,客户端需要向授权服务器提供
code_verifier - 授权服务器收到验证器后,根据之前客户端提供的
code_challenge_method自己计算一版code_challenge,通过比较两个挑战值是否一致来验证客户端身份
在 PKCE 中,攻击者如果无法得到或者猜出 code_verifier,就无法通过客户端验证。
如果要看 PKCE 的详细介绍,请看这篇文章。
按照注释的说法,其实如果 MCPHub 正确应用了 PKCE,那么验不验证 client_secret 其实无可厚非,但事实并非如此。
默认值#
关于 oauthConfig.requireClientSecret 的默认值,需要去追踪 storedConfig:
const systemConfigDao = getSystemConfigDao();
const systemConfig = await systemConfigDao.get();
const storedConfig = systemConfig?.oauthServer;
const oauthConfig = { ...cloneDefaultOAuthServerConfig(), ...storedConfig };能发现它会去 mcp_settings.json 文件中获取配置信息,requireClientSecret 的默认值就在里面:
"requireClientSecret": false,当然,获取其默认值的最方便的方式就是调试,将关键逻辑扒下来(注意相对路径):
import { getSystemConfigDao } from '../dao/index.js';
import { cloneDefaultOAuthServerConfig } from '../constants/oauthServerDefaults.js';
const systemConfigDao = getSystemConfigDao();
const systemConfig = await systemConfigDao.get();
const storedConfig = systemConfig?.oauthServer;
// Merge stored config with defaults so partial configs still inherit all default values
const oauthConfig = { ...cloneDefaultOAuthServerConfig(), ...storedConfig };
console.log('debug done.');用 node.js 调试 TypeScript 代码需要使用 TypeScript Execute (tsx):

在第九行下断点,然后调试:

同样也能得到 requireClientSecret 的默认值为 false。
(根据三元表达式)这意味着 requireClientAuthentication 的默认值是:
{ authorization_code: false, refresh_token: false },通过 rg 在 node-oauth2-server 模块中搜索 requireClientAuthentication,得到:
> rg 'requireClientAuthentication' .\node_modules\.pnpm\@node-oauth+oauth2-server@5.3.0\
[snip]
.\node_modules\.pnpm\@node-oauth+oauth2-server@5.3.0\node_modules\@node-oauth\oauth2-server\lib\handlers\token-handler.js
62: this.requireClientAuthentication = options.requireClientAuthentication || {};
298: if (Object.keys(this.requireClientAuthentication).length > 0) {
299: return (typeof this.requireClientAuthentication[grantType] !== 'undefined') ? this.requireClientAuthentication[grantType] : true;能发现 token-handler.js 中似有利用 requireClientAuthentication 作某些判断的逻辑。
对应代码:
isClientAuthenticationRequired (grantType) {
if (Object.keys(this.requireClientAuthentication).length > 0) {
return (typeof this.requireClientAuthentication[grantType] !== 'undefined') ? this.requireClientAuthentication[grantType] : true;
} else {
return true;
}
}isClientAuthenticationRequired 被用于:
const grantType = request.body.grant_type;
if (this.isClientAuthenticationRequired(grantType) && !credentials.clientSecret && !isPkce) {
throw new InvalidRequestError('Missing parameter: `client_secret`');
}在 OAuth 授权码授权类型中,grant_type 的值必须是 authorization_code,即 grantType 的值就是它。
根据三元表达式:
typeof this.requireClientAuthentication[grantType] !== 'undefined') ? this.requireClientAuthentication[grantType] : true;由于 authorization_code 的值为 fasle,那么 isClientAuthenticationRequired 的返回值也是 flase。
这将导致下面这条 if 判断被跳过:
if (this.isClientAuthenticationRequired(grantType) && !credentials.clientSecret && !isPkce)即不检验请求中是否包含 client_secret 或者 code_verifier,而这两个都是授权服务器用于客户端验证的关键信息。
当然,这不单单是默认值的锅。
MCPHub 的实现#
由上述分析可知:在默认情况下,node-oauth2-server 模块不帮忙校验客户端是否传来“用于客户端验证”的信息。
但 MCPHub 自身也没有做好这方面的校验。
无论是 client_secret 还是 code_verifier,都是在访问令牌请求中被发送的(会访问授权服务器的 Token Endpoint),查看路由:
app.post('/oauth/token', express.urlencoded({ extended: true }), mcpConnectionRateLimiter, postToken);postToken:
export const postToken = async (req: Request, res: Response): Promise<void> => {
try {
const token = await handleTokenRequest(req, res);
res.json({
access_token: token.accessToken,
token_type: 'Bearer',
expires_in: Math.floor(((token.accessTokenExpiresAt?.getTime() || 0) - Date.now()) / 1000),
refresh_token: token.refreshToken,
scope: Array.isArray(token.scope) ? token.scope.join(' ') : token.scope,
});
} catch (error) {
[snip]
}
};其主要逻辑都在 handleTokenRequest 中:
export const handleTokenRequest = async (
req: ExpressRequest,
res: ExpressResponse,
): Promise<OAuth2Server.Token> => {
if (!oauth) {
throw new Error('OAuth server not initialized');
}
const request = new Request(req);
const response = new Response(res);
return await oauth.token(request, response);
};oauth 就是 OAuth2Server 的实例,其中的 token 方法:
token (request, response, options) {
options = Object.assign({
accessTokenLifetime: 60 * 60, // 1 hour.
refreshTokenLifetime: 60 * 60 * 24 * 14, // 2 weeks.
allowExtendedTokenAttributes: false,
requireClientAuthentication: {} // defaults to true for all grant types
}, this.options, options);
return new TokenHandler(options).handle(request, response);
}这会调用 TokenHandler 类中的 handle 方法:
async handle (request, response) {
[snip]
try {
const client = await this.getClient(request, response);
const data = await this.handleGrantType(request, client);
const model = new TokenModel(data, { allowExtendedTokenAttributes: this.allowExtendedTokenAttributes });
const tokenType = this.getTokenType(model);
this.updateSuccessResponse(response, tokenType);
return data;
} catch (err) {
[snip]
}
}其中的 getClient 方法:
async getClient (request, response) {
[snip]
if (this.isClientAuthenticationRequired(grantType) && !credentials.clientSecret && !isPkce) {
throw new InvalidRequestError('Missing parameter: `client_secret`');
}
[snip]
if (credentials.clientSecret && !isFormat.vschar(credentials.clientSecret)) {
throw new InvalidRequestError('Invalid parameter: `client_secret`');
}
try {
const client = await this.model.getClient(credentials.clientId, credentials.clientSecret);
[snip]
} catch (e) {
[snip]
}
}前面已经分析过,第一个 if 会因默认值而被绕过。而第二个 if 仅仅只在存在 client_secret 的时候才进行检测,而攻击者完全可以不提供这个参数。
随后,MCPHub 自己还进行了一次客户端验证操作,这在 this.model.getClient 中。
对 OAuth2Server 构造函数的调用中,提供了 model 的值:
oauth = new OAuth2Server({
model: oauthModel,
[snip]
});oauthModel 中的 getClient 方法:
getClient: async (clientId: string, clientSecret?: string) => {
const client = await findOAuthClientById(clientId);
if (!client) {
return false;
}
// If client secret is provided, verify it
if (clientSecret && client.clientSecret) {
if (!safeCompare(client.clientSecret, clientSecret)) {
return false;
}
}
return {
id: client.clientId,
clientId: client.clientId,
clientSecret: client.clientSecret,
redirectUris: client.redirectUris,
grants: client.grants,
};
},第二个 if 判断就是 MCPHub 自己实现的客户端验证,但其验证的前提是“要有 client_secret”
综上所述,只要攻击者不在访问令牌请求中携带:
client_secretcode_verifier
授权服务器会跳过客户端身份验证。
Diff#
补丁中,MCPHub 针对授权码授权流程强制引入了 PKCE 和 client_secret 检查。
针对授权请求:
+ // RFC 7636 + GHSA-3m7m: public clients (no registered secret) must present
+ // an S256 code challenge up front. Confidential clients may omit PKCE.
+ if (!client.clientSecret && (!code_challenge || code_challenge_method !== 'S256')) {
+ res.status(400).json({
+ error: 'invalid_request',
+ error_description: 'PKCE with code_challenge_method=S256 is required for public clients',
+ });
+ return;
+ }如果客户端如果:
- 没有被分配
client_secret,或者 - 没有在请求中提供
code_challenge,或者 - 对于挑战码计算算法,不是
S256
则会拒绝本次请求。
对于访问令牌请求:
- // If client secret is provided, verify it
- if (clientSecret && client.clientSecret) {
- if (!safeCompare(client.clientSecret, clientSecret)) {
+ // If the registered client has a secret, it must be presented and match.
+ if (client.clientSecret) {
+ if (!clientSecret || !safeCompare(client.clientSecret, clientSecret)) {
return false;
}
}即如果客户端有 client_secret,则必须进行校验。
如果客户端没有 client_secret,也需要通过 PKCE 来验证客户端身份:
+ if (!client.clientSecret) {
+ if (!code.codeChallenge || code.codeChallengeMethod !== 'S256') {
+ throw new OAuth2Server.InvalidRequestError(
+ 'PKCE with code_challenge_method=S256 is required for public clients',
+ );
+ }
+ }