Skip to content

Liquid Glass and Apple Music touches for the Spotify iOS app. A GPL-3.0 fork of spoti.pw 0.21.1.

C

fix: the home screen widget shows what is playing again, Spotify's App Groups moved into one the re-signed IPA has

Vojtěch Škopek4b63f3599eb5
diff --git a/docs/tweaks.md b/docs/tweaks.md
index 1b9ba0b1..30668f34 100644
--- a/docs/tweaks.md
+++ b/docs/tweaks.md
@@ -16,8 +16,11 @@
backup and signing, the welcome tour
tweak/Sources/Diagnostics/ screen dumps, the tree server and the main thread hang sampler of FLEX builds
extension/LiveActivity/ the redesign's Live Activity widget, a WidgetKit extension of its own
+ extension/AppGroups/ a dylib loaded by Spotify and its home screen widget that moves Spotify's App Groups
+ into a group the re-signed IPA has; without it the widget stays a placeholder
scripts/ pipeline.sh (build + inject), build-extension.sh (the widget extension, without an
- Xcode project), merge-appintents.py (the widget's intents into Spotify's), install.sh (sign + install), record-trees.py, record-session.py,
+ Xcode project), merge-appintents.py (the widget's intents into Spotify's), insert-dylib.py (a load command into
+ Spotify's widget), install.sh (sign + install), record-trees.py, record-session.py,
dump-log.sh, extract-flags.py
trees/ recorded view trees, one per screen; the input for every new hook. trees/clean/ holds
the numbered snapshots per screen of record-session.py, taken of Spotify as it came
diff --git a/extension/AppGroups/AppGroups.m b/extension/AppGroups/AppGroups.m
new file mode 100644
index 00000000..8c3d7da6
--- /dev/null
+++ b/extension/AppGroups/AppGroups.m
@@ -0,0 +1,92 @@
+// Spotify's App Groups, redirected into whichever group the sideload signature really has.
+//
+// The main app hands the home screen widget everything it shows through UserDefaults suites named
+// group.com.spotify.client.widget, .widget.data and .widget.lifecycle. Those groups belong to
+// Spotify's team, so a re-signed IPA never has them: its profile carries groups of its own
+// (group.<hash>.N from a paid certificate service, something else from AltStore), each process
+// then gets a private, empty suite, and the widget stays on its "Open Spotify and play" placeholder.
+//
+// This dylib is loaded by both Spotify and WidgetExtension.appex (scripts/pipeline.sh adds the load
+// command to the extension). In each it maps every group.* identifier the process is not entitled
+// to onto a folder inside one group it is entitled to, the same folder in both processes, so the
+// suites and container files meet again. It is plain runtime swizzling, no Substrate, because the
+// extension carries nothing else of the tweak.
+#import <Foundation/Foundation.h>
+#import <objc/runtime.h>
+#import <os/log.h>
+
+typedef struct __SecTask *SecTaskRef;
+extern SecTaskRef SecTaskCreateFromSelf(CFAllocatorRef allocator);
+extern CFTypeRef SecTaskCopyValueForEntitlement(SecTaskRef task, CFStringRef entitlement, CFErrorRef *error);
+
+@interface NSUserDefaults (SGAppGroups)
+- (instancetype)_initWithSuiteName:(NSString *)suiteName container:(NSURL *)container;
+@end
+
+static os_log_t sLog;
+static NSArray<NSString *> *sEntitled;
+static NSURL *(*sOrigContainerURL)(id, SEL, NSString *);
+static id (*sOrigInitWithSuiteName)(id, SEL, NSString *);
+
+static NSArray<NSString *> *EntitledGroups(void) {
+ SecTaskRef task = SecTaskCreateFromSelf(NULL);
+ if (!task) return @[];
+ CFTypeRef value = SecTaskCopyValueForEntitlement(task, CFSTR("com.apple.security.application-groups"), NULL);
+ CFRelease(task);
+ NSArray *groups = CFBridgingRelease(value);
+ if (![groups isKindOfClass:NSArray.class]) return @[];
+ // Sorted, so the app and its extensions pick the same host group whatever order the signer wrote.
+ return [groups sortedArrayUsingSelector:@selector(compare:)];
+}
+
+// The folder that stands in for `group`, or nil when the process may use the group as it is (or
+// has no group at all to put it in).
+static NSURL *RedirectedContainer(NSString *group) {
+ if (![group isKindOfClass:NSString.class] || ![group hasPrefix:@"group."]) return nil;
+ if ([sEntitled containsObject:group]) return nil;
+ NSFileManager *fm = NSFileManager.defaultManager;
+ // AltStore and SideStore register the original group with the team appended, which is the
+ // real thing already.
+ NSString *prefix = [group stringByAppendingString:@"."];
+ for (NSString *entitled in sEntitled) {
+ if ([entitled hasPrefix:prefix]) return sOrigContainerURL(fm, @selector(containerURLForSecurityApplicationGroupIdentifier:), entitled);
+ }
+ for (NSString *entitled in sEntitled) {
+ NSURL *host = sOrigContainerURL(fm, @selector(containerURLForSecurityApplicationGroupIdentifier:), entitled);
+ if (!host) continue;
+ NSURL *url = [host URLByAppendingPathComponent:group isDirectory:YES];
+ [fm createDirectoryAtURL:[url URLByAppendingPathComponent:@"Library/Preferences" isDirectory:YES]
+ withIntermediateDirectories:YES attributes:nil error:NULL];
+ return url;
+ }
+ return nil;
+}
+
+static NSURL *SGContainerURL(id self, SEL _cmd, NSString *group) {
+ NSURL *url = RedirectedContainer(group);
+ return url ?: sOrigContainerURL(self, _cmd, group);
+}
+
+// initWithSuiteName: is the public door every UserDefaults(suiteName:) goes through; the container
+// variant is what App Group suites resolve to inside Foundation, so it is handed the stand-in.
+static id SGInitWithSuiteName(NSUserDefaults *self, SEL _cmd, NSString *suite) {
+ NSURL *url = RedirectedContainer(suite);
+ if (url && [self respondsToSelector:@selector(_initWithSuiteName:container:)]) {
+ os_log(sLog, "[spotifyglass] appgroups: %{public}@ -> %{public}@", suite, url.path);
+ return [self _initWithSuiteName:suite container:url];
+ }
+ return sOrigInitWithSuiteName(self, _cmd, suite);
+}
+
+__attribute__((constructor)) static void SGAppGroupsInit(void) {
+ sLog = os_log_create("spotifyglass", "appgroups");
+ sEntitled = EntitledGroups();
+ os_log(sLog, "[spotifyglass] appgroups: %{public}@ entitled to %{public}@",
+ NSBundle.mainBundle.bundleIdentifier, [sEntitled componentsJoinedByString:@", "]);
+ if (sEntitled.count == 0) return;
+
+ Method m = class_getInstanceMethod(NSFileManager.class, @selector(containerURLForSecurityApplicationGroupIdentifier:));
+ sOrigContainerURL = (void *)method_setImplementation(m, (IMP)SGContainerURL);
+ m = class_getInstanceMethod(NSUserDefaults.class, @selector(initWithSuiteName:));
+ sOrigInitWithSuiteName = (void *)method_setImplementation(m, (IMP)SGInitWithSuiteName);
+}
diff --git a/scripts/insert-dylib.py b/scripts/insert-dylib.py
new file mode 100755
index 00000000..21e10034
--- /dev/null
+++ b/scripts/insert-dylib.py
@@ -0,0 +1,48 @@
+#!/usr/bin/env python3
+"""Adds an LC_LOAD_WEAK_DYLIB to a thin 64-bit Mach-O, in place, if it is not there yet.
+
+ scripts/insert-dylib.py <binary> <install name, e.g. @rpath/X.dylib>
+
+The command goes into the free space between the load commands and the first section, which
+every Xcode-linked binary leaves. The signature is invalidated; the IPA is signed again later.
+"""
+import struct
+import sys
+
+LC_LOAD_WEAK_DYLIB = 0x80000018
+LC_SEGMENT_64 = 0x19
+
+path, name = sys.argv[1:3]
+with open(path, "r+b") as f:
+ data = bytearray(f.read())
+ magic, _, _, _, ncmds, sizeofcmds, _, _ = struct.unpack_from("<IiiIIIII", data, 0)
+ if magic != 0xFEEDFACF:
+ sys.exit(f"{path}: not a thin 64-bit Mach-O")
+
+ first_section = len(data)
+ off = 32
+ for _ in range(ncmds):
+ cmd, cmdsize = struct.unpack_from("<II", data, off)
+ if cmd in (0xC, 0x18, 0x1F) and data[off + 24:off + cmdsize].split(b"\0")[0] == name.encode():
+ print(f" {name} already loaded")
+ sys.exit(0)
+ if cmd == LC_SEGMENT_64:
+ nsects = struct.unpack_from("<I", data, off + 64)[0]
+ for s in range(nsects):
+ sect_off = struct.unpack_from("<I", data, off + 72 + s * 80 + 48)[0]
+ if sect_off:
+ first_section = min(first_section, sect_off)
+ off += cmdsize
+
+ raw = name.encode() + b"\0"
+ cmdsize = (24 + len(raw) + 7) & ~7
+ end = 32 + sizeofcmds
+ if end + cmdsize > first_section or any(data[end:end + cmdsize]):
+ sys.exit(f"{path}: no room for another load command")
+
+ cmd = struct.pack("<IIIIII", LC_LOAD_WEAK_DYLIB, cmdsize, 24, 2, 0x10000, 0x10000) + raw
+ data[end:end + cmdsize] = cmd.ljust(cmdsize, b"\0")
+ struct.pack_into("<II", data, 16, ncmds + 1, sizeofcmds + cmdsize)
+ f.seek(0)
+ f.write(data)
+print(f" {name} added")
diff --git a/scripts/pipeline.sh b/scripts/pipeline.sh
index bea0356b..bd4846a8 100755
--- a/scripts/pipeline.sh
+++ b/scripts/pipeline.sh
@@ -94,10 +94,34 @@ else
echo "==> no Xcode selected: building without the Live Activity extension"
fi
+# Spotify's widget reads what the app writes through App Group suites the re-signed IPA is not entitled
+# to; this dylib, loaded by the app and by the widget, puts both on a group the signature does have.
+echo "==> building the App Group shim"
+GROUPS_DYLIB="$ROOT/out/SpotifyGlassAppGroups.dylib"
+xcrun --sdk iphoneos clang -target arm64-apple-ios16.0 -dynamiclib -fobjc-arc -Os -framework Foundation -framework Security \
+ -install_name @rpath/SpotifyGlassAppGroups.dylib -o "$GROUPS_DYLIB" "$ROOT/extension/AppGroups/AppGroups.m"
+FILES+=("$GROUPS_DYLIB")
+
echo "==> injecting"
# -w drops the Watch app: its companion-app key would still name com.spotify.client and block the install.
cyan -i "$IN" -o "$OUT" -f "${FILES[@]}" -l "$ROOT/plist/liquid-glass.plist" ${BUNDLE_ID:+-b "$BUNDLE_ID"} ${NAME:+-n "$NAME"} ${ICON:+-k "$ICON"} -w -s --overwrite
+echo "==> loading the App Group shim in the home screen widget"
+WIDGET_BIN="${APP_DIR}PlugIns/WidgetExtension.appex/WidgetExtension"
+if unzip -l "$OUT" "$WIDGET_BIN" >/dev/null 2>&1; then
+ PATCH="$(mktemp -d)"
+ unzip -q "$OUT" "$WIDGET_BIN" -d "$PATCH"
+ "$ROOT/scripts/insert-dylib.py" "$PATCH/$WIDGET_BIN" @rpath/SpotifyGlassAppGroups.dylib
+ # Fakesigned again with its own entitlements, the way cyan -s left it, for TrollStore.
+ ldid -e "$PATCH/$WIDGET_BIN" > "$PATCH/ents.plist"
+ ldid -S"$PATCH/ents.plist" "$PATCH/$WIDGET_BIN"
+ OUT_ABS="$(cd "$(dirname "$OUT")" && pwd)/$(basename "$OUT")"
+ (cd "$PATCH" && zip -q "$OUT_ABS" "$WIDGET_BIN")
+ rm -rf "$PATCH"
+else
+ echo " no WidgetExtension.appex in this IPA"
+fi
+
if [ -n "${EXT_DIR:-}" ]; then
echo "==> adding the Live Activity intents to Spotify's App Intents metadata"
"$ROOT/scripts/merge-appintents.py" "$OUT" "$APP_DIR" "$EXT_DIR/app/Metadata.appintents"