Appearance
Learned API schema Since v0.2
Most APIs have no published description. With learning on, the positive model also learns the structure of the JSON bodies an endpoint receives and, when asked, of its JSON answers: fields, types, formats, which fields are always present, string lengths, number ranges, array sizes and short value lists. A ready endpoint is offered as a schema suggestion. An approved or hand-written schema is enforced like an imported OpenAPI description, and you can download the schemas as an OpenAPI 3.1 document.
Before you begin
- Role:
operatorfor the application (viewercan read). - Learning is on for the application (
policy.positive.learning.enabled: true, Request checks > Learned model). - Only clean requests teach the model: no module flagged them, the signature score is 0 and the upstream answered below 400.
Turn on body and answer learning
text
1. Open Applications > All applications > the application > Request checks > Learned model.
2. In the learning group, keep Learn JSON body structure on.
3. To learn answers too, turn on Learn JSON answers and set Answers learned per second.
4. Select Preview changes, then Apply.sh
waap apps set -app shop -m "learn bodies" policy.positive.learning.bodies=true policy.positive.learning.responses=trueyaml
# applications[].policy.positive.learning
learning:
enabled: true
bodies: true
responses: true
response_sample_rate: 1Answer learning adds latency to sampled answers
Answers are read only when they are JSON, at most 256 KiB, in the response scope and within the sample rate. Those answers wait for their whole body. With answer learning on, the product asks upstreams for uncompressed answers; the data plane compresses towards clients again.
Approve a suggested schema
text
1. Open the application's API protection page, Learned schema tab.
2. Under Suggested schemas, select an endpoint to see the evidence and the proposed schema.
3. Select Preview changes, then Apply. Or select Approve all new.
4. To hide a suggestion, select Reject this schema. Select Propose it again to undo it.sh
waap api learned suggestions -app shop
waap api learned schema -app shop -endpoint "POST /api/orders"
waap api learned approve -app shop -id ID -m "orders schema"
waap api learned reject -app shop -id ID
waap api learned restore -app shop -id IDyaml
# applications[].policy.api.learned_schemas
- endpoint: POST /api/v1/orders
source: learned
request:
type: object
required: [item, qty]
properties:
item: {type: string, format: uuid}
qty: {type: integer, minimum: 1, maximum: 100}
note: {type: string, max_length: 500, nullable: true}
additional: log
response:
status: [200]
type: object
properties: {id: {type: string, format: uuid}}Approving writes an entry with source: learned as a new configuration version. An entry you edit becomes source: manual: relearning never overwrites it and only shows the difference. A rejected suggestion returns when the traffic changes.
Enforce the schemas
text
1. On the Learned schema tab, open Settings.
2. Set Learned schema mode, Fields not in the schema and Check answers.
3. Select Preview changes, then Apply.sh
waap api set -app shop -m "enforce learned schemas" learned.mode=block learned.unknown_fields=refuseyaml
# applications[].policy.api
learned:
mode: block
unknown_fields: refuse
responses: detectStart in detect
Nothing is refused until both the application and the feature are in block. A field the schema does not name is only logged unless Fields not in the schema (or the object's additional) says refuse. Answers are only logged, never refused.
When an imported OpenAPI description describes the operation, the description wins and the learned schema is not applied.
Export as OpenAPI
text
1. On the Learned schema tab, select Export as OpenAPI.
2. Optionally include the learned but unapproved schemas.sh
waap api learned openapi -app shop -suggested -o shop-learned.yamlThe document holds one operation per endpoint, path variables as string parameters, the request schema as a JSON request body, and the response schema per listed status (2XX when none is listed). Lengths are in bytes here and in characters in JSON Schema.
What becomes a schema
The same thresholds as parameter rules apply (min_samples, min_clients, min_duration, min_confidence).
| Learned | Rule |
|---|---|
| type | The JSON type of at least 99.9% of the non-null values (integer counts as number); a mix gives no type. |
| nullable | A null was seen. |
| required | Present in at least 99.9% of the parent object's occurrences. |
| format | uuid, email, date-time, hex, base64, integer, decimal, when nearly all values match. |
| lengths | The learned range widened by a quarter (at least 1 byte). |
| value list | At most 32 values, each sent by enough clients and seen at least 10 times. |
| number range | At least 0 when no negative was seen; the largest seen plus a quarter (at least 1). |
| array size | The longest array seen plus a quarter. |
A field with too few values or clients is known (never counted as unknown) but not constrained. Values of sensitive fields are never kept, only their shape.
Settings reference
Learning
| Setting | What it does | Default | Allowed values | Applies in |
|---|---|---|---|---|
Learn JSON body structure (policy.positive.learning.bodies) | Records the structure of clean JSON request bodies. | true | true, false | learning enabled |
Learn JSON answers (policy.positive.learning.responses) | Also records JSON answers below 400. | false | true, false | learning enabled |
Answers learned per second (policy.positive.learning.response_sample_rate) | Answers read per second, per endpoint and node. | 1 | 1 to 100 | answer learning on |
Enforcement
| Setting | What it does | Default | Allowed values | Applies in |
|---|---|---|---|---|
Learned schema mode (learned.mode) | Whether violations refuse or only log. | empty (same as API protection) | detect, block, empty | detect wins over the application's block |
Fields not in the schema (learned.unknown_fields) | What happens to body fields the schema does not name. | log | log, refuse | block mode refuses |
Check answers (learned.responses) | Checks answers against response schemas. | detect | off, detect | findings are only logged |
Schema entry
| Setting | What it does | Default | Allowed values | Applies in |
|---|---|---|---|---|
endpoint | The endpoint. | none (required) | METHOD /path/{var} | all |
source | Who wrote it. | learned when approved | learned, manual | manual is never overwritten |
request, response | The schema of the body and of the answer. | none | schema nodes below | all |
additional | What happens to unknown fields of an object. | the Fields not in the schema setting | log, refuse, allow | block mode refuses |
response.status | Statuses the response schema applies to. | every 2xx answer | status list | all |
Schema nodes: type (object, array, string, integer, number, boolean, null; empty: any), format, nullable; objects: properties, required, additional; arrays: items, max_items; strings: min_length, max_length, enum; numbers: minimum, maximum.
Rules
| Rule | Meaning |
|---|---|
api.learned.body | A value breaks a constraint. The event shows the JSON pointer and the broken constraint. |
api.learned.unknown_field | A body field is not in the schema (for example /admin in a sign-up body: mass assignment). |
api.learned.response | An answer breaks its schema. Always only logged. |
One reason per kind and request is reported. Exceptions accept these IDs and api.learned.*.
Limits
| Limit | Value |
|---|---|
| Depth examined per body | 16 levels |
| Array elements examined | 32 |
| Values examined per body | 2,048 |
| Field positions per endpoint and direction | 256 (more are counted as overflow) |
| Candidate values per field | 32, each up to 64 bytes |
| Answer size read | 256 KiB |
| Stored observation per endpoint and day | 256 KiB |
| Schemas per application | 2,000 (one per endpoint) |
| Levels and nodes per endpoint | 16 and 512 (request and response together) |
Troubleshooting
No schema is suggested for an endpoint
- Cause: the endpoint has too few clean requests, too few clients or too short a learning time.
- Check:
waap api learned schema -app shop -endpoint "POST /api/orders". - Fix: wait for more traffic.
A valid request breaks api.learned.unknown_field
- Cause: the schema was learned before the field existed, or the object overflowed 256 field positions.
- Fix: edit the schema (it becomes
manual) or setadditional: allowon that object.