<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Manoel Martins</title>
    <description>The latest articles on DEV Community by Manoel Martins (@manoelmartins1980).</description>
    <link>https://dev.to/manoelmartins1980</link>
    <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%2F4098759%2F376be760-46c9-41ba-83ef-b231d862a2d1.jpg</url>
      <title>DEV Community: Manoel Martins</title>
      <link>https://dev.to/manoelmartins1980</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/manoelmartins1980"/>
    <language>en</language>
    <item>
      <title>Six things I learned putting a REST API in front of Google Tasks</title>
      <dc:creator>Manoel Martins</dc:creator>
      <pubDate>Thu, 29 Oct 2026 23:03:59 +0000</pubDate>
      <link>https://dev.to/manoelmartins1980/six-things-i-learned-putting-a-rest-api-in-front-of-google-tasks-3pce</link>
      <guid>https://dev.to/manoelmartins1980/six-things-i-learned-putting-a-rest-api-in-front-of-google-tasks-3pce</guid>
      <description>&lt;p&gt;&lt;strong&gt;Tested on Python 3.12, FastAPI 0.156, Uvicorn 0.54 and google-api-python-client 2.201, on Windows 10&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I keep my personal to-dos in Google Tasks, and I wanted my scripts — and my AI assistant — to be able to read and write them. The official client library works, but every script would need its own OAuth flow, its own token file and its own quirks.&lt;/p&gt;

&lt;p&gt;So I put a small FastAPI service in front of it. One OAuth token, one place to handle errors, and a plain HTTP interface with a single API key for everything else on my machine.&lt;/p&gt;

&lt;p&gt;Writing the endpoints took an afternoon. Getting it to the point where it starts with Windows and runs unattended took a few more evenings. These are the six things that cost me time along the way.&lt;/p&gt;




&lt;h2&gt;
  
  
  The setup
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;scripts / AI assistant ──(HTTP + X-API-Key)──&amp;gt; google-tasks-api ──(OAuth)──&amp;gt; Google Tasks API
                                               127.0.0.1:8001
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Endpoint&lt;/th&gt;
&lt;th&gt;What it does&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;GET/POST /tasklists&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;list / create task lists&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;GET/PATCH/DELETE /tasklists/{id}&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;read / rename / delete a list&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;GET/POST /tasklists/{id}/tasks&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;list / create tasks (&lt;code&gt;parent&lt;/code&gt; for subtasks)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;GET/PATCH/DELETE /tasklists/{id}/tasks/{task}&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;read / edit / delete&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;POST .../{task}/complete&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;mark as done&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;POST .../{task}/move&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;reorder, nest under a parent, or move to another list&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;GET /health&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;liveness, no key required&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;code&gt;@default&lt;/code&gt; works as a list ID for the user's default list. Adding a task from any script is one request:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl &lt;span class="nt"&gt;-X&lt;/span&gt; POST http://127.0.0.1:8001/tasklists/@default/tasks &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;-H&lt;/span&gt; &lt;span class="s2"&gt;"X-API-Key: &lt;/span&gt;&lt;span class="nv"&gt;$API_KEY&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="nt"&gt;-H&lt;/span&gt; &lt;span class="s2"&gt;"Content-Type: application/json"&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;-d&lt;/span&gt; &lt;span class="s1"&gt;'{"title": "Renew passport", "due": "2026-11-30"}'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The service itself is about 300 lines. The interesting part was everything around it.&lt;/p&gt;




&lt;h2&gt;
  
  
  1 — The API has no "due time". Only a date.
&lt;/h2&gt;

&lt;p&gt;In the Google Tasks app you can set a time on a task. Through the API, you can't. The documentation says it plainly once you find it: the time portion of &lt;code&gt;due&lt;/code&gt; is discarded when you write it, and it is never returned when you read it. A task with a time set in the app comes back from the API as midnight UTC.&lt;/p&gt;

&lt;p&gt;If the API accepts a &lt;code&gt;datetime&lt;/code&gt;, users will send &lt;code&gt;2026-10-22T15:00&lt;/code&gt;, get a &lt;code&gt;200 OK&lt;/code&gt;, and silently lose the &lt;code&gt;15:00&lt;/code&gt;. So the input model only accepts a date:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;TaskIn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;BaseModel&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Field&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;min_length&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;max_length&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1024&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;due&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;date&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Field&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;description&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Date only: the Google Tasks API discards the time.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;due_to_rfc3339&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;date&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isoformat&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;T00:00:00.000Z&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now &lt;code&gt;"22/10/2026"&lt;/code&gt; or &lt;code&gt;"tomorrow"&lt;/code&gt; fail with a &lt;code&gt;422&lt;/code&gt; before anything reaches Google, and nobody is promised a precision the backend doesn't have. If you really need a time, that's a calendar event, not a task.&lt;/p&gt;




&lt;h2&gt;
  
  
  2 — "Internal" vs "External" decides whether your token dies every week
&lt;/h2&gt;

&lt;p&gt;For a personal tool, the OAuth consent screen looks like bureaucracy. It isn't. With an &lt;strong&gt;External&lt;/strong&gt; app in &lt;strong&gt;Testing&lt;/strong&gt; status, Google issues refresh tokens that expire after 7 days. Your "unattended" service stops working every week until you click through the browser again. You also get the "Google hasn't verified this app" warning every time.&lt;/p&gt;

&lt;p&gt;If the account belongs to a Google Workspace organization, choose &lt;strong&gt;Internal&lt;/strong&gt; instead. No test-user list, no unverified-app warning, and the refresh token doesn't have the 7-day limit.&lt;/p&gt;

&lt;p&gt;The client type matters too. It's &lt;strong&gt;Desktop app&lt;/strong&gt;: the authorization flow opens a browser and receives the code on a temporary &lt;code&gt;localhost&lt;/code&gt; port. A "Web application" client would require registered redirect URIs and doesn't fit this flow.&lt;/p&gt;




&lt;h2&gt;
  
  
  3 — The OAuth loopback flow has a stopwatch
&lt;/h2&gt;

