I think this is a pretty US-only type thing; at least it's not very common in Europe. Usually (in Europe) ownership of code depends entirely on when you did the coding -- if it's on your own time you own the code. If it's when you're at work then obviously it's the company's code. Of course there are the usual caveats about "is it too closely related to work code?", etc. but if it's a totally different area of business, then you should be fine. There may be differences between employers, but even if employers explicitly specify the "we own everything" clause you can usually get specific exemptions as long as you ask in advance.[1]
[1] I don't know if there's any legal theory (in Europe) to support the claim of ownership over everything you do, but Europe is pretty fragmented when it comes to law, so it's probably pointless to speculate on this point. I'm actually not sure if the "we own everything" clause would hold (or has held) up in court in any European country.
In Germany you usually have some paragraphs on this in the (work) contract (there are a couple popular variations - there isn't much leeway provided by German law here, which is good). "We own everything you do" is definitely illegal in Germany, and also not put into work contracts (such a clause would be void anyway).
The legal construction is basically that the employee grants an exclusive, irrevocable license[1] to any IP produced by the employee for the employer. There is of course a grey zone here, but it's quite narrow, and directly non-existent if the side project is unrelated to the current core business of the employer.
FOSS contributions aren't really touched by this; you work on behalf of the employer to contribute to a FOSS project and the exact same terms and conditions apply as always (you put your name on it, not your employers; since exclusivity of the license implicitly granted to your employer conflicts with the FOSS terms, but the employer explicitly commissioned you to do that, the latter "wins" over the former, ie. the license of your employer to the diffs you write becomes non-exclusive in these cases, because the exclusivity is implicitly revoked by the work assignment, essentially action implying intention) -- or so it was explained to me).
[1] This also means that you retain your copyright: it's not possible to transfer copyright in Germany. Very rarely things like "Copyright (c) 1871-1918 Softwareschmiede GmbH" pop up in open sourced stuff, but it's obviously something stamped on when open sourcing, and equally obviously incorrect. The copyright still belongs to the developers involved in the project, but because the company "Softwareschmiede GmbH" has an exclusive license they can re-license it on their own accord under any terms they want. They'd still have to, technically, state the correct copyright, though.
---
btw. seems that I was wrong about copyright attribution. Got a mail, apparently some contracts include a clause that the employer can in fact attribute any work to herself, so "Copyright (c) 1871 Foobar GmbH" can happen and can be correct.
Despite all the paranoia that would suggest otherwise, that's quite often the case in the US as well. Regardless of what your employment agreement says, it can't trump state law, and many (hell, maybe most) states have laws that clarify that work you do on your own time, on your own equipment, that isn't in the course of your employer's business, belongs to you. Of course IANAL, but I did a lot of research on this topic a while back, since I have been in the position of working on a side project while maintaining full-time employment elsewhere.
You can ever be sure what a given court will decide on a given day, if it comes to it, but I think a lot of people overstate the extent to which your employer "owns your brain".
That said, plenty of employers do at least try to "own your brain" and I think the reasonable response to that is to demand they pay you commensurately - including opportunity cost. If someone wants to "own your brain" they need to be paying on the order of millions of dollars per year, IMO.
Which is why the FSF requires documents from contributors' employers: https://www.gnu.org/prep/maintain/html_node/Copyright-Papers...
IANAL but... i believe (in the UK so presumably other common law jurisdiction like the US) the idea of agency solves this. If you sign that contract on behalf of your employer, the other party has the right to assume that you are authorised to sign and the contract is sound. The fact you just gave away something that belongs to your employer is now an issue between you and your employer. I would struggle to see how this could get beyond an employee disciplinary process unless it could be shown that the employee was showing some kind of intent in deliberately leaking secrets they knew they should not leak.