{"id":41544,"date":"2023-12-21T17:00:03","date_gmt":"2023-12-21T22:00:03","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=41544"},"modified":"2023-12-21T17:00:03","modified_gmt":"2023-12-21T22:00:03","slug":"state-restoration-with-nssecurecoding-on-sonoma","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2023\/12\/21\/state-restoration-with-nssecurecoding-on-sonoma\/","title":{"rendered":"State Restoration With NSSecureCoding on Sonoma"},"content":{"rendered":"<p><a href=\"https:\/\/milen.me\/writings\/appkit-state-restoration-macos-14-sonoma\/\">Milen Dzhumerov<\/a>:<\/p>\n<blockquote cite=\"https:\/\/milen.me\/writings\/appkit-state-restoration-macos-14-sonoma\/\"><p>AppKit state restoration behaviour changed on macOS 14 Sonoma in a subtle way\nthat can lead to apps not restoring their state correctly. The change can lead\nto <em>silent<\/em> breakages which can be hard to debug.<\/p><p>[&#8230;]<\/p><p>Violations can now arise in any <code>-restoreStateWithCoder:<\/code> implementations,\nso they need to be audited.<\/p><ul><li>Check for any usages of <code>-[NSCoder decodeObjectForKey:]<\/code>.\n<ul><li>Replace with the appropriate secure variants.<\/li><\/ul><\/li><li>At the end of <code>-restoreStateWithCoder:<\/code>, check the value of <code>NSCoder.error<\/code> property.\n<ul><li>If it&rsquo;s non-<code>nil<\/code>, an error must have occurred earlier.<\/li><\/ul><\/li><\/ul><\/blockquote>\n<p>This is necessary because AppKit uses\n<code>NSDecodingFailurePolicySetErrorAndReturn<\/code>, rather than raising an exception that couldn&rsquo;t be caught with Swift.<\/p>\n\n<p>I was already opting into using secure coding but have sometimes seen this error logged even though I was implementing that method:<\/p>\n\n<blockquote><p>WARNING: Secure coding is not enabled for restorable state! Enable secure coding by implementing NSApplicationDelegate.applicationSupportsSecureRestorableState: and returning YES.<\/p><\/blockquote>\n\n<p>See also: <a href=\"https:\/\/www.swiftjectivec.com\/nssecurecoding\/\">Jordan Morgan<\/a>.<\/p>\n\n<p>Previously:<\/p>\n<ul>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2015\/11\/08\/the-java-deserialization-bug-and-nssecurecoding\/\">The Java Deserialization Bug and NSSecureCoding<\/a><\/li>\n<\/ul>","protected":false},"excerpt":{"rendered":"<p>Milen Dzhumerov: AppKit state restoration behaviour changed on macOS 14 Sonoma in a subtle way that can lead to apps not restoring their state correctly. The change can lead to silent breakages which can be hard to debug.[&#8230;]Violations can now arise in any -restoreStateWithCoder: implementations, so they need to be audited.Check for any usages of [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"2023-12-21T22:00:06Z","apple_news_api_id":"7f3df04c-d4a8-48f1-8613-0f5d80d0321d","apple_news_api_modified_at":"2023-12-21T22:00:06Z","apple_news_api_revision":"AAAAAAAAAAD\/\/\/\/\/\/\/\/\/\/w==","apple_news_api_share_url":"https:\/\/apple.news\/Afz3wTNSoSPGGEw9dgNAyHQ","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[69,30,2385,71],"class_list":["post-41544","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-cocoa","tag-mac","tag-macos-14-sonoma","tag-programming"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/41544","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=41544"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/41544\/revisions"}],"predecessor-version":[{"id":41545,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/41544\/revisions\/41545"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=41544"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=41544"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=41544"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}