&lt;p&gt;The first authorization run timed out while I was still in the console. By the time I approved access, the browser landed on:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;localhost:62838/?state=...&amp;amp;code=4/0AX...
ERR_CONNECTION_REFUSED
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The approval itself had worked: the code is right there in the URL. But the tiny local server that was supposed to receive it had already given up, so the code went nowhere.&lt;/p&gt;

&lt;p&gt;Two lessons:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Have the console set up &lt;strong&gt;before&lt;/strong&gt; you run the authorization script. Approve right away.&lt;/li&gt;
&lt;li&gt;If you see &lt;code&gt;ERR_CONNECTION_REFUSED&lt;/code&gt; on a &lt;code&gt;localhost&lt;/code&gt; URL that has a &lt;code&gt;code=&lt;/code&gt; in it, nothing is broken. Just run the authorization script again.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Then test the part everyone forgets: expiry. I edited the token file's &lt;code&gt;expiry&lt;/code&gt; to 2020 and called the API. It answered &lt;code&gt;200&lt;/code&gt;, and &lt;code&gt;token.json&lt;/code&gt; came back with a fresh expiry. The refresh path works without anyone touching it, which is the whole point of an unattended service.&lt;/p&gt;




&lt;h2&gt;
  
  
  4 — &lt;code&gt;pythonw&lt;/code&gt; has no stdout, and Uvicorn's logging needs one
&lt;/h2&gt;

&lt;p&gt;To start the API at login without a console window, the obvious choice on Windows is a scheduled task running &lt;code&gt;pythonw.exe -m uvicorn ...&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The task registered fine, started, and showed &lt;code&gt;Ready&lt;/code&gt; a second later. Nothing was listening on the port. Last run result: &lt;code&gt;1&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;pythonw&lt;/code&gt; gives the process no &lt;code&gt;sys.stdout&lt;/code&gt; or &lt;code&gt;sys.stderr&lt;/code&gt;; they're &lt;code&gt;None&lt;/code&gt;. Uvicorn's default logging configuration creates stream handlers on them and fails during startup, before the app ever loads. You can reproduce it in one line:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;stdout&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;stderr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;logging.config&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;uvicorn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;config&lt;/span&gt;
&lt;span class="n"&gt;logging&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;dictConfig&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;uvicorn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;LOGGING_CONFIG&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# exits with an error
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The fix is a tiny launcher that gives the process somewhere to write. As a bonus, you finally get a log file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# scripts/serve.py
&lt;/span&gt;&lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;stdout&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;stderr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;log_dir&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;api.log&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;a&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;encoding&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;utf-8&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;buffering&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;uvicorn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;app.main:app&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;host&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;settings&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;host&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;port&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;settings&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;port&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The scheduled task now runs &lt;code&gt;pythonw -m scripts.serve&lt;/code&gt;, with no &lt;code&gt;--reload&lt;/code&gt;. Reload belongs to development, not to a service.&lt;/p&gt;




&lt;h2&gt;
  
  
  5 — "Protect every route" also protected &lt;code&gt;/health&lt;/code&gt;, and still left &lt;code&gt;/docs&lt;/code&gt; open
&lt;/h2&gt;

&lt;p&gt;My first version added the API key check at the application level:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;FastAPI&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dependencies&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nc"&gt;Depends&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;require_api_key&lt;/span&gt;&lt;span class="p"&gt;)])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That looked like the safe default. A test showed two surprises:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;/health&lt;/code&gt; required the key.&lt;/strong&gt; Any monitor would need a secret just to ask "are you alive?". The health endpoint returns &lt;code&gt;{"status": "ok"}&lt;/code&gt;: no data, no Google call, no token check. Leaving it open costs nothing.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;/docs&lt;/code&gt; and &lt;code&gt;/openapi.json&lt;/code&gt; did &lt;em&gt;not&lt;/em&gt; require the key.&lt;/strong&gt; FastAPI serves the documentation routes outside your dependencies. Anyone who can reach the port sees the full map of the API, even though they can't call it.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So the key moved to the routers that hold data, and &lt;code&gt;/health&lt;/code&gt; stays deliberately open:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;include_router&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tasklists&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;router&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;dependencies&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nc"&gt;Depends&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;require_api_key&lt;/span&gt;&lt;span class="p"&gt;)])&lt;/span&gt;
&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;include_router&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tasks&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;router&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;dependencies&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nc"&gt;Depends&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;require_api_key&lt;/span&gt;&lt;span class="p"&gt;)])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;While the service listens on &lt;code&gt;127.0.0.1&lt;/code&gt; only, an open &lt;code&gt;/docs&lt;/code&gt; is acceptable. The day it moves to a home lab server, &lt;code&gt;/docs&lt;/code&gt; gets protected or disabled, and HTTPS goes in front. That's written down as a checklist item, not left to memory.&lt;/p&gt;

&lt;p&gt;The security review before publishing found one more thing: the check itself &lt;strong&gt;failed open&lt;/strong&gt;. With &lt;code&gt;API_KEY&lt;/code&gt; empty, it simply returned, so anyone who cloned the repo and forgot the &lt;code&gt;.env&lt;/code&gt; exposed their whole Google Tasks. Now it fails closed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;require_api_key&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Security&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_header&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;settings&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;api_key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nc"&gt;HTTPException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;HTTP_503_SERVICE_UNAVAILABLE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;API_KEY is not configured in .env&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;secrets&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;compare_digest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;settings&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;api_key&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nc"&gt;HTTPException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;HTTP_401_UNAUTHORIZED&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Invalid X-API-Key&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A missing configuration should stop the service, not open it.&lt;/p&gt;




&lt;h2&gt;
  
  
  6 — The real API disagrees with your mocks in small ways
&lt;/h2&gt;

