It's not illegal, but it is frowned upon. I personally think that this should be illegal though, and people who do it should be punished, as it is cheating.
The only way to block something like this is unenable punching any blocks. So you won't be able to break any block or either try. If you do it will just punch the block.
People start in adventure mode now to prevent block glitching/griefing, but people who started before that are still in survival. Block glitching is technically cheating, but I'll have to see what Cyp says about it. Putting invisible blocks in the places where it can be possible can be a solution for now.