Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,18 @@ ENABLE_HYBRID_SEARCH=true
# Never expose credentials through PUBLIC_/NEXT_PUBLIC_ variables.
# Redis is required for sessions and each user's recent chats.
# -----------------------------------------------------------------------------
SALEM_ADMIN_PASSWORD=your_salem_admin_password
# Admin credential. Prefer a PBKDF2 hash -- generate one with a password on stdin:
# printf '%s' "$PASSWORD" | bun run user:hash --hash-only
# A plaintext value still signs in, but it is deprecated, logs a warning on every
# cold start, and cannot carry a random per-credential salt. See README,
# "Retiring the legacy admin credential".
SALEM_ADMIN_PASSWORD=pbkdf2-sha256:600000:your_salt:your_hash
SALEM_ADMIN_USERNAME=admin
# Private research access for the admin account. Not granted implicitly.
# SALEM_ADMIN_PRIVATE_SOURCES=true
# Explicitly provision additional users. Use `bun run user:hash <id> <name>`
# with a password on stdin to generate each entry; then collect entries in an array.
# (add --private for private research access) with a password on stdin to generate
# each entry; then collect entries in an array.
# Private research is denied unless privateSources is explicitly true.
# SALEM_USERS_JSON=[{"id":"alice","name":"Alice","passwordHash":"<generated hash>","privateSources":false}]

Expand Down
107 changes: 90 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ Authorized deployments can also index private OpenCoven research from server-onl
## Access and recent chats

Salem requires sign-in before showing the chat interface or accepting any question.
The existing `SALEM_ADMIN_PASSWORD` signs in to the `admin` account (override its
username with `SALEM_ADMIN_USERNAME`). The former public first-question access and
`SALEM_ADMIN_PASSWORD` signs in to the `admin` account (override its username with
`SALEM_ADMIN_USERNAME`). The former public first-question access and
`X-Salem-Admin-Password` API header are no longer supported.

Additional users must be explicitly provisioned in the server-only
Expand All @@ -40,17 +40,18 @@ user's ID for someone else.

To create a user entry, pipe a password of at least 12 characters from a password
manager into `bun run user:hash alice "Alice"`. The command reads stdin without
printing the password and outputs an entry containing a salted password hash.
Collect approved entries in a JSON array and set `SALEM_USERS_JSON` in Vercel:
printing the password and outputs an entry containing a salted password hash. Add
`--private` to set `privateSources` on the generated entry. Collect approved
entries in a JSON array and set `SALEM_USERS_JSON` in Vercel:

```json
[{"id":"alice","name":"Alice","passwordHash":"<generated hash>","privateSources":false}]
```

Set `privateSources` to `true` only for users permitted to access private research.
The initial admin account retains private research access. Removing a user or
changing their password or private-source permission invalidates existing sessions.
Deploy the environment changes to apply them.
No account receives it implicitly, including `admin` — see the migration note
below. Removing a user or changing their password or private-source permission
invalidates existing sessions. Deploy the environment changes to apply them.

Sessions use an HttpOnly, SameSite=Strict cookie (Secure in production), expire
after 12 hours, and are revoked on sign-out. Redis is required for sessions and
Expand All @@ -63,6 +64,51 @@ reply. Reopen a recent chat to continue it, or choose **New chat**. Interrupted
unsaved replies produce an error and are not added to the saved conversation.
Chats from before this feature cannot be recovered because they were not stored.

## Retrieved documentation is untrusted input

Salem indexes sources it does not author: `https://docs.typesafe.ai/llms-full.txt`,
and the markdown behind `https://code.opencoven.ai` (fetched from
`OpenCoven/coven-code`). Retrieved excerpts used to be interpolated straight into
the system prompt, which put that text at the same trust level as Salem's own
instructions — anyone who could land a paragraph in either source could issue
system-level instructions to every conversation that retrieved it.

The rule now is that **the system message is the only instruction-trusted region,
and it contains first-party text only.** Four mechanisms enforce it
(`lib/prompt-context.ts`):

