@cryptotaxi247 / kubo / commits / 0bac56c3a

feat: add Gateway.DisableHTMLErrors option (#10137)

Henrique Dias committed Sep 21, 2023 at 17:03 UTC 0bac56c3aa85292b1a74dd6a35800b5c8fb5c52c
11 files changed +76 -11
config/gateway.go
+5
@@ -3,6 +3,7 @@ package config
3 const (
4 DefaultInlineDNSLink = false
5 DefaultDeserializedResponses = true
6 + DefaultDisableHTMLErrors = false
7 DefaultExposeRoutingAPI = false
8 )
9
@@ -70,6 +71,10 @@ type Gateway struct {
71 // be overridden per FQDN in PublicGateways.
72 DeserializedResponses Flag
73
74 + // DisableHTMLErrors disables pretty HTML pages when an error occurs. Instead, a `text/plain`
75 + // page will be sent with the raw error message.
76 + DisableHTMLErrors Flag
77 +
78 // PublicGateways configures behavior of known public gateways.
79 // Each key is a fully qualified domain name (FQDN).
80 PublicGateways map[string]*GatewaySpec
core/corehttp/gateway.go
+1
@@ -237,6 +237,7 @@ func getGatewayConfig(n *core.IpfsNode) (gateway.Config, error) {
237 gwCfg := gateway.Config{
238 Headers: headers,
239 DeserializedResponses: cfg.Gateway.DeserializedResponses.WithDefault(config.DefaultDeserializedResponses),
240 + DisableHTMLErrors: cfg.Gateway.DisableHTMLErrors.WithDefault(config.DefaultDisableHTMLErrors),
241 NoDNSLink: cfg.Gateway.NoDNSLink,
242 PublicGateways: map[string]*gateway.PublicGateway{},
243 }
docs/changelogs/v0.23.md
+8
@@ -8,6 +8,7 @@
8 - [🔦 Highlights](#-highlights)
9 - [Mplex deprecation](#mplex-deprecation)
10 - [Gateway: meaningful CAR responses on Not Found errors](#gateway-meaningful-car-responses-on-not-found-errors)
11 + - [Gateway: added `Gateway.DisableHTMLErrors` configuration option](#gateway-added-gatewaydisablehtmlerrors-configuration-option)
12 - [Binary characters in file names: no longer works with old clients and new Kubo servers](#binary-characters-in-file-names-no-longer-works-with-old-clients-and-new-kubo-servers)
13 - [Self-hosting `/routing/v1` endpoint for delegated routing needs](#self-hosting-routingv1-endpoint-for-delegated-routing-needs)
14 - [Trustless Gateway Over Libp2p Experiment](#trustless-gateway-over-libp2p-experiment)
@@ -47,6 +48,13 @@ the path does not exist, a CAR will be sent with a root of `bafkqaaa` (empty CID
48 This CAR will contain all blocks necessary to validate that the path does not
49 exist without having to trust the gateway.
50
51 +#### Gateway: added `Gateway.DisableHTMLErrors` configuration option
52 +
53 +The `Gateway.DisableHTMLErrors` configuration option forces errors to be
54 +displayed in browsers as plain text (`text/plain`) rather than HTML error
55 +pages. It's especially beneficial for whitelabel or middleware deployments that
56 +wish to avoid IPFS branding and links on error pages in browsers.
57 +
58 #### Binary characters in file names: no longer works with old clients and new Kubo servers
59
60 In this version, we updated Kubo to support Go 1.20+. In Go 1.20, a regression
docs/config.md
+18 -2
@@ -51,6 +51,8 @@ config file at runtime.
51 - [`Gateway.NoFetch`](#gatewaynofetch)
52 - [`Gateway.NoDNSLink`](#gatewaynodnslink)
53 - [`Gateway.DeserializedResponses`](#gatewaydeserializedresponses)
54 + - [`Gateway.DisableHTMLErrors`](#gatewaydisablehtmlerrors)
55 + - [`Gateway.ExposeRoutingAPI`](#gatewayexposeroutingapi)
56 - [`Gateway.HTTPHeaders`](#gatewayhttpheaders)
57 - [`Gateway.RootRedirect`](#gatewayrootredirect)
58 - [`Gateway.FastDirIndexThreshold`](#gatewayfastdirindexthreshold)
@@ -648,7 +650,7 @@ Default: `false`
650
651 Type: `bool`
652
651 -#### `Gateway.DeserializedResponses`
653 +### `Gateway.DeserializedResponses`
654
655 An optional flag to explicitly configure whether this gateway responds to deserialized
656 requests, or not. By default, it is enabled. When disabling this option, the gateway
@@ -658,12 +660,26 @@ Default: `true`
660
661 Type: `flag`
662
661 -#### `Gateway.ExposeRoutingAPI`
663 +### `Gateway.DisableHTMLErrors`
664 +
665 +An optional flag to disable the pretty HTML error pages of the gateway. Instead,
666 +a `text/plain` page will be returned with the raw error message from Kubo.
667 +
668 +It is useful for whitelabel or middleware deployments that wish to avoid
669 +`text/html` responses with IPFS branding and links on error pages in browsers.
670 +
671 +Default: `false`
672 +
673 +Type: `flag`
674 +
675 +### `Gateway.ExposeRoutingAPI`
676
677 An optional flag to expose Kubo `Routing` system on the gateway port as a [Routing
678 V1](https://specs.ipfs.tech/routing/routing-v1/) endpoint. This only affects your
679 local gateway, at `127.0.0.1`.
680
681 +This endpoint can be used by other Kubo instance, as illustrated in [`delegated_routing_v1_http_proxy_test.go`](https://github.com/ipfs/kubo/blob/master/test/cli/delegated_routing_v1_http_proxy_test.go).
682 +
683 Default: `false`
684
685 Type: `flag`
docs/examples/kubo-as-a-library/go.mod
+1 -1
@@ -7,7 +7,7 @@ go 1.20
7 replace github.com/ipfs/kubo => ./../../..
8
9 require (
10 - github.com/ipfs/boxo v0.13.0
10 + github.com/ipfs/boxo v0.13.1
11 github.com/ipfs/kubo v0.0.0-00010101000000-000000000000
12 github.com/libp2p/go-libp2p v0.31.0
13 github.com/multiformats/go-multiaddr v0.11.0
docs/examples/kubo-as-a-library/go.sum
+2 -2
@@ -300,8 +300,8 @@ github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:
300 github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
301 github.com/ipfs/bbloom v0.0.4 h1:Gi+8EGJ2y5qiD5FbsbpX/TMNcJw8gSqr7eyjHa4Fhvs=
302 github.com/ipfs/bbloom v0.0.4/go.mod h1:cS9YprKXpoZ9lT0n/Mw/a6/aFV6DTjTLYHeA+gyqMG0=
303 -github.com/ipfs/boxo v0.13.0 h1:uzCQekieYS4PysbYYdodNmKLuqOdLjlUziXVZ19oDeQ=
304 -github.com/ipfs/boxo v0.13.0/go.mod h1:btrtHy0lmO1ODMECbbEY1pxNtrLilvKSYLoGQt1yYCk=
303 +github.com/ipfs/boxo v0.13.1 h1:nQ5oQzcMZR3oL41REJDcTbrvDvuZh3J9ckc9+ILeRQI=
304 +github.com/ipfs/boxo v0.13.1/go.mod h1:btrtHy0lmO1ODMECbbEY1pxNtrLilvKSYLoGQt1yYCk=
305 github.com/ipfs/go-bitfield v1.1.0 h1:fh7FIo8bSwaJEh6DdTWbCeZ1eqOaOkKFI74SCnsWbGA=
306 github.com/ipfs/go-bitfield v1.1.0/go.mod h1:paqf1wjq/D2BBmzfTVFlJQ9IlFOZpg422HL0HqsGWHU=
307 github.com/ipfs/go-block-format v0.0.2/go.mod h1:AWR46JfpcObNfg3ok2JHDUfdiHRgWhJgCQF+KIgOPJY=
go.mod
+1 -1
@@ -15,7 +15,7 @@ require (
15 github.com/fsnotify/fsnotify v1.6.0
16 github.com/google/uuid v1.3.0
17 github.com/hashicorp/go-multierror v1.1.1
18 - github.com/ipfs/boxo v0.13.0
18 + github.com/ipfs/boxo v0.13.1
19 github.com/ipfs/go-block-format v0.1.2
20 github.com/ipfs/go-cid v0.4.1
21 github.com/ipfs/go-cidutil v0.1.0
go.sum
+2 -2
@@ -335,8 +335,8 @@ github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:
335 github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
336 github.com/ipfs/bbloom v0.0.4 h1:Gi+8EGJ2y5qiD5FbsbpX/TMNcJw8gSqr7eyjHa4Fhvs=
337 github.com/ipfs/bbloom v0.0.4/go.mod h1:cS9YprKXpoZ9lT0n/Mw/a6/aFV6DTjTLYHeA+gyqMG0=
338 -github.com/ipfs/boxo v0.13.0 h1:uzCQekieYS4PysbYYdodNmKLuqOdLjlUziXVZ19oDeQ=
339 -github.com/ipfs/boxo v0.13.0/go.mod h1:btrtHy0lmO1ODMECbbEY1pxNtrLilvKSYLoGQt1yYCk=
338 +github.com/ipfs/boxo v0.13.1 h1:nQ5oQzcMZR3oL41REJDcTbrvDvuZh3J9ckc9+ILeRQI=
339 +github.com/ipfs/boxo v0.13.1/go.mod h1:btrtHy0lmO1ODMECbbEY1pxNtrLilvKSYLoGQt1yYCk=
340 github.com/ipfs/go-bitfield v1.1.0 h1:fh7FIo8bSwaJEh6DdTWbCeZ1eqOaOkKFI74SCnsWbGA=
341 github.com/ipfs/go-bitfield v1.1.0/go.mod h1:paqf1wjq/D2BBmzfTVFlJQ9IlFOZpg422HL0HqsGWHU=
342 github.com/ipfs/go-bitswap v0.11.0 h1:j1WVvhDX1yhG32NTC9xfxnqycqYIlhzEzLXG/cU1HyQ=
test/cli/gateway_test.go
+35
@@ -593,4 +593,39 @@ func TestGateway(t *testing.T) {
593 t.Run(test.message, makeTest(test))
594 }
595 })
596 +
597 + t.Run("DisableHTMLErrors", func(t *testing.T) {
598 + t.Parallel()
599 +
600 + t.Run("Returns HTML error without DisableHTMLErrors, Accept contains text/html", func(t *testing.T) {
601 + t.Parallel()
602 +
603 + node := harness.NewT(t).NewNode().Init()
604 + node.StartDaemon()
605 + client := node.GatewayClient()
606 +
607 + res := client.Get("/ipfs/invalid-thing", func(r *http.Request) {
608 + r.Header.Set("Accept", "text/html")
609 + })
610 + assert.NotEqual(t, http.StatusOK, res.StatusCode)
611 + assert.Contains(t, res.Resp.Header.Get("Content-Type"), "text/html")
612 + })
613 +
614 + t.Run("Does not return HTML error with DisableHTMLErrors enabled, and Accept contains text/html", func(t *testing.T) {
615 + t.Parallel()
616 +
617 + node := harness.NewT(t).NewNode().Init()
618 + node.UpdateConfig(func(cfg *config.Config) {
619 + cfg.Gateway.DisableHTMLErrors = config.True
620 + })
621 + node.StartDaemon()
622 + client := node.GatewayClient()
623 +
624 + res := client.Get("/ipfs/invalid-thing", func(r *http.Request) {
625 + r.Header.Set("Accept", "text/html")
626 + })
627 + assert.NotEqual(t, http.StatusOK, res.StatusCode)
628 + assert.NotContains(t, res.Resp.Header.Get("Content-Type"), "text/html")
629 + })
630 + })
631 }
test/dependencies/go.mod
+1 -1
@@ -7,7 +7,7 @@ replace github.com/ipfs/kubo => ../../
7 require (
8 github.com/Kubuxu/gocovmerge v0.0.0-20161216165753-7ecaa51963cd
9 github.com/golangci/golangci-lint v1.54.1
10 - github.com/ipfs/boxo v0.13.0
10 + github.com/ipfs/boxo v0.13.1
11 github.com/ipfs/go-cid v0.4.1
12 github.com/ipfs/go-cidutil v0.1.0
13 github.com/ipfs/go-datastore v0.6.0
test/dependencies/go.sum
+2 -2
@@ -396,8 +396,8 @@ github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2
396 github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
397 github.com/ipfs/bbloom v0.0.4 h1:Gi+8EGJ2y5qiD5FbsbpX/TMNcJw8gSqr7eyjHa4Fhvs=
398 github.com/ipfs/bbloom v0.0.4/go.mod h1:cS9YprKXpoZ9lT0n/Mw/a6/aFV6DTjTLYHeA+gyqMG0=
399 -github.com/ipfs/boxo v0.13.0 h1:uzCQekieYS4PysbYYdodNmKLuqOdLjlUziXVZ19oDeQ=
400 -github.com/ipfs/boxo v0.13.0/go.mod h1:btrtHy0lmO1ODMECbbEY1pxNtrLilvKSYLoGQt1yYCk=
399 +github.com/ipfs/boxo v0.13.1 h1:nQ5oQzcMZR3oL41REJDcTbrvDvuZh3J9ckc9+ILeRQI=
400 +github.com/ipfs/boxo v0.13.1/go.mod h1:btrtHy0lmO1ODMECbbEY1pxNtrLilvKSYLoGQt1yYCk=
401 github.com/ipfs/go-bitfield v1.1.0 h1:fh7FIo8bSwaJEh6DdTWbCeZ1eqOaOkKFI74SCnsWbGA=
402 github.com/ipfs/go-bitfield v1.1.0/go.mod h1:paqf1wjq/D2BBmzfTVFlJQ9IlFOZpg422HL0HqsGWHU=
403 github.com/ipfs/go-block-format v0.1.2 h1:GAjkfhVx1f4YTODS6Esrj1wt2HhrtwTnhEr+DyPUaJo=