Files
HackBrowserData/output/cookie_editor.go
T
freddy77andGitHub 18d841f68f feat: Add support for sameSite field (#628)
Recent browsers (and HTTP) added "sameSite" setting to limit cross site attacks.
Add support for exporting this property from Firefox and Chrome and to export to csv and cookie-editor format.
Also extract new "session" and "hostOnly" properties for cookie-editor format.
Tested on Firefox, Chrome and Chromium.

Signed-off-by: Frediano Ziglio <freddy77@gmail.com>
2026-08-07 10:14:40 +08:00

78 lines
2.1 KiB
Go

package output
import (
"encoding/json"
"io"
"strings"
"github.com/moond4rk/hackbrowserdata/types"
)
// cookieEditorFormatter outputs cookies in the CookieEditor browser extension
// format. Non-cookie categories fall back to standard JSON output.
type cookieEditorFormatter struct {
fallback *jsonFormatter
}
func (f *cookieEditorFormatter) ext() string { return "json" }
var sameSiteNone = "no_restriction"
func (f *cookieEditorFormatter) format(w io.Writer, rows []row) error {
if len(rows) == 0 {
return nil
}
// aggregate() guarantees all rows in a batch share the same type;
// check the first row to decide the format.
if _, ok := rows[0].entry.(types.CookieEntry); !ok {
return f.fallback.format(w, rows)
}
entries := make([]cookieEditorEntry, 0, len(rows))
for _, r := range rows {
c, _ := r.entry.(types.CookieEntry)
var expDate float64
if !c.ExpireAt.IsZero() {
expDate = float64(c.ExpireAt.Unix())
}
sameSite := &c.SameSite
switch c.SameSite {
case "none":
sameSite = &sameSiteNone
case "", "unspecified":
sameSite = nil
}
entries = append(entries, cookieEditorEntry{
Domain: c.Host,
ExpirationDate: expDate,
HTTPOnly: c.IsHTTPOnly,
Name: c.Name,
Path: c.Path,
Secure: c.IsSecure,
Value: c.Value,
SameSite: sameSite,
Session: expDate == 0.,
HostOnly: !strings.HasPrefix(c.Host, "."),
})
}
enc := json.NewEncoder(w)
enc.SetIndent("", " ")
enc.SetEscapeHTML(false)
return enc.Encode(entries)
}
// cookieEditorEntry matches the CookieEditor browser extension's import format.
type cookieEditorEntry struct {
Domain string `json:"domain"`
ExpirationDate float64 `json:"expirationDate,omitempty"`
HTTPOnly bool `json:"httpOnly"`
Name string `json:"name"`
Path string `json:"path"`
Secure bool `json:"secure"`
Value string `json:"value"`
SameSite *string `json:"sameSite"`
Session bool `json:"session"`
HostOnly bool `json:"hostOnly"`
}