readytoship 0.4.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- readytoship/__init__.py +3 -0
- readytoship/__main__.py +5 -0
- readytoship/advice/__init__.py +87 -0
- readytoship/android/__init__.py +14 -0
- readytoship/android/checks/__init__.py +27 -0
- readytoship/android/checks/edge_to_edge.py +149 -0
- readytoship/android/checks/foreground_services.py +247 -0
- readytoship/android/checks/large_screen.py +144 -0
- readytoship/android/checks/page_size.py +180 -0
- readytoship/android/checks/play_permissions.py +104 -0
- readytoship/android/checks/target_api.py +120 -0
- readytoship/android/formats/__init__.py +2 -0
- readytoship/android/formats/_guard.py +41 -0
- readytoship/android/formats/arsc.py +140 -0
- readytoship/android/formats/axml.py +214 -0
- readytoship/android/formats/bundle.py +195 -0
- readytoship/android/formats/elf.py +84 -0
- readytoship/android/formats/protobuf.py +96 -0
- readytoship/android/load.py +233 -0
- readytoship/android/manifest.py +215 -0
- readytoship/android/model.py +172 -0
- readytoship/cli.py +240 -0
- readytoship/core/__init__.py +36 -0
- readytoship/core/artifact.py +48 -0
- readytoship/core/check.py +58 -0
- readytoship/core/engine.py +112 -0
- readytoship/core/finding.py +71 -0
- readytoship/core/frozen.py +22 -0
- readytoship/core/platform.py +32 -0
- readytoship/core/rulebook.py +109 -0
- readytoship/ios/__init__.py +14 -0
- readytoship/ios/checks/__init__.py +33 -0
- readytoship/ios/checks/ipad_multitasking.py +102 -0
- readytoship/ios/checks/liquid_glass.py +74 -0
- readytoship/ios/checks/privacy_manifest.py +141 -0
- readytoship/ios/checks/privacy_reasons.py +213 -0
- readytoship/ios/checks/sdk_manifests.py +85 -0
- readytoship/ios/checks/upload_settings.py +152 -0
- readytoship/ios/checks/usage_descriptions.py +278 -0
- readytoship/ios/checks/xcode_sdk.py +145 -0
- readytoship/ios/formats/__init__.py +1 -0
- readytoship/ios/formats/macho.py +118 -0
- readytoship/ios/load.py +243 -0
- readytoship/ios/model.py +168 -0
- readytoship/ios/privacy.py +113 -0
- readytoship/knowledge/__init__.py +0 -0
- readytoship/knowledge/fixes.toml +77 -0
- readytoship/knowledge/frameworks.toml +25 -0
- readytoship/knowledge/rules.toml +275 -0
- readytoship/py.typed +0 -0
- readytoship/report/__init__.py +2 -0
- readytoship/report/baseline.py +69 -0
- readytoship/report/data.py +57 -0
- readytoship/report/policy.py +17 -0
- readytoship/report/sarif.py +97 -0
- readytoship/report/text.py +125 -0
- readytoship-0.4.0.dist-info/METADATA +69 -0
- readytoship-0.4.0.dist-info/RECORD +63 -0
- readytoship-0.4.0.dist-info/WHEEL +5 -0
- readytoship-0.4.0.dist-info/entry_points.txt +2 -0
- readytoship-0.4.0.dist-info/licenses/LICENSE +202 -0
- readytoship-0.4.0.dist-info/licenses/NOTICE +20 -0
- readytoship-0.4.0.dist-info/top_level.txt +1 -0
readytoship/__init__.py
ADDED
readytoship/__main__.py
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
"""Advice: recognises the framework an app was built with and attaches known, sourced fixes.
|
|
2
|
+
|
|
3
|
+
Works on core.Facts and core.Finding only, so it serves every platform without importing any.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
import tomllib
|
|
9
|
+
from collections.abc import Callable
|
|
10
|
+
from dataclasses import dataclass
|
|
11
|
+
from fnmatch import fnmatchcase
|
|
12
|
+
from importlib import resources
|
|
13
|
+
from typing import Any
|
|
14
|
+
|
|
15
|
+
from readytoship.core import Facts, Finding, Platform
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def _knowledge(name: str) -> dict[str, Any]:
|
|
19
|
+
text = resources.files("readytoship.knowledge").joinpath(name).read_text(encoding="utf-8")
|
|
20
|
+
return tomllib.loads(text)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def frameworks(platform: Platform, facts: Facts, data: dict[str, Any] | None = None) -> list[str]:
|
|
24
|
+
"""Frameworks the app is built with, most specific first."""
|
|
25
|
+
data = data or _knowledge("frameworks.toml")
|
|
26
|
+
found: set[str] = set()
|
|
27
|
+
if platform is Platform.ANDROID:
|
|
28
|
+
markers = data.get("android", {}).get("native_libraries", {})
|
|
29
|
+
found |= {markers[n] for n in facts.native_libraries if n in markers}
|
|
30
|
+
else:
|
|
31
|
+
markers = data.get("ios", {}).get("frameworks", {})
|
|
32
|
+
found |= {markers[n] for n in facts.frameworks if n in markers}
|
|
33
|
+
order: list[str] = data.get("order", [])
|
|
34
|
+
return sorted(found, key=lambda f: order.index(f) if f in order else len(order))
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
@dataclass(frozen=True)
|
|
38
|
+
class Fix:
|
|
39
|
+
check: str
|
|
40
|
+
owner: str
|
|
41
|
+
text: str
|
|
42
|
+
source: str
|
|
43
|
+
subject_glob: str = "*"
|
|
44
|
+
framework: str = ""
|
|
45
|
+
evidence: str = ""
|
|
46
|
+
inferred: bool = False
|
|
47
|
+
|
|
48
|
+
def matches(self, finding: Finding, built_with: frozenset[str]) -> bool:
|
|
49
|
+
return (
|
|
50
|
+
finding.check == self.check
|
|
51
|
+
and _glob(finding.subject, self.subject_glob)
|
|
52
|
+
and (not self.framework or self.framework in built_with)
|
|
53
|
+
and (not self.evidence or any(self.evidence in e.detail for e in finding.evidence))
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def load_fixes(data: dict[str, Any] | None = None) -> tuple[Fix, ...]:
|
|
58
|
+
data = data or _knowledge("fixes.toml")
|
|
59
|
+
return tuple(Fix(**entry) for entry in data.get("fix", []))
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def advisor(
|
|
63
|
+
built_with: list[str], fixes: tuple[Fix, ...] | None = None
|
|
64
|
+
) -> Callable[[Finding], Finding]:
|
|
65
|
+
"""An enricher that names the owner and the specific fix of findings it recognises."""
|
|
66
|
+
known = load_fixes() if fixes is None else fixes
|
|
67
|
+
frameworks_set = frozenset(built_with)
|
|
68
|
+
|
|
69
|
+
def enrich(finding: Finding) -> Finding:
|
|
70
|
+
if finding.level == 0:
|
|
71
|
+
return finding
|
|
72
|
+
for fix in known:
|
|
73
|
+
if fix.matches(finding, frameworks_set):
|
|
74
|
+
note = " (inferred from the source)" if fix.inferred else ""
|
|
75
|
+
return finding.with_(owner=fix.owner, fix=f"{fix.text}{note} See {fix.source}")
|
|
76
|
+
return finding if finding.owner else finding.with_(owner="app")
|
|
77
|
+
|
|
78
|
+
return enrich
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def _glob(subject: str, pattern: str) -> bool:
|
|
82
|
+
"""fnmatch with {a,b} alternatives."""
|
|
83
|
+
if "{" in pattern and "}" in pattern:
|
|
84
|
+
head, rest = pattern.split("{", 1)
|
|
85
|
+
options, tail = rest.split("}", 1)
|
|
86
|
+
return any(_glob(subject, head + option + tail) for option in options.split(","))
|
|
87
|
+
return fnmatchcase(subject, pattern)
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"""Android: reads APKs and App Bundles and checks them against Google Play and Android rules."""
|
|
2
|
+
|
|
3
|
+
from readytoship.android.checks import CHECKS
|
|
4
|
+
from readytoship.android.load import load
|
|
5
|
+
from readytoship.core import Platform, PlatformSupport
|
|
6
|
+
|
|
7
|
+
SUPPORT = PlatformSupport(
|
|
8
|
+
platform=Platform.ANDROID,
|
|
9
|
+
suffixes=frozenset({".apk", ".aab"}),
|
|
10
|
+
load=load,
|
|
11
|
+
checks=CHECKS,
|
|
12
|
+
)
|
|
13
|
+
|
|
14
|
+
__all__ = ["SUPPORT"]
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"""Android checks: one module per store rule, each implementing core.Check[AndroidApp]."""
|
|
2
|
+
|
|
3
|
+
from readytoship.android.checks.edge_to_edge import EdgeToEdge
|
|
4
|
+
from readytoship.android.checks.foreground_services import ForegroundServices
|
|
5
|
+
from readytoship.android.checks.large_screen import LargeScreen
|
|
6
|
+
from readytoship.android.checks.page_size import PageSize
|
|
7
|
+
from readytoship.android.checks.play_permissions import PlayPermissions
|
|
8
|
+
from readytoship.android.checks.target_api import TargetApi
|
|
9
|
+
|
|
10
|
+
CHECKS = (
|
|
11
|
+
TargetApi(),
|
|
12
|
+
PageSize(),
|
|
13
|
+
LargeScreen(),
|
|
14
|
+
EdgeToEdge(),
|
|
15
|
+
ForegroundServices(),
|
|
16
|
+
PlayPermissions(),
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
__all__ = [
|
|
20
|
+
"CHECKS",
|
|
21
|
+
"EdgeToEdge",
|
|
22
|
+
"ForegroundServices",
|
|
23
|
+
"LargeScreen",
|
|
24
|
+
"PageSize",
|
|
25
|
+
"PlayPermissions",
|
|
26
|
+
"TargetApi",
|
|
27
|
+
]
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
"""Apps that target API 35+ are drawn edge to edge; the theme opt-out
|
|
2
|
+
(windowOptOutEdgeToEdgeEnforcement) stops working when they target API 36."""
|
|
3
|
+
|
|
4
|
+
from __future__ import annotations
|
|
5
|
+
|
|
6
|
+
from collections.abc import Mapping
|
|
7
|
+
|
|
8
|
+
from readytoship.android.model import AndroidApp
|
|
9
|
+
from readytoship.core import CheckDoc, CheckInfo, Context, Evidence, Finding, Level, Platform
|
|
10
|
+
|
|
11
|
+
FIX = (
|
|
12
|
+
"Handle window insets (status bar, navigation bar, display cutout) on every screen, "
|
|
13
|
+
"then remove windowOptOutEdgeToEdgeEnforcement from the theme."
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
DOC = CheckDoc(
|
|
18
|
+
reads=("Manifest", "Resources"),
|
|
19
|
+
what="Reads the app's resource table and lists the themes, including those that inherit it "
|
|
20
|
+
"from a parent, that set `windowOptOutEdgeToEdgeEnforcement`, and which activities use them.",
|
|
21
|
+
why="Apps that target API {android.edge_to_edge.enforced_from_target} or higher are drawn "
|
|
22
|
+
"edge to edge, under the status and navigation bars. The theme opt-out is ignored once an "
|
|
23
|
+
"app targets API {android.edge_to_edge.opt_out_removed_at_target} and runs on Android 16 "
|
|
24
|
+
"or later, the level Google Play requires for new phone and tablet apps and updates from "
|
|
25
|
+
"{android.target_api.phone.deadline}.",
|
|
26
|
+
fix="Handle window insets on every screen (status bar, navigation bar, display cutout), then "
|
|
27
|
+
"remove the opt-out from the theme.",
|
|
28
|
+
example="""[BREAKS] edge-to-edge: Screens handle being drawn edge to edge
|
|
29
|
+
- (breaks) Theme AppTheme opts out of edge-to-edge (used by MainActivity).
|
|
30
|
+
Works until the app targets API 36.""",
|
|
31
|
+
limitations=(
|
|
32
|
+
"Themes set in code are not seen. An opt-out theme that no activity uses directly is "
|
|
33
|
+
"reported as a note.",
|
|
34
|
+
),
|
|
35
|
+
since="0.2.0",
|
|
36
|
+
)
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
class EdgeToEdge:
|
|
40
|
+
info = CheckInfo(
|
|
41
|
+
id="edge-to-edge",
|
|
42
|
+
platform=Platform.ANDROID,
|
|
43
|
+
title="Screens handle being drawn edge to edge",
|
|
44
|
+
rules=("android.edge_to_edge",),
|
|
45
|
+
doc=DOC,
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
def run(self, app: AndroidApp, ctx: Context) -> list[Finding]:
|
|
49
|
+
rule = ctx.rule("android.edge_to_edge")
|
|
50
|
+
enforced: int = rule["enforced_from_target"]
|
|
51
|
+
removed: int = rule["opt_out_removed_at_target"]
|
|
52
|
+
target = app.manifest.target_or_assumed(removed) # unknown: judge as current
|
|
53
|
+
opt_outs = app.edge_to_edge_opt_outs
|
|
54
|
+
|
|
55
|
+
if target < enforced:
|
|
56
|
+
return [
|
|
57
|
+
Finding(
|
|
58
|
+
self.info.id,
|
|
59
|
+
Level.BREAKS,
|
|
60
|
+
subject="app",
|
|
61
|
+
title=f"Targets API {target}; screens are drawn edge to edge once it targets "
|
|
62
|
+
f"API {enforced}.",
|
|
63
|
+
detail="Content will then draw under the status and navigation bars "
|
|
64
|
+
"unless the app handles window "
|
|
65
|
+
"insets.",
|
|
66
|
+
evidence=(
|
|
67
|
+
Evidence("manifest", f"targetSdkVersion={target}", "AndroidManifest.xml"),
|
|
68
|
+
),
|
|
69
|
+
fix=FIX.split(", then")[0] + ".",
|
|
70
|
+
source=rule.source,
|
|
71
|
+
)
|
|
72
|
+
]
|
|
73
|
+
if opt_outs is None:
|
|
74
|
+
return [
|
|
75
|
+
Finding(
|
|
76
|
+
self.info.id,
|
|
77
|
+
Level.NOTE,
|
|
78
|
+
subject="app",
|
|
79
|
+
title="Couldn't read the resource table to look for the edge-to-edge opt-out.",
|
|
80
|
+
source=rule.source,
|
|
81
|
+
)
|
|
82
|
+
]
|
|
83
|
+
used = self._themes_in_use(app, opt_outs)
|
|
84
|
+
if not opt_outs:
|
|
85
|
+
return [
|
|
86
|
+
Finding(
|
|
87
|
+
self.info.id,
|
|
88
|
+
Level.OK,
|
|
89
|
+
subject="app",
|
|
90
|
+
title="No theme opts out of edge-to-edge.",
|
|
91
|
+
source=rule.source,
|
|
92
|
+
)
|
|
93
|
+
]
|
|
94
|
+
findings = []
|
|
95
|
+
for style_id, name in sorted(opt_outs.items(), key=lambda kv: kv[1]):
|
|
96
|
+
where = used.get(style_id)
|
|
97
|
+
if target >= removed:
|
|
98
|
+
level, when = (
|
|
99
|
+
Level.BREAKS,
|
|
100
|
+
(
|
|
101
|
+
f"Ignored on Android 16 and later for apps that target API {removed} or higher."
|
|
102
|
+
),
|
|
103
|
+
)
|
|
104
|
+
else:
|
|
105
|
+
level, when = (
|
|
106
|
+
Level.BREAKS,
|
|
107
|
+
(f"Works until the app targets API {removed}."),
|
|
108
|
+
)
|
|
109
|
+
if where is None:
|
|
110
|
+
level = Level.NOTE
|
|
111
|
+
when += " No activity in the manifest uses this theme directly."
|
|
112
|
+
findings.append(
|
|
113
|
+
Finding(
|
|
114
|
+
self.info.id,
|
|
115
|
+
level,
|
|
116
|
+
subject=_style_subject(style_id, name),
|
|
117
|
+
title=f"Theme {name} opts out of edge-to-edge"
|
|
118
|
+
+ (f" (used by {where})." if where else "."),
|
|
119
|
+
detail=when,
|
|
120
|
+
evidence=(
|
|
121
|
+
Evidence(
|
|
122
|
+
"resources", "windowOptOutEdgeToEdgeEnforcement=true", f"style/{name}"
|
|
123
|
+
),
|
|
124
|
+
),
|
|
125
|
+
fix=FIX,
|
|
126
|
+
source=rule.source,
|
|
127
|
+
)
|
|
128
|
+
)
|
|
129
|
+
return findings
|
|
130
|
+
|
|
131
|
+
@staticmethod
|
|
132
|
+
def _themes_in_use(app: AndroidApp, opt_outs: Mapping[int, str]) -> dict[int, str]:
|
|
133
|
+
used: dict[int, str] = {}
|
|
134
|
+
m = app.manifest
|
|
135
|
+
for activity in m.activities:
|
|
136
|
+
theme = activity.theme or m.app_theme
|
|
137
|
+
if theme is not None and theme in opt_outs:
|
|
138
|
+
used.setdefault(theme, activity.name.rsplit(".", 1)[-1])
|
|
139
|
+
if m.app_theme is not None and m.app_theme in opt_outs:
|
|
140
|
+
used.setdefault(m.app_theme, "the application")
|
|
141
|
+
return used
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
def _style_subject(style_id: int, name: str) -> str:
|
|
145
|
+
"""Style names are stable across builds, ids are not, so name the subject by the style's
|
|
146
|
+
name, unless a resource shrinker obfuscated it."""
|
|
147
|
+
if not name or name == "?" or "obfuscated" in name:
|
|
148
|
+
return f"style/0x{style_id:08x}"
|
|
149
|
+
return f"style/{name}"
|
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
"""Foreground services in apps that target API 34+ need a type and the permissions that go with
|
|
2
|
+
it, or starting them throws."""
|
|
3
|
+
|
|
4
|
+
from __future__ import annotations
|
|
5
|
+
|
|
6
|
+
import re
|
|
7
|
+
from collections.abc import Sequence
|
|
8
|
+
|
|
9
|
+
from readytoship.android.model import AndroidApp, Manifest, Service
|
|
10
|
+
from readytoship.core import (
|
|
11
|
+
CheckDoc,
|
|
12
|
+
CheckInfo,
|
|
13
|
+
Context,
|
|
14
|
+
Evidence,
|
|
15
|
+
Finding,
|
|
16
|
+
Level,
|
|
17
|
+
Platform,
|
|
18
|
+
Rule,
|
|
19
|
+
)
|
|
20
|
+
|
|
21
|
+
FOREGROUND_SERVICE = "android.permission.FOREGROUND_SERVICE"
|
|
22
|
+
JOB_SERVICE = "android.permission.BIND_JOB_SERVICE" # a job, never started in the foreground
|
|
23
|
+
# Types whose prerequisite can be met outside the manifest; a missing permission is a note.
|
|
24
|
+
MET_ELSEWHERE = {"connectedDevice": "a USB permission request", "phoneCall": "the dialer role"}
|
|
25
|
+
|
|
26
|
+
DOC = CheckDoc(
|
|
27
|
+
since="0.3.0",
|
|
28
|
+
reads=("Manifest",),
|
|
29
|
+
what="For each `<service>` with `android:foregroundServiceType`, checks that the app "
|
|
30
|
+
"requests `FOREGROUND_SERVICE`, the `FOREGROUND_SERVICE_<TYPE>` permission of each type, "
|
|
31
|
+
"and the permission each type depends on (for example `CAMERA` for `camera`). Also notes "
|
|
32
|
+
"the background time limit on `dataSync` and `mediaProcessing` services and the Play "
|
|
33
|
+
"Console declaration of each type.",
|
|
34
|
+
why="For apps that target API {android.foreground_services.types_required_from_target} or "
|
|
35
|
+
"higher, starting a foreground service whose type lacks its permission or prerequisite "
|
|
36
|
+
"throws a `SecurityException`, which crashes the app.\n\n"
|
|
37
|
+
"From API {android.foreground_services.timeout_from_target}, `dataSync` and "
|
|
38
|
+
"`mediaProcessing` services may run 6 hours in 24 while the app is in the background; a "
|
|
39
|
+
"service that doesn't stop after `onTimeout()` crashes the app.\n\n"
|
|
40
|
+
"Google Play asks apps to declare each foreground service type they use in Play Console.",
|
|
41
|
+
fix="Add the missing `<uses-permission>` elements, or remove a type the service doesn't "
|
|
42
|
+
"use. Implement `Service.onTimeout()` for `dataSync` and `mediaProcessing` services.",
|
|
43
|
+
example="""[BREAKS] foreground-services: Foreground services have the permissions their types need
|
|
44
|
+
- (breaks) Tracker (type location) is missing FOREGROUND_SERVICE_LOCATION.
|
|
45
|
+
For apps that target API 34 or higher, starting it in the foreground with this type throws
|
|
46
|
+
a SecurityException.
|
|
47
|
+
Fix: Add the FOREGROUND_SERVICE_LOCATION permission to the manifest, or remove the type if
|
|
48
|
+
the service doesn't use it.""",
|
|
49
|
+
limitations=(
|
|
50
|
+
"Services without `foregroundServiceType` aren't checked: the manifest doesn't say "
|
|
51
|
+
"which services are started in the foreground.",
|
|
52
|
+
"Runtime permissions such as `CAMERA` must also be granted by the user; readytoship "
|
|
53
|
+
"checks only that the app requests them.",
|
|
54
|
+
"A missing permission is a note, not a break, when the service comes from a library "
|
|
55
|
+
"(a class outside the app's own namespace), which may never be started in the "
|
|
56
|
+
"foreground, or when the service has another type with everything it needs, since the "
|
|
57
|
+
"app can start it with that type alone. Job services are skipped.",
|
|
58
|
+
),
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def type_permission(service_type: str) -> str:
|
|
63
|
+
"""dataSync -> android.permission.FOREGROUND_SERVICE_DATA_SYNC"""
|
|
64
|
+
snake = re.sub(r"(?<!^)(?=[A-Z])", "_", service_type).upper()
|
|
65
|
+
return f"{FOREGROUND_SERVICE}_{snake}"
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
class ForegroundServices:
|
|
69
|
+
info = CheckInfo(
|
|
70
|
+
id="foreground-services",
|
|
71
|
+
platform=Platform.ANDROID,
|
|
72
|
+
title="Foreground services have the permissions their types need",
|
|
73
|
+
rules=("android.foreground_services",),
|
|
74
|
+
doc=DOC,
|
|
75
|
+
)
|
|
76
|
+
|
|
77
|
+
def run(self, app: AndroidApp, ctx: Context) -> list[Finding]:
|
|
78
|
+
rule = ctx.rule("android.foreground_services")
|
|
79
|
+
m = app.manifest
|
|
80
|
+
applies: int = rule["types_required_from_target"]
|
|
81
|
+
# A target set through a resource reference is judged as if the rules apply.
|
|
82
|
+
target = m.target_or_assumed(max(applies, rule["timeout_from_target"]))
|
|
83
|
+
services = [s for s in m.services if s.permission != JOB_SERVICE]
|
|
84
|
+
typed = [s for s in services if s.foreground_types]
|
|
85
|
+
findings = [self._by_reference(s, rule) for s in services if s.types_by_reference]
|
|
86
|
+
if not typed:
|
|
87
|
+
return findings or [
|
|
88
|
+
self._ok("No service declares a foreground service type.", rule.source)
|
|
89
|
+
]
|
|
90
|
+
if target < applies:
|
|
91
|
+
return findings or [
|
|
92
|
+
self._ok(
|
|
93
|
+
f"Targets API {target}; foreground service type rules apply from API "
|
|
94
|
+
f"{applies}.",
|
|
95
|
+
rule.source,
|
|
96
|
+
)
|
|
97
|
+
]
|
|
98
|
+
|
|
99
|
+
for service in typed:
|
|
100
|
+
findings += self._missing(service, m, rule, target)
|
|
101
|
+
findings += self._advice(service, rule, target)
|
|
102
|
+
# One finding per type, so a baseline doesn't hide a type the app adds later.
|
|
103
|
+
for t in sorted({t for s in typed for t in s.foreground_types or ()}):
|
|
104
|
+
findings.append(
|
|
105
|
+
Finding(
|
|
106
|
+
self.info.id,
|
|
107
|
+
Level.NOTE,
|
|
108
|
+
subject=f"play-declaration:{t}",
|
|
109
|
+
title=f"Google Play asks for a declaration of foreground service type {t}.",
|
|
110
|
+
detail="Apps that target API 34 or higher declare each type they use in "
|
|
111
|
+
"Play Console (App content), with a description, the impact of interrupting "
|
|
112
|
+
"the task, and a video.",
|
|
113
|
+
fix="Complete the foreground service declaration in Play Console.",
|
|
114
|
+
source=rule["declaration_source"],
|
|
115
|
+
)
|
|
116
|
+
)
|
|
117
|
+
return findings
|
|
118
|
+
|
|
119
|
+
def _missing(self, s: Service, m: Manifest, r: Rule, target: int) -> list[Finding]:
|
|
120
|
+
"""Permissions without which startForeground() throws. Breaks only when it must throw:
|
|
121
|
+
a service in the app's own code with no type it can start with. A library's service
|
|
122
|
+
may never be started in the foreground (plugins tell the app to add the permission when
|
|
123
|
+
it does), and a service with a usable type can be started with that type alone (Knit's
|
|
124
|
+
mesh service, connectedDevice|location, passes only connectedDevice on API 34+)."""
|
|
125
|
+
own = m.is_own_code(s.name)
|
|
126
|
+
types = s.foreground_types or ()
|
|
127
|
+
gaps = {t: self._gaps(t, m, r, target) for t in types}
|
|
128
|
+
# Several types: the app can start the service with only the ones it has everything
|
|
129
|
+
# for (startForeground takes a subset), so gaps in the others are only notes.
|
|
130
|
+
usable = [t for t in types if not gaps[t]]
|
|
131
|
+
crash = Level.BREAKS if own and not usable else Level.NOTE
|
|
132
|
+
needed: list[tuple[str, str, str, Level]] = [] # (id, what is missing, why, level)
|
|
133
|
+
if not m.requests(FOREGROUND_SERVICE):
|
|
134
|
+
fgs = _short(FOREGROUND_SERVICE)
|
|
135
|
+
needed.append(
|
|
136
|
+
(fgs, fgs, "every foreground service", Level.BREAKS if own else Level.NOTE)
|
|
137
|
+
)
|
|
138
|
+
for t in types:
|
|
139
|
+
for key, missing, elsewhere in gaps[t]:
|
|
140
|
+
why = f"type {t}" + (f", unless granted through {elsewhere}" if elsewhere else "")
|
|
141
|
+
needed.append((key, missing, why, Level.NOTE if elsewhere else crash))
|
|
142
|
+
if own and usable and any(gaps.values()):
|
|
143
|
+
note = (
|
|
144
|
+
f" The service also declares {', '.join(usable)}, which has what it needs, so "
|
|
145
|
+
"this matters only if the app starts it with this type."
|
|
146
|
+
)
|
|
147
|
+
elif not own:
|
|
148
|
+
note = (
|
|
149
|
+
" The service comes from a library, so this happens only if the app starts it "
|
|
150
|
+
"in the foreground."
|
|
151
|
+
)
|
|
152
|
+
else:
|
|
153
|
+
note = ""
|
|
154
|
+
return [
|
|
155
|
+
Finding(
|
|
156
|
+
self.info.id,
|
|
157
|
+
level,
|
|
158
|
+
subject=f"{s.name}:{key}",
|
|
159
|
+
title=f"{_short(s.name)} ({why}) is missing {missing}.",
|
|
160
|
+
detail=f"For apps that target API {r['types_required_from_target']} or higher, "
|
|
161
|
+
"starting it in the foreground with this type throws a SecurityException." + note,
|
|
162
|
+
evidence=(
|
|
163
|
+
Evidence(
|
|
164
|
+
"manifest",
|
|
165
|
+
f"foregroundServiceType={'|'.join(s.foreground_types or ())}",
|
|
166
|
+
s.name,
|
|
167
|
+
),
|
|
168
|
+
),
|
|
169
|
+
fix=f"Add the {missing} permission to the manifest, or remove the type if the "
|
|
170
|
+
"service doesn't use it.",
|
|
171
|
+
source=r.source,
|
|
172
|
+
)
|
|
173
|
+
for key, missing, why, level in needed
|
|
174
|
+
]
|
|
175
|
+
|
|
176
|
+
def _gaps(self, t: str, m: Manifest, r: Rule, target: int) -> list[tuple[str, str, str | None]]:
|
|
177
|
+
"""What type t lacks: (id, missing permission, where else it can come from)."""
|
|
178
|
+
out: list[tuple[str, str, str | None]] = []
|
|
179
|
+
if t not in r["no_type_permission"]:
|
|
180
|
+
perm = type_permission(t)
|
|
181
|
+
if not m.requests(perm):
|
|
182
|
+
out.append((_short(perm), _short(perm), None))
|
|
183
|
+
cap = r["prerequisite_max_target"]
|
|
184
|
+
options: Sequence[str] = [
|
|
185
|
+
p for p in r["prerequisites"].get(t, ()) if target <= cap.get(p, target)
|
|
186
|
+
]
|
|
187
|
+
if options and not any(m.requests(p) for p in options):
|
|
188
|
+
names = " or ".join(_short(p) for p in options[:4])
|
|
189
|
+
if len(options) > 4:
|
|
190
|
+
names += " or another listed permission"
|
|
191
|
+
out.append((f"{t}:prerequisite", names, MET_ELSEWHERE.get(t)))
|
|
192
|
+
return out
|
|
193
|
+
|
|
194
|
+
def _advice(self, s: Service, r: Rule, target: int) -> list[Finding]:
|
|
195
|
+
out = []
|
|
196
|
+
limited = [t for t in s.foreground_types or () if t in r["timeout_types"]]
|
|
197
|
+
if limited and target >= r["timeout_from_target"]:
|
|
198
|
+
out.append(
|
|
199
|
+
Finding(
|
|
200
|
+
self.info.id,
|
|
201
|
+
Level.NOTE,
|
|
202
|
+
subject=f"{s.name}:timeout",
|
|
203
|
+
title=f"{_short(s.name)} ({', '.join(limited)}) may run 6 hours in 24 while "
|
|
204
|
+
"the app is in the background.",
|
|
205
|
+
detail="The limit counts each type separately and resets when the user "
|
|
206
|
+
"opens the app. At the limit the system calls Service.onTimeout(); a "
|
|
207
|
+
"service that doesn't stop then crashes the app, and starting another of "
|
|
208
|
+
"that type throws ForegroundServiceStartNotAllowedException.",
|
|
209
|
+
fix="Implement onTimeout() and call stopSelf() from it, or move the work to "
|
|
210
|
+
"WorkManager.",
|
|
211
|
+
source=r["timeout_source"],
|
|
212
|
+
)
|
|
213
|
+
)
|
|
214
|
+
if "specialUse" in (s.foreground_types or ()) and not s.special_use_subtype:
|
|
215
|
+
out.append(
|
|
216
|
+
Finding(
|
|
217
|
+
self.info.id,
|
|
218
|
+
Level.NOTE,
|
|
219
|
+
subject=f"{s.name}:special-use",
|
|
220
|
+
title=f"{_short(s.name)} (type specialUse) doesn't describe its use.",
|
|
221
|
+
detail="Google Play reviews the use described in the "
|
|
222
|
+
"PROPERTY_SPECIAL_USE_FGS_SUBTYPE property.",
|
|
223
|
+
fix='Add <property android:name="android.app.PROPERTY_SPECIAL_USE_FGS_SUBTYPE" '
|
|
224
|
+
'android:value="..."/> to the service, saying what it does.',
|
|
225
|
+
source=r.source,
|
|
226
|
+
)
|
|
227
|
+
)
|
|
228
|
+
return out
|
|
229
|
+
|
|
230
|
+
def _by_reference(self, s: Service, r: Rule) -> Finding:
|
|
231
|
+
return Finding(
|
|
232
|
+
self.info.id,
|
|
233
|
+
Level.NOTE,
|
|
234
|
+
subject=f"{s.name}:type-reference",
|
|
235
|
+
title=f"{_short(s.name)} sets foregroundServiceType through a resource reference, "
|
|
236
|
+
"which readytoship doesn't resolve.",
|
|
237
|
+
detail="Its types and their permissions weren't checked.",
|
|
238
|
+
fix="Set foregroundServiceType directly in the manifest.",
|
|
239
|
+
source=r.source,
|
|
240
|
+
)
|
|
241
|
+
|
|
242
|
+
def _ok(self, title: str, source: str) -> Finding:
|
|
243
|
+
return Finding(self.info.id, Level.OK, subject="app", title=title, source=source)
|
|
244
|
+
|
|
245
|
+
|
|
246
|
+
def _short(name: str) -> str:
|
|
247
|
+
return name.rsplit(".", 1)[-1]
|