javascript - setAttribute() and XSS - Stack Overflow

I'm writing a script that needs to write the current page location to the DOM, and I'm concer

I'm writing a script that needs to write the current page location to the DOM, and I'm concerned about XSS. Is the following Javascript snippet safe from XSS?

var script = document.createElement('script');
script.setAttribute('src', '=' + encodeURIComponent(document.location.href));
document.getElementsByTagName('head')[0].appendChild(script);

I know that using document.write() to acplish the same thing is not safe in various browsers, but I've not seen any source discussing if using the DOM access methods is.

I'm writing a script that needs to write the current page location to the DOM, and I'm concerned about XSS. Is the following Javascript snippet safe from XSS?

var script = document.createElement('script');
script.setAttribute('src', 'http://fake.?src=' + encodeURIComponent(document.location.href));
document.getElementsByTagName('head')[0].appendChild(script);

I know that using document.write() to acplish the same thing is not safe in various browsers, but I've not seen any source discussing if using the DOM access methods is.

Share Improve this question asked Dec 3, 2010 at 22:47 Benjamin AndersonBenjamin Anderson 431 silver badge3 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

There's no need to use "setAttribute":

script.src = 'http://fake.?src=' + encodeURIComponent(document.location.href);

I don't see where an XSS vulnerability would sneak in here. The server code at "fake." has to be "hardened" against weird values of that "src" parameter, I suppose, but that's going to be true no matter what your Javascript looks like.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744727727a4590282.html

相关推荐

  • javascript - setAttribute() and XSS - Stack Overflow

    I'm writing a script that needs to write the current page location to the DOM, and I'm concer

    1天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信