Skip to content

Commit 4a79d51

Browse files
AdamVybornyclaude
andcommitted
fix: use proper URL encoding in OAuth URL generation
Replace manual string concatenation with urllib.parse functions for secure URL construction: - Use urlencode() for query parameters to prevent injection - Use urlunsplit() for proper URL assembly - Update tests to match encoded output Addresses: #211 (comment) 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent d1d26a0 commit 4a79d51

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

src/keboola_mcp_server/tools/oauth.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import logging
44
from typing import Annotated
5+
from urllib.parse import urlencode, urlunsplit
56

67
from fastmcp import Context
78
from fastmcp.tools import FunctionTool
@@ -56,9 +57,18 @@ async def create_oauth_url(
5657
storage_api_url = client.storage_client.base_api_url
5758

5859
# Generate OAuth URL
59-
oauth_url = (
60-
f'https://external.keboola.com/oauth/index.html?token={sapi_token}'
61-
f'&sapiUrl={storage_api_url}#/{component_id}/{config_id}'
62-
)
60+
query_params = urlencode({
61+
'token': sapi_token,
62+
'sapiUrl': storage_api_url
63+
})
64+
fragment = f'/{component_id}/{config_id}'
65+
66+
oauth_url = urlunsplit((
67+
'https', # scheme
68+
'external.keboola.com', # netloc
69+
'/oauth/index.html', # path
70+
query_params, # query
71+
fragment # fragment
72+
))
6373

6474
return oauth_url

tests/tools/test_oauth.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ async def test_create_oauth_url_success(
4747
expected_url = (
4848
f'https://external.keboola.com/oauth/index.html'
4949
f'?token=KBC_TOKEN_12345'
50-
f'&sapiUrl=https://connection.test.keboola.com'
50+
f'&sapiUrl=https%3A%2F%2Fconnection.test.keboola.com'
5151
f'#/{component_id}/{config_id}'
5252
)
5353
assert result == expected_url

0 commit comments

Comments
 (0)