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: a Lyricsfile nested past 16 levels or over 256 KB is refused before it is parsed

My-Name-Is-Jeff29f2775cf1ea
diff --git a/harness/lyrics-sources/check.m b/harness/lyrics-sources/check.m
index 9799f0ee..294bce45 100644
--- a/harness/lyrics-sources/check.m
+++ b/harness/lyrics-sources/check.m
@@ -273,6 +273,19 @@ int main(int argc, char **argv) {
@[@"lines:", @" stray\nlines:"]]) {
CHECK(!SGLyricsfileLines([wordFile stringByReplacingOccurrencesOfString:swap[0] withString:swap[1]]));
}
+ // Nested far past the spec's four levels, or over a megabyte: refused, not followed. The same file
+ // with its extra key nested ten deep still reads.
+ NSString *(^nested)(NSUInteger) = ^NSString *(NSUInteger levels) {
+ NSMutableString *doc = [NSMutableString stringWithString:wordFile];
+ for (NSUInteger level = 0; level < levels; level++) {
+ [doc appendFormat:@"%@k:\n", [@"" stringByPaddingToLength:level withString:@" " startingAtIndex:0]];
+ }
+ [doc appendFormat:@"%@x: 1\n", [@"" stringByPaddingToLength:levels withString:@" " startingAtIndex:0]];
+ return doc;
+ };
+ CHECK(SGLyricsfileLines(nested(10)).count == 1);
+ CHECK(!SGLyricsfileLines(nested(20)) && !SGLyricsfileLines(nested(1000)));
+ CHECK(!SGLyricsfileLines([wordFile stringByAppendingFormat:@"plain: |\n %@\n", [@"" stringByPaddingToLength:(1 << 20) withString:@"a" startingAtIndex:0]]));
CHECK(!SGLyricsfileLines(@"[not, yaml") && !SGLyricsfileLines(@"") && !SGLyricsfileLines(nil) && !SGLyricsfileLines((NSString *)@3));
// LRCLIB: a record with words timed answers with them; one whose Lyricsfile cannot be read
diff --git a/tweak/Sources/Shared/LyricsSources/Lyricsfile.m b/tweak/Sources/Shared/LyricsSources/Lyricsfile.m
index 683292e4..8cae05f3 100644
--- a/tweak/Sources/Shared/LyricsSources/Lyricsfile.m
+++ b/tweak/Sources/Shared/LyricsSources/Lyricsfile.m
@@ -23,7 +23,7 @@
@interface SGLFReader : NSObject {
@public
NSMutableArray<NSString *> *rows;
- NSUInteger at;
+ NSUInteger at, depth;
BOOL failed;
}
@end
@@ -240,10 +240,19 @@ static NSArray *sequence(SGLFReader *r, NSUInteger indent) {
return items;
}
+// The spec nests four deep; a reply nested far deeper is refused before it can run the stack out.
+static const NSUInteger kMaxDepth = 16;
+
static id node(SGLFReader *r, NSUInteger indent) {
NSString *row = nextRow(r);
if (!row) return NSNull.null;
- return isDash(row, indent) ? sequence(r, indent) : mapping(r, indent);
+ if (++r->depth > kMaxDepth) {
+ r->failed = YES;
+ return nil;
+ }
+ id value = isDash(row, indent) ? sequence(r, indent) : mapping(r, indent);
+ r->depth--;
+ return value;
}
// The document as dictionaries, arrays and strings; nil when it is not YAML this reads.
@@ -261,6 +270,9 @@ static id parseYAML(NSString *yaml) {
#pragma mark - the lines
+// A whole song is tens of kilobytes; anything past this is no Lyricsfile.
+static const NSUInteger kMaxLength = 1 << 20;
+
// A time in milliseconds: digits only, none negative. -1 for anything else.
static NSInteger msOf(id value) {
if (![value isKindOfClass:NSString.class] || ![value length]) return -1;
@@ -323,7 +335,7 @@ static SGKaraokeLine *wordLine(NSArray *rawWords, NSInteger start, NSInteger end
}
NSArray<SGKaraokeLine *> *SGLyricsfileLines(NSString *yaml) {
- if (![yaml isKindOfClass:NSString.class]) return nil;
+ if (![yaml isKindOfClass:NSString.class] || yaml.length > kMaxLength) return nil;
NSDictionary *root = parseYAML(yaml);
if (![root isKindOfClass:NSDictionary.class] || ![root[@"version"] isEqual:@"1.0"]) return nil;
NSArray *rawLines = root[@"lines"];