# Mengamankan header HTTP dengan helmet di Gofiber

Konsep keamanan header menggunakan paket `github.com/gofiber/helmet/v2` memungkinkan kita untuk meningkatkan keamanan aplikasi web dengan mengatur header HTTP yang spesifik. Berikut adalah langkah-langkahnya:

#### Langkah-langkah:

1. **Tambahkan Middleware Helmet ke Aplikasi Fiber Anda**: Middleware Helmet akan menambahkan berbagai header keamanan ke respons HTTP Anda, seperti `X-Content-Type-Options`, `X-Frame-Options`, `X-XSS-Protection`, dan lainnya.
2. **Perbarui Konfigurasi CORS (Opsional)**: Jika Anda telah mengatur konfigurasi CORS, pastikan untuk menyinkronkannya dengan header yang ditambahkan oleh Helmet.

Berikut adalah implementasi di dalam `main.go`:

```go
package main

import (
	"log"

	"github.com/gofiber/fiber/v2"
	"github.com/gofiber/fiber/v2/middleware/cors"
	"github.com/gofiber/helmet/v2"
	"github.com/joho/godotenv"
	"gofiber/src/configs"
	"gofiber/src/helpers"
	"gofiber/src/routes"
)

func main() {
	err := godotenv.Load()
	if err != nil {
		log.Fatal("Error loading .env file")
	}

	app := fiber.New()

	// Middleware Helmet
	app.Use(helmet.New())

	// Middleware CORS
	app.Use(cors.New(cors.Config{
		AllowOrigins:  "*", // Atur "*" untuk mengizinkan akses dari semua origin
		AllowMethods:  "GET,POST,PUT,DELETE",
		AllowHeaders:  "*",
		ExposeHeaders: "Content-Length",
	}))

	configs.InitDB()
	helpers.Migration()
	routes.Router(app)

	app.Listen(":3000")
}
```

Dalam kode di atas, kita telah menambahkan middleware Helmet ke aplikasi Fiber kita menggunakan `app.Use(helmet.New())`. Middleware ini akan menambahkan header keamanan yang standar ke setiap respons HTTP yang dihasilkan oleh aplikasi.

Sebelum menggunakan Helmet, respons HTTP mungkin tidak memiliki header keamanan yang diperlukan untuk melindungi aplikasi dari berbagai serangan, seperti serangan XSS (Cross-Site Scripting), clickjacking, dan penyerangan bentuk MIME.

Setelah menggunakan Helmet, respons HTTP akan memiliki header keamanan yang tepat, seperti `X-Content-Type-Options`, `X-Frame-Options`, dan `X-XSS-Protection`, yang membantu melindungi aplikasi dari berbagai ancaman keamanan.

<figure><img src="/files/e6yvsSkupBS2g4zrUaZe" alt=""><figcaption><p>Sebelum menerapkan Helmet</p></figcaption></figure>

<figure><img src="/files/4JerfqrKWZNDNEHHDBiu" alt=""><figcaption><p>Setelah menerapkan Helmet</p></figcaption></figure>

Sumber : [Dokumentasi](https://github.com/goddtriffin/helmet)

{% embed url="<https://github.com/zakimaliki/basic_gofiber_part_1/tree/aa87f6506f068b92217b87e4d132719a48680a46>" %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://zakimaliki.gitbook.io/gofiber/basic-backend-dengan-gofiber-part-2/mengamankan-header-http-dengan-helmet-di-gofiber.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
