Skip to content

OpenRAG is a comprehensive, single package Retrieval-Augmented Generation platform built on Langflow, Docling, and Opensearch.

Python

Add SharePoint URL validation

Edwin Josea67ac9654124
diff --git a/src/api/connectors.py b/src/api/connectors.py
index 9824e6a71..3fc9e35d0 100644
--- a/src/api/connectors.py
+++ b/src/api/connectors.py
@@ -1,11 +1,42 @@
from starlette.requests import Request
from starlette.responses import JSONResponse, PlainTextResponse
+from urllib.parse import urlparse
from utils.logging_config import get_logger
from utils.telemetry import TelemetryClient, Category, MessageId
logger = get_logger(__name__)
+def is_valid_sharepoint_url(url: str) -> bool:
+ """
+ Validate that a URL belongs to a SharePoint domain.
+
+ Performs proper hostname validation to prevent URL substring
+ sanitization attacks. Only accepts URLs where the hostname
+ ends with .sharepoint.com.
+ """
+ try:
+ parsed = urlparse(url)
+ hostname = parsed.hostname
+
+ # Ensure we have a valid hostname
+ if not hostname:
+ return False
+
+ # Convert to lowercase for case-insensitive comparison
+ hostname = hostname.lower()
+
+ # Validate that hostname ends with .sharepoint.com
+ # This prevents attacks like: evil.sharepoint.com.attacker.com
+ # Also ensure it's not just ".sharepoint.com" (no bare suffix)
+ return hostname.endswith(".sharepoint.com") and len(hostname) > len(
+ ".sharepoint.com"
+ )
+ except (ValueError, AttributeError):
+ # Invalid URL or parsing error
+ return False
+
+
async def list_connectors(request: Request, connector_service, session_manager):
"""List available connector types with metadata"""
try:
@@ -550,8 +581,8 @@ async def connector_token(request: Request, connector_service, session_manager):
# Check if a specific resource is requested (for SharePoint File Picker v8)
# The File Picker requires a token with SharePoint as the audience, not Graph
resource = request.query_params.get("resource")
-
- if resource and ".sharepoint.com" in resource:
+
+ if resource and is_valid_sharepoint_url(resource):
# SharePoint File Picker v8 needs a SharePoint-scoped token
logger.info(f"Acquiring SharePoint-scoped token for resource: {resource}")
if hasattr(connector.oauth, "get_access_token_for_resource"):
diff --git a/src/connectors/sharepoint/utils.py b/src/connectors/sharepoint/utils.py
new file mode 100644
index 000000000..f12eb022a
--- /dev/null
+++ b/src/connectors/sharepoint/utils.py
@@ -0,0 +1,32 @@
+from urllib.parse import urlparse
+
+
+def is_valid_sharepoint_url(url: str) -> bool:
+ """
+ Validate that a URL belongs to a SharePoint domain.
+
+ Performs proper hostname validation to prevent URL substring
+ sanitization attacks. Only accepts URLs where the hostname
+ ends with .sharepoint.com.
+ """
+ try:
+ parsed = urlparse(url)
+ hostname = parsed.hostname
+
+ # Ensure we have a valid hostname
+ if not hostname:
+ return False
+
+ # Convert to lowercase for case-insensitive comparison
+ hostname = hostname.lower()
+
+ # Validate that hostname ends with .sharepoint.com
+ # This prevents attacks like: evil.sharepoint.com.attacker.com
+ # Also ensure it's not just ".sharepoint.com" (no bare suffix)
+ return hostname.endswith(".sharepoint.com") and len(hostname) > len(
+ ".sharepoint.com"
+ )
+ except (ValueError, AttributeError):
+ # Invalid URL or parsing error
+ return False
+