Skip to content

Commit

Permalink
Exclude incoming containers from sync as well
Browse files Browse the repository at this point in the history
  • Loading branch information
stoically committed Jun 24, 2022
1 parent ec414a7 commit 0a7e2d6
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 14 deletions.
46 changes: 39 additions & 7 deletions src/js/background/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,17 +128,12 @@ const sync = {
await sync.checkForListenersMaybeAdd();

async function updateSyncIdentities() {
const { syncExcludeRegExp } = await browser.storage.local.get("syncExcludeRegExp");
let excludeRegExp;
if (syncExcludeRegExp) {
excludeRegExp = new RegExp(syncExcludeRegExp, "i");
}
const excludeRegExp = await getExcludeRegExp();
const identities = await browser.contextualIdentities.query({});

for (const identity of identities) {
// skip excluded identities
if (excludeRegExp && identity.name.match(excludeRegExp)) {
if (SYNC_DEBUG) console.log("Skipping sync backup because regexp matched");
if (excludeRegExpMatchesIdentity(excludeRegExp, identity)) {
continue;
}

Expand Down Expand Up @@ -338,6 +333,7 @@ async function restore() {
*/
async function reconcileIdentities(){
if (SYNC_DEBUG) console.log("reconcileIdentities");
const excludeRegExp = await getExcludeRegExp();

// first delete any from the deleted list
const deletedIdentityList =
Expand All @@ -347,6 +343,14 @@ async function reconcileIdentities(){
const deletedCookieStoreId =
await identityState.lookupCookieStoreId(deletedUUID);
if (deletedCookieStoreId){
if (excludeRegExp) {
const deletedIdentity = await identityState.get(deletedCookieStoreId);
// skip excluded identities
if (excludeRegExpMatchesIdentity(excludeRegExp, deletedIdentity)) {
continue;
}
}

try{
await browser.contextualIdentities.remove(deletedCookieStoreId);
} catch (error) {
Expand All @@ -361,6 +365,11 @@ async function reconcileIdentities(){
await sync.storageArea.getIdentities();
// find any local dupes created on sync storage and delete from sync storage
for (const localIdentity of localIdentities) {
// skip excluded identities
if (excludeRegExpMatchesIdentity(excludeRegExp, localIdentity)) {
continue;
}

const syncIdentitiesOfName = syncIdentitiesRemoveDupes
.filter(identity => identity.name === localIdentity.name);
if (syncIdentitiesOfName.length > 1) {
Expand All @@ -374,6 +383,11 @@ async function reconcileIdentities(){
await sync.storageArea.getIdentities();
// now compare all containers for matching names.
for (const syncIdentity of syncIdentities) {
// skip excluded identities
if (excludeRegExpMatchesIdentity(excludeRegExp, syncIdentity)) {
continue;
}

if (syncIdentity.macAddonUUID){
const localMatch = localIdentities.find(
localIdentity => localIdentity.name === syncIdentity.name
Expand Down Expand Up @@ -589,3 +603,21 @@ async function setAssignmentWithUUID(assignedSite, urlKey) {
}
throw new Error (`No cookieStoreId found for: ${uuid}, ${urlKey}`);
}

async function getExcludeRegExp() {
const { syncExcludeRegExp } = await browser.storage.local.get("syncExcludeRegExp");
if (syncExcludeRegExp) {
return new RegExp(syncExcludeRegExp, "i");
} else {
return false;
}
}

function excludeRegExpMatchesIdentity(excludeRegExp, identity) {
if (excludeRegExp && identity.name.match(excludeRegExp)) {
if (SYNC_DEBUG) console.log(`Exclude regexp matches identity '${identity.name}'`);
return true;
} else {
return false;
}
}
31 changes: 25 additions & 6 deletions test/features/sync.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,20 +197,39 @@ describe("Sync", function() {

// We excluded nothing, so all identities should be part of the sync storage.
const identities = await this.syncHelper.getIdentitiesFromSyncStorage();
identities.length.should.equal(TEST_CONTAINERS.length);
TEST_CONTAINERS.length.should.equal(identities.length);
});

it("excludeTest", async function() {
it("excludeOutgoingTest", async function() {
await this.syncHelper.stopSyncListeners();
await this.syncHelper.setState({}, LOCAL_DATA, TEST_CONTAINERS, []);
const lastTestContainerName = TEST_CONTAINERS.at(-1).name;
await this.syncHelper.setExcludeRegexp(`^${lastTestContainerName}$`);

// Our outgoing container is the last test container.
const outgoingContainerName = TEST_CONTAINERS.at(-1).name;
await this.syncHelper.setExcludeRegexp(`^${outgoingContainerName}$`);

await this.webExt.background.window.sync.runSync();

// We excluded the last test container, so its identity should not be part of the sync storage.
// We excluded an outgoing container, so its identity should not be part of the sync storage.
const identities = await this.syncHelper.getIdentitiesFromSyncStorage();
this.syncHelper.lookupIdentityBy(identities, {name: lastTestContainerName}).should.be.false;
this.syncHelper.lookupIdentityBy(identities, {name: outgoingContainerName}).should.be.false;
});

it("excludeIncomingTest", async function() {
await this.syncHelper.stopSyncListeners();
await this.syncHelper.setState(SYNC_DATA, LOCAL_DATA, TEST_CONTAINERS, []);

// Our incoming container is the first that's in the sync storage but not in the local storage
const incomingContainerName = Object.values(SYNC_DATA).find((sync_container) =>
!TEST_CONTAINERS.find((local_container) => sync_container.name === local_container.name)
).name;
await this.syncHelper.setExcludeRegexp(`^${incomingContainerName}$`);

await this.webExt.background.window.sync.runSync();

// We excluded an incoming container, so its identity should not be part of the local storage.
const identities = await this.webExt.browser.contextualIdentities.query({});
this.syncHelper.lookupIdentityBy(identities, {name: incomingContainerName}).should.be.false;
});
});

Expand Down

0 comments on commit 0a7e2d6

Please sign in to comment.