{"@attributes":{"version":"2.0"},"channel":{"title":"DEV Community: K M Shahriar Hossain","description":"The latest articles on DEV Community by K M Shahriar Hossain (@devshakib).","link":"https:\/\/dev.to\/devshakib","image":{"url":"https:\/\/media2.dev.to\/dynamic\/image\/width=90,height=90,fit=cover,gravity=auto,format=auto\/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4106725%2Fb3e12c6b-18f9-4a00-a919-bd5fd68ee09d.jpg","title":"DEV Community: K M Shahriar Hossain","link":"https:\/\/dev.to\/devshakib"},"language":"en","item":[{"title":"The Same Seed Picked Minute 13 on the Phone and 18 on the Web","pubDate":"Mon, 28 Sep 2026 18:24:21 +0000","link":"https:\/\/dev.to\/devshakib\/the-same-seed-picked-minute-13-on-the-phone-and-18-on-the-web-4o6i","guid":"https:\/\/dev.to\/devshakib\/the-same-seed-picked-minute-13-on-the-phone-and-18-on-the-web-4o6i","description":"<p>A dev.to thread last week was about daily reminders: fire one at a<br>\nrandom minute each afternoon, but make the minute the same for a given day, so<br>\nthe schedule can be rebuilt as often as you like without a reminder appearing<br>\nto move. The standard trick is a seeded generator:<br>\n<\/p>\n\n<div class=\"highlight js-code-highlight\">\n<pre class=\"highlight dart\"><code><span class=\"kd\">final<\/span> <span class=\"n\">minute<\/span> <span class=\"o\">=<\/span> <span class=\"n\">Random<\/span><span class=\"p\">(<\/span><span class=\"s\">'2026-09-29'<\/span><span class=\"o\">.<\/span><span class=\"na\">hashCode<\/span><span class=\"p\">)<\/span><span class=\"o\">.<\/span><span class=\"na\">nextInt<\/span><span class=\"p\">(<\/span><span class=\"mi\">60<\/span><span class=\"p\">);<\/span>\n<\/code><\/pre>\n\n<\/div>\n\n\n\n<p>Same date, same seed, same minute. I compiled that one line four ways on Dart<br>\n3.13.4 and ran each on a Mac: the native builds directly, the JavaScript and<br>\nWasm output under Node, and later all of it again in Chrome.<\/p>\n\n<div class=\"table-wrapper-paragraph\"><table>\n<thead>\n<tr>\n<th>compiler<\/th>\n<th>where it runs<\/th>\n<th>minute<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>AOT (<code>dart compile exe<\/code>)<\/td>\n<td>Android and iOS release builds<\/td>\n<td><strong>13<\/strong><\/td>\n<\/tr>\n<tr>\n<td>JIT (<code>dart run<\/code>)<\/td>\n<td>debug builds, tests<\/td>\n<td><strong>13<\/strong><\/td>\n<\/tr>\n<tr>\n<td>dart2wasm<\/td>\n<td>Flutter web built with <code>--wasm<\/code>\n<\/td>\n<td><strong>13<\/strong><\/td>\n<\/tr>\n<tr>\n<td>dart2js<\/td>\n<td>Flutter web, the default build<\/td>\n<td><strong>18<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table><\/div>\n\n<p>The phone and the website disagree about the date.<\/p>\n<h2>\n  \n  \n  Random isn't the problem\n<\/h2>\n\n<p>The first suspect is <code>Random<\/code>, and it's innocent. <code>Random(42)<\/code> produced the<br>\nsame sequence, <code>587, 758, 4, 885, 77<\/code>, under all four compilers, and so did<br>\nevery seed I tried from \u22122\u00b3\u00b9\u22121 up to 2\u00b3\u00b3, along with <code>List.shuffle(Random(7))<\/code>.<\/p>\n\n<p>What changed was the seed. <code>'2026-09-29'.hashCode<\/code> is <code>160143713<\/code> on the VM<br>\nand on Wasm, and <code>374027133<\/code> when compiled to JavaScript. It isn't only<br>\nstrings:<\/p>\n\n<div class=\"table-wrapper-paragraph\"><table>\n<thead>\n<tr>\n<th>expression<\/th>\n<th>VM, AOT<\/th>\n<th>dart2wasm<\/th>\n<th>dart2js<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>'2026-09-29'.hashCode<\/code><\/td>\n<td>160143713<\/td>\n<td>160143713<\/td>\n<td>374027133<\/td>\n<\/tr>\n<tr>\n<td><code>7.hashCode<\/code><\/td>\n<td>81207<\/td>\n<td>81207<\/td>\n<td>7<\/td>\n<\/tr>\n<tr>\n<td><code>true.hashCode<\/code><\/td>\n<td>1231<\/td>\n<td>1231<\/td>\n<td>519018<\/td>\n<\/tr>\n<tr>\n<td><code>3.5.hashCode<\/code><\/td>\n<td>3377700795056128<\/td>\n<td>786432<\/td>\n<td>349197722<\/td>\n<\/tr>\n<tr>\n<td><code>DateTime.utc(2026, 9, 29).hashCode<\/code><\/td>\n<td>487035471<\/td>\n<td>487035471<\/td>\n<td>518150374<\/td>\n<\/tr>\n<tr>\n<td><code>Object.hash(1, 2)<\/code><\/td>\n<td>different every run<\/td>\n<td>different every run<\/td>\n<td>different every run<\/td>\n<\/tr>\n<\/tbody>\n<\/table><\/div>\n\n<p>None of this is a bug. <code>Object.hashCode<\/code> is documented as something that \"need<br>\nnot be consistent between executions of the same program\", and <code>Object.hash<\/code><br>\nsays outright that it may depend on values that change on each run, and every<br>\nrun here gave a new number. A hash code exists to put things in a<br>\n<code>Map<\/code> during one execution. It was never a fingerprint.<\/p>\n\n<p>What makes it slip through is that the VM happens to be stable for strings:<br>\nsame value every run, so every test on your machine and every run on your<br>\nphone agrees with itself. <code>'a'.hashCode<\/code> even matches on all three. The<br>\ndifference only shows up on the build you probably tested least.<\/p>\n<h2>\n  \n  \n  So hash it yourself \u2014 and it breaks again\n<\/h2>\n\n<p>The obvious fix is to stop borrowing <code>hashCode<\/code> and hash the bytes. FNV-1a is<br>\na few lines:<br>\n<\/p>\n\n<div class=\"highlight js-code-highlight\">\n<pre class=\"highlight dart\"><code><span class=\"kt\">int<\/span> <span class=\"nf\">fnv1a<\/span><span class=\"p\">(<\/span><span class=\"kt\">String<\/span> <span class=\"n\">s<\/span><span class=\"p\">)<\/span> <span class=\"p\">{<\/span>\n  <span class=\"kd\">var<\/span> <span class=\"n\">h<\/span> <span class=\"o\">=<\/span> <span class=\"mh\">0x811c9dc5<\/span><span class=\"p\">;<\/span>\n  <span class=\"k\">for<\/span> <span class=\"p\">(<\/span><span class=\"kd\">final<\/span> <span class=\"n\">b<\/span> <span class=\"k\">in<\/span> <span class=\"n\">utf8<\/span><span class=\"o\">.<\/span><span class=\"na\">encode<\/span><span class=\"p\">(<\/span><span class=\"n\">s<\/span><span class=\"p\">))<\/span> <span class=\"p\">{<\/span>\n    <span class=\"n\">h<\/span> <span class=\"o\">^=<\/span> <span class=\"n\">b<\/span><span class=\"p\">;<\/span>\n    <span class=\"n\">h<\/span> <span class=\"o\">=<\/span> <span class=\"p\">(<\/span><span class=\"n\">h<\/span> <span class=\"o\">*<\/span> <span class=\"mh\">0x01000193<\/span><span class=\"p\">)<\/span> <span class=\"o\">&amp;<\/span> <span class=\"mh\">0xffffffff<\/span><span class=\"p\">;<\/span>\n  <span class=\"p\">}<\/span>\n  <span class=\"k\">return<\/span> <span class=\"n\">h<\/span><span class=\"p\">;<\/span>\n<span class=\"p\">}<\/span>\n<\/code><\/pre>\n\n<\/div>\n\n\n\n<p>Phone: minute 7. dart2js: minute 24.<\/p>\n\n<p>On the web, a Dart <code>int<\/code> is a JavaScript number, and Dart's number<br>\ndocumentation spells out the two consequences. Integers \"lose precision past 53<br>\nbits\", and for bitwise operators \"the operands are truncated to 32-bit<br>\nintegers\". Here the multiply is the problem: a 32-bit hash times a 25-bit prime<br>\nis a 57-bit product, and the low bits, the only ones the mask keeps, are<br>\nexactly the ones JavaScript rounded away.<\/p>\n\n<p>Shifts fail the same way. <code>1 &lt;&lt; 40<\/code> is <code>1099511627776<\/code> on the phone and <code>0<\/code> on<br>\nthe web, so a seed built with a shift was never the same number in the first<br>\nplace.<\/p>\n<h2>\n  \n  \n  One web app, two answers\n<\/h2>\n\n<p>The table's last two rows are both Flutter web, and a <code>--wasm<\/code> build doesn't<br>\nchoose between them \u2014 the browser does. A <code>--wasm<\/code> build compiles to<br>\nJavaScript as well, and Flutter's loader decides which one to start. In<br>\nFlutter 3.47, <code>flutter.js<\/code> ships a default allow-list of<br>\n<code>{blink: true, gecko: false, webkit: false}<\/code>: Chrome and Edge get the Wasm<br>\nbuild, and Safari, Firefox and every browser on iOS get the JavaScript one.<\/p>\n\n<p>So the same deploy, at the same URL, picks minute 13 for someone in Chrome and<br>\nminute 18 for someone on an iPhone. Without <code>--wasm<\/code> it's 18 for everyone on<br>\nthe web and 13 in the app, which is at least consistent within each.<\/p>\n\n<p>That matters well beyond reminders: A\/B bucketing by user id, a daily puzzle<br>\nseeded by the date, procedurally generated levels, shuffled quiz order, a<br>\ncache key or shard derived from a string. Anything where two devices are meant<br>\nto agree.<\/p>\n<h2>\n  \n  \n  What held\n<\/h2>\n\n<p>Two versions gave the same answer under all four compilers.<\/p>\n\n<p><strong>A real hash from <code>package:crypto<\/code><\/strong>, which does its arithmetic in 32-bit<br>\npieces on purpose:<br>\n<\/p>\n\n<div class=\"highlight js-code-highlight\">\n<pre class=\"highlight dart\"><code><span class=\"kt\">int<\/span> <span class=\"nf\">stableHash<\/span><span class=\"p\">(<\/span><span class=\"kt\">String<\/span> <span class=\"n\">s<\/span><span class=\"p\">)<\/span> <span class=\"p\">{<\/span>\n  <span class=\"kd\">final<\/span> <span class=\"n\">d<\/span> <span class=\"o\">=<\/span> <span class=\"n\">sha256<\/span><span class=\"o\">.<\/span><span class=\"na\">convert<\/span><span class=\"p\">(<\/span><span class=\"n\">utf8<\/span><span class=\"o\">.<\/span><span class=\"na\">encode<\/span><span class=\"p\">(<\/span><span class=\"n\">s<\/span><span class=\"p\">))<\/span><span class=\"o\">.<\/span><span class=\"na\">bytes<\/span><span class=\"p\">;<\/span>\n  <span class=\"k\">return<\/span> <span class=\"p\">(<\/span><span class=\"n\">d<\/span><span class=\"p\">[<\/span><span class=\"mi\">0<\/span><span class=\"p\">]<\/span> <span class=\"p\">&lt;&lt;<\/span> <span class=\"mi\">24<\/span> <span class=\"o\">|<\/span> <span class=\"n\">d<\/span><span class=\"p\">[<\/span><span class=\"mi\">1<\/span><span class=\"p\">]<\/span> <span class=\"p\">&lt;&lt;<\/span> <span class=\"mi\">16<\/span> <span class=\"o\">|<\/span> <span class=\"n\">d<\/span><span class=\"p\">[<\/span><span class=\"mi\">2<\/span><span class=\"p\">]<\/span> <span class=\"p\">&lt;&lt;<\/span> <span class=\"mi\">8<\/span> <span class=\"o\">|<\/span> <span class=\"n\">d<\/span><span class=\"p\">[<\/span><span class=\"mi\">3<\/span><span class=\"p\">])<\/span> <span class=\"o\">&amp;<\/span> <span class=\"mh\">0x7fffffff<\/span><span class=\"p\">;<\/span>\n<span class=\"p\">}<\/span>\n<\/code><\/pre>\n\n<\/div>\n\n\n\n<p><strong>Or FNV-1a with every intermediate kept under 2\u2075\u00b3<\/strong>, by multiplying the low<br>\nand high halves separately:<br>\n<\/p>\n\n<div class=\"highlight js-code-highlight\">\n<pre class=\"highlight dart\"><code><span class=\"kt\">int<\/span> <span class=\"nf\">fnv1a<\/span><span class=\"p\">(<\/span><span class=\"kt\">String<\/span> <span class=\"n\">s<\/span><span class=\"p\">)<\/span> <span class=\"p\">{<\/span>\n  <span class=\"kd\">var<\/span> <span class=\"n\">h<\/span> <span class=\"o\">=<\/span> <span class=\"mh\">0x811c9dc5<\/span><span class=\"p\">;<\/span>\n  <span class=\"k\">for<\/span> <span class=\"p\">(<\/span><span class=\"kd\">final<\/span> <span class=\"n\">b<\/span> <span class=\"k\">in<\/span> <span class=\"n\">utf8<\/span><span class=\"o\">.<\/span><span class=\"na\">encode<\/span><span class=\"p\">(<\/span><span class=\"n\">s<\/span><span class=\"p\">))<\/span> <span class=\"p\">{<\/span>\n    <span class=\"n\">h<\/span> <span class=\"o\">=<\/span> <span class=\"p\">(<\/span><span class=\"n\">h<\/span> <span class=\"o\">^<\/span> <span class=\"n\">b<\/span><span class=\"p\">)<\/span> <span class=\"o\">&amp;<\/span> <span class=\"mh\">0xffffffff<\/span><span class=\"p\">;<\/span>\n    <span class=\"n\">h<\/span> <span class=\"o\">=<\/span> <span class=\"p\">((<\/span><span class=\"n\">h<\/span> <span class=\"o\">&amp;<\/span> <span class=\"mh\">0xffff<\/span><span class=\"p\">)<\/span> <span class=\"o\">*<\/span> <span class=\"mh\">0x01000193<\/span> <span class=\"o\">+<\/span>\n            <span class=\"p\">(((<\/span><span class=\"n\">h<\/span> <span class=\"p\">&gt;&gt;&gt;<\/span> <span class=\"mi\">16<\/span><span class=\"p\">)<\/span> <span class=\"o\">*<\/span> <span class=\"mh\">0x0193<\/span> <span class=\"o\">&amp;<\/span> <span class=\"mh\">0xffff<\/span><span class=\"p\">)<\/span> <span class=\"p\">&lt;&lt;<\/span> <span class=\"mi\">16<\/span><span class=\"p\">))<\/span> <span class=\"o\">&amp;<\/span>\n        <span class=\"mh\">0xffffffff<\/span><span class=\"p\">;<\/span>\n  <span class=\"p\">}<\/span>\n  <span class=\"k\">return<\/span> <span class=\"n\">h<\/span><span class=\"p\">;<\/span>\n<span class=\"p\">}<\/span>\n<\/code><\/pre>\n\n<\/div>\n\n\n\n<p>Then take the value from the hash, not from a generator seeded with it:<br>\n<\/p>\n\n<div class=\"highlight js-code-highlight\">\n<pre class=\"highlight dart\"><code><span class=\"kd\">final<\/span> <span class=\"n\">minute<\/span> <span class=\"o\">=<\/span> <span class=\"n\">stableHash<\/span><span class=\"p\">(<\/span><span class=\"n\">day<\/span><span class=\"p\">)<\/span> <span class=\"o\">%<\/span> <span class=\"mi\">60<\/span><span class=\"p\">;<\/span>\n<\/code><\/pre>\n\n<\/div>\n\n\n\n<p><code>Random(seed)<\/code> does agree across compilers today, but its own documentation<br>\nsays \"the implementation of the random stream can change between releases of<br>\nthe library.\" An SDK upgrade could move every reminder. A hash reduced with<br>\n<code>%<\/code> can't.<\/p>\n<h2>\n  \n  \n  Test it where it breaks\n<\/h2>\n\n<p>A golden value catches all of this, but only if the test runs on each<br>\ncompiler. Pin the answer once and run the same file three ways:<br>\n<\/p>\n\n<div class=\"highlight js-code-highlight\">\n<pre class=\"highlight dart\"><code><span class=\"n\">test<\/span><span class=\"p\">(<\/span><span class=\"s\">'the minute for a date is fixed'<\/span><span class=\"p\">,<\/span> <span class=\"p\">()<\/span> <span class=\"p\">{<\/span>\n  <span class=\"n\">expect<\/span><span class=\"p\">(<\/span><span class=\"n\">minuteFor<\/span><span class=\"p\">(<\/span><span class=\"s\">'2026-09-29'<\/span><span class=\"p\">),<\/span> <span class=\"mi\">53<\/span><span class=\"p\">);<\/span> <span class=\"c1\">\/\/ pinned from one VM run<\/span>\n<span class=\"p\">});<\/span>\n<\/code><\/pre>\n\n<\/div>\n\n\n\n\n\n<div class=\"highlight js-code-highlight\">\n<pre class=\"highlight shell\"><code>dart <span class=\"nb\">test<\/span> <span class=\"nt\">-p<\/span> vm\ndart <span class=\"nb\">test<\/span> <span class=\"nt\">-p<\/span> chrome\ndart <span class=\"nb\">test<\/span> <span class=\"nt\">-p<\/span> chrome <span class=\"nt\">-c<\/span> dart2wasm\n<\/code><\/pre>\n\n<\/div>\n\n\n\n<p>With <code>hashCode<\/code> or the first FNV-1a, the VM and Wasm runs pass and the Chrome<br>\nrun fails, with <code>Expected: &lt;53&gt;<\/code> and <code>Actual: &lt;33&gt;<\/code> for <code>hashCode<\/code>. With either<br>\nof the versions above, all three pass. <code>-p vm<\/code> alone passes everything, which<br>\nis how this ships.<\/p>\n\n\n\n\n<p><em>Originally published at <a href=\"https:\/\/devshakib.jumyn.com\/blog\/the-same-seed-picked-minute-13-on-the-phone-and-18-on-the-web\" rel=\"noopener noreferrer\">devshakib.jumyn.com<\/a>. I write about Flutter, Dart and the parts of shipping that are genuinely awkward \u2014 and publish the packages that came out of them at <a href=\"https:\/\/pub.dev\/publishers\/jumyn.com\/packages\" rel=\"noopener noreferrer\">pub.dev\/publishers\/jumyn.com<\/a>.<\/em><\/p>\n\n","category":["flutter","dart","webdev","testing"]},{"title":"Firestore Security Rules: Deny by Default, Then Open the Narrowest Hole","pubDate":"Thu, 24 Sep 2026 12:05:45 +0000","link":"https:\/\/dev.to\/devshakib\/firestore-security-rules-deny-by-default-then-open-the-narrowest-hole-4nnf","guid":"https:\/\/dev.to\/devshakib\/firestore-security-rules-deny-by-default-then-open-the-narrowest-hole-4nnf","description":"<p>Every few months someone opens an issue on a Firebase project along the lines of \"your API key is exposed in the JavaScript bundle.\" It isn't a vulnerability, and the panic is misplaced \u2014 but the instinct behind it is right, just aimed at the wrong thing. <strong>A Firebase Web API key is a project identifier, not a credential.<\/strong> It's supposed to be public. What people are actually reaching for when they panic about it is the real question: if anyone can point a Firestore client at my project, what stops them reading everything and writing whatever they like?<\/p>\n\n<p>The answer is one file. Not obfuscation, not a proxy, not \"nobody knows the collection names.\" Security rules are the <em>entire<\/em> perimeter, and they're the only part of the stack an attacker can't route around, because they execute on Google's servers before a single byte of your data moves. Everything else \u2014 your Flutter code, your admin panel's login screen, the <code>if (isAdmin)<\/code> check that hides the delete button \u2014 is client-side theatre that a curious person can bypass with the browser console and ten minutes.<\/p>\n\n<p>My portfolio runs 114 blog posts, 98 browser tools, 32 games and an admin panel on Firestore and Cloud Storage, and it costs me exactly $0 a month on the Blaze plan. That $0 is partly a rules property: there are no public writes to content anywhere in the schema, so there is no path by which an anonymous visitor can run up my bill. This post is the rules file I actually ship, section by section, with the reasoning behind each line \u2014 including the one behaviour that makes people think their rules are broken when they're working perfectly.<\/p>\n\n<h2>\n  \n  \n  Start with the deny, then open the narrowest hole\n<\/h2>\n\n<p>The mental model that keeps rules files honest is to write them backwards from how you'd naturally think about them. Don't start with \"the blog should be readable.\" Start with <strong>nothing is readable and nothing is writable<\/strong>, and then make yourself justify every hole you punch in it.<\/p>\n\n<p>Concretely, that means the last block in my file is this, and it was the first block I wrote:<br>\n<\/p>\n\n<div class=\"highlight js-code-highlight\">\n<pre class=\"highlight javascript\"><code><span class=\"c1\">\/\/ Default deny: anything not matched above is locked down<\/span>\n<span class=\"nx\">match<\/span> <span class=\"o\">\/<\/span><span class=\"p\">{<\/span><span class=\"nb\">document<\/span><span class=\"o\">=**<\/span><span class=\"p\">}<\/span> <span class=\"p\">{<\/span>\n  <span class=\"nx\">allow<\/span> <span class=\"nx\">read<\/span><span class=\"p\">,<\/span> <span class=\"na\">write<\/span><span class=\"p\">:<\/span> <span class=\"k\">if<\/span> <span class=\"kc\">false<\/span><span class=\"p\">;<\/span>\n<span class=\"p\">}<\/span>\n<\/code><\/pre>\n\n<\/div>\n\n\n\n<p>Firestore's default is already deny \u2014 an unmatched path is denied without you saying so. Writing it explicitly anyway matters for two reasons. First, it documents intent: the next person to open the file (which is me, eight months later) sees immediately that this is a closed-by-default schema and not an accidentally incomplete one. Second, it changes how you add things. With the deny at the bottom, adding a feature means consciously adding a <code>match<\/code> block above it and deciding on read and write separately. Without it, people tend to grow rules by loosening whatever's nearest.<\/p>\n\n<p>The order of operations I follow when a new collection shows up:<\/p>\n\n<ol>\n<li>Write the <code>match<\/code> block with nothing allowed.<\/li>\n<li>Ask who needs to read it \u2014 public, admin, or nobody \u2014 and open exactly that.<\/li>\n<li>Ask who needs to write it, which is almost always \"admin only.\"<\/li>\n<li>If the answer to step 3 is \"the public,\" write a validation predicate before you write the <code>allow<\/code>.<\/li>\n<\/ol>\n\n<p>Step 4 is where most of the real work is, and where most of the bugs live. I'll get to it.<\/p>\n\n<p>One structural note: rules do <strong>not<\/strong> cascade the way file permissions do. A <code>match \/posts\/{docId}<\/code> block does not grant anything to <code>\/posts\/{docId}\/comments\/{commentId}<\/code> \u2014 subcollections need their own match, or a recursive <code>{document=**}<\/code> wildcard. This trips people in both directions: they assume a parent grant covers children (it doesn't), or they use a recursive wildcard to save typing and accidentally expose a subcollection they hadn't thought about. Be explicit.<\/p>\n\n<h2>\n  \n  \n  One admin, identified by an email claim\n<\/h2>\n\n<p>Here's the whole authorisation model for my site:<br>\n<\/p>\n\n<div class=\"highlight js-code-highlight\">\n<pre class=\"highlight javascript\"><code><span class=\"kd\">function<\/span> <span class=\"nf\">isAdmin<\/span><span class=\"p\">()<\/span> <span class=\"p\">{<\/span>\n  <span class=\"k\">return<\/span> <span class=\"nx\">request<\/span><span class=\"p\">.<\/span><span class=\"nx\">auth<\/span> <span class=\"o\">!=<\/span> <span class=\"kc\">null<\/span>\n    <span class=\"o\">&amp;&amp;<\/span> <span class=\"nx\">request<\/span><span class=\"p\">.<\/span><span class=\"nx\">auth<\/span><span class=\"p\">.<\/span><span class=\"nx\">token<\/span><span class=\"p\">.<\/span><span class=\"nx\">email<\/span> <span class=\"o\">==<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">admin@yourdomain.com<\/span><span class=\"dl\">'<\/span><span class=\"p\">;<\/span>\n<span class=\"p\">}<\/span>\n<\/code><\/pre>\n\n<\/div>\n\n\n\n<p>That's it. One function, one address, no roles collection, no permissions document, no membership lookup. Every write in the entire schema goes through it.<\/p>\n\n<p>The reason this is safe is worth stating precisely, because it looks flimsy at first glance: <strong>the email in <code>request.auth.token<\/code> is not client-supplied.<\/strong> It's a claim inside a Firebase ID token, signed by Google, and verified server-side before the rule ever runs. You can't set it from the client any more than you can set your own bank balance. To make <code>isAdmin()<\/code> return true, an attacker has to actually authenticate as that account \u2014 which means having the password, or the second factor if you've enabled one. The email address itself isn't a secret and doesn't need protecting; knowing it buys you nothing.<\/p>\n\n<p>There's a cheaper reason to like it too: it costs <strong>zero document reads<\/strong>. A role-collection approach \u2014 <code>get(\/databases\/$(database)\/documents\/roles\/$(request.auth.uid)).data.admin == true<\/code> \u2014 does a real Firestore read on every single rule evaluation, and those are billable. On a site whose entire purpose is to cost nothing, that's a design constraint, not a micro-optimisation.<\/p>\n\n<h3>\n  \n  \n  Where this is the wrong answer\n<\/h3>\n\n<p>I want to be honest about the trade, because \"single admin identified by email\" is genuinely bad advice above a team size of one.<\/p>\n\n<p>The moment there are two admins, you're editing a rules file and running a deploy to grant access, which is a terrible way to manage people. It doesn't revoke cleanly, it has no audit trail, it can't express \"this person can publish posts but not delete apps,\" and the rules file becomes a personnel record living in git history forever.<\/p>\n\n<p><strong>The right answer at team scale is custom claims<\/strong>: <code>request.auth.token.admin == true<\/code>, or <code>request.auth.token.role in ['editor', 'admin']<\/code>, set through the Firebase Admin SDK. Claims are still signed, still free to check, and they decouple identity from authorisation \u2014 you grant and revoke by running a script against a UID, not by deploying rules. The catch, and the reason I haven't done it, is that setting a custom claim requires somewhere privileged to run the Admin SDK, and I have deliberately no Cloud Functions in this project (they're the main way a \"free\" Firebase project stops being free). A one-off local Node script against a service account key works fine for a handful of users; a real team wants that wired into an onboarding flow.<\/p>\n\n<p>So: email claim for one person, custom claims the moment there are two. Role collections mostly when the permission model is genuinely dynamic and data-driven, and then accept that you're paying a read for every rule evaluation.<\/p>\n\n<p>One hardening note I'd add before enabling a second auth provider: check <code>request.auth.token.email_verified == true<\/code> alongside the address. With a single email\/password account it's redundant, but some identity providers will hand you a token carrying an email the user never proved they own, and the whole model rests on that address meaning something.<\/p>\n\n<h2>\n  \n  \n  Public read of published content only\n<\/h2>\n\n<p>Content collections split into two shapes. The always-public ones are trivial:<br>\n<\/p>\n\n<div class=\"highlight js-code-highlight\">\n<pre class=\"highlight javascript\"><code><span class=\"nx\">match<\/span> <span class=\"o\">\/<\/span><span class=\"nx\">profile<\/span><span class=\"o\">\/<\/span><span class=\"p\">{<\/span><span class=\"nx\">docId<\/span><span class=\"p\">}<\/span> <span class=\"p\">{<\/span>\n  <span class=\"nx\">allow<\/span> <span class=\"na\">read<\/span><span class=\"p\">:<\/span> <span class=\"k\">if<\/span> <span class=\"kc\">true<\/span><span class=\"p\">;<\/span>\n  <span class=\"nx\">allow<\/span> <span class=\"na\">write<\/span><span class=\"p\">:<\/span> <span class=\"k\">if<\/span> <span class=\"nf\">isAdmin<\/span><span class=\"p\">();<\/span>\n<span class=\"p\">}<\/span>\n<\/code><\/pre>\n\n<\/div>\n\n\n\n<p>The ones with drafts need a predicate:<br>\n<\/p>\n\n<div class=\"highlight js-code-highlight\">\n<pre class=\"highlight javascript\"><code><span class=\"kd\">function<\/span> <span class=\"nf\">isPublished<\/span><span class=\"p\">()<\/span> <span class=\"p\">{<\/span>\n  <span class=\"k\">return<\/span> <span class=\"nx\">resource<\/span><span class=\"p\">.<\/span><span class=\"nx\">data<\/span><span class=\"p\">.<\/span><span class=\"nf\">get<\/span><span class=\"p\">(<\/span><span class=\"dl\">'<\/span><span class=\"s1\">status<\/span><span class=\"dl\">'<\/span><span class=\"p\">,<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">draft<\/span><span class=\"dl\">'<\/span><span class=\"p\">)<\/span> <span class=\"o\">==<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">published<\/span><span class=\"dl\">'<\/span><span class=\"p\">;<\/span>\n<span class=\"p\">}<\/span>\n\n<span class=\"nx\">match<\/span> <span class=\"o\">\/<\/span><span class=\"nx\">posts<\/span><span class=\"o\">\/<\/span><span class=\"p\">{<\/span><span class=\"nx\">docId<\/span><span class=\"p\">}<\/span> <span class=\"p\">{<\/span>\n  <span class=\"nx\">allow<\/span> <span class=\"na\">read<\/span><span class=\"p\">:<\/span> <span class=\"k\">if<\/span> <span class=\"nf\">isPublished<\/span><span class=\"p\">()<\/span> <span class=\"o\">||<\/span> <span class=\"nf\">isAdmin<\/span><span class=\"p\">();<\/span>\n  <span class=\"nx\">allow<\/span> <span class=\"nx\">create<\/span><span class=\"p\">,<\/span> <span class=\"nx\">update<\/span><span class=\"p\">,<\/span> <span class=\"na\">delete<\/span><span class=\"p\">:<\/span> <span class=\"k\">if<\/span> <span class=\"nf\">isAdmin<\/span><span class=\"p\">();<\/span>\n<span class=\"p\">}<\/span>\n<\/code><\/pre>\n\n<\/div>\n\n\n\n<p>Two details in there earn their place. <code>resource<\/code> (no <code>request.<\/code> prefix) is <strong>the document as it currently exists in the database<\/strong> \u2014 this is a read rule, so we're inspecting stored data, not incoming data. And <code>.get('status', 'draft')<\/code> supplies a default, so a legacy document that predates the <code>status<\/code> field is treated as a draft rather than blowing up the rule. Defaulting to the <em>closed<\/em> state is the right instinct: if the data is ambiguous, deny.<\/p>\n\n<p>Splitting <code>write<\/code> into <code>create, update, delete<\/code> rather than using the <code>write<\/code> shorthand is deliberate too. They're identical here, but when I later needed different logic for create versus update on the leads collection, the block was already shaped to take it.<\/p>\n\n<h3>\n  \n  \n  The list-query behaviour that looks exactly like a bug\n<\/h3>\n\n<p>Now the part that generates the most confused Stack Overflow questions in the whole Firebase ecosystem.<\/p>\n\n<p>With <code>allow read: if isPublished()<\/code> in place, this query fails with <code>permission-denied<\/code>:<br>\n<\/p>\n\n<div class=\"highlight js-code-highlight\">\n<pre class=\"highlight dart\"><code><span class=\"n\">FirebaseFirestore<\/span><span class=\"o\">.<\/span><span class=\"na\">instance<\/span><span class=\"o\">.<\/span><span class=\"na\">collection<\/span><span class=\"p\">(<\/span><span class=\"s\">'posts'<\/span><span class=\"p\">)<\/span><span class=\"o\">.<\/span><span class=\"na\">get<\/span><span class=\"p\">();<\/span>\n<\/code><\/pre>\n\n<\/div>\n\n\n\n<p>Every single document in that collection might be published. It still fails. And the first time it happens it looks completely broken \u2014 you can fetch an individual published post by ID and it works, but listing the collection doesn't.<\/p>\n\n<p>The reason is a rule Firebase states plainly and everyone forgets: <strong>security rules are not filters.<\/strong> The server does not run your query, fetch the matching documents, evaluate the rule against each one, and quietly drop the failures. That would be a query-cost and information-leak problem, and it would make query latency depend on how much data you can't see. Instead, Firestore evaluates the rule against <em>the query itself<\/em> and demands that the query is provably safe before it runs. An unconstrained <code>collection('posts')<\/code> could match an unpublished draft, therefore the whole request is denied.<\/p>\n\n<p><code>allow read<\/code> covers two operations \u2014 <code>get<\/code> (one document by ID) and <code>list<\/code> (a query). Fetching a published post by ID satisfies the rule because there's a concrete <code>resource<\/code> to evaluate. A <code>list<\/code> has no single <code>resource<\/code>, so Firestore checks whether your query's constraints guarantee the rule holds for everything it could return.<\/p>\n\n<p>The fix is to make the constraint explicit in the query:<br>\n<\/p>\n\n<div class=\"highlight js-code-highlight\">\n<pre class=\"highlight dart\"><code><span class=\"n\">FirebaseFirestore<\/span><span class=\"o\">.<\/span><span class=\"na\">instance<\/span>\n    <span class=\"o\">.<\/span><span class=\"na\">collection<\/span><span class=\"p\">(<\/span><span class=\"s\">'posts'<\/span><span class=\"p\">)<\/span>\n    <span class=\"o\">.<\/span><span class=\"na\">where<\/span><span class=\"p\">(<\/span><span class=\"s\">'status'<\/span><span class=\"p\">,<\/span> <span class=\"nl\">isEqualTo:<\/span> <span class=\"s\">'published'<\/span><span class=\"p\">)<\/span>\n    <span class=\"o\">.<\/span><span class=\"na\">orderBy<\/span><span class=\"p\">(<\/span><span class=\"s\">'publishedAt'<\/span><span class=\"p\">,<\/span> <span class=\"nl\">descending:<\/span> <span class=\"kc\">true<\/span><span class=\"p\">)<\/span>\n    <span class=\"o\">.<\/span><span class=\"na\">limit<\/span><span class=\"p\">(<\/span><span class=\"mi\">20<\/span><span class=\"p\">)<\/span>\n    <span class=\"o\">.<\/span><span class=\"na\">get<\/span><span class=\"p\">();<\/span>\n<\/code><\/pre>\n\n<\/div>\n\n\n\n<p>Now the query itself proves that every document it can return satisfies <code>isPublished()<\/code>, and it's allowed. <strong>The denial was correct behaviour and the client was wrong<\/strong> \u2014 which is exactly the shape of a good security control, and exactly why it feels like a bug.<\/p>\n\n<p>Two practical consequences. First, your admin panel and your public site need genuinely different query code, not the same call with a different UI. Second, that <code>where<\/code> plus <code>orderBy<\/code> combination needs a composite index; mine lives in <code>firestore.indexes.json<\/code> as <code>status<\/code> ascending plus <code>publishedAt<\/code> descending, with a second one adding <code>tags<\/code> array-contains for the tag pages. You'll find this out from a console error with a link that creates it for you, but it's worth knowing that <strong>a rule requiring a filter implies an index requiring deployment<\/strong> \u2014 the two are coupled, and forgetting the second one breaks production while the emulator (which builds indexes on the fly) stays green.<\/p>\n\n<h2>\n  \n  \n  Validating request.resource on write\n<\/h2>\n\n<p><code>resource<\/code> is what's in the database. <strong><code>request.resource<\/code> is what the client is trying to put there.<\/strong> Read rules inspect the first; write rules inspect the second. Mixing them up is the single most common rules bug I see, and it fails in the dangerous direction \u2014 a <code>create<\/code> has no existing <code>resource<\/code>, so a rule that references <code>resource.data<\/code> on create evaluates against null and denies, which you'll notice; but a rule that checks <code>resource.data.ownerId<\/code> on <em>update<\/em> is checking the stored owner, not the incoming one, which may or may not be what you meant.<\/p>\n\n<p>For anything the public can write, the rule needs to constrain the incoming document hard. My contact form is the only such path on the site, and it looks like this:<br>\n<\/p>\n\n<div class=\"highlight js-code-highlight\">\n<pre class=\"highlight javascript\"><code><span class=\"nx\">match<\/span> <span class=\"o\">\/<\/span><span class=\"nx\">leads<\/span><span class=\"o\">\/<\/span><span class=\"p\">{<\/span><span class=\"nx\">docId<\/span><span class=\"p\">}<\/span> <span class=\"p\">{<\/span>\n  <span class=\"nx\">allow<\/span> <span class=\"na\">create<\/span><span class=\"p\">:<\/span> <span class=\"k\">if<\/span>\n    <span class=\"nx\">request<\/span><span class=\"p\">.<\/span><span class=\"nx\">resource<\/span><span class=\"p\">.<\/span><span class=\"nx\">data<\/span><span class=\"p\">.<\/span><span class=\"nf\">keys<\/span><span class=\"p\">().<\/span><span class=\"nf\">hasOnly<\/span><span class=\"p\">(<\/span>\n      <span class=\"p\">[<\/span><span class=\"dl\">'<\/span><span class=\"s1\">name<\/span><span class=\"dl\">'<\/span><span class=\"p\">,<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">email<\/span><span class=\"dl\">'<\/span><span class=\"p\">,<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">subject<\/span><span class=\"dl\">'<\/span><span class=\"p\">,<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">message<\/span><span class=\"dl\">'<\/span><span class=\"p\">,<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">type<\/span><span class=\"dl\">'<\/span><span class=\"p\">,<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">budget<\/span><span class=\"dl\">'<\/span><span class=\"p\">,<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">company<\/span><span class=\"dl\">'<\/span><span class=\"p\">,<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">read<\/span><span class=\"dl\">'<\/span><span class=\"p\">,<\/span> <span class=\"dl\">'<\/span><span class=\"s1\">createdAt<\/span><span class=\"dl\">'<\/span><span class=\"p\">]<\/span>\n    <span class=\"p\">)<\/span>\n    <span class=\"o\">&amp;&amp;<\/span> <span class=\"nx\">request<\/span><span class=\"p\">.<\/span><span class=\"nx\">resource<\/span><span class=\"p\">.<\/span><span class=\"nx\">data<\/span><span class=\"p\">.<\/span><span class=\"nx\">name<\/span> <span class=\"nx\">is<\/span> <span class=\"nx\">string<\/span>\n    <span class=\"o\">&amp;&amp;<\/span> <span class=\"nx\">request<\/span><span class=\"p\">.<\/span><span class=\"nx\">resource<\/span><span class=\"p\">.<\/span><span class=\"nx\">data<\/span><span class=\"p\">.<\/span><span class=\"nx\">name<\/span><span class=\"p\">.<\/span><span class=\"nf\">size<\/span><span class=\"p\">()<\/span> <span class=\"o\">&gt;<\/span> <span class=\"mi\">0<\/span>\n    <span class=\"o\">&amp;&amp;<\/span> <span class=\"nx\">request<\/span><span class=\"p\">.<\/span><span class=\"nx\">resource<\/span><span class=\"p\">.<\/span><span class=\"nx\">data<\/span><span class=\"p\">.<\/span><span class=\"nx\">name<\/span><span class=\"p\">.<\/span><span class=\"nf\">size<\/span><span class=\"p\">()<\/span> <span class=\"o\">&lt;<\/span> <span class=\"mi\">120<\/span>\n    <span class=\"o\">&amp;&amp;<\/span> <span class=\"nf\">isValidEmail<\/span><span class=\"p\">(<\/span><span class=\"nx\">request<\/span><span class=\"p\">.<\/span><span class=\"nx\">resource<\/span><span class=\"p\">.<\/span><span class=\"nx\">data<\/span><span class=\"p\">.<\/span><span class=\"nx\">email<\/span><span class=\"p\">)<\/span>\n    <span class=\"o\">&amp;&amp;<\/span> <span class=\"nx\">request<\/span><span class=\"p\">.<\/span><span class=\"nx\">resource<\/span><span class=\"p\">.<\/span><span class=\"nx\">data<\/span><span class=\"p\">.<\/span><span class=\"nx\">message<\/span> <span class=\"nx\">is<\/span> <span class=\"nx\">string<\/span>\n    <span class=\"o\">&amp;&amp;<\/span> <span class=\"nx\">request<\/span><span class=\"p\">.<\/span><span class=\"nx\">resource<\/span><span class=\"p\">.<\/span><span class=\"nx\">data<\/span><span class=\"p\">.<\/span><span class=\"nx\">message<\/span><span class=\"p\">.<\/span><span class=\"nf\">size<\/span><span class=\"p\">()<\/span> <span class=\"o\">&gt;<\/span> <span class=\"mi\">0<\/span>\n    <span class=\"o\">&amp;&amp;<\/span> <span class=\"nx\">request<\/span><span class=\"p\">.<\/span><span class=\"nx\">resource<\/span><span class=\"p\">.<\/span><span class=\"nx\">data<\/span><span class=\"p\">.<\/span><span class=\"nx\">message<\/span><span class=\"p\">.<\/span><span class=\"nf\">size<\/span><span class=\"p\">()<\/span> <span class=\"o\">&lt;<\/span> <span class=\"mi\">5000<\/span>\n    <span class=\"o\">&amp;&amp;<\/span> <span class=\"nx\">request<\/span><span class=\"p\">.<\/span><span class=\"nx\">resource<\/span><span class=\"p\">.<\/span><span class=\"nx\">data<\/span><span class=\"p\">.<\/span><span class=\"nx\">read<\/span> <span class=\"o\">==<\/span> <span class=\"kc\">false<\/span>\n    <span class=\"o\">&amp;&amp;<\/span> <span class=\"nx\">request<\/span><span class=\"p\">.<\/span><span class=\"nx\">resource<\/span><span class=\"p\">.<\/span><span class=\"nx\">data<\/span><span class=\"p\">.<\/span><span class=\"nx\">createdAt<\/span> <span class=\"o\">==<\/span> <span class=\"nx\">request<\/span><span class=\"p\">.<\/span><span class=\"nx\">time<\/span><span class=\"p\">;<\/span>\n  <span class=\"nx\">allow<\/span> <span class=\"nx\">read<\/span><span class=\"p\">,<\/span> <span class=\"nx\">update<\/span><span class=\"p\">,<\/span> <span class=\"na\">delete<\/span><span class=\"p\">:<\/span> <span class=\"k\">if<\/span> <span class=\"nf\">isAdmin<\/span><span class=\"p\">();<\/span>\n<span class=\"p\">}<\/span>\n<\/code><\/pre>\n\n<\/div>\n\n\n\n<p>Four separate defences in there, each closing a specific hole.<\/p>\n\n<p><strong>The field whitelist.<\/strong> <code>keys().hasOnly([...])<\/code> is the most important line in the block. Without it, a client can post a perfectly valid contact form that <em>also<\/em> carries <code>{isAdmin: true}<\/code>, or <code>{read: true}<\/code> to hide itself from my inbox, or 400 junk fields. The rule schema and the app's model class drift apart the moment someone adds a field to one and not the other, and a whitelist makes that drift a failed write instead of silent data pollution. Note it's <code>hasOnly<\/code>, not <code>hasAll<\/code> \u2014 <code>hasAll<\/code> checks required keys, <code>hasOnly<\/code> forbids extras, and you frequently want both.<\/p>\n\n<p><strong>Size caps on every free-text field.<\/strong> A Firestore document can be just under 1 MiB. A <code>message<\/code> field with no cap means an anonymous visitor can write a megabyte per submission, as fast as they can loop. That's a storage bill and a bandwidth bill in my admin panel, and it's trivially scriptable. Every string a stranger can set gets a maximum. Note the optional fields get the same treatment \u2014 a field being optional doesn't mean it's safe if present:<br>\n<\/p>\n\n<div class=\"highlight js-code-highlight\">\n<pre class=\"highlight javascript\"><code><span class=\"o\">&amp;&amp;<\/span> <span class=\"p\">(<\/span><span class=\"o\">!<\/span><span class=\"p\">(<\/span><span class=\"dl\">'<\/span><span class=\"s1\">company<\/span><span class=\"dl\">'<\/span> <span class=\"k\">in<\/span> <span class=\"nx\">request<\/span><span class=\"p\">.<\/span><span class=\"nx\">resource<\/span><span class=\"p\">.<\/span><span class=\"nx\">data<\/span><span class=\"p\">)<\/span>\n    <span class=\"o\">||<\/span> <span class=\"p\">(<\/span><span class=\"nx\">request<\/span><span class=\"p\">.<\/span><span class=\"nx\">resource<\/span><span class=\"p\">.<\/span><span class=\"nx\">data<\/span><span class=\"p\">.<\/span><span class=\"nx\">company<\/span> <span class=\"nx\">is<\/span> <span class=\"nx\">string<\/span>\n        <span class=\"o\">&amp;&amp;<\/span> <span class=\"nx\">request<\/span><span class=\"p\">.<\/span><span class=\"nx\">resource<\/span><span class=\"p\">.<\/span><span class=\"nx\">data<\/span><span class=\"p\">.<\/span><span class=\"nx\">company<\/span><span class=\"p\">.<\/span><span class=\"nf\">size<\/span><span class=\"p\">()<\/span> <span class=\"o\">&lt;<\/span> <span class=\"mi\">200<\/span><span class=\"p\">))<\/span>\n<\/code><\/pre>\n\n<\/div>\n\n\n\n<p><strong>Server-controlled fields pinned to their initial value.<\/strong> <code>read == false<\/code> on create means a submission always lands unread; a client can't ship one pre-marked. Any field whose meaning is owned by your backend rather than the user needs pinning like this, or excluding from the whitelist entirely.<\/p>\n\n<p><strong>Never trusting client timestamps.<\/strong> <code>request.resource.data.createdAt == request.time<\/code> is my favourite line in the file. <code>request.time<\/code> is the server's clock at the moment of evaluation. The only way a client can satisfy that equality is by sending <code>FieldValue.serverTimestamp()<\/code> \u2014 the sentinel that tells Firestore to fill the value in server-side. Anything the client computes itself, including <code>DateTime.now()<\/code>, fails. <strong>This turns \"please use a server timestamp\" from a code-review convention into an enforced invariant.<\/strong> Without it, a spammer can backdate submissions to the bottom of my sorted inbox, and more generally, any ordering or expiry logic built on a client clock is built on a value the client fully controls.<\/p>\n\n<h2>\n  \n  \n  Storage: content type and size are the whole game\n<\/h2>\n\n<p>Cloud Storage rules follow the same shape, with two extra levers:<br>\n<\/p>\n\n<div class=\"highlight js-code-highlight\">\n<pre class=\"highlight javascript\"><code><span class=\"kd\">function<\/span> <span class=\"nf\">isAllowedUpload<\/span><span class=\"p\">()<\/span> <span class=\"p\">{<\/span>\n  <span class=\"k\">return<\/span> <span class=\"nx\">request<\/span><span class=\"p\">.<\/span><span class=\"nx\">resource<\/span><span class=\"p\">.<\/span><span class=\"nx\">size<\/span> <span class=\"o\">&lt;<\/span> <span class=\"mi\">10<\/span> <span class=\"o\">*<\/span> <span class=\"mi\">1024<\/span> <span class=\"o\">*<\/span> <span class=\"mi\">1024<\/span>\n    <span class=\"o\">&amp;&amp;<\/span> <span class=\"nx\">request<\/span><span class=\"p\">.<\/span><span class=\"nx\">resource<\/span><span class=\"p\">.<\/span><span class=\"nx\">contentType<\/span><span class=\"p\">.<\/span><span class=\"nf\">matches<\/span><span class=\"p\">(<\/span><span class=\"dl\">'<\/span><span class=\"s1\">image\/.*|application\/pdf<\/span><span class=\"dl\">'<\/span><span class=\"p\">);<\/span>\n<span class=\"p\">}<\/span>\n\n<span class=\"nx\">match<\/span> <span class=\"o\">\/<\/span><span class=\"kr\">public<\/span><span class=\"o\">\/<\/span><span class=\"p\">{<\/span><span class=\"nx\">allPaths<\/span><span class=\"o\">=**<\/span><span class=\"p\">}<\/span> <span class=\"p\">{<\/span>\n  <span class=\"nx\">allow<\/span> <span class=\"na\">read<\/span><span class=\"p\">:<\/span> <span class=\"k\">if<\/span> <span class=\"kc\">true<\/span><span class=\"p\">;<\/span>\n  <span class=\"nx\">allow<\/span> <span class=\"na\">write<\/span><span class=\"p\">:<\/span> <span class=\"k\">if<\/span> <span class=\"nf\">isAdmin<\/span><span class=\"p\">()<\/span> <span class=\"o\">&amp;&amp;<\/span> <span class=\"nf\">isAllowedUpload<\/span><span class=\"p\">();<\/span>\n  <span class=\"nx\">allow<\/span> <span class=\"na\">delete<\/span><span class=\"p\">:<\/span> <span class=\"k\">if<\/span> <span class=\"nf\">isAdmin<\/span><span class=\"p\">();<\/span>\n<span class=\"p\">}<\/span>\n\n<span class=\"nx\">match<\/span> <span class=\"o\">\/<\/span><span class=\"p\">{<\/span><span class=\"nx\">allPaths<\/span><span class=\"o\">=**<\/span><span class=\"p\">}<\/span> <span class=\"p\">{<\/span>\n  <span class=\"nx\">allow<\/span> <span class=\"nx\">read<\/span><span class=\"p\">,<\/span> <span class=\"na\">write<\/span><span class=\"p\">:<\/span> <span class=\"k\">if<\/span> <span class=\"kc\">false<\/span><span class=\"p\">;<\/span>\n<span class=\"p\">}<\/span>\n<\/code><\/pre>\n\n<\/div>\n\n\n\n<p><code>request.resource.size<\/code> is the byte count of the incoming object and <code>contentType<\/code> is its declared MIME type, and on a bucket where uploads are admin-only these are belt-and-braces \u2014 but they're the difference between a compromised admin session costing me an afternoon and costing me a bandwidth bill. On any bucket where users can upload, they are not optional.<\/p>\n\n<p>The caveat worth naming: <strong><code>contentType<\/code> is client-declared metadata, not a content inspection.<\/strong> Restricting to <code>image\/.*<\/code> stops someone casually uploading a 200 MB video; it does not stop someone uploading a disguised payload with an image content type. Rules can constrain the envelope, not the contents. If what's inside matters, you need processing behind the upload, and that's a different system.<\/p>\n\n<p>The structural decision that matters more than either rule: <strong>large binaries don't live in Storage at all.<\/strong> APKs and DMGs for my apps sit on GitHub Releases, and the site links out. Download bandwidth is the line item that turns a free Firebase project into a surprising invoice, and the cheapest way to control it is to not serve the large files.<\/p>\n\n<h2>\n  \n  \n  Rules are a security boundary, not a validation framework\n<\/h2>\n\n<p>Everything above might read as an argument for pushing all your validation into rules. It isn't, and the distinction is the most useful thing in this post.<\/p>\n\n<p>Rules are the right place for anything that <strong>must<\/strong> hold no matter who is calling \u2014 the field whitelist, the size caps, the server timestamp, \"only the admin writes here.\" These are properties an attacker must not be able to violate, so they belong on the server side of the trust boundary, and there is no other server side in a Firebase-only architecture.<\/p>\n\n<p>Rules are a bad place for everything else, and the constraints of the language tell you why. Rules Language has no loops, no regular-expression capture groups, no way to call out to anything, a hard limit on expression complexity, and a cap on <code>get()<\/code> calls per evaluation. You can't cross-validate two documents meaningfully. You get no error message back beyond <code>permission-denied<\/code>, so a user who fills a form wrong sees a generic failure with nothing actionable in it. And every additional <code>get()<\/code> you add to satisfy a business rule is a billable read on every single request.<\/p>\n\n<p>So the split I use:<\/p>\n\n<ul>\n<li>\n<strong>Business validation lives in the client<\/strong>, where you can give a real error message next to the offending field, in real time, before anything is sent.<\/li>\n<li>\n<strong>Security invariants live in rules<\/strong>, where they're unbypassable and where the error being unhelpful is fine, because the only people hitting it are misbehaving.<\/li>\n<li>Where they overlap \u2014 an email must be well-formed, a message must be non-empty \u2014 I write it in both places and accept the duplication. The client version is a user experience; the rules version is a guarantee.<\/li>\n<\/ul>\n\n<p>The last piece is testing, because a rules file is executable code that ships with no test coverage by default. <code>@firebase\/rules-unit-testing<\/code> against the emulator lets you assert both directions, and the negative assertions are the ones that matter:<br>\n<\/p>\n\n<div class=\"highlight js-code-highlight\">\n<pre class=\"highlight javascript\"><code><span class=\"k\">await<\/span> <span class=\"nf\">assertFails<\/span><span class=\"p\">(<\/span><span class=\"nx\">db<\/span><span class=\"p\">.<\/span><span class=\"nf\">collection<\/span><span class=\"p\">(<\/span><span class=\"dl\">'<\/span><span class=\"s1\">posts<\/span><span class=\"dl\">'<\/span><span class=\"p\">).<\/span><span class=\"nf\">get<\/span><span class=\"p\">());<\/span>              <span class=\"c1\">\/\/ unfiltered list denied<\/span>\n<span class=\"k\">await<\/span> <span class=\"nf\">assertSucceeds<\/span><span class=\"p\">(<\/span><span class=\"nf\">publishedPostsQuery<\/span><span class=\"p\">(<\/span><span class=\"nx\">db<\/span><span class=\"p\">).<\/span><span class=\"nf\">get<\/span><span class=\"p\">());<\/span>          <span class=\"c1\">\/\/ filtered list allowed<\/span>\n<span class=\"k\">await<\/span> <span class=\"nf\">assertFails<\/span><span class=\"p\">(<\/span><span class=\"nx\">db<\/span><span class=\"p\">.<\/span><span class=\"nf\">collection<\/span><span class=\"p\">(<\/span><span class=\"dl\">'<\/span><span class=\"s1\">leads<\/span><span class=\"dl\">'<\/span><span class=\"p\">).<\/span><span class=\"nf\">add<\/span><span class=\"p\">({<\/span> <span class=\"p\">...<\/span><span class=\"nx\">ok<\/span><span class=\"p\">,<\/span> <span class=\"na\">isAdmin<\/span><span class=\"p\">:<\/span> <span class=\"kc\">true<\/span> <span class=\"p\">}));<\/span>\n<\/code><\/pre>\n\n<\/div>\n\n\n\n<p>I run those with <code>firebase emulators:exec<\/code> before any rules deploy. Rules changes are the highest-blast-radius deploys in the whole project \u2014 one loosened predicate and your drafts are public \u2014 and they're also the easiest to get wrong, because the failure is invisible. Nothing errors. Nothing looks different. The data is just readable now.<\/p>\n\n<h2>\n  \n  \n  Key takeaways\n<\/h2>\n\n<ul>\n<li>\n<strong>Your Firebase API key is a project identifier, not a credential<\/strong> \u2014 everything in the client bundle is public by definition, which makes security rules the only real perimeter your data has.<\/li>\n<li>\n<strong>Write the deny-by-default rule first, then justify every hole you open<\/strong>, and split <code>write<\/code> into <code>create<\/code>\/<code>update<\/code>\/<code>delete<\/code> so the block is already shaped for the day the logic diverges.<\/li>\n<li>\n<strong>A single admin identified by a signed email claim is correct at team-size one<\/strong> and wrong the moment there are two \u2014 move to custom claims before you're editing a rules file to onboard a colleague.<\/li>\n<li>\n<strong>Security rules are not filters<\/strong>: an unfiltered collection query is denied even when every document in it would pass, because Firestore validates the query rather than the results \u2014 and the fix is a <code>where<\/code> clause plus the composite index it implies.<\/li>\n<li>\n<strong><code>resource<\/code> is what's stored and <code>request.resource<\/code> is what's incoming<\/strong> \u2014 write rules must constrain the second with a <code>hasOnly<\/code> field whitelist, size caps on every free-text field, and server-owned fields pinned to their initial values.<\/li>\n<li>\n<strong>Comparing a timestamp field to <code>request.time<\/code> forces the client to use <code>FieldValue.serverTimestamp()<\/code><\/strong>, turning \"don't trust client clocks\" from a convention into an invariant the server enforces.<\/li>\n<li>\n<strong>Rules are a security boundary, not a validation framework<\/strong> \u2014 put unbypassable invariants there, put helpful user-facing validation in the client, and test the denials against the emulator before every deploy.<\/li>\n<\/ul>\n\n<p>The rules file is the smallest, highest-leverage piece of code in a Firebase project. Mine is 192 lines and it's the only thing standing between an anonymous visitor and every document I own \u2014 and because it's server-side, it's the only part of my stack whose guarantees survive a determined person with the browser console open. Write the deny first. Open the narrowest possible hole. Constrain every incoming document by shape and by size. Let the server set the clock. And when a query comes back <code>permission-denied<\/code> and the data looks like it should have been readable, consider \u2014 before you loosen anything \u2014 that the rule might simply be right.<\/p>\n\n<p>Most of the mistakes above can be seen without running a single rule, so there is now a VS Code extension that looks for them as you type: <a href=\"https:\/\/devshakib.jumyn.com\/apps\/ds_rules\" rel=\"noopener noreferrer\">ds_rules<\/a> flags open writes, test mode left on, anyone-can-edit and unchecked uploads, and stays quiet about the public reads and admin writes you meant.<\/p>\n\n\n\n\n<p><em>Originally published at <a href=\"https:\/\/devshakib.jumyn.com\/blog\/firestore-security-rules-deny-by-default-then-open-the-narrowest-hole\" rel=\"noopener noreferrer\">devshakib.jumyn.com<\/a>. I write about Flutter, Dart and the parts of shipping that are genuinely awkward \u2014 and publish the packages that came out of them at <a href=\"https:\/\/pub.dev\/publishers\/jumyn.com\/packages\" rel=\"noopener noreferrer\">pub.dev\/publishers\/jumyn.com<\/a>.<\/em><\/p>\n\n","category":["security","firebase","webdev","programming"]},{"title":"Google Indexed the Blog and Nothing Else on My Flutter Web Site","pubDate":"Fri, 18 Sep 2026 13:47:25 +0000","link":"https:\/\/dev.to\/devshakib\/google-indexed-the-blog-and-nothing-else-on-my-flutter-web-site-2o7m","guid":"https:\/\/dev.to\/devshakib\/google-indexed-the-blog-and-nothing-else-on-my-flutter-web-site-2o7m","description":"<p>On 10 September, Search Console's verdict on this site was 18 pages indexed and<br>\n401 not. Four hundred of those sat in \"Discovered \u2014 currently not indexed\",<br>\nwhich is Google saying it knows the URL exists and hasn't bothered to fetch it.<\/p>\n\n<p>The breakdown was stranger than the count. Of the 23 pages that had ever<br>\nappeared in a search result, 22 were blog posts and the 23rd was the home page.<br>\nNot one app, tool, game or package page had a single impression, and <code>\/apps<\/code>,<br>\nthe page that links to 96 of them, read \"URL is unknown to Google\".<\/p>\n\n<p>\"Discovered \u2014 currently not indexed\" usually gets blamed on crawl budget or a<br>\nyoung domain. Neither explains why one kind of page gets in and every other<br>\nkind doesn't. Something about the blog was different.<\/p>\n<h2>\n  \n  \n  Flutter web draws text Google can't read\n<\/h2>\n\n<p>The site is Flutter web with the CanvasKit renderer, which paints everything,<br>\ntext included, onto a <code>&lt;canvas&gt;<\/code>. To a crawler, a canvas full of words is an<br>\nimage with no alt text.<\/p>\n\n<p>The usual answer is to ship the words separately. A build step<br>\n(<code>tool\/prerender.py<\/code> here) writes a real HTML file for every route, with the<br>\npage's text and links inside the served document:<br>\n<\/p>\n\n<div class=\"highlight js-code-highlight\">\n<pre class=\"highlight html\"><code><span class=\"nt\">&lt;div<\/span> <span class=\"na\">id=<\/span><span class=\"s\">\"prerendered-content\"<\/span><span class=\"nt\">&gt;<\/span>\n  <span class=\"nt\">&lt;h1&gt;<\/span>Apps<span class=\"nt\">&lt;\/h1&gt;<\/span>\n  <span class=\"nt\">&lt;p&gt;<\/span>\u2026<span class=\"nt\">&lt;\/p&gt;<\/span>\n  <span class=\"nt\">&lt;a<\/span> <span class=\"na\">href=<\/span><span class=\"s\">\"\/apps\/helm\"<\/span><span class=\"nt\">&gt;<\/span>Helm<span class=\"nt\">&lt;\/a&gt;<\/span> \u2026\n<span class=\"nt\">&lt;\/div&gt;<\/span>\n<\/code><\/pre>\n\n<\/div>\n\n\n\n<p>It's clipped to one pixel rather than hidden with <code>display: none<\/code>, so it stays<br>\nin the document for crawlers and screen readers without being seen. <code>curl<\/code> any<br>\nroute and the content is there. That check passed on every page from the day<br>\nit shipped.<\/p>\n<h2>\n  \n  \n  The part <code>curl<\/code> can't see\n<\/h2>\n\n<p>Google doesn't index the HTML the server sends. It runs the page's JavaScript<br>\nand indexes the DOM that results. For a Flutter app, that means <code>main.dart.js<\/code><br>\nboots and paints a first frame.<\/p>\n\n<p>And <code>index.html<\/code> had this, written alongside the prerendering:<br>\n<\/p>\n\n<div class=\"highlight js-code-highlight\">\n<pre class=\"highlight javascript\"><code><span class=\"nb\">window<\/span><span class=\"p\">.<\/span><span class=\"nf\">addEventListener<\/span><span class=\"p\">(<\/span><span class=\"dl\">'<\/span><span class=\"s1\">flutter-first-frame<\/span><span class=\"dl\">'<\/span><span class=\"p\">,<\/span> <span class=\"nf\">function <\/span><span class=\"p\">()<\/span> <span class=\"p\">{<\/span>\n  <span class=\"c1\">\/\/ ...fade out the loading screen...<\/span>\n  <span class=\"kd\">var<\/span> <span class=\"nx\">seo<\/span> <span class=\"o\">=<\/span> <span class=\"nb\">document<\/span><span class=\"p\">.<\/span><span class=\"nf\">getElementById<\/span><span class=\"p\">(<\/span><span class=\"dl\">'<\/span><span class=\"s1\">prerendered-content<\/span><span class=\"dl\">'<\/span><span class=\"p\">);<\/span>\n  <span class=\"k\">if <\/span><span class=\"p\">(<\/span><span class=\"nx\">seo<\/span><span class=\"p\">)<\/span> <span class=\"p\">{<\/span> <span class=\"nx\">seo<\/span><span class=\"p\">.<\/span><span class=\"nf\">remove<\/span><span class=\"p\">();<\/span> <span class=\"p\">}<\/span>\n<span class=\"p\">});<\/span>\n<\/code><\/pre>\n\n<\/div>\n\n\n\n<p>A comment in the stylesheet explained why: the crawler copy goes \"the moment<br>\nFlutter paints the same content properly\". That's true for a person looking at<br>\nthe screen. It isn't true for the DOM. Under CanvasKit, \"painted properly\" means<br>\npixels, so once the handler ran, the document held a canvas and nothing else:<br>\nzero words and zero links on every route.<\/p>\n\n<p>Every route except blog posts. Posts carry a second copy: when a post opens,<br>\nthe app adds its body to the document as a hidden <code>&lt;article&gt;<\/code>. The handler<br>\nremoved one element by id, and that wasn't it. That is the whole difference<br>\nbetween the 22 pages Google knew and the rest.<\/p>\n\n<p>It also explains \"Discovered\". Google had found the other pages through the<br>\nsitemap and nothing else. The hub pages that should have linked to them<br>\nrendered as empty dead ends, and a URL that only appears in a sitemap is about<br>\nthe lowest crawl priority there is. So they waited.<\/p>\n<h2>\n  \n  \n  Let the app keep the copy\n<\/h2>\n\n<p>The handler isn't wrong in general; the loading screen should go. The copy just<br>\nshouldn't go with it. So the app claims the block before its first frame by<br>\nrenaming it, and the handler's <code>getElementById<\/code> finds nothing to remove:<br>\n<\/p>\n\n<div class=\"highlight js-code-highlight\">\n<pre class=\"highlight dart\"><code><span class=\"c1\">\/\/ In main(), before runApp.<\/span>\n<span class=\"kd\">final<\/span> <span class=\"n\">el<\/span> <span class=\"o\">=<\/span> <span class=\"n\">web<\/span><span class=\"o\">.<\/span><span class=\"na\">document<\/span><span class=\"o\">.<\/span><span class=\"na\">querySelector<\/span><span class=\"p\">(<\/span><span class=\"s\">'#prerendered-content'<\/span><span class=\"p\">);<\/span>\n<span class=\"k\">if<\/span> <span class=\"p\">(<\/span><span class=\"n\">el<\/span> <span class=\"o\">!=<\/span> <span class=\"kc\">null<\/span><span class=\"p\">)<\/span> <span class=\"p\">{<\/span>\n  <span class=\"n\">el<\/span><span class=\"o\">.<\/span><span class=\"na\">id<\/span> <span class=\"o\">=<\/span> <span class=\"s\">'seo-prerendered'<\/span><span class=\"p\">;<\/span>         <span class=\"c1\">\/\/ the first-frame handler now misses it<\/span>\n  <span class=\"n\">el<\/span><span class=\"o\">.<\/span><span class=\"na\">setAttribute<\/span><span class=\"p\">(<\/span><span class=\"s\">'style'<\/span><span class=\"p\">,<\/span> <span class=\"n\">clipped<\/span><span class=\"p\">);<\/span> <span class=\"c1\">\/\/ the clipping CSS was keyed on the old id<\/span>\n  <span class=\"n\">el<\/span><span class=\"o\">.<\/span><span class=\"na\">setAttribute<\/span><span class=\"p\">(<\/span><span class=\"s\">'aria-hidden'<\/span><span class=\"p\">,<\/span> <span class=\"s\">'true'<\/span><span class=\"p\">);<\/span>\n<span class=\"p\">}<\/span>\n<\/code><\/pre>\n\n<\/div>\n\n\n\n<p>Three details matter:<\/p>\n\n<ul>\n<li>\n<strong>Re-apply the clipping inline.<\/strong> The stylesheet rule that hid the block was\nkeyed on the old id. Rename it without this and the whole crawler copy\nappears on screen.<\/li>\n<li>\n<strong>Hide it from screen readers.<\/strong> The app turns on Flutter's semantics tree,\nwhich is the accessible version of the page. With both exposed, a screen\nreader would read every page twice.<\/li>\n<li>\n<strong>Drop it on navigation.<\/strong> The block describes the route that was served.\nWhen the visitor moves to another route inside the app, it's removed, so the\ndocument never carries text for a page that isn't showing.<\/li>\n<\/ul>\n\n<p>It's the same content the app paints, so this isn't a different page for<br>\nGoogle. The crawler just gets the text version of the page instead of a picture<br>\nof it.<\/p>\n\n<h2>\n  \n  \n  What changed\n<\/h2>\n\n<p>Search Console's live test afterwards returned \"URL is available to Google\",<br>\nand the rendered HTML of <code>\/apps<\/code> had 159 words and 96 links instead of none.<\/p>\n\n<p>Then indexing was requested for eight pages: the home page, the six hubs<br>\n(<code>\/apps<\/code>, <code>\/tools<\/code>, <code>\/blog<\/code>, <code>\/games<\/code>, <code>\/work<\/code>, <code>\/packages<\/code>) and one app page.<br>\nNot the 400 leaves. Requests are capped at roughly ten a day, and one crawled<br>\nhub hands Google about 96 links, which is how leaves get discovered without<br>\nbeing requested one at a time.<\/p>\n\n<p>By 14 September: <strong>99 indexed, up from 18.<\/strong> It isn't a controlled experiment,<br>\nsince the fix and the requests landed in the same week, but requesting an empty<br>\nhub would have handed Google nothing to follow. And 330 pages are still in<br>\n\"Discovered \u2014 currently not indexed\", so this is a slope, not a switch. The<br>\nqueries now arriving are for tools and posts (\"flutter treemap\", \"tone<br>\ngenerator\"), the kinds of page that used to be invisible.<\/p>\n\n<h2>\n  \n  \n  Check the rendered page, not the response\n<\/h2>\n\n<p>If you ship Flutter web with prerendered HTML, <code>curl<\/code> is not the test that<br>\nmatters:<\/p>\n\n<ol>\n<li>\n<strong>Search Console \u2192 URL Inspection \u2192 Test live URL \u2192 View tested page \u2192\nHTML.<\/strong> That's the DOM Google indexes. Search it for a sentence from your\npage.<\/li>\n<li>\n<strong>Or open the page, wait for it to paint, and run\n<code>document.body.innerText.length<\/code> in the console.<\/strong> If it's near zero on a\npage full of text, a crawler sees the same nothing.<\/li>\n<li>\n<strong>Search <code>index.html<\/code> for anything that removes elements on\n<code>flutter-first-frame<\/code>,<\/strong> and ask what exactly it removes.<\/li>\n<\/ol>\n\n<p>The served HTML was right the whole time. The page deleted it before the one<br>\nreader who mattered got there.<\/p>\n\n\n\n\n<p><em>Originally published at <a href=\"https:\/\/devshakib.jumyn.com\/blog\/google-indexed-the-blog-and-nothing-else\" rel=\"noopener noreferrer\">devshakib.jumyn.com<\/a>. I write about Flutter, Dart and the parts of shipping that are genuinely awkward \u2014 and publish the packages that came out of them at <a href=\"https:\/\/pub.dev\/publishers\/jumyn.com\/packages\" rel=\"noopener noreferrer\">pub.dev\/publishers\/jumyn.com<\/a>.<\/em><\/p>\n\n","category":["flutter","webdev","seo","dart"]},{"title":"Five Requests in Fifteen Seconds From One Status Bar Item","pubDate":"Mon, 14 Sep 2026 16:40:57 +0000","link":"https:\/\/dev.to\/devshakib\/five-requests-in-fifteen-seconds-from-one-status-bar-item-4kgh","guid":"https:\/\/dev.to\/devshakib\/five-requests-in-fifteen-seconds-from-one-status-bar-item-4kgh","description":"<p>Claude Code knows how much of your usage limits you have left, but it shows you<br>\nin a panel you have to go and open. The number belongs where you are already<br>\nlooking, so it went into the VS Code status bar:<br>\n<\/p>\n\n<div class=\"highlight js-code-highlight\">\n<pre class=\"highlight plaintext\"><code>Claude 5h 12% \u00b7 7d 39%\n<\/code><\/pre>\n\n<\/div>\n\n\n\n<p>Hover for every limit and when each one resets; the item turns amber at 75% and<br>\nred at 90%. The first version worked. The second, released the same evening, had<br>\nthe usage endpoint answering 429: five requests in fifteen seconds, and every one<br>\nof them extended the wait.<\/p>\n<h2>\n  \n  \n  Every window runs your extension again\n<\/h2>\n\n<p>The mistake was thinking of the extension as one program. It isn't. Each VS Code<br>\nwindow runs its own copy, with its own timers and its own event handlers, and<br>\nnothing is shared between them unless you share it.<\/p>\n\n<p>So the real schedule was never \"one request every five minutes\". It was:<\/p>\n\n<ul>\n<li>every open window, polling on its own timer,<\/li>\n<li>plus a fetch whenever any window gained focus,<\/li>\n<li>plus a fetch on every click of the item, including while the server was\nsaying wait.<\/li>\n<\/ul>\n\n<p>Move between a few windows, click the item because the number looks stale, and<br>\nyou get what happened here: five requests in fifteen seconds, each one making<br>\nthe block longer.<\/p>\n<h2>\n  \n  \n  One result, shared through a file\n<\/h2>\n\n<p>VS Code gives every extension a global storage folder,<br>\n<code>context.globalStorageUri<\/code>, and it is the same folder in every window. That makes<br>\nit the simplest place to keep a result that all of them can read.<\/p>\n\n<p>A window no longer asks the server just because it woke up. It reads the file<br>\nfirst, and only fetches if the numbers there are older than the refresh<br>\ninterval:<br>\n<\/p>\n\n<div class=\"highlight js-code-highlight\">\n<pre class=\"highlight javascript\"><code><span class=\"c1\">\/\/ Fresh enough? Then this is just a render \u2014 no request.<\/span>\n<span class=\"kd\">const<\/span> <span class=\"nx\">age<\/span> <span class=\"o\">=<\/span> <span class=\"nx\">last<\/span> <span class=\"p\">?<\/span> <span class=\"nb\">Date<\/span><span class=\"p\">.<\/span><span class=\"nf\">now<\/span><span class=\"p\">()<\/span> <span class=\"o\">-<\/span> <span class=\"nx\">last<\/span><span class=\"p\">.<\/span><span class=\"nx\">at<\/span> <span class=\"p\">:<\/span> <span class=\"kc\">Infinity<\/span><span class=\"p\">;<\/span>\n<span class=\"k\">if <\/span><span class=\"p\">(<\/span><span class=\"nx\">age<\/span> <span class=\"o\">&lt;<\/span> <span class=\"p\">(<\/span><span class=\"nx\">manual<\/span> <span class=\"p\">?<\/span> <span class=\"nx\">CLAIM_MS<\/span> <span class=\"p\">:<\/span> <span class=\"nx\">s<\/span><span class=\"p\">.<\/span><span class=\"nx\">minutes<\/span> <span class=\"o\">*<\/span> <span class=\"mi\">60<\/span><span class=\"nx\">_000<\/span><span class=\"p\">))<\/span> <span class=\"p\">{<\/span>\n  <span class=\"nf\">render<\/span><span class=\"p\">();<\/span>\n  <span class=\"k\">return<\/span><span class=\"p\">;<\/span>\n<span class=\"p\">}<\/span>\n<\/code><\/pre>\n\n<\/div>\n\n\n\n<p>Two windows can still find the same stale file at the same moment, so the one<br>\nthat fetches writes a claim first, and any window that sees a claim younger than<br>\n30 seconds stays out of it:<br>\n<\/p>\n\n<div class=\"highlight js-code-highlight\">\n<pre class=\"highlight javascript\"><code><span class=\"c1\">\/\/ Another window is already fetching.<\/span>\n<span class=\"k\">if <\/span><span class=\"p\">(<\/span><span class=\"nx\">cache<\/span><span class=\"p\">.<\/span><span class=\"nx\">fetchingAt<\/span> <span class=\"o\">&amp;&amp;<\/span> <span class=\"nb\">Date<\/span><span class=\"p\">.<\/span><span class=\"nf\">now<\/span><span class=\"p\">()<\/span> <span class=\"o\">-<\/span> <span class=\"nx\">cache<\/span><span class=\"p\">.<\/span><span class=\"nx\">fetchingAt<\/span> <span class=\"o\">&lt;<\/span> <span class=\"nx\">CLAIM_MS<\/span><span class=\"p\">)<\/span> <span class=\"k\">return<\/span><span class=\"p\">;<\/span>\n<\/code><\/pre>\n\n<\/div>\n\n\n\n<p>The timers also carry up to 20 seconds of random jitter, so windows opened<br>\ntogether don't wake together. Focusing a window now costs nothing: it re-renders<br>\nwhat is already in the file.<\/p>\n<h2>\n  \n  \n  A 429 is an instruction\n<\/h2>\n\n<p>The worst part of the old version was the click. The item looked stuck, so it<br>\ngot clicked, and each click was another request to a server that had just said<br>\nstop.<\/p>\n\n<p>Now a 429 sets a block, written to the same shared file so every window obeys it.<br>\nIts length is whichever is longer: the server's own <code>retry-after<\/code>, or 5 minutes<br>\ndoubling on each repeat up to 30.<br>\n<\/p>\n\n<div class=\"highlight js-code-highlight\">\n<pre class=\"highlight javascript\"><code><span class=\"kd\">function<\/span> <span class=\"nf\">nextBlock<\/span><span class=\"p\">(<\/span><span class=\"nx\">strikeCount<\/span><span class=\"p\">,<\/span> <span class=\"nx\">retryAfterMs<\/span><span class=\"p\">)<\/span> <span class=\"p\">{<\/span>\n  <span class=\"kd\">const<\/span> <span class=\"nx\">curve<\/span> <span class=\"o\">=<\/span> <span class=\"nb\">Math<\/span><span class=\"p\">.<\/span><span class=\"nf\">min<\/span><span class=\"p\">(<\/span><span class=\"nx\">BLOCK_MAX<\/span><span class=\"p\">,<\/span> <span class=\"nx\">BLOCK_MIN<\/span> <span class=\"o\">*<\/span> <span class=\"mi\">2<\/span> <span class=\"o\">**<\/span> <span class=\"nb\">Math<\/span><span class=\"p\">.<\/span><span class=\"nf\">max<\/span><span class=\"p\">(<\/span><span class=\"mi\">0<\/span><span class=\"p\">,<\/span> <span class=\"nx\">strikeCount<\/span> <span class=\"o\">-<\/span> <span class=\"mi\">1<\/span><span class=\"p\">));<\/span>\n  <span class=\"k\">return<\/span> <span class=\"nb\">Math<\/span><span class=\"p\">.<\/span><span class=\"nf\">max<\/span><span class=\"p\">(<\/span><span class=\"nx\">curve<\/span><span class=\"p\">,<\/span> <span class=\"nx\">retryAfterMs<\/span> <span class=\"o\">||<\/span> <span class=\"mi\">0<\/span><span class=\"p\">);<\/span>\n<span class=\"p\">}<\/span>\n<\/code><\/pre>\n\n<\/div>\n\n\n\n<p>And a click cannot override it. That felt wrong to write, because a button that<br>\ndoes nothing looks broken. So the tooltip says when the next check is due, which<br>\nturns \"why won't it refresh\" into an answer.<\/p>\n\n<h2>\n  \n  \n  A failed check is not an outage\n<\/h2>\n\n<p>The very first version had the opposite problem: it treated one failure as news.<\/p>\n\n<p>The extension reads the login Claude Code already keeps: the macOS Keychain<br>\nentry <code>Claude Code-credentials<\/code>, or <code>~\/.claude\/.credentials.json<\/code> elsewhere.<br>\nClaude Code rewrites that entry whenever it renews its login, and a read that<br>\nlands in that moment comes back without a token. The status bar showed a warning<br>\nwhere the numbers should have been, until the next poll five minutes later.<\/p>\n\n<p>Now a failure keeps the last numbers on screen, falls back to the token already<br>\nheld, and retries after 20 seconds, then 60, then 120. The warning appears only<br>\nafter three failures in a row, and the tooltip always names the last one. Nothing<br>\nis hidden; it just stops shouting.<\/p>\n\n<h2>\n  \n  \n  What it touches\n<\/h2>\n\n<p>Anything that reads a login should say plainly what it does with it:<\/p>\n\n<ul>\n<li>\n<strong>The login, read-only.<\/strong> The token is never refreshed, never written back and\nnever logged, so the extension cannot sign Claude Code out. If the stored token\nhas expired, the item says so, and using Claude Code once renews it.<\/li>\n<li>\n<strong>One request every few minutes<\/strong>, to <code>api.anthropic.com<\/code>, shared by every\nwindow.<\/li>\n<li>\n<strong>Nothing else<\/strong>: no telemetry, no other network calls.<\/li>\n<\/ul>\n\n<p>The endpoint deserves the same honesty. It is the one Claude Code's own usage<br>\npanel reads, not a published API, so a Claude Code update could change or remove<br>\nit. If that happens, the extension shows a warning instead of numbers.<\/p>\n\n<h2>\n  \n  \n  Three rules for a polling extension\n<\/h2>\n\n<ol>\n<li>\n<strong>Assume one copy per window.<\/strong> Anything on a timer or a focus event is\nmultiplied by the number of windows open. Share the result, not the\nschedule.<\/li>\n<li>\n<strong>Treat 429 as an instruction.<\/strong> Store the wait where every copy can see it,\ndon't let a button override it, and show when the next try is due.<\/li>\n<li>\n<strong>One failure is noise.<\/strong> Keep the last good value, retry quickly, and warn\nonly when failures repeat.<\/li>\n<\/ol>\n\n<p>ds_usage is free and MIT licensed, on the<br>\n<a href=\"https:\/\/marketplace.visualstudio.com\/items?itemName=devShakib.ds-usage\" rel=\"noopener noreferrer\">VS Code Marketplace<\/a>,<br>\nwith the source on <a href=\"https:\/\/github.com\/devShakib015\/ds_usage\" rel=\"noopener noreferrer\">GitHub<\/a>.<\/p>\n\n\n\n\n<p><em>Originally published at <a href=\"https:\/\/devshakib.jumyn.com\/blog\/five-requests-in-fifteen-seconds-from-one-status-bar-item\" rel=\"noopener noreferrer\">devshakib.jumyn.com<\/a>. I write about Flutter, Dart and the parts of shipping that are genuinely awkward \u2014 and publish the packages that came out of them at <a href=\"https:\/\/pub.dev\/publishers\/jumyn.com\/packages\" rel=\"noopener noreferrer\">pub.dev\/publishers\/jumyn.com<\/a>.<\/em><\/p>\n\n","category":["vscode","claude","javascript","productivity"]},{"title":"Full Disk Access Was On, and macOS Still Refused the App","pubDate":"Sat, 12 Sep 2026 19:16:35 +0000","link":"https:\/\/dev.to\/devshakib\/full-disk-access-was-on-and-macos-still-refused-the-app-55i9","guid":"https:\/\/dev.to\/devshakib\/full-disk-access-was-on-and-macos-still-refused-the-app-55i9","description":"<p>A Mac app I maintain needs Full Disk Access. It measures disk usage, and most of<br>\nwhat is worth measuring sits behind that permission. I switched it on in System<br>\nSettings \u25b8 Privacy &amp; Security \u25b8 Full Disk Access, and the app went on reporting<br>\nlimited access. Its banner said what these banners say: already granted?<br>\nRelaunch. Relaunching changed nothing, and never could have.<\/p>\n<h2>\n  \n  \n  The app did not validate\n<\/h2>\n\n<p>A permission is granted to an app, and to macOS \"the app\" is not a name. So the<br>\nfirst question is whether macOS can tell what the app is:<br>\n<\/p>\n\n<div class=\"highlight js-code-highlight\">\n<pre class=\"highlight console\"><code><span class=\"gp\">$<\/span><span class=\"w\"> <\/span>codesign <span class=\"nt\">--verify<\/span> <span class=\"nt\">--deep<\/span> <span class=\"nt\">--strict<\/span> <span class=\"nt\">--verbose<\/span><span class=\"o\">=<\/span>2 \/Applications\/Helm.app\n<span class=\"go\">\/Applications\/Helm.app: nested code is modified or invalid\nfile modified: \/Applications\/Helm.app\/Contents\/Frameworks\/App.framework\n<\/span><\/code><\/pre>\n\n<\/div>\n\n\n\n<p><code>App.framework<\/code> is where Flutter puts the compiled Dart. Verified on its own it<br>\nwas fine, and so were the other four frameworks in the bundle. What failed was<br>\nthe app around them.<\/p>\n\n<p>An app's signature seals its contents. <code>Contents\/_CodeSignature\/CodeResources<\/code><br>\nrecords, for every nested framework, the hash of the code that should be there.<br>\nIt recorded one <code>App.framework<\/code>, and the bundle held another:<br>\n<\/p>\n\n<div class=\"highlight js-code-highlight\">\n<pre class=\"highlight plaintext\"><code>seal records    Frameworks\/App.framework   4ba5cc60\u2026\nactually there  Frameworks\/App.framework   c7a45621\u2026\n\nContents\/_CodeSignature\/CodeResources      15:08:47\nContents\/MacOS\/Helm                        15:08:47\nContents\/Frameworks\/App.framework\/\u2026\/App    15:30:38\n<\/code><\/pre>\n\n<\/div>\n\n\n\n<p>The seal and the executable came from one build. The framework came from a build<br>\ntwenty-two minutes later, which put a new <code>App.framework<\/code> in place and never<br>\nre-sealed the app around it.<\/p>\n<h2>\n  \n  \n  Why a stale seal turns the switch into decoration\n<\/h2>\n\n<p>TCC, the part of macOS behind every Privacy &amp; Security switch, does not store<br>\n\"this app is allowed\". It stores a code requirement and checks the running app<br>\nagainst it. For an app signed with a Developer ID, that requirement names the<br>\ndeveloper's team. For an ad-hoc signature, which is what you have without one,<br>\nit is effectively the hash of the code.<\/p>\n\n<p>A bundle whose nested code does not match its own seal does not validate, and<br>\ncode that does not validate cannot satisfy a requirement. The grant attaches to<br>\nnothing. The switch reads ON, every protected path stays refused, and no relaunch<br>\ntouches any of it.<\/p>\n\n<p>Re-signing the bundle and granting access again fixed it: the app reported Full<br>\nDisk Access on, and kept it through a restart.<\/p>\n<h2>\n  \n  \n  How the seal went stale\n<\/h2>\n\n<p>Two things write a Flutter macOS bundle. A script phase runs<br>\n<code>macos_assemble.sh embed<\/code>, which hands off to Flutter's <code>xcode_backend.dart<\/code>: it<br>\ncopies the freshly compiled <code>App.framework<\/code> into <code>Contents\/Frameworks<\/code> and signs<br>\nit. Then Xcode signs the app, which writes the seal. In a good build the<br>\ntimestamps land a second apart, framework first:<br>\n<\/p>\n\n<div class=\"highlight js-code-highlight\">\n<pre class=\"highlight plaintext\"><code>build 1  clean              verify ok   seal 23:19:32   App.framework 23:19:31\nbuild 2  one Dart change    verify ok   seal 23:20:06   App.framework 23:20:05\n<\/code><\/pre>\n\n<\/div>\n\n\n\n<p>So it is not simply what an incremental build does, which is what I assumed at<br>\nfirst and wrote into a changelog. A clean build seals correctly, and so does a<br>\nDart-only rebuild straight after it.<\/p>\n\n<p>What reproduced it, once, was a crash. On a copy of the project, a Swift change<br>\nbuilt fine. The next build, a one-line Dart change, died:<br>\n<\/p>\n\n<div class=\"highlight js-code-highlight\">\n<pre class=\"highlight plaintext\"><code>error: unexpected service error: The Xcode build system has crashed. Build again to continue.\n<\/code><\/pre>\n\n<\/div>\n\n\n\n<p>Doing as told, with another Dart-only change:<br>\n<\/p>\n\n<div class=\"highlight js-code-highlight\">\n<pre class=\"highlight console\"><code><span class=\"go\">\u2713 Built build\/macos\/Build\/Products\/Release\/Helm.app (47.1MB)\n\n<\/span><span class=\"gp\">$<\/span><span class=\"w\"> <\/span>codesign <span class=\"nt\">--verify<\/span> <span class=\"nt\">--strict<\/span> Helm.app\n<span class=\"go\">Helm.app: nested code is modified or invalid\n\nseal records App.framework dda6dabc96dd    actual afaf15310e15\nContents\/_CodeSignature\/CodeResources      23:22:42\nContents\/MacOS\/Helm                        23:22:42\nContents\/Frameworks\/App.framework\/\u2026\/App    23:23:28\n<\/span><\/code><\/pre>\n\n<\/div>\n\n\n\n<p>The build after the crash ran Flutter's embed step, skipped the app's signing,<br>\nand reported success. Same fingerprint as the build that shipped: seal and<br>\nexecutable from one build, framework from a later one.<\/p>\n\n<p>I can't prove the shipped build went through a crash. I had kept only the last<br>\nfew lines of each build, and a crash in an earlier one is exactly what that<br>\nthrows away. Nor do I know which other failures leave the same state. An<br>\ninterrupted build is the obvious suspect, and I haven't tested it. That<br>\nuncertainty is the argument for the next two sections.<\/p>\n\n<p>(Reproduced on Flutter 3.47.3, Xcode 26.6, macOS 26.6.2. The shipped build was on<br>\nFlutter 3.47.1, and none of the patch-release changes in between are about<br>\nsigning.)<\/p>\n<h2>\n  \n  \n  Why nothing caught it\n<\/h2>\n\n<p>Not because the check is subtle. My first theory was that only <code>--deep<\/code> compares<br>\nnested code with the outer seal, so a check without it would have waved this<br>\nbundle through. It was wrong. On a copy with <code>App.framework<\/code> swapped out:<br>\n<\/p>\n\n<div class=\"highlight js-code-highlight\">\n<pre class=\"highlight console\"><code><span class=\"go\">codesign --verify App.framework        exit 0    valid on its own\ncodesign --verify Helm.app             exit 1    nested code is modified or invalid\nSecStaticCodeCheckValidity, flags 0    -67021    nested code is modified or invalid\n<\/span><\/code><\/pre>\n\n<\/div>\n\n\n\n<p>No flags needed. Any verification of the app fails it.<\/p>\n\n<p>The release script did not verify at all. Its signing and its verification lived<br>\nin the same branch:<br>\n<\/p>\n\n<div class=\"highlight js-code-highlight\">\n<pre class=\"highlight shell\"><code><span class=\"k\">if<\/span> <span class=\"o\">[[<\/span> <span class=\"nt\">-n<\/span> <span class=\"s2\">\"<\/span><span class=\"nv\">$SIGN_ID<\/span><span class=\"s2\">\"<\/span> <span class=\"o\">]]<\/span><span class=\"p\">;<\/span> <span class=\"k\">then<\/span>     <span class=\"c\"># a Developer ID certificate was found<\/span>\n  <span class=\"c\"># ...sign every framework, then the app...<\/span>\n  codesign <span class=\"nt\">--verify<\/span> <span class=\"nt\">--strict<\/span> <span class=\"s2\">\"<\/span><span class=\"nv\">$BUILD_APP<\/span><span class=\"s2\">\"<\/span>\n<span class=\"k\">fi<\/span>\n<\/code><\/pre>\n\n<\/div>\n\n\n\n<p>The app is free and deliberately has no $99-a-year Developer ID, so that branch<br>\nnever ran. The path that always ran signed nothing and checked nothing, and the<br>\nDMG shipped whatever <code>flutter build<\/code> had left behind.<\/p>\n<h2>\n  \n  \n  The fix\n<\/h2>\n\n<p>Sign and verify on every path, and let a failed verification fail the release:<br>\n<\/p>\n\n<div class=\"highlight js-code-highlight\">\n<pre class=\"highlight shell\"><code><span class=\"k\">if<\/span> <span class=\"o\">[[<\/span> <span class=\"nt\">-n<\/span> <span class=\"s2\">\"<\/span><span class=\"nv\">$SIGN_ID<\/span><span class=\"s2\">\"<\/span> <span class=\"o\">]]<\/span><span class=\"p\">;<\/span> <span class=\"k\">then\n  <\/span><span class=\"nv\">SIGN<\/span><span class=\"o\">=(<\/span><span class=\"nt\">--force<\/span> <span class=\"nt\">--timestamp<\/span> <span class=\"nt\">--options<\/span> runtime <span class=\"nt\">--sign<\/span> <span class=\"s2\">\"<\/span><span class=\"nv\">$SIGN_ID<\/span><span class=\"s2\">\"<\/span><span class=\"o\">)<\/span>\n<span class=\"k\">else\n  <\/span><span class=\"nv\">SIGN<\/span><span class=\"o\">=(<\/span><span class=\"nt\">--force<\/span> <span class=\"nt\">--sign<\/span> -<span class=\"o\">)<\/span>   <span class=\"c\"># ad-hoc: never notarised, so no runtime or timestamp<\/span>\n<span class=\"k\">fi<\/span>\n\n<span class=\"c\"># Inside-out: nested code first, then the bundle that holds it.<\/span>\n<span class=\"k\">for <\/span>fw <span class=\"k\">in<\/span> <span class=\"s2\">\"<\/span><span class=\"nv\">$APP<\/span><span class=\"s2\">\"<\/span>\/Contents\/Frameworks\/<span class=\"k\">*<\/span>.framework<span class=\"p\">;<\/span> <span class=\"k\">do\n  <\/span>codesign <span class=\"s2\">\"<\/span><span class=\"k\">${<\/span><span class=\"nv\">SIGN<\/span><span class=\"p\">[@]<\/span><span class=\"k\">}<\/span><span class=\"s2\">\"<\/span> <span class=\"s2\">\"<\/span><span class=\"nv\">$fw<\/span><span class=\"s2\">\"<\/span>\n<span class=\"k\">done\n<\/span>codesign <span class=\"s2\">\"<\/span><span class=\"k\">${<\/span><span class=\"nv\">SIGN<\/span><span class=\"p\">[@]<\/span><span class=\"k\">}<\/span><span class=\"s2\">\"<\/span> <span class=\"nt\">--entitlements<\/span> macos\/Runner\/Release.entitlements <span class=\"s2\">\"<\/span><span class=\"nv\">$APP<\/span><span class=\"s2\">\"<\/span>\n\ncodesign <span class=\"nt\">--verify<\/span> <span class=\"nt\">--deep<\/span> <span class=\"nt\">--strict<\/span> <span class=\"s2\">\"<\/span><span class=\"nv\">$APP<\/span><span class=\"s2\">\"<\/span> <span class=\"o\">||<\/span> <span class=\"o\">{<\/span> <span class=\"nb\">echo<\/span> <span class=\"s2\">\"signature broken\"<\/span> <span class=\"o\">&gt;<\/span>&amp;2<span class=\"p\">;<\/span> <span class=\"nb\">exit <\/span>1<span class=\"p\">;<\/span> <span class=\"o\">}<\/span>\n<\/code><\/pre>\n\n<\/div>\n\n\n\n<p>Sign with a loop rather than <code>codesign --deep<\/code>, which Apple advises against for<br>\nsigning. For verifying, <code>--deep<\/code> is fine, and with <code>--verbose<\/code> it names the file<br>\nthat changed.<\/p>\n\n<p>And the app now checks itself before telling anyone to relaunch. This goes<br>\nthrough the Security framework rather than running <code>\/usr\/bin\/codesign<\/code>, which<br>\ndoes ship with macOS, because it is a yes-or-no question with a status code for<br>\nan answer:<br>\n<\/p>\n\n<div class=\"highlight js-code-highlight\">\n<pre class=\"highlight swift\"><code><span class=\"kd\">import<\/span> <span class=\"kt\">Security<\/span>\n\n<span class=\"kd\">func<\/span> <span class=\"nf\">bundleValidates<\/span><span class=\"p\">()<\/span> <span class=\"o\">-&gt;<\/span> <span class=\"kt\">Bool<\/span> <span class=\"p\">{<\/span>\n  <span class=\"k\">var<\/span> <span class=\"nv\">code<\/span><span class=\"p\">:<\/span> <span class=\"kt\">SecStaticCode<\/span><span class=\"p\">?<\/span>\n  <span class=\"k\">guard<\/span> <span class=\"kt\">SecStaticCodeCreateWithPath<\/span><span class=\"p\">(<\/span><span class=\"kt\">Bundle<\/span><span class=\"o\">.<\/span><span class=\"n\">main<\/span><span class=\"o\">.<\/span><span class=\"n\">bundleURL<\/span> <span class=\"k\">as<\/span> <span class=\"kt\">CFURL<\/span><span class=\"p\">,<\/span> <span class=\"p\">[],<\/span> <span class=\"o\">&amp;<\/span><span class=\"n\">code<\/span><span class=\"p\">)<\/span> <span class=\"o\">==<\/span> <span class=\"n\">errSecSuccess<\/span><span class=\"p\">,<\/span>\n        <span class=\"k\">let<\/span> <span class=\"nv\">code<\/span> <span class=\"k\">else<\/span> <span class=\"p\">{<\/span> <span class=\"k\">return<\/span> <span class=\"kc\">true<\/span> <span class=\"p\">}<\/span>          <span class=\"c1\">\/\/ couldn't ask: don't accuse<\/span>\n  <span class=\"k\">let<\/span> <span class=\"nv\">flags<\/span> <span class=\"o\">=<\/span> <span class=\"kt\">SecCSFlags<\/span><span class=\"p\">(<\/span><span class=\"nv\">rawValue<\/span><span class=\"p\">:<\/span> <span class=\"n\">kSecCSCheckNestedCode<\/span> <span class=\"o\">|<\/span> <span class=\"n\">kSecCSStrictValidate<\/span><span class=\"p\">)<\/span>\n  <span class=\"k\">return<\/span> <span class=\"kt\">SecStaticCodeCheckValidity<\/span><span class=\"p\">(<\/span><span class=\"n\">code<\/span><span class=\"p\">,<\/span> <span class=\"n\">flags<\/span><span class=\"p\">,<\/span> <span class=\"kc\">nil<\/span><span class=\"p\">)<\/span> <span class=\"o\">==<\/span> <span class=\"n\">errSecSuccess<\/span>\n<span class=\"p\">}<\/span>\n<\/code><\/pre>\n\n<\/div>\n\n\n\n<p>When access reads as denied and that returns false, the banner stops saying<br>\n\"relaunch\" and says the true thing: this copy cannot hold the permission, so<br>\nreinstall it and grant access again. When the check cannot run at all, it<br>\nanswers true. Telling someone a working app is broken is worse than saying<br>\nnothing.<\/p>\n\n<h2>\n  \n  \n  The part that is not a bug\n<\/h2>\n\n<p>An ad-hoc requirement is the hash of the code, so to TCC every build is a<br>\ndifferent app. Every update loses Full Disk Access and needs granting again. No<br>\nrelease script fixes that. It is the cost of not having a Developer ID, whose<br>\nrequirement names the team instead of the bytes and so survives an update. For a<br>\nfree app, the honest move is to say so at the top of the release notes rather<br>\nthan let people find out.<\/p>\n\n<h2>\n  \n  \n  Three rules\n<\/h2>\n\n<ol>\n<li>\n<strong>Verify the bundle you ship, on every path.<\/strong> Plain <code>codesign --verify<\/code> is\nenough. Never gate the check behind having a certificate: the path without\none is the path that always runs.<\/li>\n<li>\n<strong>Don't trust \u2713 Built after a build that crashed.<\/strong> Build clean, or verify.\nThe build after a crash can succeed at everything except sealing the app.<\/li>\n<li>\n<strong>If your app needs a privacy permission, check its own signature before\nblaming the user.<\/strong> \"Relaunch\" is sometimes a loop with no exit.<\/li>\n<\/ol>\n\n<p>The app is <a href=\"https:\/\/github.com\/devShakib015\/helm\" rel=\"noopener noreferrer\">Helm<\/a>, a free Mac utility<br>\nbuilt with Flutter. Its release script and self-check are in the repository.<\/p>\n\n<p>The self-check is a package now: <a href=\"https:\/\/pub.dev\/packages\/macos_grants\" rel=\"noopener noreferrer\">macos_grants<\/a> reads Full Disk Access, Accessibility and Screen Recording, and answers the question underneath them \u2014 whether this copy of your app can hold a grant at all.<\/p>\n\n\n\n\n<p><em>Originally published at <a href=\"https:\/\/devshakib.jumyn.com\/blog\/full-disk-access-was-on-and-macos-still-refused-the-app\" rel=\"noopener noreferrer\">devshakib.jumyn.com<\/a>. I write about Flutter, Dart and the parts of shipping that are genuinely awkward \u2014 and publish the packages that came out of them at <a href=\"https:\/\/pub.dev\/publishers\/jumyn.com\/packages\" rel=\"noopener noreferrer\">pub.dev\/publishers\/jumyn.com<\/a>.<\/em><\/p>\n\n","category":["macos","flutter","debugging","programming"]},{"title":"macOS Made a File That Reports Zero Bytes and Occupies Sixty Kilobytes","pubDate":"Tue, 08 Sep 2026 10:16:30 +0000","link":"https:\/\/dev.to\/devshakib\/macos-made-a-file-that-reports-zero-bytes-and-occupies-sixty-kilobytes-3c4a","guid":"https:\/\/dev.to\/devshakib\/macos-made-a-file-that-reports-zero-bytes-and-occupies-sixty-kilobytes-3c4a","description":"<p>I was writing a storage analyser, which means walking every file on the disk<br>\nand adding up what it costs. Somewhere around the 140,000th file the progress<br>\nline printed a path that does not exist:<br>\n<\/p>\n\n<div class=\"highlight js-code-highlight\">\n<pre class=\"highlight plaintext\"><code>\/Users\/me\/Applications\/Chrome Apps.localized\/localized\n<\/code><\/pre>\n\n<\/div>\n\n\n\n<p>There is no <code>localized<\/code> in that folder. There is a <code>.localized<\/code>, and there is<br>\none other thing, and that other thing turned out to be two separate problems<br>\nwearing one filename.<\/p>\n<h2>\n  \n  \n  The file\n<\/h2>\n\n\n<div class=\"highlight js-code-highlight\">\n<pre class=\"highlight shell\"><code><span class=\"nv\">$ <\/span><span class=\"nb\">ls<\/span> <span class=\"nt\">-b<\/span> ~\/Applications\/Chrome<span class=\"se\">\\ <\/span>Apps.localized\/\nIcon<span class=\"se\">\\r<\/span>\n<\/code><\/pre>\n\n<\/div>\n\n\n<p>Not <code>Icon<\/code> followed by something. The name is the four characters <code>I<\/code>, <code>c<\/code>,<br>\n<code>o<\/code>, <code>n<\/code>, and then byte <code>0x0D<\/code> \u2014 a carriage return. Python is blunt about it:<br>\n<\/p>\n\n<div class=\"highlight js-code-highlight\">\n<pre class=\"highlight python\"><code><span class=\"o\">&gt;&gt;&gt;<\/span> <span class=\"n\">os<\/span><span class=\"p\">.<\/span><span class=\"nf\">listdir<\/span><span class=\"p\">(<\/span><span class=\"n\">d<\/span><span class=\"p\">)<\/span>\n<span class=\"p\">[<\/span><span class=\"sh\">'<\/span><span class=\"s\">Icon<\/span><span class=\"se\">\\r<\/span><span class=\"sh\">'<\/span><span class=\"p\">,<\/span> <span class=\"sh\">'<\/span><span class=\"s\">.localized<\/span><span class=\"sh\">'<\/span><span class=\"p\">]<\/span>\n<\/code><\/pre>\n\n<\/div>\n\n\n\n<p>macOS wrote that. Every folder with a custom icon gets one: Finder \u2192 Get Info \u2192<br>\npaste an image, and this appears. The trailing CR is a fossil from classic Mac<br>\nOS, where it was the line terminator, and it is there to keep the file out of<br>\nthe way of anything that lists a directory naively. It has outlived the reason.<\/p>\n<h2>\n  \n  \n  Problem one: it eats your log line\n<\/h2>\n\n<p>A filename on a POSIX system may contain any byte except <code>\/<\/code> and NUL. That<br>\nincludes carriage return, newline, escape, and backspace. Almost nothing you<br>\nwrite treats a path as anything other than text you can print, and this is<br>\nwhere that assumption gets paid.<\/p>\n\n<p>Here is what a progress line actually emits:<br>\n<\/p>\n\n<div class=\"highlight js-code-highlight\">\n<pre class=\"highlight console\"><code><span class=\"gp\">$<\/span><span class=\"w\"> <\/span>find box <span class=\"nt\">-type<\/span> f <span class=\"nt\">-print0<\/span> | <span class=\"k\">while <\/span><span class=\"nv\">IFS<\/span><span class=\"o\">=<\/span> <span class=\"nb\">read<\/span> <span class=\"nt\">-r<\/span> <span class=\"nt\">-d<\/span> <span class=\"s1\">''<\/span> f<span class=\"p\">;<\/span> <span class=\"k\">do<\/span>\n<span class=\"gp\">    printf 'scanning %s  42,318 files\\n' \"$<\/span>f<span class=\"s2\">\"\n<\/span><span class=\"go\">  done | hexdump -C\n00000000  73 63 61 6e 6e 69 6e 67  20 62 6f 78 2f 49 63 6f  |scanning box\/Ico|\n00000010  6e 0d 20 20 34 32 2c 33  31 38 20 66 69 6c 65 73  |n.  42,318 files|\n<\/span><\/code><\/pre>\n\n<\/div>\n\n\n\n<p>Look at offset <code>0x11<\/code>: <code>6e 0d<\/code> \u2014 the <code>n<\/code> of <code>Icon<\/code>, then <code>0d<\/code>, the carriage<br>\nreturn. The terminal receives it and does what a terminal has always done with<br>\na CR: it moves the cursor back to column zero. The rest of the line then<br>\noverwrites what you just printed. You do not see a corrupted path. You see a<br>\nline that is missing its front half, spliced onto whatever came next \u2014 which<br>\nis precisely as confusing as it sounds when the count in front of it is also<br>\nchanging.<\/p>\n\n<p>The same byte does the same damage to a single-line label in a GUI, a CI log,<br>\nand anything that later parses its own output. And the CR is the polite one: a<br>\nfilename containing <code>\\n<\/code> will break <code>find | while read line<\/code> outright, because<br>\nthat loop's entire contract is that filenames do not contain newlines. Hence<br>\n<code>-print0<\/code> and <code>read -d ''<\/code>, which exist for this reason and not for style.<\/p>\n\n<p>If you render paths anywhere a human will look, strip the control range first:<br>\n<\/p>\n\n<div class=\"highlight js-code-highlight\">\n<pre class=\"highlight dart\"><code><span class=\"kd\">final<\/span> <span class=\"n\">safe<\/span> <span class=\"o\">=<\/span> <span class=\"n\">path<\/span><span class=\"o\">.<\/span><span class=\"na\">replaceAll<\/span><span class=\"p\">(<\/span><span class=\"n\">RegExp<\/span><span class=\"p\">(<\/span><span class=\"sx\">r'[\\x00-\\x1f\\x7f]'<\/span><span class=\"p\">),<\/span> <span class=\"s\">'?'<\/span><span class=\"p\">);<\/span>\n<\/code><\/pre>\n\n<\/div>\n\n\n\n<h2>\n  \n  \n  Problem two: it is zero bytes, and it is sixty kilobytes\n<\/h2>\n\n<p>This is the one that actually mattered for a disk tool.<br>\n<\/p>\n\n<div class=\"highlight js-code-highlight\">\n<pre class=\"highlight plaintext\"><code>stat -f %z         (st_size)        0 bytes\nls -l                              0\nos.path.getsize()                  0\n------------------------------------------------\nst_blocks \u00d7 512                61,440 bytes\ndu -k                              60 KB\n<\/code><\/pre>\n\n<\/div>\n\n\n\n<p>The data fork is genuinely empty. The icon \u2014 61,010 bytes of it \u2014 lives in an<br>\nextended attribute:<br>\n<\/p>\n\n<div class=\"highlight js-code-highlight\">\n<pre class=\"highlight shell\"><code><span class=\"nv\">$ <\/span>xattr ~\/Applications\/Chrome<span class=\"se\">\\ <\/span>Apps.localized\/Icon^M\ncom.apple.FinderInfo\ncom.apple.ResourceFork\ncom.apple.provenance\ncom.apple.quarantine\n<\/code><\/pre>\n\n<\/div>\n\n\n\n<p><code>com.apple.ResourceFork<\/code> is the resource fork, the other half of a classic Mac<br>\nfile, kept alive on APFS as an xattr and reachable through a magic path:<br>\n<\/p>\n\n<div class=\"highlight js-code-highlight\">\n<pre class=\"highlight shell\"><code><span class=\"nv\">$ <\/span><span class=\"nb\">stat<\/span> <span class=\"nt\">-f<\/span> %z ~\/Applications\/...\/Icon^M\/..namedfork\/rsrc\n61010\n<\/code><\/pre>\n\n<\/div>\n\n\n\n<p><code>st_size<\/code> describes the data fork. It always has. So a scanner that sums<br>\n<code>st_size<\/code> \u2014 which is the obvious thing to sum, and what <code>os.path.getsize<\/code> hands<br>\nyou \u2014 reports this file as free. Sixty kilobytes, invisible, once per folder<br>\nwith a custom icon.<\/p>\n\n<p>Sixty kilobytes is nothing. The habit is not nothing. <code>st_size<\/code> is a statement<br>\nabout content; <strong><code>st_blocks<\/code> is a statement about the disk<\/strong>, and a disk usage<br>\ntool is answering the second question. They come apart in three directions and<br>\nresource forks are only one of them:<\/p>\n\n<ul>\n<li>\n<strong>Sparse files<\/strong> report a large <code>st_size<\/code> and occupy almost no blocks.<\/li>\n<li>\n<strong>Compressed files<\/strong> \u2014 macOS transparently compresses much of <code>\/System<\/code> and\nmany app bundles \u2014 report the uncompressed size and occupy less.<\/li>\n<li>\n<strong>Resource forks and xattrs<\/strong> report nothing and occupy real space.<\/li>\n<\/ul>\n\n<p>Sum <code>st_size<\/code> across a volume and your total will not match About This Mac, in<br>\neither direction, and the user will believe Apple. Correctly. <code>du<\/code> has always<br>\nused <code>st_blocks<\/code>; that is the whole reason <code>du<\/code> and <code>ls -l<\/code> disagree, and I had<br>\nread that fact several times without it meaning anything until a zero-byte file<br>\nturned up holding an icon.<\/p>\n<h2>\n  \n  \n  The catch, if you write Dart\n<\/h2>\n\n<p>Knowing to use <code>st_blocks<\/code> and being able to are different problems. Dart's<br>\n<code>FileStat<\/code> exposes <code>size<\/code>, <code>mode<\/code>, <code>type<\/code> and three timestamps. There is no<br>\n<code>blocks<\/code>. <code>File.length()<\/code> is <code>st_size<\/code> as well. The number you want is in the<br>\n<code>stat<\/code> struct the VM already called and did not surface, so reaching it means<br>\ngoing around the standard library:<br>\n<\/p>\n\n<div class=\"highlight js-code-highlight\">\n<pre class=\"highlight dart\"><code><span class=\"c1\">\/\/ dart:ffi against stat(2), or a platform channel to<\/span>\n<span class=\"c1\">\/\/ URLResourceKey.totalFileAllocatedSizeKey on the Swift side.<\/span>\n<\/code><\/pre>\n\n<\/div>\n\n\n\n<p>Which is the actual reason so many size-summing tools get this wrong. It is not<br>\nthat <code>st_size<\/code> is chosen over <code>st_blocks<\/code>; it is that in most high-level<br>\nlanguages <code>st_size<\/code> is the only one handed to you, and the difference between<br>\n\"how big is this file\" and \"what does this file cost me\" never comes up until<br>\nsomething reports zero and takes sixty kilobytes.<\/p>\n\n<p>Two rules I would now write down before starting a tool like this:<\/p>\n\n<ol>\n<li>\n<strong>Sum <code>st_blocks<\/code>, not <code>st_size<\/code><\/strong>, and if the language will not give you\n<code>st_blocks<\/code>, that is a dependency you take on day one rather than a\nrefactor you do after the totals disagree with About This Mac.<\/li>\n<li>\n<strong>Strip <code>[\\x00-\\x1f]<\/code> from any path before rendering it.<\/strong> Paths are bytes,\nnot strings, and one of them on your Mac right now ends in a carriage\nreturn.<\/li>\n<\/ol>\n\n<p>The file is still there. It is on your Mac too \u2014 <code>find ~ -name 'Icon?' -type f<\/code><br>\nwill tell you, and the <code>?<\/code> is the only way to match it that does not require<br>\nyou to type a carriage return into your shell.<\/p>\n\n\n\n\n<p><em>Originally published at <a href=\"https:\/\/devshakib.jumyn.com\/blog\/the-zero-byte-file-that-occupies-sixty-kilobytes\" rel=\"noopener noreferrer\">devshakib.jumyn.com<\/a>. I write about Flutter, Dart and the parts of shipping that are genuinely awkward \u2014 and publish the packages that came out of them at <a href=\"https:\/\/pub.dev\/publishers\/jumyn.com\/packages\" rel=\"noopener noreferrer\">pub.dev\/publishers\/jumyn.com<\/a>.<\/em><\/p>\n\n","category":["macos","dart","programming"]},{"title":"Flutter Assumes There Is Only One Window. I Gave It Two.","pubDate":"Sun, 06 Sep 2026 17:02:19 +0000","link":"https:\/\/dev.to\/devshakib\/flutter-assumes-there-is-only-one-window-i-gave-it-two-fmm","guid":"https:\/\/dev.to\/devshakib\/flutter-assumes-there-is-only-one-window-i-gave-it-two-fmm","description":"<p>Document Picture-in-Picture is a browser API that gives you a real operating<br>\nsystem window. Not an overlay pinned inside your page \u2014 an actual window the<br>\nbrowser owns, floating above your editor and your terminal and everything else,<br>\nwhich keeps running when you switch tabs.<\/p>\n\n<p>Chrome and Edge have had it since 116. Firefox shipped it in 151. Almost<br>\nnothing uses it, and I could not find anything reaching it from Flutter, so I<br>\nwrote <a href=\"https:\/\/pub.dev\/packages\/document_pip\" rel=\"noopener noreferrer\">document_pip<\/a> to find out why.<\/p>\n\n<p>The API itself is about four lines. The difficulty is entirely on the Flutter<br>\nside, and it has one cause: <strong>the engine is built on the assumption that there<br>\nis one window.<\/strong> Not stated anywhere as a constraint \u2014 just quietly baked into<br>\nthree different singletons. A second window makes each of them wrong, and every<br>\none of them fails silently, in a way that looks like a different problem.<\/p>\n<h2>\n  \n  \n  A package cannot turn multi-view on\n<\/h2>\n\n<p>The first wall is structural. Multi-view Flutter has no single root, so an app<br>\nthat can grow a second view calls <code>runWidget<\/code>, not <code>runApp<\/code>. That part is<br>\ndocumented.<\/p>\n\n<p>The part that is not: <strong>only the JavaScript app object returned by<br>\n<code>engine.runApp()<\/code> can add a view.<\/strong> <code>dart:ui_web<\/code> exposes the view list<br>\nread-only. So the object that can create the pop-out's view lives in the<br>\nbootstrap, before any Dart has run, and a package published to pub.dev cannot<br>\nreach it. The app has to hand it over:<br>\n<\/p>\n\n<div class=\"highlight js-code-highlight\">\n<pre class=\"highlight javascript\"><code><span class=\"kd\">const<\/span> <span class=\"nx\">engine<\/span> <span class=\"o\">=<\/span> <span class=\"k\">await<\/span> <span class=\"nx\">engineInitializer<\/span><span class=\"p\">.<\/span><span class=\"nf\">initializeEngine<\/span><span class=\"p\">({<\/span>\n  <span class=\"na\">multiViewEnabled<\/span><span class=\"p\">:<\/span> <span class=\"kc\">true<\/span><span class=\"p\">,<\/span>\n<span class=\"p\">});<\/span>\n<span class=\"kd\">const<\/span> <span class=\"nx\">app<\/span> <span class=\"o\">=<\/span> <span class=\"k\">await<\/span> <span class=\"nx\">engine<\/span><span class=\"p\">.<\/span><span class=\"nf\">runApp<\/span><span class=\"p\">();<\/span>\n<span class=\"nb\">window<\/span><span class=\"p\">.<\/span><span class=\"nx\">documentPipApp<\/span> <span class=\"o\">=<\/span> <span class=\"nx\">app<\/span><span class=\"p\">;<\/span>          <span class=\"c1\">\/\/ the package needs this<\/span>\n<span class=\"nx\">app<\/span><span class=\"p\">.<\/span><span class=\"nf\">addView<\/span><span class=\"p\">({<\/span> <span class=\"na\">hostElement<\/span><span class=\"p\">:<\/span> <span class=\"nb\">document<\/span><span class=\"p\">.<\/span><span class=\"nf\">querySelector<\/span><span class=\"p\">(<\/span><span class=\"dl\">'<\/span><span class=\"s1\">#app<\/span><span class=\"dl\">'<\/span><span class=\"p\">)<\/span> <span class=\"p\">});<\/span>\n<\/code><\/pre>\n\n<\/div>\n\n\n\n<p>Multi-view is a property of how the engine <em>starts<\/em>. Nothing published to<br>\npub.dev can switch it on from the inside, which is worth knowing before you go<br>\nlooking for the API that does.<\/p>\n\n<p>One trap in that snippet. Never pass <code>document.body<\/code> as <code>hostElement<\/code>. Flutter<br>\nclears a host element's children and sizes the view to 100% of it, so <code>body<\/code><br>\nwipes your page \u2014 script tags included \u2014 and then measures zero. A blank screen<br>\nand no exception. I had that exact line sitting in an error message until the<br>\npre-publish audit caught it \u2014 of the three copies of that snippet in the<br>\npackage, the wrong one was the copy a stuck developer actually reaches, and the<br>\nonly copy never run.<\/p>\n<h2>\n  \n  \n  The window freezes exactly when you need it\n<\/h2>\n\n<p>Here is the good one.<\/p>\n\n<p>You open the pop-out, switch to another tab to do the thing you opened it for,<br>\nand it stops. Frozen frame, still floating, still on top, completely dead.<\/p>\n\n<p>The cause is a disagreement between two true statements. Chromium keeps<br>\npainting a document-picture-in-picture opener at full rate while its tab is in<br>\nthe background \u2014 that is the entire point, the window has to keep working. But<br>\nit <em>still reports the page as hidden<\/em>, because by any normal definition it is:<br>\nthe user is looking at another tab.<\/p>\n\n<p>Flutter's web engine reads <code>visibilityState: \"hidden\"<\/code> and turns it into<br>\n<code>AppLifecycleState.hidden<\/code>. <code>SchedulerBinding<\/code> responds by clearing<br>\n<code>framesEnabled<\/code>, after which <code>scheduleFrame()<\/code> returns early forever. That is<br>\ncorrect behaviour and good citizenship \u2014 nobody wants a backgrounded tab<br>\nburning battery. It is also exactly wrong here, because the one surface the<br>\nuser <em>can<\/em> still see is the one Flutter just stopped drawing.<\/p>\n\n<p>Measured rather than argued, because the whole failure is about believing a<br>\nreport instead of checking:<\/p>\n\n<div class=\"table-wrapper-paragraph\"><table>\n<thead>\n<tr>\n<th>Chromium, tab in the background<\/th>\n<th>browser frames<\/th>\n<th>Flutter frames<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>no pop-out open<\/td>\n<td>2 in 2.5s<\/td>\n<td>\u2014<\/td>\n<\/tr>\n<tr>\n<td>pop-out open<\/td>\n<td>302 in 2.5s<\/td>\n<td><strong>0 in 3s<\/strong><\/td>\n<\/tr>\n<tr>\n<td>pop-out open, with the fix<\/td>\n<td>302 in 2.5s<\/td>\n<td><strong>311 in 3s<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table><\/div>\n\n<p>The browser was drawing at roughly 120fps. Flutter drew nothing.<\/p>\n\n<p><code>scheduleForcedFrame()<\/code> is the documented way past it \u2014 it ignores<br>\n<code>framesEnabled<\/code> and only checks whether a frame is already pending. So the root<br>\nwidget re-arms it for exactly as long as both conditions hold:<br>\n<\/p>\n\n<div class=\"highlight js-code-highlight\">\n<pre class=\"highlight dart\"><code><span class=\"kt\">void<\/span> <span class=\"nf\">_keepPaintingWhileHidden<\/span><span class=\"p\">(<\/span><span class=\"n\">Duration<\/span> <span class=\"n\">_<\/span><span class=\"p\">)<\/span> <span class=\"p\">{<\/span>\n  <span class=\"k\">if<\/span> <span class=\"p\">(<\/span><span class=\"o\">!<\/span><span class=\"n\">mounted<\/span><span class=\"p\">)<\/span> <span class=\"k\">return<\/span><span class=\"p\">;<\/span>\n  <span class=\"kd\">final<\/span> <span class=\"n\">binding<\/span> <span class=\"o\">=<\/span> <span class=\"n\">WidgetsBinding<\/span><span class=\"o\">.<\/span><span class=\"na\">instance<\/span><span class=\"p\">;<\/span>\n  <span class=\"k\">if<\/span> <span class=\"p\">(<\/span><span class=\"n\">binding<\/span><span class=\"o\">.<\/span><span class=\"na\">framesEnabled<\/span><span class=\"p\">)<\/span> <span class=\"k\">return<\/span><span class=\"p\">;<\/span>              <span class=\"c1\">\/\/ page is back<\/span>\n  <span class=\"k\">if<\/span> <span class=\"p\">(<\/span><span class=\"n\">DocumentPip<\/span><span class=\"o\">.<\/span><span class=\"na\">popOutViewIds<\/span><span class=\"o\">.<\/span><span class=\"na\">isEmpty<\/span><span class=\"p\">)<\/span> <span class=\"k\">return<\/span><span class=\"p\">;<\/span>  <span class=\"c1\">\/\/ last window closed<\/span>\n  <span class=\"n\">binding<\/span><span class=\"o\">.<\/span><span class=\"na\">scheduleForcedFrame<\/span><span class=\"p\">();<\/span>\n  <span class=\"n\">binding<\/span><span class=\"o\">.<\/span><span class=\"na\">addPostFrameCallback<\/span><span class=\"p\">(<\/span><span class=\"n\">_keepPaintingWhileHidden<\/span><span class=\"p\">);<\/span>\n<span class=\"p\">}<\/span>\n<\/code><\/pre>\n\n<\/div>\n\n\n\n<p>Both guards end the loop on their own, which matters: this is a hand-rolled<br>\nframe pump, and one that cannot stop is a battery bug wearing the costume of a<br>\nfix. A post-frame callback rather than a persistent one for the same reason \u2014<br>\na persistent frame callback can never be removed, and would pin the <code>State<\/code><br>\nforever.<\/p>\n\n<p>And it is a Chromium problem specifically. Firefox 151 and 155 keep reporting<br>\nthe opener <code>visible<\/code> with a pop-out open \u2014 308 frames in 2.5s against 9 for the<br>\nsame page without one \u2014 so <code>framesEnabled<\/code> is never cleared and that first<br>\nguard returns immediately. The workaround is gated on the failure rather than<br>\non the browser, so where the failure does not exist it never runs.<\/p>\n<h2>\n  \n  \n  The keyboard is dead, but typing works\n<\/h2>\n\n<p>The second singleton. Flutter's <code>KeyboardBinding<\/code> attaches capture-phase<br>\n<code>keydown<\/code>\/<code>keyup<\/code> listeners <strong>once, globally, to the opener's <code>window<\/code><\/strong>. A<br>\npicture-in-picture window is a separate browsing context with its own <code>window<\/code>,<br>\nso while it has focus the engine hears nothing.<\/p>\n\n<p>What makes this genuinely nasty is which half breaks. Type into a text field in<br>\nthe pop-out and it works perfectly \u2014 the browser routes characters to the<br>\nfocused DOM element natively, and the engine reads them back off that element.<br>\nSo the feature you would test first is the one that is fine.<\/p>\n\n<p>Everything that travels as a <em>key event<\/em> is dead: <code>Shortcuts<\/code>, <code>Actions<\/code>,<br>\n<code>Focus.onKeyEvent<\/code>, <code>HardwareKeyboard<\/code>, Escape, Tab traversal. Text selection<br>\nhas the same shape, since all three of the engine's <code>selectionchange<\/code><br>\nsubscriptions are on the opener's <code>document<\/code> \u2014 so moving the caret with the<br>\narrow keys never reaches Flutter's editing state either.<\/p>\n\n<p>The fix is to replay the events into the opener and hand Flutter's focus to the<br>\npop-out's view when its window takes focus. Which produces its own trap worth<br>\nstealing:<br>\n<\/p>\n\n<div class=\"highlight js-code-highlight\">\n<pre class=\"highlight dart\"><code><span class=\"c1\">\/\/ One tear-off each, kept: every `.toJS` makes a NEW JS function, so<\/span>\n<span class=\"c1\">\/\/ removing with a second one silently leaves the listener attached.<\/span>\n<span class=\"kd\">late<\/span> <span class=\"kd\">final<\/span> <span class=\"n\">JSFunction<\/span> <span class=\"n\">_onKeyRef<\/span> <span class=\"o\">=<\/span> <span class=\"n\">_onKey<\/span><span class=\"o\">.<\/span><span class=\"na\">toJS<\/span><span class=\"p\">;<\/span>\n<\/code><\/pre>\n\n<\/div>\n\n\n\n<p><code>.toJS<\/code> on the same Dart function twice gives you two different JavaScript<br>\nobjects. <code>removeEventListener<\/code> compares by identity, finds nothing, removes<br>\nnothing, and reports success. That is not specific to picture-in-picture \u2014 it<br>\napplies to every <code>addEventListener<\/code> you write in Dart, and it fails as a leak<br>\nrather than as an error.<\/p>\n\n<p>Keys held when a window closes are released explicitly, too. Otherwise<br>\n<code>HardwareKeyboard<\/code> still believes they are down, and the next real press of the<br>\nsame key trips its consistency assertion \u2014 a crash somewhere else entirely,<br>\nminutes later.<\/p>\n<h2>\n  \n  \n  Two rules the browser imposes\n<\/h2>\n\n<p><strong><code>open()<\/code> must be the first <code>await<\/code> in a gesture handler.<\/strong> The browser only<br>\npermits this while handling a real click, and awaiting anything beforehand<br>\nspends the gesture. Load your data <em>after<\/em>:<br>\n<\/p>\n\n<div class=\"highlight js-code-highlight\">\n<pre class=\"highlight dart\"><code><span class=\"nl\">onPressed:<\/span> <span class=\"p\">()<\/span> <span class=\"kd\">async<\/span> <span class=\"p\">{<\/span>\n  <span class=\"kd\">final<\/span> <span class=\"n\">window<\/span> <span class=\"o\">=<\/span> <span class=\"k\">await<\/span> <span class=\"n\">DocumentPip<\/span><span class=\"o\">.<\/span><span class=\"na\">open<\/span><span class=\"p\">();<\/span>   <span class=\"c1\">\/\/ first<\/span>\n  <span class=\"kd\">final<\/span> <span class=\"n\">data<\/span> <span class=\"o\">=<\/span> <span class=\"k\">await<\/span> <span class=\"n\">fetchTrack<\/span><span class=\"p\">();<\/span>           <span class=\"c1\">\/\/ then<\/span>\n<span class=\"p\">}<\/span>\n<\/code><\/pre>\n\n<\/div>\n\n\n\n<p>Get it wrong and Chrome says <code>NotAllowedError<\/code> \u2014 which it also says when you<br>\ncall from an iframe, and when a window is already open. Three causes, one error<br>\nname. The package lists all three rather than asserting the common one, because<br>\nsending someone to fix a click handler that was already correct costs them an<br>\nafternoon.<\/p>\n\n<p><strong>One window, browser-wide.<\/strong> Not per tab, per browser. Opening a second closes<br>\nthe first, including one belonging to a completely different site. Your window<br>\ncan vanish because someone else opened theirs, so <code>closed<\/code> completes for that<br>\ntoo and you handle it like any other close.<\/p>\n\n<h2>\n  \n  \n  What the pattern actually is\n<\/h2>\n\n<p>None of these are bugs in Flutter, and I want to be precise about that. Turning<br>\nframes off for a hidden page is right. Binding the keyboard once is right.<br>\nAssuming one view is right for every app that has one view, which is very<br>\nnearly all of them.<\/p>\n\n<p>They are all the same <em>shape<\/em> of problem: a framework generalisation that holds<br>\nfor every case but yours, failing in a way that produces no error. A frozen<br>\nwindow looks like a rendering bug. A dead shortcut looks like a focus bug \u2014<br>\nespecially when typing still works. Neither logs anything.<\/p>\n\n<p>The only thing that actually worked was refusing to reason about it. Every<br>\nnumber in this post came from driving a real browser and counting frames,<br>\nbecause in all three cases the platform's report was true and the conclusion<br>\ndrawn from it was wrong, and no amount of reading the source would have told me<br>\nwhich.<\/p>\n\n<p>document_pip is MIT and on<br>\n<a href=\"https:\/\/pub.dev\/packages\/document_pip\" rel=\"noopener noreferrer\">pub.dev<\/a>; the source is at<br>\n<a href=\"https:\/\/github.com\/devShakib015\/flutter_packages\" rel=\"noopener noreferrer\">github.com\/devShakib015\/flutter_packages<\/a>.<\/p>\n\n\n\n\n<p><em>Originally published at <a href=\"https:\/\/devshakib.jumyn.com\/blog\/flutter-assumes-there-is-only-one-window\" rel=\"noopener noreferrer\">devshakib.jumyn.com<\/a>. I write about Flutter, Dart and the parts of shipping that are genuinely awkward \u2014 and publish the packages that came out of them at <a href=\"https:\/\/pub.dev\/publishers\/jumyn.com\/packages\" rel=\"noopener noreferrer\">pub.dev\/publishers\/jumyn.com<\/a>.<\/em><\/p>\n\n","category":["flutter","webdev","dart","programming"]},{"title":"What It Actually Takes to Run a Cross-Border Marketplace: Six Years of Shpper","pubDate":"Sun, 06 Sep 2026 12:16:06 +0000","link":"https:\/\/dev.to\/devshakib\/what-it-actually-takes-to-run-a-cross-border-marketplace-six-years-of-shpper-1a8h","guid":"https:\/\/dev.to\/devshakib\/what-it-actually-takes-to-run-a-cross-border-marketplace-six-years-of-shpper-1a8h","description":"<p>Shpper is a cross-border personal-shopping marketplace. A buyer wants something<br>\nthey cannot get where they live. A traveller is already flying that route with<br>\nunused luggage space. The platform introduces them, holds the money until the<br>\nitem arrives, and turns an empty few kilos in someone's suitcase into a delivery<br>\nnetwork.<\/p>\n\n<p>I am the CTO. I own the platform end to end \u2014 the Flutter apps for both sides,<br>\nthe backend, payments and escrow, identity verification, and the release<br>\npipeline. We are on version 14.2.0, rated 4.64 on the App Store from 72 ratings.<\/p>\n\n<p>Fourteen major versions is enough distance to say something useful about what<br>\nthis kind of product actually demands. Almost none of the hard parts were the<br>\nones I expected.<\/p>\n\n<h2>\n  \n  \n  You are not building one app\n<\/h2>\n\n<p>The first structural fact about a two-sided marketplace is that \"the app\" is two<br>\nproducts with opposed interests, and you ship both.<\/p>\n\n<p>The buyer wants their item cheaply, quickly, and with certainty it will arrive.<br>\nThe traveller wants to be paid well, carry as little risk as possible, and not be<br>\nblamed for customs. Those are not complementary desires. Every feature has to be<br>\ndesigned twice, from two points of view, and any change to the shared middle \u2014<br>\nthe request, the offer, the trip \u2014 reaches into both.<\/p>\n\n<p>This has a consequence people underestimate: <strong>your release cadence is bounded by<br>\nthe slower side.<\/strong> A change to how offers work is not shipped when the buyer app<br>\nships. It is shipped when both apps are live, on both platforms, and enough of<br>\nboth populations have updated. Mobile app review is not a build step you can<br>\noptimise away; it is a scheduling constraint that shapes how you design changes,<br>\nwhich is why so much of the logic has to be able to change without a release.<\/p>\n\n<h2>\n  \n  \n  The hard problem is trust, and it is not a feature\n<\/h2>\n\n<p>A marketplace's actual product is trust between strangers. Everything else is<br>\nplumbing.<\/p>\n\n<p>Consider what the platform is asking. A buyer sends money for an item that does<br>\nnot exist yet, to be bought by someone they have never met, in a country they<br>\nare not in, and carried across a border. A traveller agrees to spend their own<br>\nmoney on someone else's goods, in exchange for a promise of repayment, and to<br>\ncarry those goods through customs under their own name.<\/p>\n\n<p>Neither side would do that for a stranger. They do it because the platform<br>\nstands between them, and everything about the engineering follows from that<br>\nposition:<\/p>\n\n<p><strong>Escrow is not a payments feature, it is the entire trust mechanism.<\/strong> Money is<br>\nheld from the moment a deal is struck until the item is confirmed delivered. That<br>\nsingle design decision is what makes the rest possible \u2014 the buyer risks nothing<br>\nuntil they have the item, and the traveller knows the money genuinely exists<br>\nbefore they spend their own.<\/p>\n\n<p><strong>Identity verification is what makes escrow meaningful.<\/strong> Held funds only<br>\nprotect you if there is a real, identified person on the other end when<br>\nsomething goes wrong. Verification is not a compliance checkbox bolted on for<br>\nregulators; it is the thing that makes the escrow promise enforceable.<\/p>\n\n<p><strong>The state machine is the product.<\/strong> A request becomes an offer, becomes an<br>\naccepted deal, becomes a purchase, becomes a carried item, becomes a delivery,<br>\nbecomes a release of funds. Every transition can fail, and each failure needs a<br>\ndefined answer: what happens to the money, what each side sees, who is told what<br>\nand when. The unhappy paths outnumber the happy one, and they are where the<br>\nproduct either holds together or falls apart.<\/p>\n\n<h2>\n  \n  \n  Money makes everything irreversible\n<\/h2>\n\n<p>Ordinary app bugs are annoying. Bugs that touch money are a different category,<br>\nbecause you usually cannot fix them by shipping a patch.<\/p>\n\n<p>A double-charge is not resolved by correcting the code \u2014 the money has already<br>\nmoved, and now there is a human on the other end who is angry and correct to be.<br>\nA payout that fires twice is gone. A state transition that releases escrow early<br>\ncannot be un-released by a hotfix.<\/p>\n\n<p>This changes how you write things. Every money-moving operation has to be<br>\nidempotent, because networks retry and users tap twice and both of those are<br>\nnormal. State transitions have to be atomic, because a partial write in the<br>\nmiddle of a payment is worse than a failure. And ledgers have to be<br>\nappend-only \u2014 the record of what happened is not something you edit, because the<br>\nmoment it is editable it stops being evidence.<\/p>\n\n<p>None of this is exotic. All of it is the difference between a bug you fix and a<br>\nbug you refund.<\/p>\n\n<h2>\n  \n  \n  Cross-border is a category of its own\n<\/h2>\n\n<p>Everything above applies to any escrow marketplace. Crossing a border adds<br>\nproblems that are not software problems at all, and the engineering has to<br>\nabsorb them anyway.<\/p>\n\n<p>Prices move while a deal is open, because currencies move. Customs and duty are<br>\nreal costs that land on somebody, and the product has to be unambiguous about who<br>\nbefore anyone commits, not after. Items that are perfectly ordinary in one<br>\ncountry are restricted in another. Trips get cancelled, delayed, or rerouted, and<br>\na delivery network built from other people's travel plans inherits all the<br>\nvolatility of those plans.<\/p>\n\n<p>The engineering lesson is that a design which assumes stability will spend the<br>\nrest of its life being patched. Timelines slip, prices change, routes vanish.<br>\nBuilding for that from the start is much cheaper than discovering it across<br>\nfourteen versions.<\/p>\n\n<h2>\n  \n  \n  What being CTO of this actually means\n<\/h2>\n\n<p>The title suggests architecture diagrams. The reality is that owning it end to<br>\nend means the boundary between \"engineering problem\" and \"business problem\" does<br>\nnot exist.<\/p>\n\n<p>When a delivery goes wrong, the question is not only what the code did. It is<br>\nwhat the policy should be, who absorbs the cost, what both users are told, and<br>\nwhat changes so it happens less often. That is one conversation, not four, and<br>\nbeing the person who understands both the state machine and the commercial<br>\nconsequence is most of the job.<\/p>\n\n<p>The other half is choosing what not to build. Every marketplace has an infinite<br>\nbacklog because both sides always want more, and the sides want different things.<br>\nFourteen versions in, the decisions I am most glad about are almost all<br>\nsubtractions.<\/p>\n\n<h2>\n  \n  \n  The stack, briefly\n<\/h2>\n\n<p>Flutter for both apps, so one codebase covers iOS and Android on each side \u2014<br>\nwhich matters enormously when \"shipping a feature\" already means shipping two<br>\nproducts. Firebase for the backend. The choice that has aged best is not a<br>\ntechnology but a principle: <strong>keep as much behaviour as possible changeable<br>\nwithout an app release<\/strong>, because you cannot hotfix your way out of a marketplace<br>\nproblem when app review sits between you and your users.<\/p>\n\n\n\n\n<p>The thing I would tell someone starting one of these: you are not building<br>\nsoftware that connects buyers and sellers. You are building the reason two<br>\nstrangers are willing to take a risk on each other, and every technical decision<br>\neither strengthens that or quietly erodes it. Escrow, verification, and an<br>\nhonest state machine are not features on a roadmap. They are the product, and<br>\neverything else is how it is delivered.<\/p>\n\n<p>Shpper is at <a href=\"https:\/\/www.shpper.com\" rel=\"noopener noreferrer\">shpper.com<\/a>, and there is a longer<br>\n<a href=\"https:\/\/devshakib.jumyn.com\/work\/shpper\" rel=\"noopener noreferrer\">case study<\/a> on this site.<\/p>\n\n\n\n\n<p><em>Originally published at <a href=\"https:\/\/devshakib.jumyn.com\/blog\/what-it-takes-to-run-a-cross-border-marketplace\" rel=\"noopener noreferrer\">devshakib.jumyn.com<\/a>. I write about Flutter, Dart and the parts of shipping that are genuinely awkward \u2014 and publish the packages that came out of them at <a href=\"https:\/\/pub.dev\/publishers\/jumyn.com\/packages\" rel=\"noopener noreferrer\">pub.dev\/publishers\/jumyn.com<\/a>.<\/em><\/p>\n\n","category":["flutter","firebase","programming"]},{"title":"Your Flutter 404 Page Is Probably Crashing, and Your Server Is Probably Lying About It","pubDate":"Sun, 06 Sep 2026 12:16:05 +0000","link":"https:\/\/dev.to\/devshakib\/your-flutter-404-page-is-probably-crashing-and-your-server-is-probably-lying-about-it-k43","guid":"https:\/\/dev.to\/devshakib\/your-flutter-404-page-is-probably-crashing-and-your-server-is-probably-lying-about-it-k43","description":"<p>Someone sent me a screenshot of my own 404 page. Washed-out grey text on a light<br>\nbackground, barely readable, nothing like the dark theme every other page uses.<br>\nMy first thought was a styling bug.<\/p>\n\n<p>It was not a styling bug. The page was crashing before it could paint, and the<br>\nfallback I had built for exactly that case was doing its job. Underneath it was a<br>\nsecond bug that had been hiding the first one for months.<\/p>\n<h2>\n  \n  \n  Bug one: the status code was a lie\n<\/h2>\n\n<p>The site is a Flutter web app with every route prerendered to a real HTML file.<br>\nFirebase Hosting served those files, and anything unmatched hit a catch-all<br>\nrewrite:<br>\n<\/p>\n\n<div class=\"highlight js-code-highlight\">\n<pre class=\"highlight json\"><code><span class=\"nl\">\"rewrites\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"p\">[<\/span><span class=\"w\">\n  <\/span><span class=\"p\">{<\/span><span class=\"w\"> <\/span><span class=\"nl\">\"source\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"s2\">\"**\"<\/span><span class=\"p\">,<\/span><span class=\"w\"> <\/span><span class=\"nl\">\"destination\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"s2\">\"\/404.html\"<\/span><span class=\"w\"> <\/span><span class=\"p\">}<\/span><span class=\"w\">\n<\/span><span class=\"p\">]<\/span><span class=\"w\">\n<\/span><\/code><\/pre>\n\n<\/div>\n\n\n\n<p>That looks right. It is not, and the reason is worth internalising: <strong>a Firebase<br>\nrewrite always responds 200.<\/strong> That is what a rewrite <em>is<\/em> \u2014 serve this other<br>\ncontent under the requested URL. So every nonexistent URL on the site returned<br>\n<code>200 OK<\/code> with a page that said \"Page not found\".<\/p>\n\n<p>Browsers do not care. Crawlers care a great deal. A 200 means \"this is a real<br>\npage, index it\", so every typo, every dead inbound link, every scanner probing<br>\nfor <code>\/wp-admin<\/code> was eligible to be indexed as a real page. This is the soft 404,<br>\nand it is invisible from a browser because the page <em>looks<\/em> correct.<\/p>\n\n<p>The fix is to delete the catch-all rather than repoint it. With every real route<br>\nprerendered as a file, Firebase serves those directly, and for anything with no<br>\nmatching file it falls through to its own handling \u2014 which serves <code>404.html<\/code><br>\n<strong>with an actual 404 status<\/strong>.<\/p>\n\n<p>One caveat that will bite you if your app has client-only routes. My admin panel<br>\nhas no prerendered file, so removing the catch-all 404'd my own admin URL. It<br>\nneeds a rewrite scoped to that path alone:<br>\n<\/p>\n\n<div class=\"highlight js-code-highlight\">\n<pre class=\"highlight json\"><code><span class=\"nl\">\"rewrites\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"p\">[<\/span><span class=\"w\">\n  <\/span><span class=\"p\">{<\/span><span class=\"w\"> <\/span><span class=\"nl\">\"source\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"s2\">\"\/your-client-only-path\/**\"<\/span><span class=\"p\">,<\/span><span class=\"w\"> <\/span><span class=\"nl\">\"destination\"<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"s2\">\"\/index.html\"<\/span><span class=\"w\"> <\/span><span class=\"p\">}<\/span><span class=\"w\">\n<\/span><span class=\"p\">]<\/span><span class=\"w\">\n<\/span><\/code><\/pre>\n\n<\/div>\n\n\n\n<p>Scoped rewrites for the routes that genuinely need them; no catch-all; real 404s<br>\nfor everything else.<\/p>\n<h2>\n  \n  \n  Bug two: the page was crashing the whole time\n<\/h2>\n\n<p>Fixing the status code is what made me actually look at the page, and that is<br>\nwhen the grey rendering stopped looking like a theme problem.<\/p>\n\n<p>My prerendered HTML carries the page's text in the DOM, clipped to a single<br>\npixel, with a boot guard that reveals it if Flutter never paints. The idea is<br>\nthat a failed boot shows the content rather than a spinner turning forever. So<br>\nthe washed-out text was the guard firing correctly, telling me Flutter had<br>\ndied.<\/p>\n\n<p>The console had it:<br>\n<\/p>\n\n<div class=\"highlight js-code-highlight\">\n<pre class=\"highlight plaintext\"><code>GoError: There is no GoRouterState above the current context.\nThis method should only be called under the sub tree of a RouteBase.builder.\n<\/code><\/pre>\n\n<\/div>\n\n\n\n<p>Here is the chain. <code>errorBuilder<\/code> renders my <code>NotFoundPage<\/code>. That page uses the<br>\nsame shell as every other page, and the shell contains the nav bar, and the nav<br>\nbar asks which route is current so it can highlight the right link:<br>\n<\/p>\n\n<div class=\"highlight js-code-highlight\">\n<pre class=\"highlight dart\"><code><span class=\"kd\">final<\/span> <span class=\"n\">current<\/span> <span class=\"o\">=<\/span> <span class=\"n\">GoRouterState<\/span><span class=\"o\">.<\/span><span class=\"na\">of<\/span><span class=\"p\">(<\/span><span class=\"n\">context<\/span><span class=\"p\">)<\/span><span class=\"o\">.<\/span><span class=\"na\">uri<\/span><span class=\"o\">.<\/span><span class=\"na\">path<\/span><span class=\"p\">;<\/span>\n<\/code><\/pre>\n\n<\/div>\n\n\n\n<p>On every real route that is fine. On the 404 page it throws, because<br>\n<strong><code>errorBuilder<\/code> renders outside any <code>RouteBase.builder<\/code><\/strong> \u2014 there is no route<br>\nsubtree above it and therefore no <code>GoRouterState<\/code> to inherit.<\/p>\n<h2>\n  \n  \n  The fix that does not exist, and the one that also fails\n<\/h2>\n\n<p>The instinct is <code>maybeOf<\/code>. In go_router 17 <strong>there is no <code>GoRouterState.maybeOf<\/code>.<\/strong><br>\n<code>of()<\/code> throws unconditionally; there is no nullable variant to fall back to.<\/p>\n\n<p>So the next instinct is to ask the router itself, which definitely sits above<br>\neverything:<br>\n<\/p>\n\n<div class=\"highlight js-code-highlight\">\n<pre class=\"highlight dart\"><code><span class=\"n\">current<\/span> <span class=\"o\">=<\/span> <span class=\"n\">GoRouter<\/span><span class=\"o\">.<\/span><span class=\"na\">of<\/span><span class=\"p\">(<\/span><span class=\"n\">context<\/span><span class=\"p\">)<\/span><span class=\"o\">.<\/span><span class=\"na\">state<\/span><span class=\"o\">.<\/span><span class=\"na\">uri<\/span><span class=\"o\">.<\/span><span class=\"na\">path<\/span><span class=\"p\">;<\/span>   <span class=\"c1\">\/\/ also throws<\/span>\n<\/code><\/pre>\n\n<\/div>\n\n\n\n<p>I tried exactly this, and it fails too \u2014 differently, which is what makes it<br>\ninteresting. <code>GoRouter.state<\/code> reads <code>matches.last<\/code> on the current match list, and<br>\non an unmatched URL <strong>that list is empty<\/strong>, so you get a <code>StateError: Bad state:<br>\nNo element<\/code>. The accessor fails for precisely the reason you are on the 404 page<br>\nat all.<\/p>\n\n<p>Both router-side answers are dead ends. But the browser knows the path regardless<br>\nof what the router made of it:<br>\n<\/p>\n\n<div class=\"highlight js-code-highlight\">\n<pre class=\"highlight dart\"><code><span class=\"kt\">String<\/span> <span class=\"n\">current<\/span><span class=\"p\">;<\/span>\n<span class=\"k\">try<\/span> <span class=\"p\">{<\/span>\n  <span class=\"n\">current<\/span> <span class=\"o\">=<\/span> <span class=\"n\">GoRouterState<\/span><span class=\"o\">.<\/span><span class=\"na\">of<\/span><span class=\"p\">(<\/span><span class=\"n\">context<\/span><span class=\"p\">)<\/span><span class=\"o\">.<\/span><span class=\"na\">uri<\/span><span class=\"o\">.<\/span><span class=\"na\">path<\/span><span class=\"p\">;<\/span>\n<span class=\"p\">}<\/span> <span class=\"k\">catch<\/span> <span class=\"p\">(<\/span><span class=\"n\">_<\/span><span class=\"p\">)<\/span> <span class=\"p\">{<\/span>\n  <span class=\"n\">current<\/span> <span class=\"o\">=<\/span> <span class=\"kt\">Uri<\/span><span class=\"o\">.<\/span><span class=\"na\">base<\/span><span class=\"o\">.<\/span><span class=\"na\">path<\/span><span class=\"p\">;<\/span>\n<span class=\"p\">}<\/span>\n<\/code><\/pre>\n\n<\/div>\n\n\n\n<p>The broad catch is deliberate, and I would defend it specifically here.<br>\nDetermining which nav item to highlight is decoration. This widget renders inside<br>\nevery page on the site. Failing at decoration must never be able to take down the<br>\npage around it, and the two failure modes above are different exception types<br>\nfrom different call sites \u2014 narrowing the catch buys nothing except a chance to<br>\nmiss the third one.<\/p>\n\n<p>Nothing highlights on a 404, which is correct: no nav item corresponds to a page<br>\nthat does not exist.<\/p>\n\n<h2>\n  \n  \n  Why it survived so long\n<\/h2>\n\n<p>Because the two bugs concealed each other. The 404 page had been broken for<br>\nmonths and rendered identically whether it answered 200 or 404 \u2014 so nothing<br>\nlooked wrong from a browser, and nothing in the logs distinguished it. Crawlers<br>\nsaw 200 and were happy. I never visited my own 404 page, because why would I.<\/p>\n\n<p>The status-code fix did not cause the crash. It made someone look at the page,<br>\nwhich is a different and more useful thing.<\/p>\n\n<p>Two habits fall out of this. <strong>Check your 404 page's status code, not just its<br>\nappearance<\/strong> \u2014 <code>curl -o \/dev\/null -w '%{http_code}' https:\/\/yoursite\/nonsense<\/code><br>\ntakes two seconds and is the only way to see a soft 404. And <strong>actually load your<br>\nerror page in a browser after a routing change<\/strong>, because it is the one page in<br>\nyour app that renders through a completely different code path from every other<br>\npage, and therefore the one page your testing never touches.<\/p>\n\n\n\n\n<p><em>Originally published at <a href=\"https:\/\/devshakib.jumyn.com\/blog\/your-flutter-404-page-is-probably-crashing\" rel=\"noopener noreferrer\">devshakib.jumyn.com<\/a>. I write about Flutter, Dart and the parts of shipping that are genuinely awkward \u2014 and publish the packages that came out of them at <a href=\"https:\/\/pub.dev\/publishers\/jumyn.com\/packages\" rel=\"noopener noreferrer\">pub.dev\/publishers\/jumyn.com<\/a>.<\/em><\/p>\n\n","category":["flutter","seo","dart","mobile"]},{"title":"A Squarified Treemap by Hand, Because Charting Packages Cannot Drill Down","pubDate":"Sun, 06 Sep 2026 12:15:09 +0000","link":"https:\/\/dev.to\/devshakib\/a-squarified-treemap-by-hand-because-charting-packages-cannot-drill-down-353g","guid":"https:\/\/dev.to\/devshakib\/a-squarified-treemap-by-hand-because-charting-packages-cannot-drill-down-353g","description":"<p>Helm's storage tool shows your disk as a treemap: every folder a rectangle,<br>\nevery rectangle sized by how much space it takes. Click one and you are inside<br>\nit, looking at its children, with a breadcrumb trail back out. It goes from a<br>\n494 GB volume down to a single file.<\/p>\n\n<p>I drew it by hand. Not because the packages are bad, but because the thing that<br>\nmakes a treemap useful for disk space is the part they do not expose.<\/p>\n\n<h2>\n  \n  \n  What the packages give you, and what they don't\n<\/h2>\n\n<p>Search for a Flutter treemap and you will find several that work. Hand them a<br>\nlist of values, get back a coloured rectangle layout. For a dashboard showing<br>\nrevenue by region, that is the whole job.<\/p>\n\n<p>A disk browser needs four things beyond that, and each one reaches into the<br>\nlayout rather than sitting on top of it:<\/p>\n\n<ul>\n<li>\n<strong>Hit-testing that returns the node, not a coordinate.<\/strong> A tap has to resolve\nto <em>which folder<\/em>, at whatever depth you are currently at.<\/li>\n<li>\n<strong>Navigation as a first-class state.<\/strong> Diving into a rectangle re-lays out the\nentire canvas from a new root, and the breadcrumb has to be able to climb back.<\/li>\n<li>\n<strong>Labels that adapt.<\/strong> A rectangle 400 px wide gets a name and a size. One\n20 px wide gets nothing, because a clipped half-word is worse than blank space.<\/li>\n<li>\n<strong>Layout over a subtree, not a list.<\/strong> The input is not fifty values, it is a\ntree with a million nodes, and you lay out one level at a time.<\/li>\n<\/ul>\n\n<p>Any one of those is a fork of the package. All four means you are writing it<br>\nanyway, and you would rather own it than fight an abstraction built for a<br>\ndifferent problem.<\/p>\n\n<h2>\n  \n  \n  Squarified, and why it is worth the trouble\n<\/h2>\n\n<p>The naive treemap slices the rectangle repeatedly along one axis. It is easy and<br>\nit produces slivers \u2014 long thin shapes that are impossible to read, impossible<br>\nto tap, and misleading, because human eyes judge area badly when the aspect<br>\nratio is extreme.<\/p>\n\n<p>The squarified algorithm keeps rectangles as close to square as it can. The idea<br>\nis simple enough to hold in your head: sort children largest first, then add<br>\nthem one at a time to the current row while doing so <em>improves<\/em> the worst aspect<br>\nratio in that row. The moment adding one makes the worst ratio worse, close the<br>\nrow, and start a new one in the remaining space.<\/p>\n\n<p>That is the entire algorithm. Sort descending, accumulate greedily, close the<br>\nrow when the worst ratio stops improving, recurse into the space that is left.<\/p>\n\n<p>Two implementation notes that cost me time:<\/p>\n\n<p><strong>Sort descending or the greedy step is meaningless.<\/strong> The whole method assumes<br>\nyou place the biggest item first. Feed it unsorted input and it still produces<br>\na layout \u2014 a bad one, with no error to tell you why.<\/p>\n\n<p><strong>Guard against zero.<\/strong> Empty folders, and the remaining space after the last<br>\nrow, both produce zero-width or zero-height rectangles, which turn into<br>\ndivide-by-zero in the ratio computation and <code>NaN<\/code> in the layout. <code>NaN<\/code><br>\npropagates silently through a layout pass and you get an empty canvas with no<br>\nexception, which is a genuinely unpleasant thing to debug.<\/p>\n\n<h2>\n  \n  \n  Making it fast enough over a real disk\n<\/h2>\n\n<p>Scanning a 347 GB volume walks several hundred thousand files. Two things keep<br>\nthat from freezing the UI, and neither is about the treemap itself.<\/p>\n\n<p><strong>Scan off the main isolate.<\/strong> Walking a filesystem is not CPU-heavy so much as<br>\nrelentless \u2014 hundreds of thousands of stat calls. Done on the main isolate it<br>\nblocks the frame loop, and the app appears hung exactly while it is doing the<br>\nwork the user asked for.<\/p>\n\n<p><strong>Lay out one level at a time.<\/strong> The tree has a million nodes; the screen shows<br>\na few dozen rectangles. There is no reason to lay out anything but the current<br>\nlevel's children. Diving in is a fresh layout over a smaller subtree, which is<br>\nwhy it stays instant no matter how deep you go \u2014 the work is bounded by what is<br>\nvisible, not by what exists.<\/p>\n\n<p>The second point is the one that also solves the label problem. Because you only<br>\nlay out the visible level, you know each rectangle's real pixel size at paint<br>\ntime, so deciding whether a name fits is a measurement rather than a guess.<\/p>\n\n<h2>\n  \n  \n  The bit that has nothing to do with drawing\n<\/h2>\n\n<p>The hardest part of Helm's storage tool is not the treemap. It is making the<br>\nnumbers agree with the operating system.<\/p>\n\n<p>macOS reports <strong>purgeable<\/strong> space \u2014 snapshots and caches the system will reclaim<br>\nwhen it needs to. If you sum file sizes and ignore purgeable, your total<br>\ndisagrees with About This Mac, and when a user sees two numbers they believe the<br>\none from Apple. Correctly, too: yours is the one that is wrong.<\/p>\n\n<p>So the categories have to be disjoint buckets that reconcile to the volume's<br>\nreported capacity, purgeable included as its own slice rather than quietly<br>\ndropped or quietly counted as used. A beautiful visualisation of numbers the<br>\nuser does not trust is worth nothing, and trust here is a single comparison<br>\nagainst a system dialog they already know.<\/p>\n\n<h2>\n  \n  \n  Was it worth writing?\n<\/h2>\n\n<p>For a dashboard, no \u2014 use a package. For anything where the treemap is the<br>\ninterface rather than a picture of the data, you will end up writing it, because<br>\ndrill-down, hit-testing and adaptive labels all live below the API surface that<br>\na general charting package exposes.<\/p>\n\n<p>The reward is that it is genuinely the fastest way to answer \"what is eating my<br>\ndisk\". A list of the largest folders makes you read and compare. A treemap makes<br>\nthe answer the biggest thing on screen.<\/p>\n\n<p>Helm is free and MIT, and the treemap is in<br>\n<code>lib\/tools\/storage\/ui\/widgets\/treemap.dart<\/code> if you want to read it rather than<br>\nreimplement it: <a href=\"https:\/\/github.com\/devShakib015\/helm\" rel=\"noopener noreferrer\">github.com\/devShakib015\/helm<\/a>.<\/p>\n\n\n\n\n<p><em>Originally published at <a href=\"https:\/\/devshakib.jumyn.com\/blog\/a-squarified-treemap-by-hand-in-flutter\" rel=\"noopener noreferrer\">devshakib.jumyn.com<\/a>. I write about Flutter, Dart and the parts of shipping that are genuinely awkward \u2014 and publish the packages that came out of them at <a href=\"https:\/\/pub.dev\/publishers\/jumyn.com\/packages\" rel=\"noopener noreferrer\">pub.dev\/publishers\/jumyn.com<\/a>.<\/em><\/p>\n\n","category":["flutter","dart","macos","mobile"]},{"title":"The DNS Field That Could Run Anything as Root","pubDate":"Sun, 06 Sep 2026 12:15:08 +0000","link":"https:\/\/dev.to\/devshakib\/the-dns-field-that-could-run-anything-as-root-3jmo","guid":"https:\/\/dev.to\/devshakib\/the-dns-field-that-could-run-anything-as-root-3jmo","description":"<p>Sonar has a Control tab. One of the things it does is switch the DNS resolver<br>\nfor your Mac \u2014 tap Cloudflare, tap Quad9, or paste in the address of your own<br>\nPi-hole. Changing a network service's DNS servers is a privileged operation, so<br>\nthe app asks macOS for authorisation and runs <code>networksetup<\/code> on the other side<br>\nof that prompt.<\/p>\n\n<p>That is a completely ordinary feature. It is also where I put a<br>\ncommand-injection that ran as root.<\/p>\n\n<h2>\n  \n  \n  The shape of the bug\n<\/h2>\n\n<p>The custom-resolver field exists because people run Pi-hole and NextDNS on their<br>\nown networks, and hardcoding four public resolvers would have been useless to<br>\nthem. So the field takes a string. The string goes into the arguments of a<br>\nprivileged command. Between those two facts there was nothing at all.<\/p>\n\n<p>The mental model that produced this is worth naming, because it is the one that<br>\nproduces most injection bugs: <em>it's my own app, and the field is for an IP<br>\naddress, so it will contain an IP address.<\/em> Both halves are wrong. The field is<br>\nfor whatever the user types, and \"my own app\" is not a security boundary \u2014 the<br>\nwhole point of asking for authorisation is that the code on the other side of<br>\nthe prompt has more power than the code before it.<\/p>\n\n<p>A privileged call is a trust boundary even when both sides are yours. Especially<br>\nwhen both sides are yours, because that is exactly when you stop looking.<\/p>\n\n<h2>\n  \n  \n  Why \"it's just a settings field\" is not a defence\n<\/h2>\n\n<p>Two arguments came to mind while I was fixing it, and both are bad.<\/p>\n\n<p><strong>\"The user would only be attacking themselves.\"<\/strong> Sometimes. But the value<br>\nthat reaches that field does not have to be typed by the person sitting at the<br>\nmachine. Anything that can put text into a field \u2014 a paste from a webpage, a<br>\nsupport article that says \"paste this into the custom DNS box\", an<br>\naccessibility-driven automation \u2014 becomes a way to run a command with elevated<br>\nprivileges. Social engineering is a lot easier when the last step is <em>paste this<br>\nstring into the box and click Apply<\/em>, and the app has already trained the user<br>\nto expect an admin prompt at that point.<\/p>\n\n<p><strong>\"The authorisation prompt is the protection.\"<\/strong> The prompt authorises <em>the<br>\noperation the app described<\/em>, not whatever ends up in the argument list. The<br>\nuser consenting to \"change DNS servers\" has not consented to anything else the<br>\nstring can be made to mean. Consent obtained for one action does not cover a<br>\ndifferent one that happens to travel through the same code path.<\/p>\n\n<h2>\n  \n  \n  The fix, in the right order\n<\/h2>\n\n<p>The instinct is to reach for escaping \u2014 quote the string, strip the dangerous<br>\ncharacters, block a list of metacharacters. That is the wrong first move. Every<br>\nblocklist is a bet that you thought of everything, and you did not; that is what<br>\na blocklist <em>is<\/em>.<\/p>\n\n<p>Validate against what the value is allowed to be, then pass it in a way that<br>\ncannot be reinterpreted.<\/p>\n\n<p><strong>Allowlist the shape.<\/strong> A DNS server is an IPv4 or IPv6 address. That is a<br>\ntiny, completely specified grammar. Parse the string into an address type and<br>\nreject anything that does not parse \u2014 not \"remove the bad characters\", but<br>\n<em>refuse the input<\/em>. If it is not an address, there is no version of it that is<br>\nsafe to pass along, so there is nothing to sanitise.<\/p>\n\n<p><strong>Never build a shell string.<\/strong> Pass arguments as an array to the process, so<br>\nthere is no shell to interpret them and no quoting to get right. This is the<br>\npart people skip because a formatted string is quicker to write, and it is the<br>\nsingle highest-value habit in this whole area: if there is no shell, an<br>\ninjection has nothing to inject into.<\/p>\n\n<p><strong>Keep the privileged surface small.<\/strong> The privileged side should accept the<br>\nnarrowest possible instruction \u2014 \"set the resolvers for this service to these<br>\nvalidated addresses\" \u2014 not a command to run. The less expressive the interface<br>\nacross the trust boundary, the less there is to abuse.<\/p>\n\n<p>Those three in that order. Validation is the fix; the array-argument call is the<br>\nbelt; the narrow interface is the braces.<\/p>\n\n<h2>\n  \n  \n  The other twenty-five\n<\/h2>\n\n<p>I found this during a full pass over the app before shipping version 2, which<br>\nturned up twenty-six fixes. Three others are worth repeating because they are<br>\nthe same category of mistake wearing different clothes.<\/p>\n\n<p><strong>Credentials in a plist.<\/strong> NextDNS and Pi-hole integrations need API tokens.<br>\nThey were sitting in preferences, which is a plain file that any process running<br>\nas you can read, and which gets swept into backups and sync. They belong in the<br>\nKeychain, which is the one place on macOS designed to hold them. That is not a<br>\nhard fix; it is a fix nobody makes until they look.<\/p>\n\n<p><strong>CSV export as an attack.<\/strong> Sonar exports the network map. A device name is<br>\nattacker-controlled \u2014 anyone on your network can name their device. If a name<br>\nstarts with <code>=<\/code>, a spreadsheet treats it as a formula when the file is opened.<br>\nExport is a place where your data becomes someone else's input, and that<br>\ndirection gets almost no attention compared to import.<\/p>\n\n<p><strong>Corrupt-safe writes.<\/strong> The device history was written in place. A crash or a<br>\nfull disk mid-write left a truncated file, and the next launch would read it,<br>\nfail, and start empty \u2014 silently destroying months of history. Writes are now<br>\natomic, and a file that fails to parse is preserved rather than overwritten. The<br>\ndistinction matters: losing data is bad, but losing it <em>quietly<\/em> means nobody<br>\nfinds out until they need it.<\/p>\n\n<h2>\n  \n  \n  What I would tell myself a version earlier\n<\/h2>\n\n<p>None of these were exotic. Every one is in the first chapter of anything written<br>\nabout the subject, and I still shipped them, in a tool whose entire premise is<br>\nthat it tells you when your network is exposed.<\/p>\n\n<p>The reason is not ignorance, it is attention. Features get scrutiny while you<br>\nbuild them and then never again. The DNS field was written in an afternoon,<br>\nworked immediately, and was never reopened, because working code does not ask<br>\nfor anything. A settings field that has never failed is the safest-feeling code<br>\nin the project and one of the most dangerous.<\/p>\n\n<p>So the practice that actually helps is not a checklist. It is a question, asked<br>\nof the code you are least worried about: <em>where does user input cross into<br>\nsomething with more power than the code that received it?<\/em> Find those crossings<br>\nand look at every one, especially the boring ones you wrote in an afternoon and<br>\nnever thought about again.<\/p>\n\n<p>Sonar is free and open source under MIT, and all of this is in the history if<br>\nyou want to read the diffs rather than take my word for it:<br>\n<a href=\"https:\/\/github.com\/devShakib015\/sonar\" rel=\"noopener noreferrer\">github.com\/devShakib015\/sonar<\/a>.<\/p>\n\n\n\n\n<p><em>Originally published at <a href=\"https:\/\/devshakib.jumyn.com\/blog\/the-dns-field-that-could-run-anything-as-root\" rel=\"noopener noreferrer\">devshakib.jumyn.com<\/a>. I write about Flutter, Dart and the parts of shipping that are genuinely awkward \u2014 and publish the packages that came out of them at <a href=\"https:\/\/pub.dev\/publishers\/jumyn.com\/packages\" rel=\"noopener noreferrer\">pub.dev\/publishers\/jumyn.com<\/a>.<\/em><\/p>\n\n","category":["security","macos","swift","programming"]},{"title":"Saving a Real File From Flutter Web, Instead of Downloading Another Copy","pubDate":"Sat, 05 Sep 2026 23:32:13 +0000","link":"https:\/\/dev.to\/devshakib\/saving-a-real-file-from-flutter-web-instead-of-downloading-another-copy-5ah3","guid":"https:\/\/dev.to\/devshakib\/saving-a-real-file-from-flutter-web-instead-of-downloading-another-copy-5ah3","description":"<p>Build an editor on Flutter Web. The user opens <code>budget.csv<\/code>, edits it, hits<br>\nsave. They get <code>budget.csv<\/code> in their Downloads folder.<\/p>\n\n<p>They edit again, save again: <code>budget (1).csv<\/code>.<\/p>\n\n<p>Again: <code>budget (2).csv<\/code>.<\/p>\n\n<p>Their real file \u2014 the one on their Desktop that they opened \u2014 has never been<br>\ntouched. By the end of an afternoon they have nine numbered copies and no idea<br>\nwhich is current. This is not a Flutter problem; it is what \"saving\" has meant on<br>\nthe web for twenty years. The browser hands you a download, and a download is<br>\nalways a new file.<\/p>\n<h2>\n  \n  \n  The API that changed this\n<\/h2>\n\n<p>The <strong>File System Access API<\/strong> gives a page a real handle to a real file, with<br>\nthe user's permission, granted through the browser's own picker. With a handle<br>\nyou can write back to the same file. Not a copy. That one.<\/p>\n\n<p>It is a genuine capability, gated properly: the user picks the file, the browser<br>\nmediates every access, and the permission can be revoked. There is no way for a<br>\npage to reach a file the user has not deliberately handed over.<br>\n<\/p>\n\n<div class=\"highlight js-code-highlight\">\n<pre class=\"highlight dart\"><code><span class=\"kd\">final<\/span> <span class=\"n\">file<\/span> <span class=\"o\">=<\/span> <span class=\"k\">await<\/span> <span class=\"n\">FileSystemAccess<\/span><span class=\"o\">.<\/span><span class=\"na\">openFile<\/span><span class=\"p\">();<\/span>\n<span class=\"k\">await<\/span> <span class=\"n\">file<\/span><span class=\"o\">.<\/span><span class=\"na\">write<\/span><span class=\"p\">(<\/span><span class=\"n\">bytes<\/span><span class=\"p\">);<\/span>   <span class=\"c1\">\/\/ the same file, in place<\/span>\n<\/code><\/pre>\n\n<\/div>\n\n\n\n<p>No download. No <code>(1)<\/code>. The file on their Desktop now has their edits.<\/p>\n\n<h3>\n  \n  \n  Why the old way works the way it does\n<\/h3>\n\n<p>The download-a-copy behaviour is not laziness on anyone's part. For most of the<br>\nweb's history, a page having write access to your filesystem would have been an<br>\nenormous security hole, so the only sanctioned direction was <em>out<\/em>: the page<br>\nproduces bytes, the browser saves them somewhere the page cannot see or reach.<\/p>\n\n<p>That is why <code>&lt;a download&gt;<\/code> and the blob-URL trick that every Flutter Web file-save<br>\nsnippet uses cannot ever overwrite. They are not writing a file. They are handing<br>\nthe browser a payload and asking it to save one, and \"save one\" means a new one.<\/p>\n\n<p>Understanding that is what tells you the File System Access API is a genuinely<br>\ndifferent mechanism rather than a nicer wrapper over the same thing. The<br>\npermission model changed; the API followed.<\/p>\n<h2>\n  \n  \n  Coming back after a reload\n<\/h2>\n\n<p>The second half is what makes it feel like an application rather than a web page.<\/p>\n\n<p>Handles can be <strong>persisted<\/strong>. Store one, and after a page reload \u2014 or the next<br>\nmorning \u2014 you can ask for that same file again. The browser will re-prompt for<br>\npermission, which is correct and is the point, but the user is confirming access<br>\nto a file they already chose rather than hunting through a picker to find it<br>\nagain.<\/p>\n\n<p>That single behaviour is the difference between \"a web tool I paste things into\"<br>\nand \"the thing I edit my file with\".<\/p>\n\n<p>Handles are stored in IndexedDB \u2014 they are structured-cloneable objects, not<br>\nstrings \u2014 so they persist like any other browser data and survive until the user<br>\nclears site data. Which means the recovery flow is:<\/p>\n\n<ol>\n<li>On startup, look for a stored handle.<\/li>\n<li>If there is one, ask whether permission is still granted.<\/li>\n<li>If it is, open silently and show the document.<\/li>\n<li>If it is not, show one button: <em>Reopen budget.csv<\/em>.<\/li>\n<\/ol>\n\n<p>Step four is the honest version of \"restore my session\". You are not<br>\nre-prompting for a file the user must find again; you are asking them to confirm<br>\na file you both already know about.<\/p>\n<h3>\n  \n  \n  Directories, not just files\n<\/h3>\n\n<p>The same API grants handles to whole directories. That unlocks a different class<br>\nof application entirely \u2014 a project folder, a photo set, a folder of Markdown<br>\nnotes \u2014 where the app can enumerate, read and write within the folder the user<br>\nchose, and nothing outside it.<\/p>\n\n<p>For anything resembling an editor, that is the difference between a single-file<br>\ntoy and something people keep their work in.<\/p>\n<h2>\n  \n  \n  What you must handle\n<\/h2>\n\n<p>This is where an honest package earns its keep, because the API is not<br>\nuniversally available and pretending otherwise produces a broken app on a third<br>\nof browsers.<\/p>\n\n<p><strong>Support is real but partial.<\/strong> Chrome, Edge and other Chromium browsers have<br>\nit. Safari and Firefox largely do not. So the capability check is not optional<br>\nhousekeeping \u2014 it decides which UI you show:<br>\n<\/p>\n\n<div class=\"highlight js-code-highlight\">\n<pre class=\"highlight dart\"><code><span class=\"k\">if<\/span> <span class=\"p\">(<\/span><span class=\"k\">await<\/span> <span class=\"n\">FileSystemAccess<\/span><span class=\"o\">.<\/span><span class=\"na\">isSupported<\/span><span class=\"p\">)<\/span> <span class=\"p\">{<\/span>\n  <span class=\"c1\">\/\/ \"Save\" \u2014 writes in place<\/span>\n<span class=\"p\">}<\/span> <span class=\"k\">else<\/span> <span class=\"p\">{<\/span>\n  <span class=\"c1\">\/\/ \"Download a copy\" \u2014 the old behaviour, honestly labelled<\/span>\n<span class=\"p\">}<\/span>\n<\/code><\/pre>\n\n<\/div>\n\n\n\n<p>Label the fallback accurately. A button that says <em>Save<\/em> and silently produces<br>\n<code>budget (3).csv<\/code> is worse than a button that says <em>Download a copy<\/em>, because the<br>\nfirst one lies about what just happened.<\/p>\n\n<p><strong>Permission is per-handle and revocable.<\/strong> A write can fail because the user<br>\nrevoked access, or the file moved, or the browser dropped the grant. Handle it as<br>\na normal outcome rather than an exception path you never test.<\/p>\n\n<p><strong>It needs a user gesture.<\/strong> The picker cannot be opened from a timer or an<br>\nasync continuation far from a tap. Open it directly in the button handler.<\/p>\n\n<h3>\n  \n  \n  Design for the fallback, do not bolt it on\n<\/h3>\n\n<p>The temptation is to write the good path and add a fallback later. That produces<br>\nan app whose Safari experience is an afterthought, and Safari is not a rounding<br>\nerror.<\/p>\n\n<p>A structure that works: define one interface with <code>open<\/code>, <code>save<\/code> and <code>saveAs<\/code>.<br>\nImplement it twice \u2014 once over the File System Access API, once over the classic<br>\npicker-and-download. Choose at startup. The rest of your app never branches, and<br>\nthe difference is confined to which implementation is behind the interface and<br>\nwhat the save button says.<\/p>\n\n<p>The label matters more than it sounds. If the fallback button says <strong>Save<\/strong> and<br>\nproduces <code>budget (3).csv<\/code>, the app has lied about what it did. If it says<br>\n<strong>Download a copy<\/strong>, the user understands the platform limitation immediately and<br>\ndoes not blame the app when their original file is unchanged.<\/p>\n\n<h2>\n  \n  \n  Why this matters more on Flutter Web than elsewhere\n<\/h2>\n\n<p>Flutter Web is unusually good at the kind of app this unlocks \u2014 editors,<br>\nspreadsheets, diagram tools, code playgrounds, anything with a document. Those<br>\nare exactly the apps where \"download another copy\" is most obviously wrong.<\/p>\n\n<p>It is also the gap people cite when they say Flutter Web \"isn't ready for real<br>\napps\". Not being able to save a file properly is a fair thing to hold against a<br>\nplatform. It is no longer true, on the browsers that matter, and it is worth<br>\nknowing.<\/p>\n\n<h2>\n  \n  \n  A note on security, since people ask\n<\/h2>\n\n<p>Every time this API comes up someone asks whether a web page can now read their<br>\nwhole disk. It cannot, and the reasons are worth knowing so you can answer the<br>\nquestion when a user asks you:<\/p>\n\n<ul>\n<li>\n<strong>Every handle comes from a user gesture<\/strong> through the browser's own picker.\nA page cannot construct a handle to a path it names.<\/li>\n<li>\n<strong>Access is per-handle.<\/strong> Being granted <code>budget.csv<\/code> grants nothing about the\nfolder it sits in, or any other file.<\/li>\n<li>\n<strong>The browser blocks sensitive locations<\/strong> \u2014 system directories, and in\nChromium's case a maintained blocklist that includes things like the user's\nhome root and library folders.<\/li>\n<li>\n<strong>Permission is revocable<\/strong> and is re-confirmed after a reload rather than\ngranted permanently in the background.<\/li>\n<li>\n<strong>It requires a secure context.<\/strong> No HTTPS, no API.<\/li>\n<\/ul>\n\n<p>The design is closer to \"the user hands your app a file\" than to \"your app gets<br>\nfilesystem access\", which is the right shape for a capability this significant.<\/p>\n\n<h2>\n  \n  \n  Try it\n<\/h2>\n\n<p><a href=\"https:\/\/devshakib.jumyn.com\/packages\/file_system_access\" rel=\"noopener noreferrer\"><code>file_system_access<\/code><\/a> is on pub.dev \u2014 MIT, 160\/160<br>\npub points. It wraps the picker, the in-place write, the persisted handle and the<br>\ncapability check.<\/p>\n\n<p>It returns <code>false<\/code> from the capability check on browsers that cannot do this,<br>\nrather than throwing or half-working \u2014 so your fallback is a decision you make at<br>\nstartup rather than a crash you discover from a Safari user three weeks after<br>\nlaunch.<\/p>\n\n\n\n\n<p><em>Originally published at <a href=\"https:\/\/devshakib.jumyn.com\/blog\/flutter-web-save-file-in-place\" rel=\"noopener noreferrer\">devshakib.jumyn.com<\/a>. I write about Flutter, Dart and the parts of shipping that are genuinely awkward \u2014 and publish the packages that came out of them at <a href=\"https:\/\/pub.dev\/publishers\/jumyn.com\/packages\" rel=\"noopener noreferrer\">pub.dev\/publishers\/jumyn.com<\/a>.<\/em><\/p>\n\n","category":["flutter","dart","mobile","programming"]}]}}