Ignoring, for now, spells that completely bypass illusions in the first place, yes I agree that detect magic can get you part way there. It does not identify the hat of disguise for what it is. Beyond that..
The problem I have with this assertion is that you start with 'if someone is...