&lt;p&gt;The test suite has 31 tests with the Google client mocked: every route, plus &lt;code&gt;401&lt;/code&gt;, &lt;code&gt;404&lt;/code&gt;, &lt;code&gt;422&lt;/code&gt; and &lt;code&gt;503&lt;/code&gt; paths. One thing helped a lot: a &lt;strong&gt;fresh mock per test&lt;/strong&gt;. My first version shared a single &lt;code&gt;MagicMock&lt;/code&gt; across tests, so one test's &lt;code&gt;side_effect&lt;/code&gt; could leak into the next.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nd"&gt;@pytest.fixture&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;autouse&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;svc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;monkeypatch&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;mock&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;MagicMock&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;dependency_overrides&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;tasks_service&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;lambda&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;mock&lt;/span&gt;
    &lt;span class="n"&gt;monkeypatch&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setattr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;settings&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;api_key&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="n"&gt;mock&lt;/span&gt;
    &lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;dependency_overrides&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;clear&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then I ran the same scenarios against my real account, on a throwaway list deleted at the end. The real API had two behaviors the mocks didn't:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;A malformed task ID returns &lt;code&gt;400&lt;/code&gt;, not &lt;code&gt;404&lt;/code&gt;.&lt;/strong&gt; The service just forwards Google's status, so the client sees exactly what Google said. My mock had assumed &lt;code&gt;404&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Moving a parent task to another list takes its subtasks with it.&lt;/strong&gt; My next call tried to edit a subtask in the old list and got a &lt;code&gt;404&lt;/code&gt;. That's correct behavior, but you only learn it by trying.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Mocks prove that your code calls the API the way you think it does. Only the real API tells you what that call actually does.&lt;/p&gt;




&lt;h2&gt;
  
  
  Takeaways
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Model what the backend can store, not what users wish it stored: a date is a &lt;code&gt;date&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;On Workspace, an &lt;strong&gt;Internal&lt;/strong&gt; consent screen is the difference between a service and a weekly chore.&lt;/li&gt;
&lt;li&gt;If the OAuth redirect hits &lt;code&gt;ERR_CONNECTION_REFUSED&lt;/code&gt; with a &lt;code&gt;code=&lt;/code&gt; in the URL, the local listener timed out. Run the authorization again.&lt;/li&gt;
&lt;li&gt;Windows + &lt;code&gt;pythonw&lt;/code&gt; + Uvicorn needs a real stdout. Give it a log file.&lt;/li&gt;
&lt;li&gt;Put auth on the routers that hold data, keep &lt;code&gt;/health&lt;/code&gt; open, and remember that &lt;code&gt;/docs&lt;/code&gt; is open by default.&lt;/li&gt;
&lt;li&gt;Make the auth check fail closed: no key configured means &lt;code&gt;503&lt;/code&gt;, not an open API.&lt;/li&gt;
&lt;li&gt;Run your scenarios once against the real API. That's where the &lt;code&gt;400&lt;/code&gt; instead of &lt;code&gt;404&lt;/code&gt; shows up.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The code is on GitHub: &lt;strong&gt;&lt;a href="https://github.com/manoelmartins1980/google-tasks-api" rel="noopener noreferrer"&gt;manoelmartins1980/google-tasks-api&lt;/a&gt;&lt;/strong&gt; (MIT, with installation and operations guides). My AI assistant already uses it through a small CLI: it reads my lists freely and only adds or completes tasks after I confirm.&lt;/p&gt;

</description>
      <category>python</category>
      <category>fastapi</category>
      <category>api</category>
      <category>productivity</category>
    </item>
    <item>
      <title>What my automation is never allowed to do</title>
      <dc:creator>Manoel Martins</dc:creator>
      <pubDate>Fri, 16 Oct 2026 22:10:25 +0000</pubDate>
      <link>https://dev.to/manoelmartins1980/what-my-automation-is-never-allowed-to-do-1kkd</link>
      <guid>https://dev.to/manoelmartins1980/what-my-automation-is-never-allowed-to-do-1kkd</guid>
      <description>&lt;p&gt;&lt;strong&gt;Tested on NetBox 4.5, FortiOS 7.4, RouterOS 7.23 (long-term) and Proxmox VE 8.3&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I wanted NetBox to reflect what my lab actually runs, not what I remembered to type into it. So I wrote a script that reads a FortiGate, a MikroTik and a Proxmox host every night and reconciles NetBox with what they report.&lt;/p&gt;

&lt;p&gt;Writing the collectors took an afternoon. The hard part was a different question: this thing runs unattended, every day, with write access to my source of truth. What must it &lt;strong&gt;never&lt;/strong&gt; do?&lt;/p&gt;

&lt;p&gt;I ended up with four rules. Each one I either tested on purpose or learned by watching it fail.&lt;/p&gt;




&lt;h2&gt;
  
  
  The setup
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;FortiGate (REST API)   ─┐
MikroTik (RouterOS API)─┼─(read-only)─&amp;gt; netbox-sync ─(write)─&amp;gt; NetBox
Proxmox VE (API token) ─┘                    │
                                             └─ local network sweep (ping + ARP)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Source&lt;/th&gt;
&lt;th&gt;What it reports&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;FortiGate&lt;/td&gt;
&lt;td&gt;serial, interfaces, VLANs, MACs, IPs, DHCP pools, ARP&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;MikroTik&lt;/td&gt;
&lt;td&gt;serial, interfaces, VLANs, MACs, IPs, DHCP pools, ARP&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Proxmox VE&lt;/td&gt;
&lt;td&gt;VMs and containers, vCPU, RAM, disks, interfaces, host capacity&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;It runs from a systemd timer once a day, and on demand from a double-click shortcut. Every run that changes something writes a Journal Entry on the NetBox device.&lt;/p&gt;




&lt;h2&gt;
  
  
  Rule 1 — Least privilege: the devices are only read
&lt;/h2&gt;

&lt;p&gt;The script never needs to change the network, so it never gets the ability to.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Platform&lt;/th&gt;
&lt;th&gt;Account&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;FortiGate&lt;/td&gt;
&lt;td&gt;API admin with a read-only &lt;code&gt;accprofile&lt;/code&gt;, &lt;code&gt;trusthost&lt;/code&gt; pinned to the sync host&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;MikroTik&lt;/td&gt;
&lt;td&gt;group with &lt;code&gt;read,api&lt;/code&gt; and &lt;strong&gt;without&lt;/strong&gt; &lt;code&gt;write&lt;/code&gt;, &lt;code&gt;policy&lt;/code&gt; or &lt;code&gt;sensitive&lt;/code&gt;; user bound to the sync host's IP&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Proxmox VE&lt;/td&gt;
&lt;td&gt;API token with the built-in &lt;code&gt;PVEAuditor&lt;/code&gt; role&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The only credential that can write anything is the NetBox token — also restricted to the sync host's IP.&lt;/p&gt;

