From 893401b5dcfefa300e324bbe93af6ab2be49e384 Mon Sep 17 00:00:00 2001 From: Alexander Eichhorn Date: Sun, 2 May 2021 23:03:29 +0200 Subject: [PATCH] correctly sets YouTube consent cookie --- XCDYouTubeKit/XCDYouTubeVideoOperation.m | 39 ++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/XCDYouTubeKit/XCDYouTubeVideoOperation.m b/XCDYouTubeKit/XCDYouTubeVideoOperation.m index be4ce9ee..9a7f6068 100644 --- a/XCDYouTubeKit/XCDYouTubeVideoOperation.m +++ b/XCDYouTubeKit/XCDYouTubeVideoOperation.m @@ -270,10 +270,49 @@ - (void) handleConnectionError:(NSError *)connectionError requestType:(XCDYouTub #pragma mark - Response Parsing +- (void) initializeConsentWithResponse:(NSURLResponse *)response { + NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response; + if (httpResponse && response.URL) { + NSArray *cookies = [NSHTTPCookie cookiesWithResponseHeaderFields:httpResponse.allHeaderFields forURL:(NSURL *_Nonnull)response.URL]; + + for (NSHTTPCookie *cookie in cookies) { + if ([cookie.name isEqualToString:@"__Secure-3PSID"]) return; + } + + for (NSHTTPCookie *cookie in cookies) { + if ([cookie.name isEqualToString:@"CONSENT"]) { + if ([cookie.value isEqualToString:@"YES"]) return; + + NSString *rawConsentID = [cookie.value stringByReplacingOccurrencesOfString:@"PENDING+" withString:@""]; + int consentID = [rawConsentID intValue]; + + // generate random consent id, if doesn't match expected format + if (consentID < 100 || consentID > 999) { + consentID = 100 + (int)arc4random_uniform((uint32_t)(999 - 100 + 1)); + } + + NSString *cookieValue = [[NSString alloc] initWithFormat:@"YES+cb.20210328-17-p0.en+FX+%i", consentID]; + NSHTTPCookie *consentCookie = [NSHTTPCookie cookieWithProperties:@{ + NSHTTPCookiePath: @"/", + NSHTTPCookieName: @"CONSENT", + NSHTTPCookieValue: cookieValue, + NSHTTPCookieDomain:@".youtube.com", + NSHTTPCookieSecure:@"TRUE" + }]; + [self.session.configuration.HTTPCookieStorage setCookie:consentCookie]; + return; + } + } + + } +} + - (void) handleVideoInfoResponseWithInfo:(NSDictionary *)info response:(NSURLResponse *)response { XCDYouTubeLogDebug(@"Handling video info response"); + [self initializeConsentWithResponse:response]; + NSError *error = nil; XCDYouTubeVideo *video = [[XCDYouTubeVideo alloc] initWithIdentifier:self.videoIdentifier info:info playerScript:self.playerScript response:response error:&error]; if (video)