Blog

Secure RAG: how AI can work with your data without leaking it

Breytt 6 min read
Two men walking down an office corridor

One of the most common things leaders want from AI is simple: for staff to ask questions and get answers based on the company's own information. Handbooks, contracts, procedures, quotes and meeting notes. The technology that makes this possible is called RAG, and it's now within reach of companies of every size.

But the moment AI gets access to company data, sensible questions follow. Who can see what? Where does the data go? And what happens if someone tries to misuse the system? In this article we look at how RAG works and what it takes to make it secure.

What is RAG?

RAG stands for retrieval-augmented generation. Instead of the language model answering only from what it learned in training, the system first searches your own documents for the passages that are relevant to the question. Those passages are added to the prompt, and the model answers from them.

In broad strokes, the process looks like this:

  • Documents are split into chunks and indexed in a searchable database.
  • When someone asks a question, the system finds the chunks that best match it.
  • The language model receives the question and the chunks and writes an answer, ideally with references to its sources.

The advantage is obvious. Answers are grounded in your data, not general knowledge from the internet, and you can see where each answer comes from. Nor does the model need retraining when your data changes. Updating the database is enough. You'll find more terms like this explained in our glossary.

Where can data leak?

The risk in a RAG system rarely lies in the language model itself. It lies in how the data around it is handled. Four areas matter most.

Access control. If everyone can ask the system about everything, someone on reception could get answers drawn from payroll data or confidential contracts. The system must respect the same permissions that apply to the documents themselves. This is one of the most common weaknesses, and also one of the easiest to prevent if you plan for it from the start.

Where the data goes. When chunks of documents are sent to a language model, you need to know where the model runs, whether the data is stored and whether it's used for training. Terms vary between services and plans, and the free version of a service can follow different rules from the business version. Get the answers in writing.

Prompt injection. A document, email or web page can contain hidden instructions designed to make the model ignore its rules or reveal information it shouldn't. Prompt injection tops the OWASP list of the most critical security risks for applications built on large language models. The same list also includes weaknesses in vectors and embeddings, which sit at the very heart of RAG systems.

Personal data. If your documents contain personal data, data protection law applies, in Iceland the Data Protection Act (No. 90/2018). Among other things, processing needs a clear purpose and a legal basis, and you shouldn't process more information than you need.

The system should never know more than the person asking.

A checklist for secure RAG

Go through these points before the system goes live, and again every time new data is added.

  1. Map the data before connecting anything. What's sensitive, what's out of date and what doesn't belong in the system at all?
  2. Have the system inherit document permissions, so each user only gets answers from data they can already access.
  3. Choose where the model runs, and confirm in writing how data is stored, where, and whether it's used for training.
  4. Remove or anonymise personal data wherever it isn't needed.
  5. Make every answer cite its sources so users can check them.
  6. Log questions and answers, and review the logs regularly.
  7. Test the system deliberately with prompt injection attempts before it goes into use.
  8. Decide who owns the system and how to respond if something goes wrong.

How do you know the answers are good?

Security isn't only about keeping data where it belongs. It's also about being able to trust what the system tells you. RAG greatly reduces the risk of made-up answers, but it doesn't eliminate it. The model can misread the question, pick the wrong chunk or draw too broad a conclusion from the right data.

So put together a small set of real questions from day-to-day work that you know the correct answers to. Run them through the system before it goes live, and every time the data, the model or the settings change. That way you'll see straight away if quality slips.

Teach staff to read the sources, too. An answer with a reference is only useful if someone clicks it when the stakes are high.

Start small, with the right data

It's tempting to connect the whole drive, every inbox and every system in one go. It's also the surest way to get into trouble. Start with a defined collection, such as procedures or product information, that many people need to look things up in and that contains no sensitive data.

That gives you real experience of answer quality, shows you how staff actually use the system and builds trust before more sensitive data is added. The quality of the answers also depends on the quality of the data. Outdated versions, duplicate documents and messy records lead straight to muddled answers, so tidying up your data is often the first and most important step.

Remember that security isn't something you bolt on at the end. Access control, logging and testing need to be part of the design from day one. That's cheaper, and safer, than trying to plug gaps once the system is in use.

Next steps

A secure knowledge base is one of the solutions we set up through Applied AI: staff query company data directly and get answers with references, within the permissions they already have.

If your technical team wants to understand the technology first, our talk AI That Doesn't Break covers RAG, security, database design and what can go wrong when usage scales fast. It's aimed at developers and engineering teams.