|
| 1 | +import process from 'node:process'; |
| 2 | +import { type HttpRequest } from '@azure/functions'; |
| 3 | + |
| 4 | +/** |
| 5 | + * Generates the user security context which contains several parameters that describe the AI application itself, and the end user that interacts with the AI application. |
| 6 | + * These fields assist your security operations teams to investigate and mitigate security incidents by providing a comprehensive approach to protecting your AI applications. |
| 7 | + * [Learn more](https://learn.microsoft.com/azure/defender-for-cloud/gain-end-user-context-ai) about protecting AI applications using Microsoft Defender for Cloud. |
| 8 | + * @param request - The HTTP request |
| 9 | + * @returns A json string which represents the user context |
| 10 | + */ |
| 11 | +export function getMsDefenderUserJson(request: HttpRequest): UserSecurityContext { |
| 12 | + const sourceIp = getSourceIp(request); |
| 13 | + const authenticatedUserDetails = getAuthenticatedUserDetails(request); |
| 14 | + |
| 15 | + const userSecurityContext = { |
| 16 | + end_user_tenant_id: authenticatedUserDetails.get('tenantId'), |
| 17 | + end_user_id: authenticatedUserDetails.get('userId'), |
| 18 | + source_ip: sourceIp, |
| 19 | + application_name: process.env.APPLICATION_NAME, |
| 20 | + } as UserSecurityContext; |
| 21 | + |
| 22 | + return userSecurityContext; |
| 23 | +} |
| 24 | + |
| 25 | +/** |
| 26 | + * Extracts user authentication details from the 'X-Ms-Client-Principal' header. |
| 27 | + * This is based on [Azure Static Web App documentation](https://learn.microsoft.com/en-us/azure/static-web-apps/user-information) |
| 28 | + * @param request - The HTTP request |
| 29 | + * @returns A dictionary containing authentication details of the user |
| 30 | + */ |
| 31 | +function getAuthenticatedUserDetails(request: HttpRequest): Map<string, string> { |
| 32 | + const authenticatedUserDetails = new Map<string, string>(); |
| 33 | + const principalHeader = request.headers.get('X-Ms-Client-Principal'); |
| 34 | + if (principalHeader === null) { |
| 35 | + return authenticatedUserDetails; |
| 36 | + } |
| 37 | + |
| 38 | + const principal = parsePrincipal(principalHeader); |
| 39 | + if (principal === null) { |
| 40 | + return authenticatedUserDetails; |
| 41 | + } |
| 42 | + |
| 43 | + const tenantId = process.env.AZURE_TENANT_ID; |
| 44 | + if (principal!.identityProvider === 'aad') { |
| 45 | + // TODO: add only when userId represents actual IDP user id |
| 46 | + // authenticatedUserDetails.set('userId', principal['userId']); |
| 47 | + authenticatedUserDetails.set('tenantId', tenantId!); |
| 48 | + } |
| 49 | + |
| 50 | + return authenticatedUserDetails; |
| 51 | +} |
| 52 | + |
| 53 | +function parsePrincipal(principal: string | undefined): Principal | undefined { |
| 54 | + if (principal === null) { |
| 55 | + return undefined; |
| 56 | + } |
| 57 | + |
| 58 | + try { |
| 59 | + return JSON.parse(Buffer.from(principal!, 'base64').toString('utf8')) as Principal; |
| 60 | + } catch { |
| 61 | + return undefined; |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +function getSourceIp(request: HttpRequest) { |
| 66 | + const xForwardFor = request.headers.get('X-Forwarded-For'); |
| 67 | + if (xForwardFor === null) { |
| 68 | + return null; |
| 69 | + } |
| 70 | + |
| 71 | + const ip = xForwardFor.split(',')[0]; |
| 72 | + const colonIndex = ip.lastIndexOf(':'); |
| 73 | + |
| 74 | + // Case of ipv4 |
| 75 | + if (colonIndex !== -1 && ip.indexOf(':') === colonIndex) { |
| 76 | + return ip.slice(0, Math.max(0, colonIndex)); |
| 77 | + } |
| 78 | + |
| 79 | + // Case of ipv6 |
| 80 | + if (ip.startsWith('[') && ip.includes(']:')) { |
| 81 | + return ip.slice(0, Math.max(0, ip.indexOf(']:') + 1)); |
| 82 | + } |
| 83 | + |
| 84 | + return ip; |
| 85 | +} |
| 86 | + |
| 87 | +type Principal = { |
| 88 | + identityProvider: string; |
| 89 | + userId: string; |
| 90 | +}; |
| 91 | + |
| 92 | +export type UserSecurityContext = { |
| 93 | + application_name: string; |
| 94 | + end_user_id: string; |
| 95 | + end_user_tenant_id: string; |
| 96 | + source_ip: string; |
| 97 | +}; |
0 commit comments