Hacker News new | past | comments | ask | show | jobs | submit login

> Have you ever encountered a website that runs jQuery(location.hash)?

No. Actually I have never seen a website do that. What sites do that? What is the actual use of grabbing an element that has an ID that matches the URL hash?

And this attack will only work on those sites.

This is just one more variation of the best practice: don't trust user/client supplied data.

Edit: Though academically I actually find how this was implemented to be really interesting. I'm just not sure what uses it would have in the wild.




> What is the actual use of grabbing an element that has an ID that matches the URL hash

Well normally when you load such a page the browser will scroll down to that element. Perhaps this JavaScript wants to do something like highlight the section or extract the heading to send to some analytics?


I use it for linking to comments on my site. Users can copy a link to a particular comment, and the link directs people to the appropriate topic and location on the page.

I'm assuming that using X-Frame-Options to prevent the page from appearing in a frame prevents this type of attack.


> I use it for linking to comments on my site. Users can copy a link to a particular comment, and the link directs people to the appropriate topic and location on the page.

That works out of the box, it's a native HTML/browser feature.

Why do you pass location.hash to jQuery?


Sometimes, due to your in-page navigation or some other fixed position element, you need to actually scroll to a point before the actual element, so that it doesn’t get covered. The native behavior would not give it the lead space and you’d see some of the element’s content covered by a static element.

In scenarios where you can’t redesign the html structure or css to give the element more padding, you pretty much have to use javascript to get the window-x position of the element.


Oh, my mistake, it's been a while since I wrote the code. I use it to style the linked comment. So, the browser automatically scrolls to the appropriate location, and then jQuery adds a style to that comment so the user can easily locate it, or refind it if they scroll up and down the page.


In modern browsers, you can sometimes achieve this effect with `:target`. (https://developer.mozilla.org/en-US/docs/Web/CSS/:target)


I’m not 100% sure but the first thought I had when reading it are the SPA routers that start with # by default?

I only read it on my phone without following along but if that’s the case then the text seems to hint at potentially brute forcing the authentication tokens? At least judging from the variable names.


Yes, it happens. It happens enough that jquery has fixed several times previous bugs that let you get immediate xss out of this. But yea, devs still need to also validate their input.


> What is the actual use of grabbing an element that has an ID that matches the URL hash?

Ha! Browsers do that by default. On page load, they scroll to an anchor tag with the id of the URL fragment.

I could imagine some JS-driven dynamic loading/scrolling using the same convention.


Browser do it based on name="" on anchor tags not ID. And if you are implementing your own version you should probably also be using name not ID. Best practice is t use JS to progressively enhance the page not reinvent the wheel so name is the logical choice.

Edit: Also, word of advise. Lose the "Ha!"... it could potentially make your comment come across as arrogant.

Edit 2: Leaving my original comment intact even though I was wrong. Apparently my knowledge is out of date on this one. Thank you "re" for correcting me.


> Browser do it based on name="" on anchor tags not ID

Fragment identifiers can refer to elements by ID; this has been supported by browsers for over a decade and is the preferred way to specify a target: https://www.w3.org/TR/html4/struct/links.html#h-12.2.3

In fact, `<a name="">` is now considered obsolete: https://www.w3.org/TR/html5/obsolete.html#obsolete


Thank you. That is good to know. I really appreciate this info and the links. I'll file it under "things I learned 20 years ago that are now obsolete :("




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: