| 
 | 1 | +---  | 
 | 2 | +pcx_content_type: how-to  | 
 | 3 | +title: Set up Data Loss Prevention (DLP)  | 
 | 4 | +sidebar:  | 
 | 5 | +  order: 2  | 
 | 6 | +---  | 
 | 7 | + | 
 | 8 | +Add Data Loss Prevention (DLP) to any AI Gateway to start scanning AI prompts and responses for sensitive data.  | 
 | 9 | + | 
 | 10 | +## Prerequisites  | 
 | 11 | + | 
 | 12 | +- An existing [AI Gateway](/ai-gateway/get-started/)  | 
 | 13 | + | 
 | 14 | +## Enable DLP for AI Gateway  | 
 | 15 | + | 
 | 16 | +1. Log into the [Cloudflare dashboard](https://dash.cloudflare.com/) and select your account.  | 
 | 17 | +2. Go to **AI** > **AI Gateway**.  | 
 | 18 | +3. Select a gateway where you want to enable DLP.  | 
 | 19 | +4. Go to the **Firewall** tab.  | 
 | 20 | +5. Toggle **Data Loss Prevention (DLP)** to **On**.  | 
 | 21 | + | 
 | 22 | +## Add DLP policies  | 
 | 23 | + | 
 | 24 | +After enabling DLP, you can create policies to define how sensitive data should be handled:  | 
 | 25 | + | 
 | 26 | +1. Under the DLP section, click **Add Policy**.  | 
 | 27 | +2. Configure the following fields for each policy:  | 
 | 28 | +   - **Policy ID**: Enter a unique name for this policy (e.g., "Block-PII-Requests")  | 
 | 29 | +   - **DLP Profiles**: Select the DLP profiles to check against. AI requests/responses will be checked against each of the selected profiles. Available profiles include:  | 
 | 30 | +     - **Financial Information** - Credit cards, bank accounts, routing numbers  | 
 | 31 | +     - **Personal Identifiable Information (PII)** - Names, addresses, phone numbers    | 
 | 32 | +     - **Government Identifiers** - SSNs, passport numbers, driver's licenses  | 
 | 33 | +     - **Healthcare Information** - Medical record numbers, patient data  | 
 | 34 | +     - **Custom Profiles** - Organization-specific data patterns  | 
 | 35 | +       | 
 | 36 | +     :::note  | 
 | 37 | +     DLP profiles can be created and managed in the [Zero Trust DLP dashboard](/cloudflare-one/policies/data-loss-prevention/dlp-profiles/).  | 
 | 38 | +     :::  | 
 | 39 | + | 
 | 40 | +   - **Action**: Choose the action to take when any of the selected profiles match:  | 
 | 41 | +     - **Flag** - Record the detection for audit purposes without blocking  | 
 | 42 | +     - **Block** - Prevent the request/response from proceeding  | 
 | 43 | + | 
 | 44 | +   - **Check**: Select what to scan:  | 
 | 45 | +     - **Request** - Scan user prompts sent to AI providers  | 
 | 46 | +     - **Response** - Scan AI model responses before returning to users  | 
 | 47 | +     - **Both** - Scan both requests and responses  | 
 | 48 | + | 
 | 49 | +3. Click **Save** to save your policy configuration.  | 
 | 50 | + | 
 | 51 | +## Manage DLP policies  | 
 | 52 | + | 
 | 53 | +You can create multiple DLP policies with different configurations:  | 
 | 54 | + | 
 | 55 | +- **Add multiple policies**: Click **Add Policy** to create additional policies with different profile combinations or actions  | 
 | 56 | +- **Enable/disable policies**: Use the toggle next to each policy to individually enable or disable them without deleting the configuration  | 
 | 57 | +- **Edit policies**: Click on any existing policy to modify its settings  | 
 | 58 | +- **Save changes**: Always click **Save** after making any changes to apply them  | 
 | 59 | + | 
 | 60 | +## Test your configuration  | 
 | 61 | + | 
 | 62 | +After configuring DLP settings:  | 
 | 63 | + | 
 | 64 | +1. Make a test AI request through your gateway that contains sample sensitive data.  | 
 | 65 | +2. Check the **AI Gateway Logs** to verify DLP scanning is working.  | 
 | 66 | +3. Review the detection results and adjust profiles or actions as needed.  | 
 | 67 | + | 
 | 68 | +## Monitor DLP events  | 
 | 69 | + | 
 | 70 | +### Viewing DLP logs in AI Gateway  | 
 | 71 | + | 
 | 72 | +DLP events are integrated into your AI Gateway logs:  | 
 | 73 | + | 
 | 74 | +1. Go to **AI** > **AI Gateway** > your gateway > **Logs**.  | 
 | 75 | +2. Click on any log entry to view detailed information. For requests where DLP policies were triggered, additional details are included:  | 
 | 76 | +   - **DLP Action Taken**: Shows whether the action was "Flag" or "Block"  | 
 | 77 | +   - **DLP Policies Matched**: Detailed information about each policy that matched, including:  | 
 | 78 | +     - Which DLP profiles triggered within each policy  | 
 | 79 | +     - Whether the match occurred in the request or response  | 
 | 80 | +     - Specific entries that matched within each DLP profile  | 
 | 81 | + | 
 | 82 | +### Filter DLP events  | 
 | 83 | + | 
 | 84 | +To view only DLP-related requests:  | 
 | 85 | + | 
 | 86 | +1. On the **Logs** tab, click **Add Filter**.  | 
 | 87 | +2. Select **DLP Action** from the filter options.  | 
 | 88 | +3. Choose to filter by:  | 
 | 89 | +   - **FLAG** - Show only requests where sensitive data was flagged  | 
 | 90 | +   - **BLOCK** - Show only requests that were blocked due to DLP policies  | 
 | 91 | + | 
 | 92 | + | 
 | 93 | +## Error handling  | 
 | 94 | + | 
 | 95 | +When DLP policies are triggered, your application will receive additional information through response headers and error codes.  | 
 | 96 | + | 
 | 97 | +### DLP response header  | 
 | 98 | + | 
 | 99 | +When a request matches DLP policies (whether flagged or blocked), an additional `cf-aig-dlp` header is returned containing detailed information about the match:  | 
 | 100 | + | 
 | 101 | +#### Header schema  | 
 | 102 | + | 
 | 103 | +```json  | 
 | 104 | +{  | 
 | 105 | +  "findings": [  | 
 | 106 | +    {  | 
 | 107 | +      "profile": {  | 
 | 108 | +        "context": {},  | 
 | 109 | +        "entry_ids": ["string"],  | 
 | 110 | +        "profile_id": "string"  | 
 | 111 | +      },  | 
 | 112 | +      "policy_ids": ["string"],  | 
 | 113 | +      "check": "REQUEST" | "RESPONSE"  | 
 | 114 | +    }  | 
 | 115 | +  ],  | 
 | 116 | +  "action": "BLOCK" | "FLAG"  | 
 | 117 | +}  | 
 | 118 | +```  | 
 | 119 | + | 
 | 120 | +#### Example header value  | 
 | 121 | + | 
 | 122 | +```json  | 
 | 123 | +{  | 
 | 124 | +  "findings": [  | 
 | 125 | +    {  | 
 | 126 | +      "profile": {  | 
 | 127 | +        "context": {},  | 
 | 128 | +        "entry_ids": ["a1b2c3d4-e5f6-7890-abcd-ef1234567890", "f7e8d9c0-b1a2-3456-789a-bcdef0123456"],  | 
 | 129 | +        "profile_id": "12345678-90ab-cdef-1234-567890abcdef"  | 
 | 130 | +      },  | 
 | 131 | +      "policy_ids": ["block_financial_data"],  | 
 | 132 | +      "check": "REQUEST"  | 
 | 133 | +    }  | 
 | 134 | +  ],  | 
 | 135 | +  "action": "BLOCK"  | 
 | 136 | +}  | 
 | 137 | +```  | 
 | 138 | + | 
 | 139 | +Use this header to programmatically detect which DLP profiles and entries were matched, which policies triggered, and whether the match occurred in the request or response.  | 
 | 140 | + | 
 | 141 | +### Error codes for blocked requests  | 
 | 142 | + | 
 | 143 | +When DLP blocks a request, your application will receive structured error responses:  | 
 | 144 | + | 
 | 145 | +- **Request blocked by DLP**  | 
 | 146 | +  - `"code": 2029`  | 
 | 147 | +  - `"message": "Request content blocked due to DLP policy violations"`  | 
 | 148 | + | 
 | 149 | +- **Response blocked by DLP**  | 
 | 150 | +  - `"code": 2030`  | 
 | 151 | +  - `"message": "Response content blocked due to DLP policy violations"`  | 
 | 152 | + | 
 | 153 | +Handle these errors in your application:  | 
 | 154 | + | 
 | 155 | +```js  | 
 | 156 | +try {  | 
 | 157 | +  const res = await env.AI.run('@cf/meta/llama-3.1-8b-instruct', {  | 
 | 158 | +    prompt: userInput  | 
 | 159 | +  }, {  | 
 | 160 | +    gateway: {id: 'your-gateway-id'}  | 
 | 161 | +  })  | 
 | 162 | +  return Response.json(res)  | 
 | 163 | +} catch (e) {  | 
 | 164 | +  if ((e as Error).message.includes('2029')) {  | 
 | 165 | +    return new Response('Request contains sensitive data and cannot be processed.')  | 
 | 166 | +  }  | 
 | 167 | +  if ((e as Error).message.includes('2030')) {  | 
 | 168 | +    return new Response('AI response was blocked due to sensitive content.')  | 
 | 169 | +  }  | 
 | 170 | +  return new Response('AI request failed')  | 
 | 171 | +}  | 
 | 172 | +```  | 
 | 173 | + | 
 | 174 | +## Best practices  | 
 | 175 | + | 
 | 176 | +- **Start with flagging**: Begin with "Flag" actions to understand what data is being detected before implementing blocking  | 
 | 177 | +- **Tune confidence levels**: Adjust detection sensitivity based on your false positive tolerance  | 
 | 178 | +- **Use appropriate profiles**: Select DLP profiles that match your data protection requirements  | 
 | 179 | +- **Monitor regularly**: Review DLP events to ensure policies are working as expected  | 
 | 180 | +- **Test thoroughly**: Validate DLP behavior with sample sensitive data before production deployment  | 
 | 181 | + | 
 | 182 | +## Troubleshooting  | 
 | 183 | + | 
 | 184 | +### DLP not triggering  | 
 | 185 | + | 
 | 186 | +- Verify DLP toggle is enabled for your gateway  | 
 | 187 | +- Ensure selected DLP profiles are appropriate for your test data  | 
 | 188 | +- Confirm confidence levels aren't set too high  | 
 | 189 | + | 
 | 190 | +### Unexpected blocking  | 
 | 191 | + | 
 | 192 | +- Review DLP logs to see which profiles triggered  | 
 | 193 | +- Consider lowering confidence levels for problematic profiles  | 
 | 194 | +- Test with different sample data to understand detection patterns  | 
 | 195 | +- Adjust profile selections if needed  | 
 | 196 | + | 
 | 197 | +For additional support with DLP configuration, refer to the [Cloudflare Data Loss Prevention documentation](/cloudflare-one/policies/data-loss-prevention/) or contact your Cloudflare support team.  | 
0 commit comments