How to prevent PDF Injection leads to Cross-site Scripting
during pdf upload and when viewing the uploaded pdf (MVC)
Please share sample code
Thanks
How to prevent PDF Injection leads to Cross-site Scripting
during pdf upload and when viewing the uploaded pdf (MVC)
Please share sample code
Thanks
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Tuhin PaulRank #7 · 71.9K reputationPosted Jan 14, 2024, 12:47 AM EST
To prevent PDF injection leading to Cross-Site Scripting (XSS) during PDF upload and viewing in an MVC (Model-View-Controller) application, you need to implement proper input validation, sanitize user input, and employ secure handling practices.
Tuhin PaulRank #7 · 71.9K reputationPosted Jan 14, 2024, 12:48 AM EST
[HttpPost] public ActionResult UploadPdf(HttpPostedFileBase file) { if (file != null && file.ContentLength > 0) { // Validate file type if (file.ContentType.ToLower() != "application/pdf") { ModelState.AddModelError("file", "Please upload a valid PDF file."); return View(); } // Generate a secure filename var fileName = Guid.NewGuid().ToString() + Path.GetExtension(file.FileName); // Save the file var filePath = Path.Combine(Server.MapPath("~/Uploads"), fileName); file.SaveAs(filePath); // Additional processing... return RedirectToAction("Index"); } ModelState.AddModelError("file", "Please choose a file to upload."); return View(); }