Repository navigation
feat: add provider compliance policies #2701
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
Changes from 1 commit
a6514f8
46ee611
b5dcf0f
06aa366
72498cc
b85045f
b2f7fab
4bd213a
dd596a1
4f99033
66d9f31
8b4abea
d3ad1c1
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 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -135,6 +135,7 @@ import { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| getProviderDefinition, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| getRegionSpecificEnvVarName, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| getProviderEnvValue, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| isProviderCompliant, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } from "@llmgateway/models"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| detectCodingAgentFromReferer, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -2415,6 +2416,70 @@ chat.openapi(completions, async (c) => { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| : routingExpandedModelProviders; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Enterprise provider compliance guardrails: drop providers that do not meet | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // the org's required certifications/data policies, and block the request when | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // none remain. Applied after every (re)computation of the IAM-filtered arrays. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const compliancePolicy = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| organization.plan === "enterprise" && | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| organization.providerCompliancePolicy?.enabled | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ? organization.providerCompliancePolicy | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| : undefined; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
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.
This policy is only wired into the chat completions router, but other gateway endpoints still send user content directly to providers without any Useful? React with 👍 / 👎. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const filterCompliantProviders = <T extends { providerId: string }>( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| list: T[], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ): T[] => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!compliancePolicy) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return list; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return list.filter((p) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const definition = getProviderDefinition(p.providerId); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return definition | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ? isProviderCompliant(definition, compliancePolicy) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| : false; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const enforceCompliancePolicy = async () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!compliancePolicy) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| iamFilteredModelProviders = filterCompliantProviders( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| iamFilteredModelProviders, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expandedIamFilteredModelProviders = filterCompliantProviders( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expandedIamFilteredModelProviders, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // A pinned provider (e.g. "deepseek/...") is selected directly rather than | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // from the filtered array, so check it explicitly. Auto/unpinned routing | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // relies on the emptiness check below. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const pinnedDefinition = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| usedProvider && usedProvider !== "llmgateway" && usedProvider !== "custom" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ? getProviderDefinition(usedProvider) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| : undefined; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const pinnedBlocked = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pinnedDefinition !== undefined && | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| !isProviderCompliant(pinnedDefinition, compliancePolicy); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (iamFilteredModelProviders.length === 0 || pinnedBlocked) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await logViolation( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| project.organizationId, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ruleId: "provider_compliance", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ruleName: "Provider compliance policy", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| category: "provider_compliance", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| action: "block", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { apiKeyId: apiKey.id, model: requestedModel }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } catch { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Silently ignore logging failures | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
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. Don’t silently drop compliance security-event failures. A 403 can be returned with no Suggested fix- } catch {
- // Silently ignore logging failures
+ } catch (error) {
+ logger.error("Failed to log provider compliance violation", {
+ error: toError(error),
+ organizationId: project.organizationId,
+ apiKeyId: apiKey.id,
+ model: requestedModel,
+ });
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| throw new HTTPException(403, { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| message: `This request was blocked by your organization's provider compliance policy. No available provider for ${modelInfo.id} meets the required certifications. Contact your LLMGateway admin to adjust the policy.`, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (isDevPlanRestricted) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| iamFilteredModelProviders = iamFilteredModelProviders.filter( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| providerSupportsCachedInput, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -2428,6 +2493,8 @@ chat.openapi(completions, async (c) => { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await enforceCompliancePolicy(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
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.
For Useful? React with 👍 / 👎. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Validate the custom provider against the database if one was requested | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (requestedProvider === "custom" && customProviderName) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const customProviderKey = await findCustomProviderKey( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -2881,6 +2948,7 @@ chat.openapi(completions, async (c) => { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expandedIamFilteredModelProviders = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expandedIamFilteredModelProviders.filter(providerSupportsCachedInput); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await enforceCompliancePolicy(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } else if ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| (usedProvider === "llmgateway" && usedInternalModel === "custom") || | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| usedInternalModel === "custom" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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.
Saving the new policy through this route updates the organization with the uncached
dbclient, while the gateway reads organizations through the cachedcdbpath infindOrganizationById; only cached-client mutations triggerRedisCache.onMutateand SWR invalidation. If an admin enables a stricter policy after the org was cached, the gateway can continue using the oldproviderCompliancePolicyuntil the cache expires, allowing non-compliant traffic during that window.Useful? React with 👍 / 👎.