&lt;p&gt;I tested it instead of trusting it. A &lt;code&gt;POST&lt;/code&gt; to start a container on Proxmox with the sync token:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;403 Permission check failed (/vms/101, VM.PowerMgmt)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On MikroTik, leaving out &lt;code&gt;sensitive&lt;/code&gt; has a nice side effect: the account can read the whole configuration but not the Wi-Fi keys or VPN secrets stored in it.&lt;/p&gt;

&lt;p&gt;If the script is ever wrong — or its config file leaks — the worst case is a wrong inventory. Not a wrong firewall.&lt;/p&gt;




&lt;h2&gt;
  
  
  Rule 2 — Retire, never delete
&lt;/h2&gt;

&lt;p&gt;The naive version of a sync is: &lt;em&gt;if it's in NetBox but not on the device, delete it.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;That rule is fine until an API call times out. Then one bad night wipes good data, and the next morning your source of truth says the VLAN never existed.&lt;/p&gt;

&lt;p&gt;"Missing from the source" is not the same as "gone". So a normal run never deletes. It &lt;strong&gt;retires&lt;/strong&gt;:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Gone from the source&lt;/th&gt;
&lt;th&gt;Normal run does&lt;/th&gt;
&lt;th&gt;If it comes back&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;DHCP pool&lt;/td&gt;
&lt;td&gt;IP Range → &lt;code&gt;deprecated&lt;/code&gt;, no longer counted as used&lt;/td&gt;
&lt;td&gt;reactivated&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Proxmox VM&lt;/td&gt;
&lt;td&gt;VM → &lt;code&gt;decommissioning&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;status restored&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Device interface&lt;/td&gt;
&lt;td&gt;alert in the log&lt;/td&gt;
&lt;td&gt;—&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Deleting is a separate, explicit step: &lt;code&gt;--prune&lt;/code&gt;. It never runs from the timer, it is not available through the on-demand shortcut, and it only deletes objects &lt;strong&gt;the script itself created&lt;/strong&gt; — tagged &lt;code&gt;[netbox-sync]&lt;/code&gt;. Anything I entered by hand is out of its reach, even when it is retired.&lt;/p&gt;

&lt;p&gt;I tested this without touching a single device, by creating fake objects in NetBox:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Fake object&lt;/th&gt;
&lt;th&gt;Tag&lt;/th&gt;
&lt;th&gt;Normal run&lt;/th&gt;
&lt;th&gt;&lt;code&gt;--prune&lt;/code&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;VM + interface + IP&lt;/td&gt;
&lt;td&gt;yes&lt;/td&gt;
&lt;td&gt;retired&lt;/td&gt;
&lt;td&gt;
&lt;strong&gt;deleted&lt;/strong&gt;, with its IP&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;VM&lt;/td&gt;
&lt;td&gt;no (manual)&lt;/td&gt;
&lt;td&gt;retired&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;kept&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;DHCP IP Range&lt;/td&gt;
&lt;td&gt;yes&lt;/td&gt;
&lt;td&gt;retired, range free again&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;deleted&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;A second normal run changed nothing. It only reminded me what was waiting for a decision.&lt;/p&gt;




&lt;h2&gt;
  
  
  Rule 3 — A dry-run must show everything
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;--dry-run&lt;/code&gt; is the command I run before I trust anything. My first version printed this when I created a test VLAN on each device:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[change] interface created: vlan999 (vlan)
[change] interface created: vlan999 (vlan)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both VLANs had an IP address and a parent interface. Apply would have created those too. The preview simply didn't say so.&lt;/p&gt;

&lt;p&gt;A preview that shows less than the real run is worse than no preview: it gives you confidence you didn't earn. The fixed version prints every change apply will make:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fw-01
  [change] interface created: vlan999 (vlan VLAN ID 999 on internal3)
  [change] vlan999: IP created 192.0.2.1/24
rtr-01
  [change] interface created: vlan999 (vlan VLAN ID 1 on ether3)
  [change] vlan999: IP created 192.0.2.2/24
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Look at the second block. The MikroTik interface was &lt;strong&gt;named&lt;/strong&gt; &lt;code&gt;vlan999&lt;/code&gt; — but it was tagging traffic as &lt;strong&gt;VLAN 1&lt;/strong&gt;. RouterOS uses &lt;code&gt;vlan-id=1&lt;/code&gt; when you don't specify one. The name looked right, so nobody would have noticed. The old dry-run would have hidden it too.&lt;/p&gt;

&lt;p&gt;Fixing the preview found a real misconfiguration on its first run.&lt;/p&gt;




&lt;h2&gt;
  
  
  Rule 4 — "Free" must mean free
&lt;/h2&gt;

&lt;p&gt;The reason to have an IPAM is to answer one question safely: &lt;em&gt;which address can I use?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;NetBox was happy to suggest addresses inside my DHCP pools — addresses the DHCP server could hand to a phone at any moment. So active DHCP pools are now recorded as one IP Range each, with two flags set:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;mark_populated&lt;/code&gt; — the range disappears from "available IPs"&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;mark_utilized&lt;/code&gt; — the range counts toward prefix utilization&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A pool counts as active only if &lt;strong&gt;both&lt;/strong&gt; the DHCP server and its interface are up. My firewall had five DHCP servers configured on SSIDs that were switched off; those are recorded as &lt;code&gt;deprecated&lt;/code&gt;, not as addresses in use.&lt;/p&gt;

&lt;p&gt;Individual leases are never recorded. They are volatile, and a lab VM that lives for a week on DHCP should leave no trace in the inventory.&lt;/p&gt;

&lt;p&gt;The second half of the rule: an inventory only knows what it sees. A host with a static IP that nobody documented is invisible to every API. So each run also sweeps the directly connected networks — a ping to every address, then a look at the sync host's own ARP table — and alerts on anything answering on the wire that NetBox doesn't know. About 9 seconds for a /24. Addresses inside DHCP pools never alert.&lt;/p&gt;

