-
Notifications
You must be signed in to change notification settings - Fork 714
Stage #9276
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
Merged
Merged
Stage #9276
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,214 @@ | ||
| /** | ||
| * Authorization types for MCP Server | ||
| * | ||
| * These types are defined locally to avoid importing from @gauzy/auth, | ||
| * which causes TypeScript compilation performance issues due to its complex | ||
| * dependency chain in the monorepo. | ||
| */ | ||
|
|
||
| import { Request, Response } from 'express'; | ||
|
|
||
| /** | ||
| * Authorization server configuration | ||
| */ | ||
| export interface AuthorizationServerConfig { | ||
| issuer: string; | ||
| jwksUri?: string; | ||
| tokenEndpoint?: string; | ||
| authorizationEndpoint?: string; | ||
| } | ||
|
|
||
| /** | ||
| * Authorization configuration | ||
| */ | ||
| export interface AuthorizationConfig { | ||
| enabled: boolean; | ||
| requiredScopes: string[]; | ||
| resourceUri?: string; | ||
| authorizationServers: AuthorizationServerConfig[]; | ||
| tokenValidation?: { | ||
| audience?: string; | ||
| issuer?: string | string[]; | ||
| clockTolerance?: number; | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * OAuth 2.0 Protected Resource Metadata (RFC 9728) | ||
| */ | ||
| export interface ProtectedResourceMetadata { | ||
| resource: string; | ||
| authorizationServers: string[]; | ||
| scopesRequired?: string[]; | ||
| bearerMethodsSupported?: string[]; | ||
| policyUri?: string; | ||
| } | ||
|
|
||
| /** | ||
| * OAuth authorization error | ||
| */ | ||
| export interface OAuthAuthorizationError { | ||
| error: string; | ||
| errorDescription?: string; | ||
| errorUri?: string; | ||
| scope?: string; | ||
| } | ||
|
|
||
| /** | ||
| * Token validation result | ||
| */ | ||
| export interface TokenValidationResult { | ||
| valid: boolean; | ||
| error?: string; | ||
| payload?: unknown; | ||
| scopes?: string[]; | ||
| subject?: string; | ||
| clientId?: string; | ||
| } | ||
|
|
||
| /** | ||
| * Load authorization configuration from environment | ||
| */ | ||
| export function loadAuthorizationConfig(): AuthorizationConfig { | ||
| const enabled = process.env.MCP_AUTHORIZATION_ENABLED === 'true'; | ||
|
|
||
| return { | ||
| enabled, | ||
| requiredScopes: (process.env.MCP_REQUIRED_SCOPES || 'mcp:read mcp:write').split(' ').filter(Boolean), | ||
| resourceUri: process.env.MCP_RESOURCE_URI, | ||
| authorizationServers: enabled | ||
| ? [ | ||
| { | ||
| issuer: process.env.MCP_AUTH_ISSUER || 'https://auth.example.com', | ||
| jwksUri: process.env.MCP_AUTH_JWKS_URI, | ||
| tokenEndpoint: process.env.MCP_AUTH_TOKEN_ENDPOINT, | ||
| authorizationEndpoint: process.env.MCP_AUTH_AUTHORIZATION_ENDPOINT | ||
| } | ||
| ] | ||
| : [] | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * OAuth validator for token validation | ||
| */ | ||
| export class OAuthValidator { | ||
| constructor(private config: AuthorizationConfig) {} | ||
|
|
||
| /** | ||
| * Extract bearer token from request | ||
| */ | ||
| extractBearerToken(req: Request): string | null { | ||
| const authHeader = req.headers.authorization; | ||
| if (!authHeader || !authHeader.startsWith('Bearer ')) { | ||
| return null; | ||
| } | ||
| return authHeader.substring(7); | ||
| } | ||
|
|
||
| /** | ||
| * Validate an OAuth token | ||
| */ | ||
| async validateToken(token: string, requiredScopes?: string[]): Promise<TokenValidationResult> { | ||
| if (!this.config.enabled) { | ||
| return { valid: true }; | ||
| } | ||
|
|
||
| // TODO: Implement actual JWT validation when authorization is enabled | ||
| // For now, return invalid if authorization is enabled but no implementation | ||
| return { | ||
| valid: false, | ||
| error: 'Token validation not implemented' | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Create authorization error object | ||
| */ | ||
| static createAuthorizationError( | ||
| error: string, | ||
| errorDescription?: string, | ||
| scope?: string | ||
| ): OAuthAuthorizationError { | ||
| return { | ||
| error, | ||
| errorDescription, | ||
| scope | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Format WWW-Authenticate header | ||
| */ | ||
| static formatWWWAuthenticateHeader(resourceUri: string, error?: OAuthAuthorizationError): string { | ||
| let header = 'Bearer'; | ||
|
|
||
| if (resourceUri) { | ||
| header += ` resource="${resourceUri}"`; | ||
| } | ||
|
|
||
| if (error) { | ||
| if (error.error) { | ||
| header += ` error="${error.error}"`; | ||
| } | ||
| if (error.errorDescription) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2: Values interpolated into the WWW-Authenticate header should be escaped to prevent header injection. Double quotes and backslashes in error values would create malformed headers per RFC 7230. Prompt for AI agents |
||
| header += ` error_description="${error.errorDescription}"`; | ||
| } | ||
| if (error.scope) { | ||
| header += ` scope="${error.scope}"`; | ||
| } | ||
| } | ||
|
|
||
| return header; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Response builder utilities | ||
| */ | ||
| export class ResponseBuilder { | ||
| /** | ||
| * Set security headers on response | ||
| */ | ||
| static setSecurityHeaders(res: Response): void { | ||
| // Content-Type options | ||
| res.setHeader('X-Content-Type-Options', 'nosniff'); | ||
|
|
||
| // Frame options | ||
| res.setHeader('X-Frame-Options', 'DENY'); | ||
|
|
||
| // XSS protection | ||
| res.setHeader('X-XSS-Protection', '1; mode=block'); | ||
|
|
||
| // Referrer policy | ||
| res.setHeader('Referrer-Policy', 'strict-origin-when-cross-origin'); | ||
|
|
||
| // Content Security Policy | ||
| res.setHeader( | ||
| 'Content-Security-Policy', | ||
| "default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; font-src 'self'; object-src 'none'; frame-ancestors 'none'; base-uri 'self'; form-action 'self'" | ||
| ); | ||
|
|
||
| // Strict Transport Security (for HTTPS) | ||
| res.setHeader('Strict-Transport-Security', 'max-age=31536000; includeSubDomains'); | ||
|
|
||
| // Cache control for API responses | ||
| res.setHeader('Cache-Control', 'no-store, no-cache, must-revalidate, proxy-revalidate'); | ||
| res.setHeader('Pragma', 'no-cache'); | ||
| res.setHeader('Expires', '0'); | ||
| } | ||
|
|
||
| /** | ||
| * Build success response | ||
| */ | ||
| static success<T>(data: T, message?: string): { success: true; data: T; message?: string } { | ||
| return { success: true, data, message }; | ||
| } | ||
|
|
||
| /** | ||
| * Build error response | ||
| */ | ||
| static error(message: string, code?: string): { success: false; error: string; code?: string } { | ||
| return { success: false, error: message, code }; | ||
| } | ||
| } | ||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
logic: Token validation returns
valid: falsewhen authorization is enabled, effectively blocking all requests. This creates a security issue where the feature appears enabled but isn't functional. Is authorization intended to be non-functional in this PR, or should there be basic JWT validation implemented?Prompt To Fix With AI