From 7c8fe1d4277970e4b39f46baa95f9308f19e1757 Mon Sep 17 00:00:00 2001 From: Nathan Manceaux-Panot <2979318+Cykelero@users.noreply.github.com> Date: Wed, 12 Jul 2023 07:07:42 +0200 Subject: [PATCH] Expose installed version string to update notes HTML page (#2373) --- Sparkle/SUUpdateAlert.m | 2 +- Sparkle/SUWKWebView.h | 2 +- Sparkle/SUWKWebView.m | 37 +++++++++++++++++++++++++++++++++++-- 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/Sparkle/SUUpdateAlert.m b/Sparkle/SUUpdateAlert.m index 5af81729..49a35116 100644 --- a/Sparkle/SUUpdateAlert.m +++ b/Sparkle/SUUpdateAlert.m @@ -356,7 +356,7 @@ static NSString *const SUUpdateAlertTouchBarIndentifier = @"" SPARKLE_BUNDLE_IDE } else #endif { - _releaseNotesView = [[SUWKWebView alloc] initWithColorStyleSheetLocation:colorStyleURL fontFamily:defaultFontFamily fontPointSize:defaultFontSize javaScriptEnabled:javaScriptEnabled customAllowedURLSchemes:customAllowedURLSchemes]; + _releaseNotesView = [[SUWKWebView alloc] initWithColorStyleSheetLocation:colorStyleURL fontFamily:defaultFontFamily fontPointSize:defaultFontSize javaScriptEnabled:javaScriptEnabled customAllowedURLSchemes:customAllowedURLSchemes installedVersion: _host.version]; } } diff --git a/Sparkle/SUWKWebView.h b/Sparkle/SUWKWebView.h index ee19a957..1d28b6a7 100644 --- a/Sparkle/SUWKWebView.h +++ b/Sparkle/SUWKWebView.h @@ -15,7 +15,7 @@ NS_ASSUME_NONNULL_BEGIN SPU_OBJC_DIRECT_MEMBERS @interface SUWKWebView : NSObject -- (instancetype)initWithColorStyleSheetLocation:(NSURL *)colorStyleSheetLocation fontFamily:(NSString *)fontFamily fontPointSize:(int)fontPointSize javaScriptEnabled:(BOOL)javaScriptEnabled customAllowedURLSchemes:(NSArray *)customAllowedURLSchemes; +- (instancetype)initWithColorStyleSheetLocation:(NSURL *)colorStyleSheetLocation fontFamily:(NSString *)fontFamily fontPointSize:(int)fontPointSize javaScriptEnabled:(BOOL)javaScriptEnabled customAllowedURLSchemes:(NSArray *)customAllowedURLSchemes installedVersion:(NSString *)installedVersion; @end diff --git a/Sparkle/SUWKWebView.m b/Sparkle/SUWKWebView.m index 7e1b7d9a..22bc4389 100644 --- a/Sparkle/SUWKWebView.m +++ b/Sparkle/SUWKWebView.m @@ -53,7 +53,27 @@ static WKUserScript *_userScriptWithInjectedStyleSource(NSString *styleSource) return [[WKUserScript alloc] initWithSource:scriptSource injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES]; } -- (instancetype)initWithColorStyleSheetLocation:(NSURL *)colorStyleSheetLocation fontFamily:(NSString *)fontFamily fontPointSize:(int)fontPointSize javaScriptEnabled:(BOOL)javaScriptEnabled customAllowedURLSchemes:(NSArray *)customAllowedURLSchemes +static WKUserScript *_userScriptForExposingCurrentRelease(NSString *releaseString) +{ + // Check that release string can be safely injected + NSMutableCharacterSet *allowedCharacterSet = [NSMutableCharacterSet alphanumericCharacterSet]; + [allowedCharacterSet addCharactersInString:@"_.- "]; + if ([releaseString rangeOfCharacterFromSet:allowedCharacterSet.invertedSet].location != NSNotFound) { + SULog(SULogLevelDefault, @"warning: App version '%@' has characters unsafe for injection. The version number will not be exposed to the release notes CSS. Only [a-zA-Z0-9._- ] is allowed.", releaseString); + return nil; + } + + // This script adds the `sparkle-installed-version` class to all elements which have a matching `data-sparkle-version` attribute + NSString *scriptSource = [NSString stringWithFormat: + @"document.querySelectorAll(\'[data-sparkle-version=\"%@\"]\')\n" + @".forEach(installedVersionElement =>\n" + @"installedVersionElement.classList.add('sparkle-installed-version')\n" + @");", releaseString]; + + return [[WKUserScript alloc] initWithSource:scriptSource injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES]; +} + +- (instancetype)initWithColorStyleSheetLocation:(NSURL *)colorStyleSheetLocation fontFamily:(NSString *)fontFamily fontPointSize:(int)fontPointSize javaScriptEnabled:(BOOL)javaScriptEnabled customAllowedURLSchemes:(NSArray *)customAllowedURLSchemes installedVersion:(NSString *)installedVersion { self = [super init]; if (self != nil) { @@ -92,7 +112,20 @@ static WKUserScript *_userScriptWithInjectedStyleSource(NSString *styleSource) // In fact, we must execute javascript to properly inject our default CSS style into the DOM // Legacy WebView has exposed methods for custom stylesheets and default fonts, // but WKWebView seems to forgo that type of API surface in favor of user scripts like this - [userContentController addUserScript:_userScriptWithInjectedStyleSource(finalStyleContents)]; + WKUserScript *userScriptWithInjectedStyleSource = _userScriptWithInjectedStyleSource(finalStyleContents); + if (userScriptWithInjectedStyleSource == nil) { + SULog(SULogLevelError, @"Failed to create script for injecting style"); + } else { + [userContentController addUserScript:userScriptWithInjectedStyleSource]; + } + + WKUserScript *userScriptForExposingCurrentRelease = _userScriptForExposingCurrentRelease(installedVersion); + if (userScriptForExposingCurrentRelease == nil) { + SULog(SULogLevelDefault, @"warning: Failed to create script for injecting version %@", installedVersion); + } else { + [userContentController addUserScript:userScriptForExposingCurrentRelease]; + } + configuration.userContentController = userContentController; _webView = [[WKWebView alloc] initWithFrame:NSZeroRect configuration:configuration];