&lt;p&gt;After both changes, asking NetBox's API for available IPs gave the right answer — including an uncomfortable one: on one of my networks, the gateway plus the DHCP pool cover the whole /24. &lt;strong&gt;There is no free address for a static host there.&lt;/strong&gt; A design finding I would otherwise have made while configuring a device.&lt;/p&gt;




&lt;h2&gt;
  
  
  What the first runs found
&lt;/h2&gt;

&lt;p&gt;Things my manual inventory had missed, in the first few days:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;A gateway IP that was never documented.&lt;/strong&gt; The first run created it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;An orphan DHCP server&lt;/strong&gt; on the MikroTik, still pointing to a bridge I had deleted weeks earlier. RouterOS shows the missing interface as &lt;code&gt;*C&lt;/code&gt;; the script flags that as an orphan.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;VM disks under-reported.&lt;/strong&gt; Two VMs have a second disk (one of them a virtual firewall with separate boot and log disks). My manual entry had counted only the first.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A VM wearing the host's MAC.&lt;/strong&gt; Found by the network sweep, outside Proxmox: a VM bridged over Wi-Fi on VMware Workstation answers ARP with the &lt;strong&gt;host's&lt;/strong&gt; Wi-Fi MAC — 802.11 doesn't let one client transmit with several MACs, so the hypervisor rewrites it. Any MAC-based troubleshooting of that VM would point at the laptop.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;RAM overcommit on the hypervisor&lt;/strong&gt;, reported on every run: allocated vs. physical, with an alert above 100%.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Things I learned the hard way
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Line endings.&lt;/strong&gt; I edited the script on Windows and saved it with CRLF. Linux read the &lt;code&gt;#!&lt;/code&gt; line with an invisible &lt;code&gt;\r&lt;/code&gt; and answered &lt;code&gt;cannot execute: required file not found&lt;/code&gt;. &lt;code&gt;py_compile&lt;/code&gt; passed — Python doesn't care. The systemd timer would have failed silently that night. The repository now has a &lt;code&gt;.gitattributes&lt;/code&gt; that forces LF.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;API endpoints move between versions.&lt;/strong&gt; On FortiOS 7.4 the ARP table is &lt;code&gt;/api/v2/monitor/network/arp&lt;/code&gt;; &lt;code&gt;/monitor/system/arp&lt;/code&gt; returns 404. My first attempt took the whole FortiGate collection down with it — now auxiliary data like ARP is isolated, and a failure there never stops the rest.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Test the silence.&lt;/strong&gt; When the ARP check first returned zero alerts, it wasn't because the network was clean. Device ARP tables only list hosts that talked to the device recently. Zero alerts meant &lt;em&gt;zero coverage&lt;/em&gt;. That is why the local sweep exists.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Known limits
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;IPs of Proxmox &lt;strong&gt;VMs&lt;/strong&gt; are not collected — that needs the qemu-guest-agent inside each VM. Container IPs are.&lt;/li&gt;
&lt;li&gt;One FortiGate VDOM per source entry.&lt;/li&gt;
&lt;li&gt;Devices without an API — an unmanaged switch, the ISP router, consumer Wi-Fi — stay manual.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;Built with Claude as a pair-programmer — every change was validated against the real devices before going live.&lt;/p&gt;

&lt;p&gt;The code is on GitHub under the MIT license, with the read-only account setup for each platform, the design notes and the test scenarios: &lt;strong&gt;&lt;a href="https://github.com/manoelmartins1980/netbox-sync" rel="noopener noreferrer"&gt;github.com/manoelmartins1980/netbox-sync&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;What's the one thing your automation is never allowed to do?&lt;/p&gt;

</description>
      <category>networking</category>
      <category>automation</category>
      <category>python</category>
      <category>homelab</category>
    </item>
    <item>
      <title>Your DHCP server is handing out addresses it never checked</title>
      <dc:creator>Manoel Martins</dc:creator>
      <pubDate>Fri, 11 Sep 2026 22:35:47 +0000</pubDate>
      <link>https://dev.to/manoelmartins1980/your-dhcp-server-is-handing-out-addresses-it-never-checked-aaj</link>
      <guid>https://dev.to/manoelmartins1980/your-dhcp-server-is-handing-out-addresses-it-never-checked-aaj</guid>
      <description>&lt;p&gt;&lt;strong&gt;Tested on Windows Server 2025 (24H2, build 26100.33296)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A machine boots, gets a &lt;code&gt;169.254.x.x&lt;/code&gt; address, and has no network. The DHCP server is up. The service is running. There are no errors in the event log. There are free addresses in the scope. Reboot the client and it works fine.&lt;/p&gt;

&lt;p&gt;I first saw this on Windows Server 2003. I assumed it had been fixed somewhere along the way. It has not — because it was never a bug. It is a default, and the default is still there in Windows Server 2025.&lt;/p&gt;




&lt;h2&gt;
  
  
  What the RFC actually says
&lt;/h2&gt;

&lt;p&gt;RFC 2131 splits address-conflict checking across both ends of the conversation:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;the allocating server SHOULD probe the reused address before allocating the address, e.g., with an ICMP echo request, and the client SHOULD probe the newly received address, e.g., with ARP.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Two recommendations. Both &lt;code&gt;SHOULD&lt;/code&gt;, neither &lt;code&gt;MUST&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Microsoft implemented both — but ships the server half &lt;strong&gt;turned off&lt;/strong&gt;. The setting is called &lt;em&gt;Conflict detection attempts&lt;/em&gt;, it lives under IPv4 → Properties → Advanced, and its default value is &lt;code&gt;0&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  The lab
&lt;/h2&gt;

