Build and test a defensive reverse proxy with an echo server and synthetic traffic. These ten Digital Qazaqstan exercises follow the original topic sequence and use harmless markers and deterministic detector scores.

Use a local echo service, a local proxy and an in-process detector stub for the exercises below. Give each fixture a fixed expected decision so it can run in a unit test.

Contents

Core project

1. Respond to health checks

Implement a health endpoint for a local service. Make the readiness response independent of upstream content. Record the method, path, status and response length.

Acceptance check. Run a local test client against your healthy instance. Assert the expected status and a stable response body. Test the upstream-unavailable case separately.

2. Proxy web requests

Place a reverse proxy in front of a local echo application you control. Preserve the request method, path, query and body. Forward only end-to-end headers and return the upstream status.

Acceptance check. Send an ordinary request containing item=blue. Compare the values seen by the echo service to the input. Check a response with an empty body.

3. Block and log

Implement a rule that looks for the literal teaching marker LAB-CHECK. In monitoring mode record a match and forward the original request. In blocking mode stop that request and record the rule identifier.

Acceptance check. Prepare one ordinary request and one containing LAB-CHECK. Assert the decision, log event and whether the local upstream received each request in both modes.

4. Normalize request inputs

Build a separate normalized copy of declared inspection fields while preserving the original bytes for forwarding. Specify the decoding order and behavior for malformed input.

Acceptance check. Use a local fixture whose query represents the word blue and a space using ordinary URL encoding. The inspection view should contain the decoded text and the mock upstream should receive the original representation.

5. Score attack signals

Assign weights to two harmless labels, BLUE and ROUND. Define when each rule contributes and compare the combined value to a configured threshold. Include the matching rule IDs in the decision record.

Acceptance check. Set the weights to 3 and 2 and the block threshold to 5. Test each label alone, both labels together, and repeated occurrences of one label. Document whether repeats count.

6. Add ML detection

Add a detector interface that returns a numeric score. Use a local stub with fixed scores. Implement a response deadline and a documented failure policy.

Acceptance check. Test deterministic scores of 0.1 and 0.9, an invalid response and a timeout. Record which decision came from the score and which came from your failure policy.

7. Tune detection decisions

Set separate thresholds for two routes and evaluate them against labelled synthetic examples. Count false positives and false negatives by route. Define a fallback for unknown routes.

Acceptance check. Keep the synthetic inputs and scores fixed. Change one threshold and recalculate both error counts; record which sample changed classification and why.

8. Inspect request bodies

Inspect supported JSON and form bodies with a configured size bound. Validate media type and parsing results. Preserve the original body for requests your policy lets through.

Acceptance check. Use a valid colour field, an empty body, malformed JSON and a body exactly one byte over the limit. Verify the documented outcome and whether the upstream received any bytes.

9. Prevent response leaks

Hold the upstream response until inspection completes within a fixed limit. Match the harmless output marker INTERNAL-DEMO and produce an appropriate replacement response when policy requires it.

Acceptance check. Test clean and marked responses, a marker that crosses a chunk boundary and an oversized response. Confirm the client receives no marked bytes.

10. Virtually patch an endpoint

Use an allowlist to restrict fields on a local demonstration export route. Allow public fields such as title and colour. Define decisions for unknown or repeated fields and record the reason.

Acceptance check. Prepare a table of allowed, unknown and mixed field selections using synthetic data. Assert the response and decision log. Keep the same cases as regression tests for the upstream fix.

Back to contents

Source and licence

Defensive laboratory exercises written by Digital Qazaqstan following the stage sequence of CodeCrafters. Upstream licence and attribution.

MIT License

Copyright (c) 2022 CodeCrafters

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.