aboutsummaryrefslogtreecommitdiff
path: root/background/cookie_filter.js
blob: fea2d23b8386118405c75c92f1963f118028ebd3 (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/**
 * part of Hachette
 * Filtering request headers to remove hachette cookies that might have slipped
 * through.
 *
 * Copyright (C) 2021 Wojtek Kosior
 * Redistribution terms are gathered in the `copyright' file.
 */

/*
 * IMPORTS_START
 * IMPORT extract_signed
 * IMPORTS_END
 */

function is_valid_hachette_cookie(cookie)
{
    const match = /^hachette-(\w*)=(.*)$/.exec(cookie);
    if (!match)
	return false;

    return !extract_signed(match.slice(1, 3)).fail;
}

function remove_hachette_cookies(header)
{
    if (header.name !== "Cookie")
	return header;

    const cookies = header.value.split("; ");
    const value = cookies.filter(c => !is_valid_hachette_cookie(c)).join("; ");

    return value ? {name: "Cookie", value} : null;
}

function filter_cookie_headers(headers)
{
    return headers.map(remove_hachette_cookies).filter(h => h);
}

/*
 * EXPORTS_START
 * EXPORT filter_cookie_headers
 * EXPORTS_END
 */