설정: kscript.json
kscript.json의 http 섹션에는 경로와 미들웨어 설정이 들어 있어요. 모든 항목은 선택 사항이에요:
{
"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
}
}모든 설정은 kscript.json 참조에도 있어요.
모든 응답의 헤더
kscript serve가 보내는 모든 응답에는 요청하지 않아도 세 가지 헤더가 포함되어, 새 사이트가 기본적으로 안전해요:
| 헤더 | 값 | 기능 |
|---|---|---|
X-Content-Type-Options |
nosniff |
브라우저는 서버가 알려준 파일 형식을 사용하며, 다른 형식으로 추측하지 않아요. |
Referrer-Policy |
strict-origin-when-cross-origin |
다른 사이트에는 방문자가 온 페이지가 아니라 이 사이트의 출처만 전달돼요. |
X-Frame-Options |
SAMEORIGIN |
다른 사이트가 이 사이트를 프레임에 표시할 수 없어요. Content-Security-Policy에 frame-ancestors가 있으면 이 헤더는 빠지는데, 그 쪽이 같은 내용을 더 정확히 나타내기 때문이에요. |
headers는 kscript serve가 응답하는 모든 것에 응답 헤더를 추가해요. 페이지, 파일, 경로, 패스키, 로그인 제공자가 포함돼요. 응답이 스스로 설정한 헤더는 그대로 유지되고, 여기에 적은 헤더는 위의 기본 헤더를 대체해요. 값을 비워 두면 헤더를 보내지 않아요: "X-Frame-Options": ""는 어떤 사이트든 이 사이트를 프레임에 넣을 수 있게 해요. 사이트의 다른 보안 헤더를 설정하는 곳이에요:
{
"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}는 로그인 제공자의 clients.json에 있는 앱들의 출처를 공백으로 구분해 적은 것이에요. 그래서 사용자를 앱으로 돌려보내는 양식을 일일이 두 번씩 적지 않아도 허용돼요.
역방향 프록시 뒤에서
Caddy나 nginx 같은 프록시 뒤에서는 모든 요청이 프록시에서 오기 때문에, 요청 한도와 로그인 시도 횟수가 모든 방문자를 하나로 묶어 버려요. 프록시가 헤더에 비밀 값을 담아 보내도록 하고, 그 값을 담은 환경 변수의 이름을 지정하세요:
{ "http": { "proxy": { "secret_env": "PROXY_SECRET", "header": "X-Proxy-Secret" } } }그 비밀 값을 담은 요청은 X-Forwarded-For의 마지막 주소, 즉 프록시가 추가한 주소로 집계되고, 그 밖의 요청은 실제 출발지 주소로 집계돼요. 그래서 다른 경로로 서버에 도달한 요청이 다른 사람인 척할 수 없어요. throttle는 IPv6 방문자를 /64 단위로 집계해요. 한 기기가 그 안의 어떤 주소든 쓸 수 있기 때문이에요.