Which has more specificity between hidden attribute in HTML or display property in CSS? And how?

Issue

Also what is the best way to hide the elements from the DOM so that the attacker won’t be able to change the css property or html attribute in order to access the element. I know we can use React or Angular to develop website and it is easier to hide or display elements. But I want to know in pure HTML & JS what is the best way?

Solution

Anyone can just use the browser console and find all elements with for example:

document.querySelectorAll('*');

It does not matter if elements are hidden with CSS.
Even if you encrypt your HTML you will have to decrypt it to show it to the browser. Then the above code still finds all the elements.

Any code you have can be deactivated by setting a breakpoint and rewriting it in-browser using the developer tools.

Even if you replace document.querySelectorAll and all like them with an empty function, developers can still just add jQuery or any DOM querying engine and find your elements that way.

Any code you can use to hide or show elements can just be executed using the browser console if someone spends the time understanding your code.
How else would you debug or test it?

Angular, Vue etc. does remove elements from the DOM but you should never expect this to be a security feature! A hacker can easily set a breakpoint anywhere in your code, inspect API results from the Network panel, go into the components’ code to find out what HTML they would be rendering and much more I haven’t started to mention.

To implement security you want to only have in the browser what the user needs to see.

There is no way around it.
DOM, stylings, scripts, assets, etc. can always be accessed using developer tools.

As for the question in your question title:

  • style attribute styles have a higher specificity than CSS from file (or style tags)
  • CSS from file (or style tags) with !important has higher specificity than styles from the style attribute
  • style attribute styles with !important have the highest specificty

So !important just overrides specificity if you want to look at it that way. Other than that you should read about CSS Specificity.

Answered By – Peter Krebs

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published