Ventura docs for M2 Macs in this comment: https://gist.github.com/henrik242/65d26a7deca30bdb9828e183809690bd?permalink_comment_id=4555340#gistcomment-4555340
Old Monterey docs in this old revision: https://gist.github.com/henrik242/65d26a7deca30bdb9828e183809690bd/32c410e3a1de73539c76fa13ea5486569c4e0c5d
Solution for Sonoma: https://gist.github.com/sghiassy/a3927405cf4ffe81242f4ecb01c382ac
Apple can not modify your hosts file, however when updating macos it can delete it and create a new blank one.
Apple can also bypass it at their will.
However, most of the time when they ignore it not necessarly because they intended to do so but because the daemon is not loaded yet as the upgrade process is not considered complete, and the boot is not complete until you click launch macOS
Why can they delete it?
Firstly, it is stored in a system path(/etc), not a user path(/usr). macOS can alter at will whatever is being stored in any system path, just like Windows and any other operating system or program within its working directory.
Secondly, as an example, hypothetically I am an Apple macOS Developer and I discovered a bug in the network manager and fix said bug. After fixing the bug I would either have the option to tell the OS that during the update it should delete the hosts file and create a new one in order to limit conflicts or problems that might arise from having the old file format, or it might actually be a software development protocol to mark for deletion related files to the service I have made major changes to.