Cookies

This document describes how to work with cookies.

Overview 

Fuzio delegates the work with cookies to the Chromium engine. Chromium decides how to download cookies from a web server, extract them from the HTTP headers and store them in the User Data directory (persistent cookies) or in the memory (session cookies).

The CookieStore class allows you to get, modify, and remove cookies. The Cookie class provides information on a particular cookie.

To obtain the CookieStore for a specific Profile please use the Profile.cookieStore() method. The Engine.cookieStore() method returns the CookieStore associated with the default profile.

To access cookie store use the following way:

Java
Kotlin
var cookieStore = profile.cookieStore();
val cookieStore = profile.cookieStore()

Supported protocols 

Fuzio supports cookies that are sent using the following protocols:

  • HTTP
  • HTTPS
  • WS (WebSocket)
  • WSS (Secured WebSocket)

If a cookie is sent using a protocol that is not on the list, e.g. ftp://, it will not be stored in the cookie storage.

Working with cookies 

Fuzio supports the following kinds of cookies:

  • Persistent cookies — these are stored in the Chromium user data directory. If you delete the Chromium user data directory, all the persistent cookies will be removed.
  • Session cookies — these are stored in the application memory. These cookies will be removed automatically when the application is terminated.
  • Secure cookies — these can only be transmitted over an encrypted connection, i.e. HTTPS. This makes the cookie less likely to be exposed to cookie theft via eavesdropping.
  • HttpOnly cookies — these cannot be accessed by the client-side APIs, such as JavaScript. This restriction eliminates the threat of cookie theft via cross-site scripting (XSS). However, the cookie remains vulnerable to cross-site tracing (XST) and cross-site request forgery (XSRF) attacks.

When you modify cookies please use the CookieStore.persist() method to save the changes.

Getting cookies 

To get all cookies, please use the cookies() method:

Java
Kotlin
cookieStore.cookies().forEach(cookie -> {
    var name = cookie.name();
    var value = cookie.value();
    var domain = cookie.domain();
    var path = cookie.path();

    var secure = cookie.isSecure();
    var sameSite = cookie.sameSite();
    var httpOnly = cookie.isHttpOnly();
    var partitionKey = cookie.partitionKey();

    var creationTime = cookie.creationTime();
    var expirationTime = cookie.expirationTime();
});
cookieStore.cookies().forEach {
    val name = it.name()
    val value = it.value()
    val domain = it.domain()
    val path = it.path()

    val secure = it.isSecure
    val sameSite = it.sameSite()
    val httpOnly = it.isHttpOnly
    val partitionKey = it.partitionKey()

    val creationTime = it.creationTime()
    val expirationTime = it.expirationTime()
}

To get all cookies by a URL, please use the cookies() method that accepts a string:

Java
Kotlin
cookieStore.cookies("https://html5test.jiku.co")
        .forEach(cookie -> System.out.println("cookie = " + cookie));
cookieStore.cookies("https://html5test.jiku.co")
    .forEach { println("cookie = $it") }

Creating cookies 

Persistent 

To create a persistent cookie with an expiration time use the following code:

Java
Kotlin
cookieStore.set(Cookie.newBuilder("https://www.baidu.com/")
        .creationTime(creationTime)
        .expirationTime(expirationTime)
        .name("name")
        .value("value")
        .path("/")
        .build()
);
cookieStore.persist();
val cookie = Cookie(
    domain = "https://www.baidu.com/",
    creationTime = creationTime,
    expirationTime = expirationTime,
    name = "name",
    value = "value",
    path = "/"
)
cookieStore.set(cookie)
cookieStore.persist()

Session 

To create a session cookie use the following code:

Java
Kotlin
cookieStore.set(Cookie.newBuilder("https://www.baidu.com/")
        .name("name")
        .value("value")
        .path("/")
        .build()
);
cookieStore.persist();
val cookie = Cookie(
    domain = "https://www.baidu.com/",
    name = "name",
    value = "value",
    path = "/"
)
cookieStore.set(cookie)
cookieStore.persist()

Deleting cookies 

To delete all cookies use the deleteAll() method:

Java
Kotlin
var numberOfDeletedCookies = cookieStore.deleteAll();
cookieStore.persist();
val numberOfDeletedCookies = cookieStore.deleteAll()
cookieStore.persist()

To delete one cookie, please use the delete(Cookie). The following code deletes all cookies one by one:

Java
Kotlin
cookieStore.cookies().forEach(cookieStore::delete);
cookieStore.persist();
cookieStore.cookies().forEach(cookieStore::delete)
cookieStore.persist()

Suppressing cookies 

You can control all incoming and outgoing cookies using the CanSetCookieCallback and CanGetCookiesCallback callbacks of the Network.

To suppress the incoming cookies use the following code:

Java
Kotlin
network.set(CanSetCookieCallback.class, params ->
        CanSetCookieCallback.Response.cannot()
);
network.register(CanSetCookieCallback {
    CanSetCookieCallback.Response.cannot()
})

To suppress the outgoing cookies use the following code:

Java
Kotlin
network.set(CanGetCookiesCallback.class, params ->
        CanGetCookiesCallback.Response.cannot()
);
network.register(CanGetCookiesCallback {
    CanGetCookiesCallback.Response.cannot()
})

Encryption 

Fuzio supports the cookie encryption by default. It uses the Chromium cookies encryption routines, so the cookies are stored exactly as in Chromium.

Linux 

On Linux Fuzio uses GNOME Keyring or KWallet to encrypt cookies. The library automatically chooses which store to use. You can manually specify which store to use via an appropriate option when constructing the Engine. For example:

Java
Kotlin
var engine = Engine.newInstance(
        EngineOptions.newBuilder(renderingMode)
                .passwordStore(PasswordStore.GNOME_KEYRING)
                .build()
);
val engine = Engine(renderingMode) {
    passwordStore = PasswordStore.GNOME_KEYRING
}

Windows 

On Windows Fuzio uses only DPAPI to encrypt cookies. There are no alternatives at the moment.

macOS 

On macOS Fuzio uses the private key stored with the Keychain Application to encrypt cookies with AES encryption.

Customer Support

QR code to follow us on WeChat

Technical Support

QR code to follow us on WeChat