# AI-assisted upload inspection pipeline

This design serves teams that accept files from their users, such as PDFs in onboarding, claims, or support flows. A [Firewall](/en/documentation/platform/firewall/) sends each upload to a [Function](/en/documentation/platform/functions/) that reads the file and asks a model on [AI Inference](/en/documentation/platform/ai-inference/) whether it is malicious. A clean file continues to the [Application](/en/documentation/platform/applications/), and a malicious one is refused before the Application receives it.

The Function writes a line for each flagged file and each error, and those lines stream to a SIEM through [Data Stream](/en/documentation/platform/data-stream/). The SIEM or a person then adds a flagged sender to a [Network List](/en/documentation/platform/firewall/network-shield/network-lists/) that a rule of the Firewall denies. A quarantine bucket in [Object Storage](/en/documentation/platform/object-storage/) can keep a refused file for an analyst to review.

The design implements the use case [Screen file uploads for malicious content](/en/documentation/use-cases/secure-applications-and-networks/screen-file-uploads-for-malicious-content/).

## Architecture diagram

Solid arrows carry the request, dotted arrows carry the response, and thick arrows carry the log record, which runs apart from any request. Each label starts with the number of the dataflow step it belongs to.

```mermaid
%%{init: {"layout": "elk", "elk": {"cycleBreakingStrategy": "MODEL_ORDER"}, "themeVariables": {"fontSize": "13px"}, "flowchart": {"nodeSpacing": 24, "rankSpacing": 36, "padding": 8, "wrappingWidth": 150, "useMaxWidth": true}, "sequence": {"width": 120, "actorMargin": 30, "useMaxWidth": true}}}%%
flowchart TD
  User(["User"])
  FW["Firewall: DDoS Protection, Web Application Firewall, Network Shield"]
  Fn["security Function on the upload route"]
  AI["AI Inference"]
  App["Application"]
  Origin[["origin"]]
  Obs["Real-Time Metrics and Real-Time Events"]
  DS["Data Stream"]
  SIEM[["SIEM"]]
  User -->|"1. HTTPS request with a PDF upload"| FW
  FW -->|"2, 3. upload route"| Fn
  Fn -->|"3, 4. file, prompt"| AI
  Fn -->|"5. safe upload"| App
  App -->|"5. safe upload"| Origin
  AI -.->|"4. verdict, malicious or safe"| Fn
  Origin -.->|"5. response"| App
  App -.->|"5. response"| User
  FW -.->|"2. blocked"| User
  Fn -.->|"5. safe response when malicious"| User
  FW ==>|"6. every request and decision"| Obs
  FW ==>|"6. logs"| DS
  DS ==>|"6. logs"| SIEM
```

### Dataflow

1. A user sends an HTTPS request that carries a PDF upload to a domain of a [Workload](/en/documentation/platform/workloads/). The data center nearest to the user receives it. The Workload's deployment sets the Firewall that inspects the request and the Application behind it.
2. [DDoS Protection](/en/documentation/platform/workloads/ddos-protection/ddos-mitigation/) acts on the request before any rule of the Firewall runs. The Firewall then runs its rules in order, and the first rule that stops the request answers the user. A rule that uses [Network Shield](/en/documentation/platform/firewall/network-shield/) to deny the senders in a Network List answers `403`. Where a rule applies a [WAF](/en/documentation/platform/firewall/waf/) rule set in *Blocking*, a request it blocks receives `400`.
3. A rule on the upload route runs the inspection Function through its [Function Instance](/en/documentation/platform/firewall/functions-instances/), with the *Run Function* behavior. The Function reads the request body as text, and decodes it first when the `Content-Type` names base64. A file whose `Content-Type` starts with `image/` continues to the Application with no model call.
4. The Function calls the model with `Azion.AI.run`, sending the content of the file and the prompt from the arguments of its Function Instance. The prompt in the use case asks the model to answer `true` only for malicious content, such as embedded scripts, and `false` otherwise. AI Inference runs the model and returns its answer to the Function in the same request, and that answer is the verdict.
5. The Function applies the verdict. On `true`, it writes a warning line and calls the event method that its `action` argument names. `deny` answers the user with `403` and Azion's default error page, and `drop` closes the request with no answer. Any other answer lets the upload continue to the Application, which forwards it to the origin and returns the response.
6. [Real-Time Events](/en/documentation/platform/real-time-events/) keeps a record of each request that reaches the Firewall, with its status, and a record of each line the Function writes. Data Stream sends those lines to the endpoint that the SIEM reads, and [Real-Time Metrics](/en/documentation/platform/real-time-metrics/) counts the invocations of Functions on Firewalls.

