Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Per-container cookie locking #123

Open
wants to merge 1 commit into
base: fpi
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ vAPI.delete_cookies = function(promise) {
for (let cookie of cookies) {
// DO NOT delete protected cookies
if (cookie.domain in protected_cookies
&& protected_cookies[cookie.domain].indexOf(cookie.name) !== -1)
&& protected_cookies[cookie.domain].find(each_cookie => each_cookie.name === cookie.name && each_cookie.container === cookie.storeId))
continue;

// Remove current cookie
Expand Down Expand Up @@ -630,21 +630,22 @@ vAPI.set_cookie_protection = function(cookies, protect_flag) {

// Check name
let name = cookie.name;
if (protect_flag && items.protected_cookies[domain].indexOf(name) === -1) {
let container = cookie.storeId;
if (protect_flag && !items.protected_cookies[domain].find(each_cookie => each_cookie.name === name && each_cookie.container === container)) {
// This cookie will be protected
console.log({'protect: add': name});
items.protected_cookies[domain].push(name);
items.protected_cookies[domain].push({name, container});
continue;
}

if ((!protect_flag) && (items.protected_cookies[domain].indexOf(name) !== -1)) {
if ((!protect_flag) && (items.protected_cookies[domain].find(each_cookie => each_cookie.name === name && each_cookie.container === container))) {
// This cookie will not be protected anymore
console.log({'protect: rm': name});
// Remove the current cookie name from list if it is already present
items.protected_cookies[domain] = items.protected_cookies[domain].filter(present_name => {
// To delete the cookie name, we have to return false if name == present_name
// So, return true if name != present_name
return name != present_name;
items.protected_cookies[domain] = items.protected_cookies[domain].filter(present_item => {
// To delete the cookie name, we have to return false if item == present_item
// So, return true if item != present_item
return !(name == present_item.name && container == present_item.container);
});
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/background-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ browser.cookies.onChanged.addListener(function(changeInfo) {

// If the deleted cookie is not in protected_cookies array: do nothing
if (protected_cookies[changeInfo.cookie.domain] === undefined ||
protected_cookies[changeInfo.cookie.domain].indexOf(changeInfo.cookie.name) === -1)
!protected_cookies[changeInfo.cookie.domain].find(each_cookie => each_cookie.name === changeInfo.cookie.name && each_cookie.container === changeInfo.cookie.storeId))
return;

// Rebuild the cookie given by the event
Expand Down
36 changes: 25 additions & 11 deletions src/cookies.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,34 +210,47 @@ $("#delete_button").click(function() {

$("#protect_button").click(function() {
// Update the protect status of the current cookie

let lock_badge = "lock-badge glyphicon glyphicon-lock";

// Do nothing if no cookie is selected
let domain = $("#domain").val();
let name = $('#name').val();
let container = $("#store").val();

// Do nothing if no cookie is selected
if (name == '')
return;

// Check domain
if (!(domain in protected_cookies))
protected_cookies[domain] = [];
// Check name
if (protected_cookies[domain].indexOf(name) === -1) {
if (!protected_cookies[domain].find(each_cookie => each_cookie.name === name && each_cookie.container === container)) {
// This cookie will be protected
protected_cookies[domain].push(name);

protected_cookies[domain].push({name, container});

if($('#cookie-list').find('li.active')[0].lastChild.className !== lock_badge) {
let badge_span = document.createElement("span");
badge_span.className = lock_badge;
$('#cookie-list').find('li.active')[0].appendChild(badge_span);
}

set_protect_lock_icon(true);
} else {
// This cookie will not be protected anymore
protected_cookies[domain] = protected_cookies[domain].filter(item => ![name,].includes(item));

protected_cookies[domain] = protected_cookies[domain].filter(item => !(item.name === name && item.container === container));

if($('#cookie-list').find('li.active')[0].lastChild.className === lock_badge)
$('#cookie-list').find('li.active')[0].lastChild.remove();
set_protect_lock_icon(false);
}
//console.log(protected_cookies);
console.log(protected_cookies);
// Set new protected_cookies on storage area
browser.storage.local.set({"protected_cookies": protected_cookies})
.then((ret) => {
// Simulate click on domain
$('#domain-list').find('li.active').click();
// $('#domain-list').find('li.active').click();
// $('#cookie-list').find('li.active').click();
}, onError)
.catch(err => console.error(err));
});
Expand Down Expand Up @@ -1365,7 +1378,7 @@ function showCookiesList(event, refresh_domain_badges) {

// Display a lock badge if cookie is protected
if (cookie.domain in protected_cookies
&& protected_cookies[cookie.domain].indexOf(cookie.name) !== -1) {
&& protected_cookies[cookie.domain].find(each_cookie => each_cookie.name === cookie.name && each_cookie.container === cookie.storeId)) {
let lock_badge = document.createElement("span");
lock_badge.className = "lock-badge glyphicon glyphicon-lock";
li.appendChild(lock_badge);
Expand Down Expand Up @@ -1479,7 +1492,7 @@ function display_cookie_details(event) {
// If the cookie is not in protected_cookies array: display lock icon
// otherwise, display unlock icon
if (protected_cookies[cookie.domain] === undefined ||
protected_cookies[cookie.domain].indexOf(cookie.name) === -1) {
!protected_cookies[cookie.domain].find(each_cookie => each_cookie.name === cookie.name && each_cookie.container === cookie.storeId)) {
// is not protected
set_protect_lock_icon(false);
} else {
Expand Down Expand Up @@ -1517,8 +1530,9 @@ function delete_current_cookie() {
// DO NOT delete protected cookie
let cookie_domain = $('#domain').val();
let cookie_name = $('#name').val();
let cookie_container = $('#store').val();
if (cookie_domain in protected_cookies
&& protected_cookies[cookie_domain].indexOf(cookie_name) !== -1) {
&& protected_cookies[cookie_domain].find(each_cookie => each_cookie.name === cookie_name && each_cookie.container === cookie_container)) {
return;
}

Expand Down