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 OpenAI-compatible server is kept in the Keychain beside its key

My-Name-Is-Jeff94ddc81a5d56
diff --git a/harness/lyrics-translation/check.m b/harness/lyrics-translation/check.m
index 60a8f159..f6de16a4 100644
--- a/harness/lyrics-translation/check.m
+++ b/harness/lyrics-translation/check.m
@@ -120,15 +120,15 @@ int main(void) {
&& [bodyOf(chat)[@"response_format"][@"json_schema"][@"schema"][@"required"] isEqualToArray:(@[@"lines"])]);
NSDictionary *plainChat = bodyOf(SGAITranslationRequest(@"openai", @"gpt-6-astra", @"P", @"k2", YES));
CHECK(!plainChat[@"response_format"] && !plainChat[@"reasoning_effort"] && plainChat[@"messages"]);
- // A server of the user's own: a pasted /chat/completions is not doubled, and a URL that could carry a
- // secret is not used at all (and not swapped for OpenAI's).
- [NSUserDefaults.standardUserDefaults setObject:@"http://192.168.1.5:11434/v1/chat/completions" forKey:@"spotifyglass.lyrics.openai.url"];
- CHECK([SGAITranslationRequest(@"openai", @"llama3.2:3b", @"P", @"k", NO).URL.absoluteString isEqualToString:@"http://192.168.1.5:11434/v1/chat/completions"]);
- [NSUserDefaults.standardUserDefaults setObject:@"https://openrouter.ai/api/v1?key=secret" forKey:@"spotifyglass.lyrics.openai.url"];
- CHECK(!SGAITranslationRequest(@"openai", @"x", @"P", @"k", NO));
- [NSUserDefaults.standardUserDefaults setObject:@"ftp://example.com" forKey:@"spotifyglass.lyrics.openai.url"];
- CHECK(!SGAITranslationRequest(@"openai", @"x", @"P", @"k", NO));
+ // A server of the user's own, kept in the Keychain beside its key: a pasted /chat/completions is not
+ // doubled, a URL that could carry a secret is not used at all (and not swapped for OpenAI's), and a server
+ // in the settings, as an imported settings file could hold, is never read.
+ [NSUserDefaults.standardUserDefaults setObject:@"https://attacker.example/v1" forKey:@"spotifyglass.lyrics.openai.url"];
+ CHECK([SGAITranslationRequest(@"openai", @"x", @"P", @"k", NO).URL.host isEqualToString:@"api.openai.com"]);
[NSUserDefaults.standardUserDefaults removeObjectForKey:@"spotifyglass.lyrics.openai.url"];
+ CHECK([SGAIServerIn(@"http://192.168.1.5:11434/v1/chat/completions") isEqualToString:@"http://192.168.1.5:11434/v1"]);
+ CHECK(!SGAIServerIn(@"https://openrouter.ai/api/v1?key=secret") && !SGAIServerIn(@"https://u:[email protected]/v1"));
+ CHECK(!SGAIServerIn(@"ftp://example.com") && !SGAIServerIn(@""));
NSURLRequest *messages = SGAITranslationRequest(@"anthropic", @"claude-haiku-5-5", @"P", @"k3", NO);
CHECK([messages.URL.absoluteString isEqualToString:@"https://api.anthropic.com/v1/messages"]);
CHECK([[messages valueForHTTPHeaderField:@"x-api-key"] isEqualToString:@"k3"] && [messages valueForHTTPHeaderField:@"anthropic-version"]);
diff --git a/tweak/Sources/Shared/LyricsTranslation/LyricsTranslation.h b/tweak/Sources/Shared/LyricsTranslation/LyricsTranslation.h
index d9fee517..fe3f7eb9 100644
--- a/tweak/Sources/Shared/LyricsTranslation/LyricsTranslation.h
+++ b/tweak/Sources/Shared/LyricsTranslation/LyricsTranslation.h
@@ -15,8 +15,8 @@ extern NSNotificationName const SGLyricsTranslationsDidChangeNotification;
SGModRow *SGTranslateEverySongRow(void);
// The AI translator: a provider's id (gemini, openai, anthropic; unset: gemini), the prompt every provider gets
-// (unset: Vitrine's), and per provider spotifyglass.lyrics.<id>.model, plus spotifyglass.lyrics.openai.url
-// for an OpenAI-compatible server (unset: OpenAI's). All read at each translation.
+// (unset: Vitrine's), and per provider spotifyglass.lyrics.<id>.model. An OpenAI-compatible server (unset: OpenAI's)
+// is kept in the Keychain beside its key, not here. All read at each translation.
#define SGKeyAITranslator @"spotifyglass.lyrics.ai"
#define SGKeyAIPrompt @"spotifyglass.lyrics.prompt"
@@ -56,6 +56,8 @@ NSString *SGLyricsGeminiLanguage(void);
// The request a provider is sent, plain being the one without the optional fields some models turn down.
// nil for an unknown provider or a server URL that is not usable.
NSURLRequest *SGAITranslationRequest(NSString *provider, NSString *model, NSString *prompt, NSString *key, BOOL plain);
+// An OpenAI-compatible server URL as typed: nil when it is not http(s) with a host or could carry a secret.
+NSString *SGAIServerIn(NSString *text);
// A provider's reply read: one translation per line when it has exactly `count`, else nil and in `problem`
// why not (the key, the limit, a filter, a refusal, a length stop, a line count that does not match).
NSArray<NSString *> *SGAITranslationsIn(NSString *provider, id root, NSInteger status, NSError *error, NSUInteger count, NSString **problem);
diff --git a/tweak/Sources/Shared/LyricsTranslation/LyricsTranslation.m b/tweak/Sources/Shared/LyricsTranslation/LyricsTranslation.m
index 9c7f9990..faf376eb 100644
--- a/tweak/Sources/Shared/LyricsTranslation/LyricsTranslation.m
+++ b/tweak/Sources/Shared/LyricsTranslation/LyricsTranslation.m
@@ -14,7 +14,10 @@ static NSString *const kDefaultPrompt =
@"the same order. Translate the meaning naturally, keep each line short like a lyric, and keep an empty line "
@"empty. A line already in {language} stays as it is.";
static NSString *const kOpenAIDefaultURL = @"https://api.openai.com/v1";
-static NSString *const kOpenAIURLKey = @"spotifyglass.lyrics.openai.url";
+// The OpenAI-compatible server is kept beside its key, never in the settings: a settings file from someone
+// else could otherwise point a key stored here at their own server.
+static NSString *const kKeyAccount = @"api-key", *const kServerAccount = @"server";
+
#pragma mark - the providers
@@ -103,7 +106,7 @@ static NSString *geminiAnswer(NSDictionary *reply, NSString *speaker, NSString *
// A server URL as typed or restored: http or https with a host, nothing that could carry a secret (a user,
// a password, a query) into an exported settings file, and without /chat/completions if it was pasted whole.
-static NSString *serverIn(NSString *text) {
+NSString *SGAIServerIn(NSString *text) {
NSString *trimmed = [text stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet];
NSURLComponents *url = trimmed.length ? [NSURLComponents componentsWithString:trimmed] : nil;
NSString *scheme = url.scheme.lowercaseString;
@@ -116,10 +119,13 @@ static NSString *serverIn(NSString *text) {
return url.string;
}
+static const SGAIProvider *providerFor(NSString *tag);
+static NSString *storedSecret(const SGAIProvider *p, NSString *account);
+
// Unset: OpenAI's own. Set to something unusable: none, so a key meant for another server never goes to OpenAI.
static NSString *openAIServer(void) {
- NSString *stored = [NSUserDefaults.standardUserDefaults stringForKey:kOpenAIURLKey];
- return stored.length ? serverIn(stored) : kOpenAIDefaultURL;
+ NSString *stored = storedSecret(providerFor(@"openai"), kServerAccount);
+ return stored.length ? SGAIServerIn(stored) : kOpenAIDefaultURL;
}
// OpenAI-compatible: chat completions with a bearer key, the lines forced into {"lines": [...]} by a strict
@@ -212,31 +218,34 @@ static NSString *speaker(const SGAIProvider *p) {
#pragma mark - the keys
-static NSDictionary *keyQuery(const SGAIProvider *p) {
+static NSDictionary *keyQuery(const SGAIProvider *p, NSString *account) {
return @{(__bridge id)kSecClass: (__bridge id)kSecClassGenericPassword,
- (__bridge id)kSecAttrService: p->service, (__bridge id)kSecAttrAccount: @"api-key"};
+ (__bridge id)kSecAttrService: p->service, (__bridge id)kSecAttrAccount: account};
}
-static NSString *storedKey(const SGAIProvider *p) {
- NSMutableDictionary *query = [keyQuery(p) mutableCopy];
+static NSString *storedSecret(const SGAIProvider *p, NSString *account) {
+ NSMutableDictionary *query = [keyQuery(p, account) mutableCopy];
query[(__bridge id)kSecReturnData] = @YES;
query[(__bridge id)kSecMatchLimit] = (__bridge id)kSecMatchLimitOne;
CFTypeRef data = NULL;
if (SecItemCopyMatching((__bridge CFDictionaryRef)query, &data) != errSecSuccess || !data) return nil;
- NSString *key = [[NSString alloc] initWithData:(__bridge_transfer NSData *)data encoding:NSUTF8StringEncoding];
- return key.length ? key : nil;
+ NSString *value = [[NSString alloc] initWithData:(__bridge_transfer NSData *)data encoding:NSUTF8StringEncoding];
+ return value.length ? value : nil;
}
-static void storeKey(const SGAIProvider *p, NSString *key) {
- SecItemDelete((__bridge CFDictionaryRef)keyQuery(p));
- if (!key.length) return;
- NSMutableDictionary *item = [keyQuery(p) mutableCopy];
- item[(__bridge id)kSecValueData] = [key dataUsingEncoding:NSUTF8StringEncoding];
+static void storeSecret(const SGAIProvider *p, NSString *account, NSString *value) {
+ SecItemDelete((__bridge CFDictionaryRef)keyQuery(p, account));
+ if (!value.length) return;
+ NSMutableDictionary *item = [keyQuery(p, account) mutableCopy];
+ item[(__bridge id)kSecValueData] = [value dataUsingEncoding:NSUTF8StringEncoding];
item[(__bridge id)kSecAttrAccessible] = (__bridge id)kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly;
OSStatus status = SecItemAdd((__bridge CFDictionaryRef)item, NULL);
- if (status != errSecSuccess) SGLog(@"ai: the %@ key could not be kept (%d)", p->tag, (int)status);
+ if (status != errSecSuccess) SGLog(@"ai: the %@ %@ could not be kept (%d)", p->tag, account, (int)status);
}
+static NSString *storedKey(const SGAIProvider *p) { return storedSecret(p, kKeyAccount); }
+static void storeKey(const SGAIProvider *p, NSString *key) { storeSecret(p, kKeyAccount, key); }
+
#pragma mark - the model and the prompt
static NSString *storedString(NSString *key) {
@@ -512,9 +521,8 @@ static void askForKey(void) {
UITextField *(^keyField)(void) = ^UITextField *{ return weakAlert.textFields.lastObject; };
UIAlertAction *save = [UIAlertAction actionWithTitle:@"Save" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
if (server) {
- NSString *url = serverIn(weakAlert.textFields.firstObject.text);
- if (url && ![url isEqualToString:kOpenAIDefaultURL]) [NSUserDefaults.standardUserDefaults setObject:url forKey:kOpenAIURLKey];
- else [NSUserDefaults.standardUserDefaults removeObjectForKey:kOpenAIURLKey];
+ NSString *url = SGAIServerIn(weakAlert.textFields.firstObject.text);
+ storeSecret(p, kServerAccount, [url isEqualToString:kOpenAIDefaultURL] ? nil : url);
}
// A key copied with the quotes around it, as from a code sample, is turned down as it stands.
NSMutableCharacterSet *around = [NSMutableCharacterSet whitespaceAndNewlineCharacterSet];
@@ -526,13 +534,13 @@ static void askForKey(void) {
void (^check)(void) = ^{
BOOL typed = [keyField().text stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet].length > 0;
NSString *url = weakAlert.textFields.firstObject.text;
- BOOL serverOK = !server || !url.length || serverIn(url);
+ BOOL serverOK = !server || !url.length || SGAIServerIn(url);
weakSave.enabled = (typed || (server && stored)) && serverOK;
};
if (server) {
[alert addTextFieldWithConfigurationHandler:^(UITextField *field) {
configure(field, kOpenAIDefaultURL);
- field.text = storedString(kOpenAIURLKey);
+ field.text = storedSecret(p, kServerAccount);
field.keyboardType = UIKeyboardTypeURL;
field.textContentType = UITextContentTypeURL;
[field addAction:[UIAction actionWithHandler:^(UIAction *action) { check(); }] forControlEvents:UIControlEventEditingChanged];
@@ -548,6 +556,7 @@ static void askForKey(void) {
if (stored) {
[alert addAction:[UIAlertAction actionWithTitle:@"Remove" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
storeKey(p, nil);
+ storeSecret(p, kServerAccount, nil);
}]];
}
[alert addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil]];