Monday, February 9, 2009

Defending against XSS attacks in Freemarker

While Freemarker is quite rich in features, it seems to lack support for programmatically declaring HTML-escaping the default behavior for property access. You need to either add a ?html to every access or wrap every single template into this:


<#escape x as x?html>
... your template code ...
</#escape>


This really has to be done on every file, including those being included such as macro definitions.

Both approaches rely on people remembering to do the right thing, and I don't trust anyone that much, particularly not myself. So instead I decided to add this bit of wrapper code programmatically in the code that loads the template. I took the idea from a posting on the freemarker-user mailing list. Instead of using the normal ClassTemplateLoader, I now do this:


final TemplateLoader templateLoader = new ClassTemplateLoader(this.getClass(), templatePath){
@Override
public Reader getReader(Object templateSource, String encoding) throws IOException {
return new WrappingReader(super.getReader(templateSource, encoding), "<#escape x as x?html>", "");
}
};
configuration.setTemplateLoader(templateLoader);


This uses the following class:


package domain.your.util;

import java.io.IOException;
import java.io.Reader;
import java.util.logging.Level;
import java.util.logging.Logger;

public class WrappingReader extends Reader {

private final Reader originalReader;
private final char[] prologue;
private final char[] epilogue;
private int pos = 0;
private int firstEpilogueChar = -1;
private boolean closed = false;

public WrappingReader(Reader originalReader, char[] prologue, char[] epilogue, Object lock) {
super(lock);
this.originalReader = originalReader;
this.prologue = prologue;
this.epilogue = epilogue;
}

public WrappingReader(Reader originalReader, char[] prologue, char[] epilogue) {
this.originalReader = originalReader;
this.prologue = prologue;
this.epilogue = epilogue;
}

public WrappingReader(Reader originalReader, String prologue, String epilogue, Object lock) {
super(lock);
this.originalReader = originalReader;
this.prologue = prologue.toCharArray();
this.epilogue = epilogue.toCharArray();
}

public WrappingReader(Reader originalReader, String prologue, String epilogue) {
this.originalReader = originalReader;
this.prologue = prologue.toCharArray();
this.epilogue = epilogue.toCharArray();
}

@Override
public int read(char[] cbuf, int off, int len) throws IOException {
if (closed) {
throw new IOException("Reader has been closed already");
}
int oldPos = pos;
Logger.getLogger(getClass().getName()).log(Level.FINE, String.format("Reading %d characters from position %d", len, pos));
if (pos < this.prologue.length) {
final int toCopy = Math.min(this.prologue.length - pos, len);
Logger.getLogger(getClass().getName()).log(Level.FINE, String.format("Copying %d characters from prologue", toCopy));
System.arraycopy(this.prologue, pos, cbuf, off, toCopy);
pos += toCopy;
if (toCopy == len) {
Logger.getLogger(getClass().getName()).log(Level.FINE, "Copied from prologue only");
return len;
}
}
if (firstEpilogueChar == -1) {
final int copiedSoFar = pos - oldPos;
final int read = originalReader.read(cbuf, off + copiedSoFar, len - copiedSoFar);
Logger.getLogger(getClass().getName()).log(Level.FINE, String.format("Got %d characters from delegate", read));
if (read != -1) {
pos += read;
if (pos - oldPos == len) {
Logger.getLogger(getClass().getName()).log(Level.FINE, "We do not reach epilogue");
return len;
}
}
firstEpilogueChar = pos;
}
final int copiedSoFar = pos - oldPos;
final int epiloguePos = pos - firstEpilogueChar;
final int toCopy = Math.min(this.epilogue.length - epiloguePos, len - copiedSoFar);
if((toCopy <= 0) && (copiedSoFar == 0)) {
return -1;
}
Logger.getLogger(getClass().getName()).log(Level.FINE, String.format("Copying %d characters from epilogue", toCopy));
System.arraycopy(this.epilogue, epiloguePos, cbuf, off + copiedSoFar, toCopy);
pos += toCopy;
Logger.getLogger(getClass().getName()).log(Level.FINE, String.format("Copied %d characters, now at position %d", pos-oldPos, pos));
return pos - oldPos;
}

@Override
public void close() throws IOException {
originalReader.close();
closed = true;
}
}


Note that this means that in some cases you might need to escape the escaping, which Freemarker allows with the <#noescape> directive. You also can't use template configuration via the <#ftl> directive anymore, since that would need to be before the <#escape>. Since I never felt the urge to use it, I don't care.

