Back to projects

Python · Knowledge base and AI · 2025

Birds of Costa Rica

Search Costa Rican birds by their features over a Prolog knowledge base, with AI-generated suggestions and an injection vulnerability found and fixed.

  • Python
  • Flask
  • Prolog
  • Azure OpenAI

The problem

Identifying a bird from what someone remembers (color, size, habitat, diet) is hard with lists or text search. It needed a way to combine several features at once over a catalog of Costa Rican species and to help interpret vague descriptions.

My role

Lead developer: Prolog knowledge base, Flask back end, Azure OpenAI integration and front end. Team academic project for the course Lenguajes y Paradigmas (programming languages and paradigms; UIA, 2025); teammates contributed specific bug fixes.

The solution

A Flask application queries an SWI-Prolog knowledge base holding the species and their features. Azure OpenAI suggests filters from a free-text description, and the result is validated against the Prolog base. From a photo, the app extracts the dominant color to use as a filter; it doesn't classify the image.

  1. Web UI
  2. Flask API
  3. Filter validation
  4. Prolog knowledge base
  5. Azure OpenAI suggestions

Technical decisions

Prolog to combine features

A fact base lets filters (color, size, habitat, diet) be combined declaratively and species be added without changing the search code.

AI suggests, the knowledge base decides

What the language model proposes is treated as a suggestion: it's only used if it matches valid values and species that exist in the base.

Safe configuration and demo mode

Azure credentials are read from the environment, never from the code. Without credentials, the app runs in demo mode and handles the service's usage limits.

Security case: code injection in Prolog queries

Vulnerability class
Code injection (CWE-94), a form of improper input neutralization (CWE-74), made worse because part of the input came from a language model.
How I found it
While reviewing the code I noticed search filters were concatenated into the Prolog query text. I confirmed it locally with a harmless test: a crafted filter managed to add a marker fact to the knowledge base.
Impact
Anyone using the form or the API could run arbitrary Prolog goals and modify the fact base; in the worst case, run commands on the server. The model's suggestions followed the same path, so a prompt injection could end up as code injection. The fact-base generator also had incomplete escaping.
Mitigation
I separated data from code: searches always call a fixed predicate, each value passes a strict allowlist and is quoted as an atom with full escaping, and anything else gets a clear 400 error. Internal errors no longer reach the client and the server logs only the exception type.
Verification
I first wrote regression tests that reproduced the issue and failed; after the fix they all pass, and valid searches return exactly the same results as before.
Lesson
Language model output is untrusted input. No interpreted language, whether SQL, shell or Prolog, should be built by concatenating external text.

What I verified

  • 33 automated tests passing, including the injection regression tests, CORS and Azure configuration handling.
  • CORS off by default and limited to configured origins; Wikimedia Commons photos under free licenses with visible attribution.
  • The new queries return the same results as the old ones for the tested searches.
  • Runs in demo mode without credentials or network calls.

Scope and limitations

  • It isn't a visual classifier: only the photo's dominant color is used.
  • Searches with accents (for example, "montaña") still return no results because of how the fact base is encoded.
  • The demo runs locally; there's no public online version.

Need something similar?

Tell me what you need and I'll prepare a no-obligation quote.