&lt;p&gt;Three VMs on a VMware Workstation &lt;strong&gt;LAN Segment&lt;/strong&gt; — a virtual switch with no uplink and no host interface, so nothing here can reach or disturb the physical network:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Role&lt;/th&gt;
&lt;th&gt;Address&lt;/th&gt;
&lt;th&gt;MAC&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;DHCP server (Windows Server 2025)&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;172.16.0.2&lt;/code&gt; (static)&lt;/td&gt;
&lt;td&gt;&lt;code&gt;00:0c:29:52:b9:9f&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Squatter (Linux, static, never speaks DHCP)&lt;/td&gt;
&lt;td&gt;&lt;code&gt;172.16.0.10&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;00:0c:29:3a:4c:11&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Client (Windows 10, &lt;code&gt;WIN10-01&lt;/code&gt;)&lt;/td&gt;
&lt;td&gt;DHCP&lt;/td&gt;
&lt;td&gt;&lt;code&gt;00:0c:29:b8:ca:88&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The scope is &lt;code&gt;172.16.0.0/24&lt;/code&gt; with a deliberately tiny range, so that the address I want the server to pick is the only one it can pick. Lease duration: 8 days.&lt;/p&gt;

&lt;p&gt;The squatter simulates what you get from a rogue DHCP server or a hand-configured host inside the scope range: something holding an address the DHCP server believes is free.&lt;/p&gt;




&lt;h2&gt;
  
  
  Test 1 — the default (&lt;code&gt;Conflict detection attempts = 0&lt;/code&gt;)
&lt;/h2&gt;

&lt;p&gt;Scope range: &lt;code&gt;172.16.0.10&lt;/code&gt; – &lt;code&gt;172.16.0.10&lt;/code&gt;.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;t (s)&lt;/th&gt;
&lt;th&gt;Who&lt;/th&gt;
&lt;th&gt;What&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;0.000000&lt;/td&gt;
&lt;td&gt;Client&lt;/td&gt;
&lt;td&gt;DISCOVER&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;0.000406&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;Server&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;OFFER — &lt;code&gt;172.16.0.10&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;0.001285&lt;/td&gt;
&lt;td&gt;Client&lt;/td&gt;
&lt;td&gt;REQUEST &lt;code&gt;172.16.0.10&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;0.003182&lt;/td&gt;
&lt;td&gt;Server&lt;/td&gt;
&lt;td&gt;ACK &lt;code&gt;172.16.0.10&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;0.280785&lt;/td&gt;
&lt;td&gt;Client&lt;/td&gt;
&lt;td&gt;ARP probe: &lt;em&gt;who has &lt;code&gt;172.16.0.10&lt;/code&gt;?&lt;/em&gt; (sender &lt;code&gt;0.0.0.0&lt;/code&gt;)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;0.280908&lt;/td&gt;
&lt;td&gt;Squatter&lt;/td&gt;
&lt;td&gt;ARP reply: &lt;em&gt;&lt;code&gt;172.16.0.10&lt;/code&gt; is at &lt;code&gt;00:0c:29:3a:4c:11&lt;/code&gt;&lt;/em&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3.130462&lt;/td&gt;
&lt;td&gt;Client&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;DHCPDECLINE&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;~7.3–10.3&lt;/td&gt;
&lt;td&gt;Client&lt;/td&gt;
&lt;td&gt;ARP probes for &lt;code&gt;169.254.248.228&lt;/code&gt; (APIPA)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fvcanbd6lvaydd3t7lcsn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fvcanbd6lvaydd3t7lcsn.png" alt="Wireshark showing DISCOVER, OFFER of 172.16.0.10, REQUEST, ACK, the client's ARP probe, the squatter's reply, and the DHCPDECLINE" width="800" height="162"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The server handed out an occupied address &lt;strong&gt;406 microseconds&lt;/strong&gt; after being asked for one. You do not need to argue about whether it checked: there is no room for a check in 406 µs. The whole capture contains zero ICMP packets and not a single ARP frame originating from the server's MAC.&lt;/p&gt;

&lt;p&gt;Here is that claim as a filter. &lt;code&gt;icmp || (arp &amp;amp;&amp;amp; eth.src == &amp;lt;server MAC&amp;gt;)&lt;/code&gt;, applied to the entire capture:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fkgx1rgi0geiwzwbib83w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fkgx1rgi0geiwzwbib83w.png" alt="Wireshark display filter for any ICMP or any ARP originating from the DHCP server, showing zero packets displayed" width="799" height="378"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The client caught it. It ARP-probed the address it had just been given, the squatter answered, and the client sent DHCPDECLINE 3.1 seconds after the ACK — then fell back to APIPA:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="go"&gt;Autoconfiguration IPv4 Address. . : 169.254.248.228(Preferred)
Subnet Mask . . . . . . . . . . . : 255.255.0.0
Default Gateway . . . . . . . . . :
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fj8ecqbyngi8djglirxx7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fj8ecqbyngi8djglirxx7.png" alt="ipconfig output on the client showing an APIPA address and no default gateway" width="693" height="235"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;This is the important part.&lt;/strong&gt; The client-side safety net works, so you usually do not get a duplicate IP. You get something that is arguably worse to diagnose: a machine with no network at all, sitting next to a healthy DHCP server that has just told it everything is fine.&lt;/p&gt;

&lt;p&gt;You get an actual duplicate address in two situations. The first is when the client does not probe at all: embedded devices, IoT, older or minimal stacks.&lt;/p&gt;

&lt;p&gt;The second is more common and more interesting: &lt;strong&gt;the probe happens and nobody answers it.&lt;/strong&gt; Detection — on either side — only works if the occupant is awake and willing to reply at the exact moment it is asked. A host that is powered off, asleep, on a link that is momentarily down, or sitting behind a fabric that suppresses ARP will be scored as absent. The address is handed out, and the duplicate appears later, when that host comes back and starts answering for an address someone else is now using.&lt;/p&gt;

&lt;p&gt;Note that this defeats both defences equally. The client's ARP probe and the server's ICMP ping are the same bet: that silence means free. Silence does not mean free. It means nobody replied.&lt;/p&gt;

&lt;h3&gt;
  
  
  What the decline costs you
&lt;/h3&gt;

