178 points · by ingve · 2y ago · 63 comments
For this project I wanted a consistent file format for my entire collection.
I have a bunch of stuff I want to save which is behind paywalls/logins/clickthroughs that are tricky for wget to reach. I know I can hand wget a cookies file, but that’s mildly fiddly. I save those pages as Safari webarchive files, and then they can drop in alongside the files I’ve collected programatically. Then I can deal with all my saved pages as a homogeneous set, rather than being split into two formats.
Plus I couldn't find anybody who'd done this, and it was fun :D
This is only for personal stuff where I know I'll be using Safari/macOS for the foreseeable future. I don't envisage using this for anything professional, or a shared archive -- you're right that a less proprietary format would be better in those contexts. I think I'm in a bit of a niche here.
(I'm honestly surprised this is on the front page; I didn't think anybody else would be that interested.)
2/ Proprietary format: it is, but before I started I did some experiments to see what's actually inside. It's a binary plist and I can recover all the underlying HTML/CSS/JS files with Python, so I'm not totally hosed if Safari goes away.
Notes on that here: https://alexwlchan.net/til/2024/whats-inside-safari-webarchi...
'Save the webpage as I see it in my browser' remains a surprisingly annoying and fiddly problem, especially programmatically, so the niche is probably a little roomier than you might initially suspect.
You may be interested in SingleFile[1]
[1] https://github.com/gildas-lormeau/SingleFile
I use it all the time to archive webpages, and I imagine it wouldn't be hard to throw together a script to use FireFox's headless mode in combination with SingleFile to selfhost a clone of the wayback machine.
https://github.com/webrecorder
It has a standardized format and acts like a recorder for what you see.
Seems JS developer just want their code to the obfuscated and unachievable as possible unless it is via their web server.
Is it really? I remember hacking around with with JavaScript's XMLSerializer (I think) like 5 years ago and solved that for ~90% of the websites I tried to archive. It'd save the DOM as-is when executed.
Internet Archive/ArchiveTeam also worked on that particular problem for a very long time, and are mostly successful as far as I can tell.
Ultimately, this is the best viewer I've found so far: https://replayweb.page/
It looks like there's a lot of tools for creating them, but not a a lot for viewing.
What they really need is browser support, or at least an extension so a browser can open the files directly.
> Although Safari is only maintained by Apple, the Safari webarchive format can be read by non-Apple tools – it’s a binary property list that stores the raw bytes of the original files. I’m comfortable that I’ll be able to open these archives for a while, even if Safari unexpectedly goes away.
It’s also relatable to find answers to Swift documentation questions in some talk by an Apple insider rather than in Apple’s official documentation.
> Once I’d written the initial version of this script and put all the pieces together, I used it to create webarchives for 6000 or so bookmarks in my Pinboard account. It worked pretty well, and captured 85% of my bookmarks – the remaining 15% are broken due to link rot. I did a spot check of a few dozen archives that did get saved, and they all look good.
I was a tad confused by this part.
Did you (or how did you) verify that the headlessly saved web archives for thousands of bookmarks visually match the pages shown in the browser?
This is the biggest problem I've had with command-line archival tools: they save some version of the page, but it often differs substantially from what I actually see in my browser — things like pop-up artifacts covering the page or news articles are full of ads that are otherwise blocked in my headed browser.
The SingleFile extension for Chrome works more completely and accurately than anything else I've come across so far, but it does still break weirdly sometimes too.
I would love to find a programmatic way to automate the visual verification, e.g., archiving a page with multiple different tools and visually diffing the rendered pages across tools with small margins of error. Maybe someone else has worked on this already.
I could find no pre-existing WebArchive package for PHP so I set forth building my own.
It's ancient and creaky, but it got the job done
> In February 2013, a vulnerability with the webarchive format was discovered and reported by Joe Vennix, a Metasploit Project developer. The exploit allows an attacker to send a crafted webarchive to a user containing code to access cookies, local files, and other data. Apple's response to the report was that it will not fix the bug, most likely because it requires action on the users' part in opening the file.
It feels weird me to dismiss as wontfix a security issue that gives the archived page far greater access to browser data than it has loaded at its original URL.
> Last updated at Tue, 16 Jan 2024 16:26:37 GMT
> tldr: For now, don't open .webarchive files, and check the Metasploit module, Apple Safari .webarchive File Format UXSS
> Safari's webarchive format saves all the resources in a web page - images, scripts, stylesheets - into a single file. A flaw exists in the security model behind webarchives that allows us to execute script in the context of any domain (a Universal Cross-site Scripting bug). In order to exploit this vulnerability, an attacker must somehow deliver the webarchive file to the victim and have the victim manually open it ^1 (e.g. through email or a forced download), after ignoring a potential "this content was downloaded from a webpage" warning message ^2.
Just look at the number of (relatively trivial) attack vectors identified by the author in this post:
> Attack Vector #1: Steal the user's cookies.
> Attack Vector #2: Steal CSRF tokens.
> Attack Vector #3: Steal local files.
> Attack Vector #4: Steal saved form passwords.
> Attack Vector #5: Store poisoned javascript in the user's cache.
https://www.rapid7.com/blog/post/2013/04/25/abusing-safaris-...
But having any other open/known file format (like an all-containing HTML/webarchive) will do the same trick just fine.
[1]: https://anybox.app/
Of course, without a working implementation, there could be hidden obstacles.
https://github.com/gildas-lormeau/SingleFile?tab=readme-ov-f...
About 10 years ago I searched and found a webarchive to MHTML converter from someone's small site. I recall there was one caveat, something like it didn't include the date in the metadata of the output MHTML.
Sorry I don't have the link on-hand, it'd be on the HDD I pulled from my old Macbook.
Edit: found an alternative script from my notes: https://blog.iefdev.se/2016/01/converting-webarchive-to-mht/
Edit 2: pretty sure this is the original converter, since I noted a caveat that it outputs the timestamp of the conversion not the time the file was originally saved (the script above handles this correctly otoh). https://langui.net/webarchive-to-mht/
At least last time I checked in only worked for local paths (file://) and only some browsers. Otherwise it would either try to download or just show plain text.
I ended up using Chrome to dump to MHTML [1] and then reshuffle the content into individual files, rewriting the path references and fixing mime types [2]. That gives a reasonably faithful static capture of a page that can be shared as link.
[1] https://chromedevtools.github.io/devtools-protocol/tot/Page/... [2] https://github.com/lacop/udrb/blob/master/app/src/chrome/mod...
Example I just tried and worked for me:
$ chrome.exe --headless --no-sandbox --disable-gpu --print-to-pdf=page.pdf https://news.ycombinator.com
Taken from: https://circumeo.io/blog/entry/html-to-pdf-using-python-and-...
I know you can specify cookies, existing sessions and existing profiles in case authentication is required.
HTML is also easier to fix is something gets in the way of content.
Is this normal?
I print with zero page margins, so in viewer it seems like continuous page. I found Firefox produced smallest pdfs. Chrome embeds fonts and other stuff. I also use UBlock rules to hide some elements.
Pretty useful for archiving discussions on Reddit.
What I do now is save the comments within the discussions (on HN, Reddit, Twitter etc) as text which is indexable and searchable with additional metadata which helps for filtering (author is the main one I use), while automatically archiving the entire URLs associated with them.[1]
For me, this is the best of both worlds - quick access via fault-tolerant search and filtering to the most interesting stuff while having a snapshot archive for the full context.
[1]: https://notado.app - I've been working on this for a few years now and have posted a lot in my HN comment history and technical blogs about how I have iterated on and evolved this workflow to the point where it is now
But the big flaw in this, especially for saving programming related pages, is that it loses the parts of scrollable content which is not currently in view, e.g. the ends of lines in a code block.
I use SingleFile for that, saves pages as a single self-contained html file. That way you can still interact with collapse comment buttons and outlinks.
- Outputs a single .html file, consumable with any web browser on any platform
- Rust, so, you know, +1 HN (;D)