ScriptSecurity.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using FastReport.Utils;
  6. using System.Text.RegularExpressions;
  7. namespace FastReport.Web
  8. {
  9. partial class WebReport
  10. {
  11. /// <summary>
  12. /// Sets custom class for checking the report script.
  13. /// </summary>
  14. /// <param name="scriptChecker"></param>
  15. public static void SetScriptSecurity(IScriptChecker scriptChecker)
  16. {
  17. ScriptSecurity.Dispose();
  18. ScriptSecurity = new ScriptSecurity(scriptChecker);
  19. }
  20. }
  21. internal sealed class ScriptSecurity : IDisposable
  22. {
  23. private readonly IScriptChecker ScriptChecker;
  24. internal ScriptSecurity(IScriptChecker checker)
  25. {
  26. ScriptChecker = checker;
  27. Config.ScriptCompile += Config_ScriptCompile;
  28. }
  29. internal void Config_ScriptCompile(object sender, ScriptSecurityEventArgs e)
  30. {
  31. if(Config.EnableScriptSecurity)
  32. e.IsValid = ScriptChecker.IsValid(e.ReportLanguage, e.ReportScript, e.References, e.Report);
  33. }
  34. public void Dispose()
  35. {
  36. Config.ScriptCompile -= Config_ScriptCompile;
  37. }
  38. }
  39. /// <summary>
  40. /// Interface for overriding the standard check of the report script
  41. /// <see cref="IsValid(Language, string, string[], Report)"/>
  42. /// </summary>
  43. public interface IScriptChecker
  44. {
  45. /// <summary>
  46. /// Method for checking the report script
  47. /// </summary>
  48. /// <param name="lang">Report script language</param>
  49. /// <param name="reportScript">Report script</param>
  50. /// <param name="references">Referenced assemblies</param>
  51. /// <param name="report">Report</param>
  52. /// <returns>Returns true if the report passed the validation check</returns>
  53. bool IsValid(Language lang, string reportScript, string[] references, Report report);
  54. }
  55. internal sealed class ScriptChecker : IScriptChecker
  56. {
  57. public bool IsValid(Language lang, string reportScript, string[] references, Report report)
  58. {
  59. // LOGIC
  60. foreach(string reference in references)
  61. {
  62. // in .Net Core need to add reference
  63. if (reference.IndexOf("System.IO.FileSystem") != -1)
  64. return false;
  65. if (reference.IndexOf("Microsoft.AspNetCore") != -1)
  66. return false;
  67. if(reference.IndexOf("System.Net") != -1)
  68. return false;
  69. }
  70. foreach (string pattern in Config.ScriptSecurityProps.StopList)
  71. {
  72. if (reportScript.IndexOf(pattern) != -1)
  73. return false;
  74. //regex = new Regex(pattern);
  75. //if (regex.IsMatch(reportScript))
  76. }
  77. return true;
  78. }
  79. }
  80. }