&lt;p&gt;The declined address goes into the lease table as &lt;code&gt;BAD_ADDRESS&lt;/code&gt;:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Client IP Address&lt;/th&gt;
&lt;th&gt;Name&lt;/th&gt;
&lt;th&gt;Lease Expiration&lt;/th&gt;
&lt;th&gt;Type&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;172.16.0.10&lt;/td&gt;
&lt;td&gt;BAD_ADDRESS&lt;/td&gt;
&lt;td&gt;9/5/2026 5:39:26 PM&lt;/td&gt;
&lt;td&gt;DHCP&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fg970c5fhvxaqtst4p0va.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fg970c5fhvxaqtst4p0va.png" alt="DHCP console showing the BAD_ADDRESS entry for 172.16.0.10 alongside the scope's 8-day lease duration" width="799" height="255"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Two things worth noticing.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The entry expires with the scope's lease duration&lt;/strong&gt; — 8 days configured, 8 days until expiry. It is not permanent. But if the squatter is still there when it expires, the address goes back in the pool, gets offered again, gets declined again, and is marked again. It is not a leak; it is a recurring tax. Each cycle costs one unlucky client a failed boot.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The &lt;code&gt;Unique ID&lt;/code&gt; is &lt;code&gt;0a0010ac&lt;/code&gt;.&lt;/strong&gt; That is not a MAC address — it is &lt;code&gt;172.16.0.10&lt;/code&gt; written as little-endian hex. The entry is a mark on the address, not a record of a client. The server does not store who claimed the conflict or who reported it. There is nothing on the server side to trace the conflict back to its source: you have to go to the switch or capture packets.&lt;/p&gt;




&lt;h2&gt;
  
  
  Test 2 — &lt;code&gt;Conflict detection attempts = 2&lt;/code&gt;, same one-address scope
&lt;/h2&gt;

&lt;p&gt;Same lab, one setting changed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="n"&gt;Set-DhcpServerv4Setting&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-ConflictDetectionAttempts&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;2&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;t (s)&lt;/th&gt;
&lt;th&gt;Who&lt;/th&gt;
&lt;th&gt;What&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;0.000000&lt;/td&gt;
&lt;td&gt;Client&lt;/td&gt;
&lt;td&gt;DISCOVER&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;0.000481&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;Server&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;
&lt;strong&gt;ICMP echo request → &lt;code&gt;172.16.0.10&lt;/code&gt;&lt;/strong&gt;, TTL 255&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;0.000812&lt;/td&gt;
&lt;td&gt;Squatter&lt;/td&gt;
&lt;td&gt;ICMP echo reply — address is alive&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fpcxzdr3kyyjb817u7312.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fpcxzdr3kyyjb817u7312.png" alt="Wireshark showing the DISCOVER followed by an ICMP echo request from the DHCP server to 172.16.0.10 and the reply, with no OFFER" width="800" height="120"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The server probed 481 microseconds after the DISCOVER, found the address occupied, and refused to offer it. With only one address in the scope, it then had nothing to give: nine DISCOVERs across the capture, zero OFFERs.&lt;/p&gt;

&lt;p&gt;Correct behaviour, but not useful to a client. Which is why the scope needs a second address.&lt;/p&gt;

&lt;p&gt;Two details worth keeping:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Despite &lt;code&gt;attempts = 2&lt;/code&gt;, the server sent &lt;strong&gt;one&lt;/strong&gt; ping. The reply came back in 331 µs, so a second attempt was unnecessary. The setting is a maximum, not a fixed count.&lt;/li&gt;
&lt;li&gt;The probe leaves a recognisable fingerprint: &lt;strong&gt;ICMP echo request, TTL 255, sourced from the DHCP server's own address&lt;/strong&gt;. If you are staring at a capture, that is how you spot Windows conflict detection.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Test 3 — &lt;code&gt;Conflict detection attempts = 2&lt;/code&gt;, two addresses
&lt;/h2&gt;

&lt;p&gt;Scope range widened to &lt;code&gt;172.16.0.10&lt;/code&gt; – &lt;code&gt;172.16.0.11&lt;/code&gt;.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;t (s)&lt;/th&gt;
&lt;th&gt;Who&lt;/th&gt;
&lt;th&gt;What&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;0.000000&lt;/td&gt;
&lt;td&gt;Client&lt;/td&gt;
&lt;td&gt;DISCOVER&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;0.000531&lt;/td&gt;
&lt;td&gt;Server&lt;/td&gt;
&lt;td&gt;ICMP echo request → &lt;code&gt;172.16.0.10&lt;/code&gt;, TTL 255&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;0.000846&lt;/td&gt;
&lt;td&gt;Squatter&lt;/td&gt;
&lt;td&gt;ICMP echo reply — occupied, skip it&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;0.001517&lt;/td&gt;
&lt;td&gt;Server&lt;/td&gt;
&lt;td&gt;ARP: &lt;em&gt;who has &lt;code&gt;172.16.0.11&lt;/code&gt;?&lt;/em&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;0.586799&lt;/td&gt;
&lt;td&gt;Server&lt;/td&gt;
&lt;td&gt;ARP: &lt;em&gt;who has &lt;code&gt;172.16.0.11&lt;/code&gt;?&lt;/em&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1.593732&lt;/td&gt;
&lt;td&gt;Server&lt;/td&gt;
&lt;td&gt;ARP: &lt;em&gt;who has &lt;code&gt;172.16.0.11&lt;/code&gt;?&lt;/em&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;1.594280&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;Server&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;OFFER — &lt;code&gt;172.16.0.11&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1.595267&lt;/td&gt;
&lt;td&gt;Client&lt;/td&gt;
&lt;td&gt;REQUEST&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1.595795&lt;/td&gt;
&lt;td&gt;Server&lt;/td&gt;
&lt;td&gt;ACK&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F1uj8nvkyp6gj1o4mxhdv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F1uj8nvkyp6gj1o4mxhdv.png" alt="Wireshark showing the ICMP probe to 172.16.0.10, three unanswered ARP requests for 172.16.0.11 spanning 1.59 seconds, then the OFFER of 172.16.0.11" width="800" height="173"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The client came up on &lt;code&gt;172.16.0.11&lt;/code&gt; with no DECLINE and no APIPA. It still ran its own ARP probes afterwards — the client-side check happens regardless — and then announced the address normally.&lt;/p&gt;

&lt;h3&gt;
  
  
  Where the time actually goes
&lt;/h3&gt;

&lt;p&gt;This is the part I did not expect, and it is the most interesting thing in the whole exercise.&lt;/p&gt;