87 comments:

  1. hi was just seeing if you minded a comment. i like your website and the thme you picked is super. I will be back. Pflegezusatzversicherung

    ReplyDelete
  2. Nice post. By reading your blog, i get inspired and this provides some useful information. Thank you for posting this exclusive post for our vision. 
    python training Course in chennai
    python training in Bangalore
    Python training institute in bangalore

    ReplyDelete
  3. I appreciate your efforts because it conveys the message of what you are trying to say. It's a great skill to make even the person who doesn't know about the subject could able to understand the subject . Your blogs are understandable and also elaborately described. I hope to read more and more interesting articles from your blog.
    rpa training in bangalore
    best rpa training in bangalore
    rpa training in pune | rpa course in bangalore
    rpa training in chennai

    ReplyDelete
  4. Awesome article. It is so detailed and well formatted that i enjoyed reading it as well as get some new information too.
    Best Devops Training in pune
    Devops Training in Bangalore
    Microsoft azure training in Bangalore
    Power bi training in Chennai

    ReplyDelete
  5. This type of message always inspiring and I prefer to read quality content, so happy to find good place to many here in the post, the writing is just great, thanks for the post. Homepage

    ReplyDelete
  6. Thanks for the nice blog. It was very useful for me. I'm happy I found this blog. Thank you for sharing with us,I too always learn something new from your post. read this article

    ReplyDelete
  7. Data Science is the future of Artificial Intelligence. Therefore, it is very important to understand what is Data Science and how can it add value to your business.
    Data Science Training in Mumbai which includes classroom and online training. Along with Classroom training, we also conduct online training using state-of-the-art technologies to ensure the wonderful experience of online interactive learning. Data Science Training

    ReplyDelete
  8. This comment has been removed by the author.

    ReplyDelete
  9. I am impressed by the information that you have on this blog. It shows how well you understand this subject.

    data science course

    ReplyDelete
  10. Thanks for sharing great information. I like your blog and highly recommendData Science Training in Hyderabad

    ReplyDelete
  11. Really nice and interesting post. I was looking for this kind of information and enjoyed reading this one. Keep posting. Thanks for sharing.

    Simple Linear Regression

    Correlation vs covariance

    KNN Algorithm

    ReplyDelete
  12. This comment has been removed by the author.

    ReplyDelete
  13. The evaluation of the test is that if you communicate to an artificial intelligence and along the process you forget to remember that it is actually a computing system and not a person, then the system passes the test data science course in india

    ReplyDelete

  14. Very interesting blog. A lot of the blogs I visit nowadays don't really provide anything that I'm interested in, but I'm definitely interested in this one Turkish e Visa is an electronic visa Turkey that allows entry into Turkey. Once available, eligible foreign will be able to complete the online application process without visiting an embassy or consulate.

    ReplyDelete
  15. Wow, ich kann sagen, dass dies ein weiterer großartiger Artikel ist, wie von diesem Blog erwartet. hyazinthe pflege

    ReplyDelete
  16. Thanks for posting the best information and the blog.data science course in Lucknow

    ReplyDelete
  17. Thank you The foreign visitors need to apply for evisa kenya online. That offers them fast and secure visa services. You also can check the al information regarding to visa here to get in the Kenya.

    ReplyDelete
  18. Es ist eine großartige Website.. Das Design sieht sehr gut aus.. Weiter so!. Saunen und Bäder in Ihrer Nähe

    ReplyDelete
  19. Thanks for sharing this information. Foreign travelers who want a tourist visa India can now apply for a visa online.

    ReplyDelete
  20. Thanks for all you do. I like the website themes and layout, you are posting amazing blogs.... (Overseas Citizenship Of India Services) OCI card application online process. You can apply for an OCI card online and you can read all the info related to OCI(Overseas Citizenship Of India Services) card via the Indian visa website.

    ReplyDelete
  21. Extremely overall quite fascinating post. I was searching for this sort of data and delighted in perusing this one.
    Continue posting. A debt of gratitude is in order for sharing.
    data scientist course in warangal

    ReplyDelete
  22. I am really bad at remembering to pin articles I liked or were helpful
    but I do have a board started.
    Pakistani Drama Website

    ReplyDelete
  23. As what is good for the gander is good for the goose, why not let good old Ephraim Inoni benefit from this
    very same largesse? His account published a few days ago should pay off his debt without much loss of sleep.
    SEO Firm Chicago
    Digital Evrima

    ReplyDelete
  24. Great man. Nice to read your helpful blog . Saudi Arabia tourism is very popular. Saudi Arabia visa tourist Is one of most demanding travel visas in the world . Saudi Arabia is a very good place to visit . Al Ula is one of the best-known destinations in Saudi Arabia .It Also included in the top 20 most visited countries in the world.

    ReplyDelete
  25. I am sure this article has impressed all internet users, it is a really great article.. You can come to India but you will need a visa. You can apply online visa for India. You can read all the info about Indian visas via our website.

    ReplyDelete
  26. It might maintain to come into the dispensation into it and prepare the music to run. It gives you full convenience for data from one records site. Rekordbox Crack

    ReplyDelete
  27. Happy Birthday To My Wife. You dazzle me. I am so happy we get to share this crazy, beautiful life together. I love you.Happy Birthday To Wife

    ReplyDelete
  28. The Internet has known us huge opportunities for being more organized, for socializing easily. This priceless threat data give us unbelievable insight into what’s occurrence at the present. Malwarebytes License Key

    ReplyDelete


  29. Thanks for letting me know about this great post. I'm glad to have found this article.
    Propellerhead

    ReplyDelete
  30. EU Transforms Schengen Visa Application Process to Fully Digital Platform , making it easier and faster for travellers to apply for visas. The new system, called the European Travel Information and Authorization System (ETIAS), will allow travellers to apply for visas online, submit supporting documents digitally and receive electronic visas via email. This new system is designed to streamline the application process and reduce wait times.

    ReplyDelete
  31. very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. Looking for the best English speaking course in Riyadh? Enhance your language skills with Ziyyara’s tailored online English classes in Saudi Arabia.
    For more info visit Spoken English Classes in Saudi Arabia or Call +971505593798

    ReplyDelete
  32. Your blog post is a literary gem! The eloquence of your prose and the depth of your insights create an immersive reading experience. The way you navigate complex topics with clarity and finesse is truly admirable. Each word feels purposeful, inviting readers to reflect and engage. Looking forward to more enlightening posts that continue to blend intellect with an engaging narrative. I want to share some information with you Securing a Turkish visa for Indians is typically uncomplicated. The process involves submitting necessary documents such as a valid passport, completed application form, flight itinerary, and hotel reservation. Adequate preparation ensures a hassle-free experience, making the question is Turkish visa easy for Indians? affirmative, encouraging individuals to embark on a seamless journey to explore Turkey's cultural wonders.

    ReplyDelete
  33. This article is a real treasure trove! It grabbed my attention right away. Your talent for simplifying complex ideas is truly noteworthy. I can't wait to try out these strategies for myself. Keep up the superb work—I'll definitely be back for more insightful content like this. Wondering about the Ethiopian transit visa cost? Look no further! Our user-friendly service offers a hassle-free visa application process with transparent pricing. Whether you're stopping over for business or exploration, our competitive rates ensure your transit experience is seamless and stress-free. Say goodbye to uncertainty and hello to convenience with our efficient Ethiopian transit visa service. Let us help you navigate your layover in Ethiopia with ease.

    ReplyDelete

  34. In this blog, each word shines brightly, akin to precious gems, crafting a treasure trove of insight and knowledge. The captivating writing style and invaluable information serve as a compass on an enlightening journey. Is Kenya eTA the same as a visa? Yes, a Kenya eTA (Electronic Travel Authorization) serves as an electronic visa. It grants travelers permission to enter Kenya for specific purposes, such as tourism or business, for a designated period, similar to a traditional visa.

    ReplyDelete
  35. This comment has been removed by the author.

    ReplyDelete
  36. This captivating blog post offers a distinctive perspective on a subject often sidelined in discussions. The author's eloquent prose and profound insights resonate deeply, encouraging introspection and sparking meaningful interactions. Permit me to provide you with some pertinent information, Acquiring a Turkey visa for Afghanistan entails certain expenses, which are subject to fluctuation. While the Turkey visa for Afghanistan price is a crucial factor, it's essential to stay updated on the latest fees.

    ReplyDelete
  37. Your blog post was fantastic! Your fresh insights and clear explanations simplify complex topics. Real-world examples enhance the content and add depth. Thanks for sharing this valuable information! I'm eager to read more of your work—keep it coming! I'd love to hear your thoughts on related subjects! Complete your visa application form for Cameroon with ease. The online form simplifies the process by guiding you through each step. Submit the necessary details and documents to secure your entry into Cameroon. Start your adventure and experience the natural beauty and vibrant culture of this amazing destination!

    ReplyDelete
  38. Outstanding job! Your blog post is a breath of fresh air in the world of online content. You have a unique ability to simplify complex topics without sacrificing depth. Your writing flows seamlessly, keeping readers hooked from the first sentence to the last. Absolutely! US passport holders must obtain a visa before traveling to Saudi Arabia. It's a crucial step in ensuring a seamless entry into the country. To answer the question, 'Do US passport holders need a visa for Saudi Arabia?'—yes, they do. Remember to check the specific visa requirements and start the application process well in advance to avoid any delays in your travel plans.

    ReplyDelete
  39. After reading this, I found it really enlightening. I appreciate you taking the time and effort to put this information together. I once again find myself spending way too much time both reading and commenting, but it was still worthwhile! The Botswana Business eVisa facilitates entry for business purposes, requiring an application, passport validity, invitation letter, and proof of accommodation. Processing times vary, typically requiring 5-7 working days.

    ReplyDelete
  40. What a fantastic blog post! You broke down complex ideas into simple, easy-to-understand points brilliantly. The real-life examples you shared helped illustrate your arguments and kept me hooked. There's something I'd like to share with you. Curious about what is the visa process for Tanzania You can apply online or at the Tanzanian. You'll need a valid passport, a completed application form, a passport-sized photo, and payment of the visa fee. Processing usually takes a few days, so plan accordingly.

    ReplyDelete
  41. This article on XSS protection in Freemarker demonstrates the importance of secure coding practices when dealing with template engines, similar to the meticulous approach needed in data analysis. Just as escaping HTML properly can prevent security vulnerabilities, IIM SKILLS' Data Science Courses in Kansas provide a structured pathway to mastering data handling, ensuring accuracy and security in data-driven projects. These courses teach critical skills in data management and analysis, crucial for delivering secure and efficient solutions. Data Science Courses in Kansas

    ReplyDelete
  42. I appreciate the examples you provided. Looking forward to more valuable content!

    Data Science Courses in Brisbane

    ReplyDelete
  43. Thanks for discussing how to defend against XSS attacks in Freemarker! Your insights into best practices, such as proper escaping and validation, are essential for developers looking to enhance the security of their applications. This resource is crucial for anyone working with Freemarker to ensure safe user input handling. Great job raising awareness about web security!
    Data science Courses in hamburg

    ReplyDelete
  44. Freemarker templates, which helps defend against XSS vulnerabilities. By wrapping the templates with escape directives at the loading stage, you mitigate reliance on developers to consistently apply manual escapes. The WrappingReader class adds prologue and epilogue to each template, ensuring comprehensive coverage and boosting security. This proactive method is especially useful for teams working on large-scale projects where consistency and security are critical. Data science courses in Gurgaon

    ReplyDelete
  45. This is a really clever approach to defending against XSS attacks in Freemarker templates by programmatically ensuring that HTML escaping is always applied. Data science courses in Visakhapatnam

    ReplyDelete
  46. "Such a well-researched article! If you’re in Brighton, the Data Science courses in Brighton offer fantastic opportunities to enhance your skills and dive deeper into the world of data science. Highly recommend checking it out."

    ReplyDelete
  47. "Thank you for helping me understand session-level model attributes!"

    ReplyDelete
  48. Thanks for sharing this insightful approach! Wrapping Freemarker templates programmatically for HTML escaping is a smart solution to enforce XSS safety consistently. The code example and explanation make it easy to understand and implement—very helpful for developers working with Freemarker!
    Data science courses in Gujarat





    ReplyDelete
  49. "Amazing content! Anyone looking to dive deeper into data science should definitely explore the Data Science courses in Kochi. It's an excellent way to gain expertise and stand out in the field."

    ReplyDelete
  50. "Well said!"
    Data science course in mumbai.

    ReplyDelete
  51. Fantastic write-up! The balance of security best practices and Freemarker-specific tips is exactly what developers need to protect their applications from XSS vulnerabilities.
    Data science Courses in Sydney

    ReplyDelete
  52. This post is a valuable resource for developers looking to protect their web applications from XSS attacks. I like the practical tips you provided, such as sanitizing input and using Content Security Policies. It's essential to stay vigilant against security threats, and posts like this help spread awareness
    Data science courses in Glasgow

    ReplyDelete
  53. This article provides a detailed overview of defending against XSS attacks, an essential read for developers concerned about cybersecurity. The practical insights and real-world examples shared here make it highly informative. Thanks for breaking down a complex topic so well!

    Data science courses in france

    ReplyDelete
  54. This is a smart approach to enhance security in Freemarker by programmatically enforcing HTML escaping across templates. It eliminates the risk of human error while ensuring consistent protection against XSS attacks. A useful strategy for any project prioritizing security!

    Data science Courses in City of Westminster

    Neel KBH
    kbhneel@gmail.com

    ReplyDelete
  55. Defending against XSS (Cross-Site Scripting) attacks in Freemarker involves implementing proper input validation and output encoding. It has built-in escape functions to sanitize data before rendering it in templates. Additionally, validate user inputs on the server side and adhere to the principle of least privilege. Keeping libraries updated ensures you're protected against known vulnerabilities, bolstering your application's security against XSS threats.
    Data science Courses in Berlin

    ReplyDelete
  56. This post demonstrates an elegant approach to programmatically enforce HTML escaping in Freemarker templates by wrapping template loading with a custom reader. While effective in reducing reliance on manual intervention, it imposes limitations like disabling <#ftl> configuration and requiring <#noescape> for specific cases. This tradeoff simplifies security management at the cost of flexibility in template authoring.
    Data science Courses in Ireland

    ReplyDelete
  57. You have a knack for taking complex concepts and presenting them in a way that feels effortless to understand. I also appreciate how you included actionable steps—it makes the content practical and useful. To Sri Lanka tourist visa apply online . Complete the application form with personal details, travel information, and passport data. Ensure you meet the necessary requirements. Upon approval, the visa is emailed to you, allowing smooth entry to Sri Lanka.

    ReplyDelete
  58. Great content! The insights shared are both practical and thoughtful. I loved how you broke down complex ideas into simple, understandable points. This post is not only informative but also engaging—definitely a great read for those looking to expand their knowledge. Madagascar visa requirements are simple for most travelers. To apply for an eVisa, you’ll need a valid passport with at least six months of validity, a recent passport-sized photo, and proof of accommodation in Madagascar. The process is done entirely online, making it easy and convenient. Ensure that all your details are accurate to avoid delays in processing. Once approved, your eVisa will allow you to explore Madagascar’s unique wildlife and beautiful landscapes without any hassle.

    ReplyDelete
  59. "Thank you for helping me understand session-level model attributes!"
    Data Analytics Courses In Chennai

    ReplyDelete
  60. Excellent post! I appreciate how you simplified such a complex topic—it made everything much easier to understand. The tips you provided are both practical and relatable, especially I'm going to give it a try. Do you have any additional resources or recommendations for beginners? I’m excited to read more of your content! The visa Zimbabwe cost varies based on the visa type. A single-entry e-visa costs around, while a multiple-entry e-visa costs approximately Fees are paid during the online application process, making it convenient for travelers. To get the most accurate and updated information on e-visa Zimbabwe.

    ReplyDelete
  61. Your blog is a treasure trove of valuable information, and this latest post was outstanding. Thank you for your dedication to providing such great content. We are continually inspired by your work and appreciate the effort you put into each post. Discover how simple it is to travel to Russia with E-visa Russia. The online visa application Russia process ensures a smooth and quick experience, letting you focus on planning your adventure. Whether for business, tourism, or cultural visits, applying for your e-visa is convenient and stress-free. Skip the complicated steps and enjoy a reliable process. Begin your journey to explore Russia's unique charm and history.

    ReplyDelete
  62. A comprehensive guide to protecting web applications from XSS vulnerabilities.
    Data Analytics Courses In Bangalore

    ReplyDelete
  63. Great post! I appreciate how clearly you break down everything. It’s rare to find content that’s both insightful and easy to apply in real life. Your tips are practical and inspiring. I’ll definitely return for more valuable updates. Keep up the fantastic work, and thanks for sharing! Myanmar visa application online simplifies your travel plans, letting you apply for a Myanmar eVisa from the comfort of your home. The process is quick, secure, and user-friendly, requiring just a few details and documents to complete. Once approved, your eVisa grants you access to Myanmar’s breathtaking temples, vibrant markets, and serene landscapes. Skip the hassle of traditional applications and experience a seamless way to prepare for your adventure in this enchanting destination.

    ReplyDelete
  64. Your detailed explanation of defending against XSS attacks is crucial for web security. Thanks for the comprehensive guide!
    digital marketing course in chennai fees

    ReplyDelete
  65. "This is a great post on defending against XSS attacks! Your explanation of how XSS works and the preventive measures developers can take is spot-on. I especially appreciated the practical advice on input sanitization and escaping data. It's an essential topic that more people should be aware of!
    Top 10 Digital marketing courses in pune

    ReplyDelete
  66. I’m really impressed by this blog. The content is well-researched and presented in such a way that keeps me interested. It's clear that a lot of work goes into these posts. I’ve learned a lot and can’t wait for more content. To visit Morocco, travelers need to meet certain Morocco entry visa requirements. These may include providing proof of accommodation, sufficient funds for your stay, and a return ticket. Depending on your nationality, you may need to apply for a visa before your trip.

    ReplyDelete