KScript docs
EN

Routes and APIs

Settings: kscript.json

The http section of kscript.json holds the settings for routes and their middleware. Every one is optional:

JSON
{
  "http": {
    "routes": ["routes.kscript", { "file": "api/v1.kscript", "prefix": "/api/v1", "api": true }],  // default: routes/web.*, routes/api.*, routes.* and api.*
    "middleware": ["log"],                      // for every route
    "groups": { "api": ["cors", "throttle"] },  // names for lists of middleware
    "views": "views",                           // where @view() looks first; never served
    "api_prefix": "/api",                       // the prefix of api.* files
    "write_files": true,                        // false: controllers and their live actions can only read files
    "cors": { "origins": ["*"], "methods": ["GET", "POST"], "headers": ["Content-Type", "Authorization"], "credentials": false },  // credentials needs listed origins
    "auth": { "token_env": "API_TOKEN", "login": "/login", "lifetime": "2h" },  // and the KScript.Auth settings in auth.md
    "throttle": { "limit": 60, "seconds": 60, "store": "memory" },  // store: memory, file or database (shared by every server)
    "upload_limit": "8MB",                      // the most a request's uploaded files may be, in all
    "body_limit": "1MB",                        // the most a body without files (a form, JSON, text) may be
    "timeout": 30,                              // seconds a request may run before it gets a 503; false for no limit
    "outbound": { "allow": ["search.internal"] },  // private hosts @api may call while answering a request
    "headers": { "X-Frame-Options": "DENY" },   // sent with every response that doesn't set its own; see below for the ones sent anyway
    "proxy": { "secret_env": "PROXY_SECRET", "header": "X-Proxy-Secret" },  // believe a reverse proxy's X-Forwarded-For
    "openapi": { "routes": "auto", "title": "People API", "version": "1.2.0", "file": "openapi.json" }  // or true / false
  }
}

Every setting is also in the kscript.json reference.

Headers on every response

Every response kscript serve sends carries three headers without being asked, so a new site starts out safe:

Header Value What it does
X-Content-Type-Options nosniff The browser uses the type the server gave a file, and never guesses another.
Referrer-Policy strict-origin-when-cross-origin Other sites are told only this site's origin, not the page a visitor came from.
X-Frame-Options SAMEORIGIN Other sites can't show this one in a frame. Left out when your Content-Security-Policy has frame-ancestors, which says the same thing more exactly.

headers adds response headers to everything kscript serve answers: pages, files, routes, passkeys and the sign-in provider. A response that sets a header itself keeps its own, and a header named here replaces the one above. An empty value sends none: "X-Frame-Options": "" lets any site frame this one. It is the place for a site's other security headers:

JSON
{
  "http": {
    "headers": {
      "Content-Security-Policy": "default-src 'self'; script-src 'self'; frame-ancestors 'none'; form-action 'self' {client_origins}",
      "X-Content-Type-Options": "nosniff",
      "Referrer-Policy": "same-origin"
    }
  }
}

{client_origins} is the origins of the apps in the sign-in provider's clients.json, separated by spaces, so a form that sends someone back to an app is allowed without listing each one twice.

Behind a reverse proxy

Behind a proxy such as Caddy or nginx, every request comes from the proxy, so rate limits and sign-in attempt counts would lump every visitor together. Have the proxy send a secret in a header, and name the environment variable that holds it:

JSON
{ "http": { "proxy": { "secret_env": "PROXY_SECRET", "header": "X-Proxy-Secret" } } }

A request carrying that secret is counted under the last address in its X-Forwarded-For, the one the proxy added; any other request under the address it came from, so nothing that reaches the server another way can pretend to be someone else. throttle counts an IPv6 visitor by their /64, since one machine can use any address in it.

Limits for a public server

A few limits keep one request, or one visitor, from taking the server down or reaching what it shouldn't. They are on without being asked.

Each request has 30 seconds. Whatever answers it, a page, a route or a live action, stops when the time runs out, even while it waits for @api or a database, and the visitor gets a 503. Slow requests then can't pile up and hold every thread. Give a route that really needs longer more time with timeout, in seconds, or false for no limit:

JSON
{ "http": { "timeout": 120 } }

Requests can't call the server's own network. While answering a request, @api and @download refuse addresses inside this computer or its private network: localhost, 10.x, 172.16.x to 172.31.x, 192.168.x, and 169.254.169.254, where cloud servers keep their keys. Without this, a route that fetches an address a visitor gives it (a link preview, a webhook tester) could be pointed at the database's admin page. Each address is checked as the connection opens, so a redirect or a name that points somewhere else by then is refused too. List the private hosts your app does call:

JSON
{ "http": { "outbound": { "allow": ["localhost", "search.internal"] } } }

Scripts you run yourself, tests and queue workers can call anything.

@api reads at most 100 MB of an answer, so a huge or endless response can't fill the memory: give options: { max_size: ... } to change it, or save a large file with @download(), which stops after 30 minutes unless you give it a timeout:.

CORS with cookies needs a list of sites. "credentials": true with "origins": ["*"] (the default) would let any website make requests as the person signed in, so it is a settings error, and kscript serve won't start with it. List the sites that may.

kscript serve also warns when it listens where other computers can reach it outside production, where visitors see each error's file and line, and when production runs without KSCRIPT_SECRET.