&lt;p&gt;For &lt;code&gt;172.16.0.10&lt;/code&gt; the server sent &lt;strong&gt;ICMP&lt;/strong&gt;. For &lt;code&gt;172.16.0.11&lt;/code&gt; it sent &lt;strong&gt;three ARP requests and no ICMP at all&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The reason is mundane and the consequence is not: the server already had an ARP cache entry for &lt;code&gt;.10&lt;/code&gt;, so it could ping it immediately. It had no entry for &lt;code&gt;.11&lt;/code&gt;, so it had to resolve the MAC first — and since nothing owns &lt;code&gt;.11&lt;/code&gt;, the ARP never resolved. The ping was never sent. The server retried ARP three times, gave up after ~1.59 seconds, concluded the address was free, and offered it.&lt;/p&gt;

&lt;p&gt;So "conflict detection by ping" only performs a ping when the address is &lt;strong&gt;occupied&lt;/strong&gt;. For a genuinely free address — which is the overwhelmingly common case in production — it degenerates into an ARP resolution that has to time out. &lt;strong&gt;The cost is not in detecting conflicts. It is in proving their absence, on every single lease.&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  The trade-off, measured
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;Detection = 0 (default)&lt;/th&gt;
&lt;th&gt;Detection = 2&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Server probes before offering&lt;/td&gt;
&lt;td&gt;No — 0 ICMP, 0 ARP from server&lt;/td&gt;
&lt;td&gt;Yes — ICMP at 481 µs, TTL 255&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Occupied address offered&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;Yes&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Who detects the conflict&lt;/td&gt;
&lt;td&gt;The client, via ARP probe&lt;/td&gt;
&lt;td&gt;The server&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Outcome for the client&lt;/td&gt;
&lt;td&gt;DHCPDECLINE → APIPA &lt;code&gt;169.254.x.x&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Valid address, first try&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Effect on the pool&lt;/td&gt;
&lt;td&gt;Address marked &lt;code&gt;BAD_ADDRESS&lt;/code&gt; for the lease duration&lt;/td&gt;
&lt;td&gt;Address skipped, pool intact&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;DISCOVER → OFFER&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;406 µs&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;1.594 s&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;That is roughly &lt;strong&gt;3,900× slower per lease&lt;/strong&gt;. On a campus network handing out thousands of leases at 8 a.m., that is not a rounding error — and it is paid on every lease, including the overwhelming majority that would never have had a conflict.&lt;/p&gt;

&lt;p&gt;This is not a Microsoft quirk. ISC, which implements the same check as &lt;code&gt;ping-check&lt;/code&gt; in &lt;code&gt;dhcpd&lt;/code&gt;, &lt;a href="https://kb.isc.org/docs/ping-check" rel="noopener noreferrer"&gt;publicly questions its value&lt;/a&gt; for exactly the same reason: it does not scale to environments that need thousands of leases per second. Two implementations, same trade-off, and neither vendor thinks the safe option is free.&lt;/p&gt;




&lt;h2&gt;
  
  
  The safety net that is not there
&lt;/h2&gt;

&lt;p&gt;The usual answer to "what about rogue DHCP servers?" is DHCP server authorization in Active Directory. It is worth being precise about what that actually does.&lt;/p&gt;

&lt;p&gt;Authorization works because the &lt;strong&gt;Microsoft&lt;/strong&gt; DHCP service refuses to start when it is not authorized in AD. A home router, a misconfigured access point, a VM running &lt;code&gt;dnsmasq&lt;/code&gt;, or any appliance with DHCP enabled asks nobody for permission. AD authorization does not see them and cannot stop them.&lt;/p&gt;

&lt;p&gt;So in the exact scenario where you need protection, both defences are absent at once: authorization does not apply to the rogue, and conflict detection is switched off.&lt;/p&gt;

&lt;h2&gt;
  
  
  What to actually do
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Set conflict detection to 1 or 2&lt;/strong&gt; if your scopes overlap with static hosts, or if you cannot rule out rogue servers. Measure the added latency against your lease rate before rolling it out broadly — it is a real cost, not a free win.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Do not treat conflict detection as a fix.&lt;/strong&gt; It is a probe, and a probe only detects hosts that are awake and answering. A machine that is powered off when the lease is granted is invisible to it, and the duplicate simply shows up when that machine wakes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Fix it at layer 2.&lt;/strong&gt; DHCP snooping on your switches stops the rogue server instead of guessing at its consequences. That is the actual solution; conflict detection is damage control after the fact.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Watch for &lt;code&gt;BAD_ADDRESS&lt;/code&gt; entries.&lt;/strong&gt; They are the only evidence the server keeps that something is wrong, they expire quietly with the lease duration, and they say nothing about the culprit.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;When a client shows up on &lt;code&gt;169.254.x.x&lt;/code&gt; with a healthy DHCP server&lt;/strong&gt;, check the lease table for &lt;code&gt;BAD_ADDRESS&lt;/code&gt; before you blame the NIC.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Method
&lt;/h2&gt;

&lt;p&gt;Lab built in VMware Workstation on a Windows host, using a LAN Segment — a virtual switch with no uplink and no host interface, fully isolated from the physical network. Server: Windows Server 2025, 24H2, build 26100.33296, DHCP role, single NIC on the lab segment. Client: Windows 10 (&lt;code&gt;WIN10-01&lt;/code&gt;). Squatter: Linux with a static address and no DHCP client running.&lt;/p&gt;

&lt;p&gt;Scope ranges were deliberately narrowed to one address (Tests 1 and 2) and two addresses (Test 3) so that address selection was deterministic. This is why the server runs out of addresses in Test 2, and it is a property of the lab, not of Windows.&lt;/p&gt;

&lt;p&gt;Captures were taken unfiltered on both the server and the client side and correlated by DHCP transaction ID; timings quoted are from the server-side capture. Before each test the &lt;code&gt;BAD_ADDRESS&lt;/code&gt; entry was deleted and the DHCP service restarted, so every run starts from an empty lease table.&lt;/p&gt;

</description>
      <category>networking</category>
      <category>windows</category>
      <category>sysadmin</category>
      <category>wireshark</category>
    </item>
  </channel>
</rss>