The rules before the Function decide from the sender's address, the route, and the attack patterns that WAF scores. The Function then has a model read the file, and that model call is the one dependency the design adds to the request path. The Function waits while the model answers, so the time the model takes adds to the response time of each upload.

The design fails open. When the model call or the parsing throws, the Function writes an error line and lets the upload continue. A model that stops answering therefore shows as error lines, not as refused uploads.

A refused upload stops at the Firewall, before the Application and the origin. A flagged sender joins the Network List only after the request, from the verdict lines.

## Components

- **Firewall**: routes each upload to the Function before the Application receives it. The deny rule on the Network List of senders sits above the rule that runs the Function, so a listed sender never reaches the model. The rule that runs the Function matches only the upload route, such as `/upload`, so every other route skips the model call. A WAF rule set in *Blocking* that scores the upload route refuses every body larger than [131,072 bytes](/en/documentation/platform/firewall/waf/scoring-and-modes/#request-body-parsing) with `400`.
- **Functions**: runs the inspection code as a Function whose execution environment is `firewall`, through a Function Instance on the Firewall. It receives the request, reads the PDF, calls the model with `Azion.AI.run`, and applies the outcome in the same request. The outcome is an [event method](/en/documentation/platform/firewall/functions/#event-methods): `continue`, `deny`, `drop`, or `respondWith`, which answers with a response that the code builds. The Function that the use case builds refuses with `deny` or `drop`, so a refused upload gets the fixed `403` page or no answer.
- **AI Inference**: runs the model that classifies the content. A Function on a Firewall calls `Azion.AI.run` as a Function on an Application does, and the answer returns in the same request. The model id, from [AI models](/en/documentation/platform/ai-inference/models/), and the prompt sit in the arguments of the Function Instance, so the policy changes with no new code. A change to those arguments reaches traffic in [up to a few minutes](/en/documentation/platform/firewall/how-it-works/#propagation), with no duration guaranteed.
- **Object Storage**: holds the quarantine bucket, where a refused upload can be kept for an analyst to review. The Function that the use case builds denies the file and writes it to no bucket.
- **Network Shield**: holds the sender blocklist, a Network List that a deny rule reads through the *Network* criterion. A flagged sender is then refused on every route of the Workload, `/upload` included. The Function logs the verdict and not the address, so the SIEM or a person adds the address that the verdict's request ID leads to. Each entry can carry a due date, which ends the block at the next write of the list's items.
- **Data Stream**: sends the lines the Function writes, the *Functions* data source, to the endpoint that the SIEM reads. The Function that the use case builds writes a warning line, `[AI] true`, for each refused upload, and an error line for each failure. Each line carries the `$request_id` of its request. A clean file adds no line.
- **SIEM** (integration): correlates the verdicts with the request records and the team's other sources, and turns a verdict into a blocklist entry. For a playbook that writes the list, refer to [SIEM-driven firewall mitigation loop](/en/documentation/use-cases/secure-applications-and-networks/security-automation-with-edge-computing/).

## Implementation

- [Screen file uploads for malicious content](/en/documentation/use-cases/secure-applications-and-networks/screen-file-uploads-for-malicious-content/) - builds this design end to end: the sender blocklist, the inspection Function, and the stream of verdict lines.
- [Scan file uploads with an AI Inference firewall function](/en/documentation/guides/ai/inference/scan-uploads-with-ai-inference/) - creates the inspection Function and the Firewall rule that runs it on the upload route.
- [Block addresses until a date](/en/documentation/guides/application-security/bots-and-network/temporary-block/) - creates a dated entry in a Network List and the Firewall rule that denies it.
- [Change the order of Firewall rules](/en/documentation/guides/application-security/bots-and-network/change-the-order-of-firewall-rules/) - moves the deny rule on the sender list above the rule that runs the inspection Function.
- [Update a Network List from an automation](/en/documentation/guides/application-security/bots-and-network/update-network-list-from-automation/) - adds each flagged sender to the list from a SIEM playbook or a script.
- [Debug functions with Data Stream](/en/documentation/guides/platform/observability/debugging-functions-data-stream/) - sends the lines the Function writes, with their level and request ID, to the endpoint that the SIEM reads.

## Related resources

- [How AI Inference works](/en/documentation/platform/ai-inference/how-it-works.md): The invocation chain of a model call, and why the wait for the model counts toward the execution time of the Function.
- [Model invocation](/en/documentation/platform/ai-inference/model-invocation.md): The request body the Function sends to the model, and the response it reads the verdict from.
- [AI Inference limits](/en/documentation/platform/ai-inference/limits.md): The access each plan gives to AI Inference, and the conditions under which Azion terminates a model.
- [List matching](/en/documentation/platform/firewall/network-shield/list-matching.md): How a Network List matches a client address, and when a dated entry stops blocking.
