-
Notifications
You must be signed in to change notification settings - Fork 3
feat(google-analytics): add allowedPropertyIds to restrict property access #481
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
base: main
Are you sure you want to change the base?
Changes from all commits
abef8e2
68f7f08
1d94f69
fcdefb1
a44901c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,11 +10,30 @@ export const getGoogleAccessToken = (env: Env): string => { | |
| return authorization.replace(/^Bearer\s+/i, ""); | ||
| }; | ||
|
|
||
| // Normalizes "1234567" → "properties/1234567"; already-prefixed IDs pass through. | ||
| function normalizePropertyId(id: string): string { | ||
|
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: For example, an allowlist entry of Prompt for AI agents |
||
| const clean = String(id).trim(); | ||
| return /^\d+$/.test(clean) ? `properties/${clean}` : clean; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the normalized allowlist of property IDs for this installation, | ||
| * or null if no allowlist is configured (meaning all properties are allowed). | ||
| */ | ||
| export const getAllowedPropertyIds = (env: Env): string[] | null => { | ||
| const ids = env.MESH_REQUEST_CONTEXT?.state?.allowedPropertyIds; | ||
| if (!ids || ids.length === 0) return null; | ||
| return ids.map(normalizePropertyId); | ||
| }; | ||
|
|
||
| /** | ||
| * Resolves the GA4 property to use for a request. | ||
| * | ||
| * Prefers the `property` passed to the tool; when omitted, falls back to the | ||
| * `propertyId` configured on the MCP installation state. | ||
| * | ||
| * When allowedPropertyIds is configured, the resolved property is validated | ||
| * against the allowlist — any property not in the list is rejected. | ||
| */ | ||
| export const resolveProperty = (env: Env, property?: string | null): string => { | ||
| const resolved = property ?? env.MESH_REQUEST_CONTEXT?.state?.propertyId; | ||
|
|
@@ -23,5 +42,12 @@ export const resolveProperty = (env: Env, property?: string | null): string => { | |
| "No GA4 property provided. Pass a `property` argument or configure a default `propertyId` in this integration's settings.", | ||
| ); | ||
| } | ||
| return resolved; | ||
| const normalized = normalizePropertyId(resolved); | ||
| const allowed = getAllowedPropertyIds(env); | ||
| if (allowed && !allowed.includes(normalized)) { | ||
| throw new Error( | ||
| `Access denied: property '${normalized}' is not in the allowlist configured for this integration.`, | ||
| ); | ||
| } | ||
| return normalized; | ||
| }; | ||
Uh oh!
There was an error while loading. Please reload this page.