-
Notifications
You must be signed in to change notification settings - Fork 14.9k
Add Authentication to the MCP server's HTTP transport #21527
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
zeroSteiner
wants to merge
3
commits into
rapid7:master
Choose a base branch
from
zeroSteiner:feat/mcp/add-bearer-auth
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Msf::MCP | ||
| module Middleware | ||
| ## | ||
| # Rack middleware that enforces Bearer token authentication on every request. | ||
| # | ||
| # Skipped (pass-through) when no token is configured -- the token is always | ||
| # present when this middleware is mounted because {Server#start_http} only | ||
| # adds it to the stack when +auth_token+ is non-nil. | ||
| # | ||
| # Clients must send: | ||
| # Authorization: Bearer <token> | ||
| # | ||
| # Returns 401 with a WWW-Authenticate challenge on any mismatch. | ||
| # Comparison is constant-time via +Rack::Utils.secure_compare+ to prevent | ||
| # timing-based token enumeration. | ||
| # | ||
| class BearerAuth | ||
| UNAUTHORIZED = [ | ||
| 401, | ||
| { | ||
| 'Content-Type' => 'application/json', | ||
| 'WWW-Authenticate' => 'Bearer realm="msfmcp"' | ||
| }, | ||
| ['{"error":"Unauthorized"}'] | ||
| ].freeze | ||
|
|
||
| def initialize(app, auth_token:) | ||
| @app = app | ||
| @auth_token = auth_token | ||
| end | ||
|
|
||
| def call(env) | ||
| expected = "Bearer #{@auth_token}" | ||
| provided = env['HTTP_AUTHORIZATION'].to_s | ||
| return UNAUTHORIZED unless Rack::Utils.secure_compare(expected, provided) | ||
|
|
||
| @app.call(env) | ||
| end | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'msf/core/mcp' | ||
| require 'rack' | ||
|
|
||
| RSpec.describe Msf::MCP::Middleware::BearerAuth do | ||
| let(:token) { 's3cr3t' } | ||
| let(:inner_app) { ->(_env) { [200, { 'Content-Type' => 'application/json' }, ['OK']] } } | ||
| let(:middleware) { described_class.new(inner_app, auth_token: token) } | ||
|
|
||
| def rack_env_for(authorization: nil) | ||
| env = Rack::MockRequest.env_for('http://localhost:3000/mcp', method: 'POST') | ||
| env['HTTP_AUTHORIZATION'] = authorization if authorization | ||
| env | ||
| end | ||
|
|
||
| describe 'UNAUTHORIZED' do | ||
| subject { described_class::UNAUTHORIZED } | ||
|
|
||
| it 'has status 401' do | ||
| expect(subject[0]).to eq(401) | ||
| end | ||
|
|
||
| it 'includes Content-Type application/json' do | ||
| expect(subject[1]['Content-Type']).to eq('application/json') | ||
| end | ||
|
|
||
| it 'includes a WWW-Authenticate Bearer challenge' do | ||
| expect(subject[1]['WWW-Authenticate']).to eq('Bearer realm="msfmcp"') | ||
| end | ||
|
|
||
| it 'has a JSON error body' do | ||
| expect(subject[2]).to eq(['{"error":"Unauthorized"}']) | ||
| end | ||
|
|
||
| it 'is frozen' do | ||
| expect(subject).to be_frozen | ||
| end | ||
| end | ||
|
|
||
| describe '#call' do | ||
| context 'with the correct Bearer token' do | ||
| it 'delegates to the inner app and returns its response' do | ||
| env = rack_env_for(authorization: "Bearer #{token}") | ||
| status, _headers, body = middleware.call(env) | ||
|
|
||
| expect(status).to eq(200) | ||
| expect(body).to eq(['OK']) | ||
| end | ||
| end | ||
|
|
||
| context 'with a wrong token value' do | ||
| it 'returns 401' do | ||
| env = rack_env_for(authorization: 'Bearer wrongtoken') | ||
| status, headers, body = middleware.call(env) | ||
|
|
||
| expect(status).to eq(401) | ||
| expect(headers['WWW-Authenticate']).to eq('Bearer realm="msfmcp"') | ||
| expect(body).to eq(['{"error":"Unauthorized"}']) | ||
| end | ||
| end | ||
|
|
||
| context 'with no Authorization header' do | ||
| it 'returns 401' do | ||
| env = rack_env_for | ||
| status, _headers, _body = middleware.call(env) | ||
|
|
||
| expect(status).to eq(401) | ||
| end | ||
| end | ||
|
|
||
| context 'with the wrong scheme' do | ||
| it 'returns 401' do | ||
| env = rack_env_for(authorization: "Basic #{token}") | ||
| status, _headers, _body = middleware.call(env) | ||
|
|
||
| expect(status).to eq(401) | ||
| end | ||
| end | ||
|
|
||
| context 'with the correct token but wrong case for the scheme' do | ||
| it 'returns 401' do | ||
| env = rack_env_for(authorization: "bearer #{token}") | ||
| status, _headers, _body = middleware.call(env) | ||
|
|
||
| expect(status).to eq(401) | ||
| end | ||
| end | ||
|
|
||
| context 'with trailing whitespace on the token' do | ||
| it 'returns 401' do | ||
| env = rack_env_for(authorization: "Bearer #{token} ") | ||
| status, _headers, _body = middleware.call(env) | ||
|
|
||
| expect(status).to eq(401) | ||
| end | ||
| end | ||
| end | ||
| end |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.