Skip to content
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
3 changes: 2 additions & 1 deletion AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ user's imported beacons and location history.

- Add a `Migration` and register it in `addMigrations(...)`
- **Never** use `fallbackToDestructiveMigration()` — recovery means redoing the macOS
export, and `allowBackup` is false so there is no backup
export, and `allowBackup` is false so there is no backup (and `data_extraction_rules.xml`
keeps the database off device-to-device transfer too, so a new phone has no copy either)
- Add a migration test, including the direct path from the oldest version (users skip
releases)
- Schemas are exported to `app/schemas/` and committed; include the new JSON in your change
Expand Down
28 changes: 17 additions & 11 deletions app/src/main/res/xml/backup_rules.xml
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
<?xml version="1.0" encoding="utf-8"?><!--
Sample backup rules file; uncomment and customize as necessary.
See https://developer.android.com/guide/topics/data/autobackup
for details.
Note: This file is ignored for devices older that API 31
See https://developer.android.com/about/versions/12/backup-restore
<?xml version="1.0" encoding="utf-8"?>
<!--
Used on Android 11 (API 30) and older, where android:allowBackup="false" already turns off
both backup and device-to-device transfer. Like the cloud-backup section of
data_extraction_rules.xml, it is filled in so that turning allowBackup on later does not
start copying the tags' keys, their history or the Keystore-encrypted session anywhere.
data_extraction_rules.xml has the reasons. KeepingAppDataOnThisPhoneTest pins both files.
-->
<full-backup-content>
<!--
<include domain="sharedpref" path="."/>
<exclude domain="sharedpref" path="device.xml"/>
-->
</full-backup-content>
<exclude domain="root" path="." />
<exclude domain="file" path="." />
<exclude domain="database" path="." />
<exclude domain="sharedpref" path="." />
<exclude domain="external" path="." />
<exclude domain="device_root" path="." />
<exclude domain="device_file" path="." />
<exclude domain="device_database" path="." />
<exclude domain="device_sharedpref" path="." />
</full-backup-content>
62 changes: 49 additions & 13 deletions app/src/main/res/xml/data_extraction_rules.xml
Original file line number Diff line number Diff line change
@@ -1,19 +1,55 @@
<?xml version="1.0" encoding="utf-8"?><!--
Sample data extraction rules file; uncomment and customize as necessary.
See https://developer.android.com/about/versions/12/backup-restore#xml-changes
for details.
<?xml version="1.0" encoding="utf-8"?>
<!--
Nothing this app stores leaves the phone, by backup or by device-to-device transfer.
Used on Android 12 (API 31) and newer; backup_rules.xml says the same for older versions.

android:allowBackup="false" turns off cloud backup, but on Android 12+ it does NOT turn off
device-to-device transfer (the phone-to-phone copy when setting up a new phone). Without a
<device-transfer> section everything was eligible. So both sections exclude every domain:

- The database holds every imported tag's private keys and its whole location history,
unencrypted.
- The Apple session, the iCloud link and the extra sources' passwords are encrypted with
Android Keystore keys, and Keystore keys never leave the phone. On a new phone those would
arrive as data nothing can decrypt.
- The Anisette identity (files/anisette, the anisette-identity preferences) is one machine
for the life of an install (AGENTS.md rules 11 and 21). A copy would put the same machine
on two phones.
- Moving some of it and not the rest (settings and "onboarding done", say, without tags or
a session) is a state no install is ever in otherwise.

So a new phone starts as a fresh install: sign in again and import the export again. Export
Tags and Export History move tags and history on purpose. Settings are not carried over
either; they are few and quick to set again, and splitting them out would need this file to
track every file the app writes.

The cloud-backup section is never used while allowBackup is false. It is filled in anyway,
so that flipping allowBackup on later does not quietly start uploading the above.

