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 },
    "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
    "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.