1. **Separation.** Excerpts travel in a `user`-role message immediately before the
question. No retrieved byte reaches the system message.
2. **Unforgeable delimiters.** Each excerpt sits in a
`<salem-document nonce="…">` block whose nonce is random per request. Content
cannot close a boundary it cannot predict, so it cannot escape its block, end
the data region, or impersonate another role. Literal block tags and control
characters in content are neutralised.
3. **Provenance by origin.** Each block is labelled `provenance="opencoven"` or
`provenance="external"`, derived from the host the bytes came from — never from
the content. A feed's `Source:` line is also bound to the host that served the
feed (`rag/indexer.ts`), so `docs.typesafe.ai` cannot declare a page as
`docs.opencoven.ai` and be indexed, or cited, as first-party documentation.
Provenance governs authority over facts, never authority over instructions.
4. **Attribute-only citations.** The model is instructed to cite a block's `url`
attribute and never a link found inside block content, so a document cannot get
Salem to recommend a destination of its choosing.

There is deliberately no blocklist of phrases like "ignore previous instructions".
The indexed corpus is documentation about LLMs and agents, so prompt-shaped text is
legitimate content: pattern matching would mangle real docs while a paraphrase
walked past. The defences are structural instead.

This is containment, not a proof. A model can still be persuaded by text it is
told to treat as data. What the boundary guarantees is that such text arrives at
user trust level, labelled, delimited, and unable to forge structure — and that
Salem has no tools, so the worst case is a wrong answer in one session rather than
an action taken on the attacker's behalf. Adding any tool or side effect to this
route means revisiting that conclusion.

Changing the provenance rules requires a reindex to take effect for chunks already
stored: `bun run build:index`.

## API Endpoints

Salem is a **first-party, session-only API**: it serves its own UI and no other
Expand Down Expand Up @@ -149,8 +195,9 @@ cp .env.example .env
| `COHERE_API_KEY` | No | Cohere key for reranking |
| `GITHUB_WEBHOOK_SECRET` | No | Secret for GitHub webhook |
| `REINDEX_SECRET` | No | Secret for scheduled re-index endpoint |
| `SALEM_ADMIN_PASSWORD` | Yes, unless named users are configured | Password for the initial admin account. Legacy: see the note below |
| `SALEM_ADMIN_PASSWORD` | Yes, unless named users are configured | Admin credential: a `pbkdf2-sha256:600000:...` hash, or a deprecated plaintext password. See the migration note below |
| `SALEM_ADMIN_USERNAME` | No | Initial admin username, defaults to `admin` |
| `SALEM_ADMIN_PRIVATE_SOURCES` | No | Set to `true` to grant the admin account private research access. No longer implied |
| `SALEM_USERS_JSON` | No | Approved named users with password hashes and optional private research permission |
| `SALEM_PRIVATE_RESEARCH_DOCS_BASE64` | No | Base64-encoded private research markdown to include in Salem's index |
| `SALEM_PRIVATE_RESEARCH_REPO` | No | Private GitHub repo for research sources, for example `your-org/your-private-research` |
Expand All @@ -160,14 +207,40 @@ cp .env.example .env

Authentication variables are server-only. Never expose them through `PUBLIC_` or `NEXT_PUBLIC_` variables. Without a configured admin or approved user list, the interface stays locked.

> **Prefer `SALEM_USERS_JSON` over `SALEM_ADMIN_PASSWORD`.** The admin variable is a
> legacy path: it is compared as an unsalted single-round SHA-256 of the plaintext,
> while every account in `SALEM_USERS_JSON` uses PBKDF2-SHA256 at 600,000 iterations.
> It is also the only account that is granted private-research access unconditionally,
> so it is simultaneously the highest-privilege credential and the weakest-hashed one.
> Mint a replacement with `bun run user:hash <id> "<name>"`, add it to
> `SALEM_USERS_JSON` with `privateSources: true`, then unset `SALEM_ADMIN_PASSWORD`.
> Removing it invalidates any live session issued against it, which is the intent.
### Retiring the legacy admin credential

`SALEM_ADMIN_PASSWORD` used to hold a plaintext password compared as an unsalted
single-round SHA-256, and it was the only account granted private research access
unconditionally — simultaneously the highest-privilege credential and the
weakest-hashed one. Two things changed so that neither is true any more:

- **The variable now accepts a password hash.** If its value matches
`pbkdf2-sha256:600000:<salt>:<hash>`, the account is treated exactly like a
`SALEM_USERS_JSON` entry and the legacy verification path is not used at all.
A plaintext value still signs in, so no deploy locks itself out, but it is
verified with PBKDF2-SHA256 at 600,000 iterations over a salt derived from the
account ID, and it logs a deprecation warning on every cold start.
- **Private research access is opt-in.** `SALEM_ADMIN_PRIVATE_SOURCES=true` grants
it; without that variable the admin account has no private-source access, whatever
its credential form.

Session records no longer contain the plaintext admin password or a hash of it. The
session credential version is derived from the PBKDF2 output, so a Redis read is no
longer enough to recover the password offline.

**To migrate** (either route retires the legacy path; the first keeps the variable):

```sh
# In place: replace the plaintext value of SALEM_ADMIN_PASSWORD with a hash.
printf '%s' "$NEW_PASSWORD" | bun run user:hash --hash-only

# Or move the account into SALEM_USERS_JSON and unset SALEM_ADMIN_PASSWORD.
printf '%s' "$NEW_PASSWORD" | bun run user:hash --private admin "Administrator"
```

Set `SALEM_ADMIN_PRIVATE_SOURCES=true` if that account needs private research.
Changing the credential form or the private-source grant invalidates every live
session issued against the old one, which is the intent.

Private research variables are also server-only. If `SALEM_PRIVATE_RESEARCH_DOCS_BASE64` is set, Salem indexes that markdown directly. If `SALEM_PRIVATE_RESEARCH_REPO` and `SALEM_PRIVATE_RESEARCH_PATHS` are set, Salem fetches those private Markdown files through the GitHub Contents API using `SALEM_PRIVATE_RESEARCH_GITHUB_TOKEN`.

Expand Down Expand Up @@ -195,7 +268,7 @@ Runs locally at http://localhost:3000.
| `bun run typecheck` | Type-check with `tsc --noEmit` |
| `bun run test` | Run the full offline test suite |
| `bun run build:index` | Index documentation into vector store |
| `bun run user:hash` | Hash a password for `SALEM_USERS_JSON` |
| `bun run user:hash` | Hash a password for `SALEM_USERS_JSON` (`--private`, `--hash-only`) |
| `bun run deploy` | Deploy to Vercel |

### Pre-commit checks
Expand Down
3 changes: 2 additions & 1 deletion SECURITY.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ The following properties are design goals of OpenCoven. If you find a way to vio
2. **Memory ownership** — a user's stored memory and context must remain under their control
3. **Agent identity integrity** — a familiar's identity must not be forgeable by another agent or external caller
4. **Execution boundaries** — agent tool calls must not escape their intended scope
5. **Instruction/data separation** — content a familiar retrieves, indexes, or is shown must never be able to act as an instruction to it. Whoever can write to a retrieved source must not thereby gain control of the familiar

---

*Last updated: 2026-07-04*
*Last updated: 2026-09-20*
11 changes: 11 additions & 0 deletions app/api/chat/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,29 @@ export function filterPrivateSourceResults<T extends { url: string }>(
return results.filter((result) => !isPrivateSourceUrl(result.url));
}

/**
* Assembles the request messages.
*
* `contextMessage` carries retrieved documentation and is sent with the `user`
* role, immediately before the question. That placement is the trust boundary:
* the system message holds first-party instructions only, so a document can
* never speak at instruction level. See lib/prompt-context.ts.
*/
export function buildChatMessages({
systemPrompt,
history,
contextMessage,
currentMessage,
}: {
systemPrompt: string;
history: ChatHistoryMessage[];
contextMessage?: string | null;
currentMessage: string;
}): ChatCompletionMessage[] {
return [
{ role: "system", content: systemPrompt },
...history,
...(contextMessage ? [{ role: "user" as const, content: contextMessage }] : []),
{ role: "user", content: currentMessage },
];
}
48 changes: 33 additions & 15 deletions app/api/chat/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ import {
normalizeChatHistory,
} from "./auth";