The cache, code_cache and no_backup directories are never backed up or transferred, so they
have no domain here. KeepingAppDataOnThisPhoneTest pins all of this.
-->
<data-extraction-rules>
<cloud-backup>
<!-- TODO: Use <include> and <exclude> to control what is backed up.
<include .../>
<exclude .../>
-->
<exclude domain="root" path="." />
<exclude domain="file" path="." />
<exclude domain="database" path="." />
<exclude domain="sharedpref" path="." />
<exclude domain="external" path="." />
<exclude domain="device_root" path="." />
<exclude domain="device_file" path="." />
<exclude domain="device_database" path="." />
<exclude domain="device_sharedpref" path="." />
</cloud-backup>
<!--
<device-transfer>
<include .../>
<exclude .../>
<exclude domain="root" path="." />
<exclude domain="file" path="." />
<exclude domain="database" path="." />
<exclude domain="sharedpref" path="." />
<exclude domain="external" path="." />
<exclude domain="device_root" path="." />
<exclude domain="device_file" path="." />
<exclude domain="device_database" path="." />
<exclude domain="device_sharedpref" path="." />
</device-transfer>
-->
</data-extraction-rules>
</data-extraction-rules>
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package dev.wander.android.opentagviewer;

import static org.junit.Assert.assertEquals;

import org.junit.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import javax.xml.parsers.DocumentBuilderFactory;

/**
* Nothing the app stores is backed up, or copied to a new phone by device-to-device transfer.
*
* <p>{@code allowBackup="false"} does not cover device-to-device transfer on Android 12 and
* newer; only a {@code <device-transfer>} section in the data extraction rules does. That file
* was the Android Studio template, so the database with every tag's private keys and history was
* eligible. Nothing fails when a section or a domain goes missing from these files, so this pins
* them. {@code data_extraction_rules.xml} says why everything is excluded, not just the database.
*/
public class KeepingAppDataOnThisPhoneTest {

// Unit tests run from the module directory
private static final String MAIN = "src/main/";

/** Every domain the backup rules accept. The cache directories are never copied anyway. */
private static final Set<String> EVERY_DOMAIN = new TreeSet<>(List.of(
"root", "file", "database", "sharedpref", "external",
"device_root", "device_file", "device_database", "device_sharedpref"));

@Test
public void theManifestTurnsBackupOffAndPointsAtBothRuleFiles() throws Exception {
final Element application = (Element) parse(MAIN + "AndroidManifest.xml")
.getElementsByTagName("application").item(0);

assertEquals("false", application.getAttribute("android:allowBackup"));
assertEquals("@xml/data_extraction_rules",
application.getAttribute("android:dataExtractionRules"));
assertEquals("@xml/backup_rules", application.getAttribute("android:fullBackupContent"));
}

@Test
public void deviceToDeviceTransferMovesNothing() throws Exception {
assertExcludesEverything("device-transfer", sectionOf("device-transfer"));
}

@Test
public void cloudBackupWouldMoveNothingEvenIfAllowBackupWereTurnedOn() throws Exception {
assertExcludesEverything("cloud-backup", sectionOf("cloud-backup"));
}

@Test
public void olderAndroidVersionsWouldBackUpNothingEither() throws Exception {
assertExcludesEverything("full-backup-content",
parse(MAIN + "res/xml/backup_rules.xml").getDocumentElement());
}

private static Element sectionOf(final String name) throws Exception {
final NodeList sections = parse(MAIN + "res/xml/data_extraction_rules.xml")
.getElementsByTagName(name);
assertEquals("expected exactly one <" + name + ">", 1, sections.getLength());
return (Element) sections.item(0);
}

/**
* An {@code <include>} anywhere would switch the section to "only what is included", so
* there must be none, and every domain must be excluded as a whole.
*/
private static void assertExcludesEverything(final String what, final Element section) {
final Set<String> excluded = new TreeSet<>();
final NodeList children = section.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
final Node child = children.item(i);
if (child.getNodeType() != Node.ELEMENT_NODE) {
continue;
}
final Element rule = (Element) child;
assertEquals(what + " has a rule that is not an <exclude>: <" + rule.getTagName() + ">",
"exclude", rule.getTagName());
assertEquals(what + " excludes only part of " + rule.getAttribute("domain"),
".", rule.getAttribute("path"));
excluded.add(rule.getAttribute("domain"));
}
assertEquals(what + " does not exclude every domain", EVERY_DOMAIN, excluded);
}

private static Document parse(final String path) throws Exception {
return DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(path);
}
}