Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
432342e963 |
No files matched your search
@@ -168,8 +168,10 @@ either.
|
||||
- Request bodies for `POST /api/v1/session`, `POST /api/v1/login` and
|
||||
`POST /api/v1/messages` are limited to `MAX_MESSAGE_SIZE` bytes (default
|
||||
`4096`). A larger body gets `400` `invalid request body`.
|
||||
- The server allows no cross-origin requests. In a browser, only pages the
|
||||
server itself serves, such as the web client, can use the API.
|
||||
- A response to a cross-origin request carries `Access-Control-Allow-Origin: *`,
|
||||
never the request's origin, and never `Access-Control-Allow-Credentials`. A
|
||||
page on another origin therefore cannot read the response to a request that
|
||||
carries the cookie.
|
||||
|
||||
### The auth cookie
|
||||
|
||||
@@ -1892,13 +1894,14 @@ The IRC listener has no TLS.
|
||||
seconds. A request still running after 60 seconds is cancelled, and the
|
||||
connection closes without a response. Headers may be up to 1 MiB.
|
||||
- A long poll waits at most 30 seconds.
|
||||
- Every response to a well-formed request carries a `Content-Security-Policy`
|
||||
header:
|
||||
- Every response to a well-formed request, except to a cross-origin preflight,
|
||||
carries a `Content-Security-Policy` header:
|
||||
`default-src 'self'; script-src 'self'; style-src 'self'; connect-src 'self'; img-src 'self'; font-src 'self'; object-src 'none'; frame-ancestors 'none'; base-uri 'self'; form-action 'self'`.
|
||||
The web client works within it.
|
||||
- The server allows no cross-origin requests. No response carries an
|
||||
`Access-Control-Allow-*` header. A preflight request gets `405`, like any
|
||||
other `OPTIONS` request.
|
||||
- A response to a cross-origin request carries `Access-Control-Allow-Origin: *`,
|
||||
never the request's origin, and never `Access-Control-Allow-Credentials`. A
|
||||
page on another origin therefore cannot read the response to a request that
|
||||
carries the cookie.
|
||||
|
||||
### Logging
|
||||
|
||||
@@ -2001,8 +2004,8 @@ and their sessions to end. Then it closes the database.
|
||||
gets a message.
|
||||
- `internal/hashcash/`: stamp checking and minting.
|
||||
- `internal/ratelimit/`: the per-address login rate limiter.
|
||||
- `internal/middleware/`: finding the client's address, request logging, the
|
||||
`Content-Security-Policy` header and metrics.
|
||||
- `internal/middleware/`: finding the client's address, request logging,
|
||||
cross-origin requests, the `Content-Security-Policy` header and metrics.
|
||||
- `internal/healthcheck/` and `internal/stats/`: the health check and its
|
||||
counters.
|
||||
- `internal/logger/` and `internal/globals/`: logging setup and build
|
||||
|
||||
@@ -7,6 +7,7 @@ require (
|
||||
github.com/gdamore/tcell/v2 v2.13.8
|
||||
github.com/getsentry/sentry-go v0.42.0
|
||||
github.com/go-chi/chi/v5 v5.2.1
|
||||
github.com/go-chi/cors v1.2.2
|
||||
github.com/google/uuid v1.6.0
|
||||
github.com/joho/godotenv v1.5.1
|
||||
github.com/prometheus/client_golang v1.23.2
|
||||
|
||||
@@ -20,6 +20,8 @@ github.com/getsentry/sentry-go v0.42.0 h1:eeFMACuZTbUQf90RE8dE4tXeSe4CZyfvR1MBL7
|
||||
github.com/getsentry/sentry-go v0.42.0/go.mod h1:eRXCoh3uvmjQLY6qu63BjUZnaBu5L5WhMV1RwYO8W5s=
|
||||
github.com/go-chi/chi/v5 v5.2.1 h1:KOIHODQj58PmL80G2Eak4WdvUzjSJSm0vG72crDCqb8=
|
||||
github.com/go-chi/chi/v5 v5.2.1/go.mod h1:L2yAIGWB3H+phAw1NxKwWM+7eUH/lU8pOMm5hHcoops=
|
||||
github.com/go-chi/cors v1.2.2 h1:Jmey33TE+b+rB7fT8MUy1u0I4L+NARQlK6LhzKPSyQE=
|
||||
github.com/go-chi/cors v1.2.2/go.mod h1:sSbTewc+6wYHBBCW7ytsFSn836hqM7JxpglAy2Vzc58=
|
||||
github.com/go-errors/errors v1.4.2 h1:J6MZopCL4uSllY1OfXM374weqZFFItUbrImctkmUxIA=
|
||||
github.com/go-errors/errors v1.4.2/go.mod h1:sIVyrIiJhuEF+Pj9Ebtd6P/rEYROXFi3BopGUQ5a5Og=
|
||||
github.com/go-viper/mapstructure/v2 v2.4.0 h1:EBsztssimR/CONLSZZ04E8qAkxNYq4Qp9LvH92wZUgs=
|
||||
|
||||
@@ -12,6 +12,7 @@ import (
|
||||
|
||||
basicauth "github.com/99designs/basicauth-go"
|
||||
chimw "github.com/go-chi/chi/v5/middleware"
|
||||
"github.com/go-chi/cors"
|
||||
metrics "github.com/slok/go-http-metrics/metrics/prometheus"
|
||||
ghmm "github.com/slok/go-http-metrics/middleware"
|
||||
"github.com/slok/go-http-metrics/middleware/std"
|
||||
@@ -23,6 +24,8 @@ import (
|
||||
"sneak.berlin/go/neoirc/internal/logger"
|
||||
)
|
||||
|
||||
const corsMaxAge = 300
|
||||
|
||||
// Params defines the dependencies for creating Middleware.
|
||||
type Params struct {
|
||||
fx.In
|
||||
@@ -197,6 +200,24 @@ func (mware *Middleware) Logging() func(http.Handler) http.Handler {
|
||||
}
|
||||
}
|
||||
|
||||
// CORS returns middleware that handles Cross-Origin Resource Sharing.
|
||||
// It allows every origin, without credentials, so a page on another
|
||||
// origin cannot read what the signed-in user sees.
|
||||
func (mware *Middleware) CORS() func(http.Handler) http.Handler {
|
||||
return cors.Handler(cors.Options{ //nolint:exhaustruct // optional fields
|
||||
AllowedOrigins: []string{"*"},
|
||||
AllowedMethods: []string{
|
||||
"GET", "POST", "PUT", "DELETE", "OPTIONS",
|
||||
},
|
||||
AllowedHeaders: []string{
|
||||
"Accept", "Authorization", "Content-Type", "X-CSRF-Token",
|
||||
},
|
||||
ExposedHeaders: []string{"Link"},
|
||||
AllowCredentials: false,
|
||||
MaxAge: corsMaxAge,
|
||||
})
|
||||
}
|
||||
|
||||
// Metrics returns middleware that records HTTP metrics.
|
||||
func (mware *Middleware) Metrics() func(http.Handler) http.Handler {
|
||||
metricsMiddleware := ghmm.New(ghmm.Config{ //nolint:exhaustruct // optional fields
|
||||
|
||||
@@ -30,6 +30,7 @@ func (srv *Server) SetupRoutes() {
|
||||
srv.router.Use(srv.mw.Metrics())
|
||||
}
|
||||
|
||||
srv.router.Use(srv.mw.CORS())
|
||||
srv.router.Use(srv.mw.CSP())
|
||||
srv.router.Use(middleware.Timeout(routeTimeout))
|
||||
|
||||
|
||||
@@ -189,12 +189,11 @@ func TestServeAfterStopDoesNotStopApp(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestNoCrossOriginAccess checks that the server allows no
|
||||
// cross-origin requests: neither a request from another origin
|
||||
// nor a preflight gets Access-Control-Allow-Origin or
|
||||
// Access-Control-Allow-Credentials, and a preflight gets 405
|
||||
// like any other OPTIONS request.
|
||||
func TestNoCrossOriginAccess(t *testing.T) {
|
||||
// TestCORSAllowsAnyOriginWithoutCredentials checks that the
|
||||
// responses to a request from another origin and to a preflight
|
||||
// both carry Access-Control-Allow-Origin: *, never the request's
|
||||
// origin, and no Access-Control-Allow-Credentials.
|
||||
func TestCORSAllowsAnyOriginWithoutCredentials(t *testing.T) {
|
||||
var srv *server.Server
|
||||
|
||||
startApp(t, fx.Populate(&srv))
|
||||
@@ -203,18 +202,15 @@ func TestNoCrossOriginAccess(t *testing.T) {
|
||||
name string
|
||||
method string
|
||||
requestMethod string
|
||||
status int
|
||||
}{
|
||||
{
|
||||
name: "request",
|
||||
method: http.MethodGet,
|
||||
status: http.StatusOK,
|
||||
},
|
||||
{
|
||||
name: "preflight",
|
||||
method: http.MethodOptions,
|
||||
requestMethod: http.MethodPost,
|
||||
status: http.StatusMethodNotAllowed,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -236,17 +232,13 @@ func TestNoCrossOriginAccess(t *testing.T) {
|
||||
|
||||
srv.ServeHTTP(recorder, request)
|
||||
|
||||
if recorder.Code != test.status {
|
||||
t.Errorf("status %d, want %d", recorder.Code, test.status)
|
||||
}
|
||||
|
||||
for _, header := range []string{
|
||||
"Access-Control-Allow-Origin",
|
||||
"Access-Control-Allow-Credentials",
|
||||
for header, want := range map[string]string{
|
||||
"Access-Control-Allow-Origin": "*",
|
||||
"Access-Control-Allow-Credentials": "",
|
||||
} {
|
||||
value := recorder.Header().Get(header)
|
||||
if value != "" {
|
||||
t.Errorf("%s is %q, want none", header, value)
|
||||
got := recorder.Header().Get(header)
|
||||
if got != want {
|
||||
t.Errorf("%s is %q, want %q", header, got, want)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
Reference in new issue
Block a user