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
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,19 @@ csp := webtools.BuildCSP(
The `With*` options append to their source-list directive, retaining the
default `'self'` source. Use `WithCSP` to replace the policy outright.

To permit inline styles (a `<style>` block or `style="..."` attribute), append
`'unsafe-inline'` to the `style-src` directive:

```go
csp := webtools.BuildCSP(
webtools.WithStyleSrc("'unsafe-inline'"),
)
```

Inline CSS cannot execute code, and `default-src 'self'` still prevents CSS
from loading cross-origin resources, so this is a modest trade-off for
applications that rely on inline styling.

##### sanitize a url

Use `Sanitize` to remove known trackers from URL parameters.
Expand Down Expand Up @@ -240,6 +253,34 @@ func errorHandler(w http.ResponseWriter, err error) {
}
```

##### Disabling indicator style injection

By default htmx injects a small inline `<style>` block to style request
indicators. Under a strict Content Security Policy (`style-src 'self'` without
`'unsafe-inline'`) this injection is blocked. If you don't use `htmx-indicator`,
disable it with a config meta tag in the page `<head>`:

```html
<meta name="htmx-config" content='{"includeIndicatorStyles":false}'>
```

Place it before the htmx `<script>` tag. If you do use indicators, include the
CSS that htmx would otherwise inject in your own stylesheet instead:

```css
.htmx-indicator {
opacity: 0;
visibility: hidden;
}

.htmx-request .htmx-indicator,
.htmx-request.htmx-indicator {
opacity: 1;
visibility: visible;
transition: opacity 200ms ease-in;
}
```

#### package webtools/middles

Provides a generic sessions `http.Handler` which can be used to set and validate
Expand Down
1 change: 0 additions & 1 deletion csp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ func Test_BuildCSP_scriptSrc(t *testing.T) {
want := "default-src 'self'; " +
"script-src 'self' https://cdn.jsdelivr.net; " +
"style-src 'self'; " +
"style-src-attr 'unsafe-inline'; " +
"img-src 'self'; " +
"font-src 'self'; " +
"connect-src 'self'; " +
Expand Down
1 change: 0 additions & 1 deletion security.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
const DefaultCSP = "default-src 'self'; " +
"script-src 'self'; " +
"style-src 'self'; " +
"style-src-attr 'unsafe-inline'; " +
"img-src 'self'; " +
"font-src 'self'; " +
"connect-src 'self'; " +
Expand Down