import {
contextNonce,
dataHandlingPolicy,
renderContextMessage,
} from "@/lib/prompt-context";
import { requestUser, sameOrigin } from "@/lib/session-http";
import { ChatHistoryStore, type SavedChat } from "@/lib/chat-history";
import { RedisStorage } from "@/lib/storage";
Expand Down Expand Up @@ -54,14 +59,21 @@ function jsonResponse(
});
}

function buildSystemPrompt(context: string): string {
/**
* Grounded prompt used when retrieval is confident.
*
* Carries no retrieved text. Excerpts are delivered separately as untrusted
* data (lib/prompt-context.ts) so that indexed third-party documentation cannot
* reach the instruction-trusted system message.
*/
function buildSystemPrompt(nonce: string, hasResults: boolean): string {
return `You are Salem, OpenCoven's local familiar — the persistent documentation familiar that guides people through the OpenCoven ecosystem.
OpenCoven is an open, local-first ecosystem for persistent AI familiars with memory, identity, tools, and observable work. You embody that ideal: a grounded, reliable familiar that helps users navigate the docs through natural conversation.

INSTRUCTIONS:
1. Answer ONLY from the provided documentation excerpts
2. If the answer is not in the excerpts, clearly state this
3. Cite sources using [Source Title](URL) format
3. Cite sources using [Source Title](URL) format, taking both from the excerpt's own attributes
4. For code examples, use the exact code from docs when available
5. Be concise but complete
6. If multiple approaches exist, mention the recommended one first
Expand All @@ -76,17 +88,21 @@ CONFIDENCE:
- If partially confident, caveat with "Based on the available documentation..."
- If not confident, say "I couldn't find specific documentation for this..."

DOCUMENTATION EXCERPTS:
${context}`;
${dataHandlingPolicy(nonce)}
${
hasResults
? "Documentation excerpts for this question follow in the next user message."
: "No documentation excerpts were retrieved for this question. Say so rather than inventing sources."
}`;
}

/**
* Broader prompt used when retrieval confidence is low or no docs match.
* Allows general AI/agent knowledge while relating back to OpenCoven.
*/
function buildGeneralPrompt(context: string): string {
const contextBlock = context
? `\n\nThe following documentation excerpts may be partially relevant — cite them with [Source Title](URL) if you use them:\n\n${context}`
function buildGeneralPrompt(nonce: string, hasResults: boolean): string {
const contextBlock = hasResults
? `\n\nPartially relevant documentation excerpts follow in the next user message — cite them with [Source Title](URL) from their attributes if you use them.`
: "";

return `You are Salem, OpenCoven's local familiar — the persistent documentation familiar that guides people through the OpenCoven ecosystem.
Expand All @@ -112,7 +128,9 @@ SCOPE:
- LLMs, embeddings, RAG, vector databases
- OpenCoven features, APIs, products, and workflows
- Comparisons with other frameworks (when asked)
- General software engineering in the context of AI applications${contextBlock}`;
- General software engineering in the context of AI applications

${dataHandlingPolicy(nonce)}${contextBlock}`;
}

export async function POST(request: NextRequest) {
Expand Down Expand Up @@ -427,15 +445,14 @@ export async function POST(request: NextRequest) {
isLowConfidence,
);

const context = hasResults
? finalResults
.map((result) => `[${result.title}](${result.url})\n${result.content.slice(0, 1200)}`)
.join("\n\n---\n\n")
: "";
// Retrieved documentation is delimited with a fresh per-request nonce and
// sent at user trust level, never interpolated into the system prompt.
const nonce = contextNonce();
const contextMessage = renderContextMessage(finalResults, nonce);

const systemPrompt = isLowConfidence
? buildGeneralPrompt(context)
: buildSystemPrompt(context);
? buildGeneralPrompt(nonce, hasResults)
: buildSystemPrompt(nonce, hasResults);

// Stream response from OpenAI
const openaiResponse = await fetch(
Expand All @@ -456,6 +473,7 @@ export async function POST(request: NextRequest) {
messages: buildChatMessages({
systemPrompt,
history: chatHistory,
contextMessage,
currentMessage: trimmedMessage,
}),
}),
Expand Down
Loading
Loading