{"version":3,"file":"email-template.mjs","sources":["../../../../server/controllers/validation/email-template.js"],"sourcesContent":["'use strict';\n\nconst { trim } = require('lodash/fp');\nconst {\n  template: { createLooseInterpolationRegExp, createStrictInterpolationRegExp },\n} = require('@strapi/utils');\n\nconst invalidPatternsRegexes = [\n  // Ignore \"evaluation\" patterns: <% ... %>\n  /<%[^=]([\\s\\S]*?)%>/m,\n  // Ignore basic string interpolations\n  /\\${([^{}]*)}/m,\n];\n\nconst authorizedKeys = [\n  'URL',\n  'ADMIN_URL',\n  'SERVER_URL',\n  'CODE',\n  'USER',\n  'USER.email',\n  'USER.username',\n  'TOKEN',\n];\n\nconst matchAll = (pattern, src) => {\n  const matches = [];\n  let match;\n\n  const regexPatternWithGlobal = RegExp(pattern, 'g');\n\n  // eslint-disable-next-line no-cond-assign\n  while ((match = regexPatternWithGlobal.exec(src))) {\n    const [, group] = match;\n\n    matches.push(trim(group));\n  }\n\n  return matches;\n};\n\nconst isValidEmailTemplate = (template) => {\n  // Check for known invalid patterns\n  for (const reg of invalidPatternsRegexes) {\n    if (reg.test(template)) {\n      return false;\n    }\n  }\n\n  const interpolation = {\n    // Strict interpolation pattern to match only valid groups\n    strict: createStrictInterpolationRegExp(authorizedKeys),\n    // Weak interpolation pattern to match as many group as possible.\n    loose: createLooseInterpolationRegExp(),\n  };\n\n  // Compute both strict & loose matches\n  const strictMatches = matchAll(interpolation.strict, template);\n  const looseMatches = matchAll(interpolation.loose, template);\n\n  // If we have more matches with the loose RegExp than with the strict one,\n  // then it means that at least one of the interpolation group is invalid\n  // Note: In the future, if we wanted to give more details for error formatting\n  // purposes, we could return the difference between the two arrays\n  if (looseMatches.length > strictMatches.length) {\n    return false;\n  }\n\n  return true;\n};\n\nmodule.exports = {\n  isValidEmailTemplate,\n};\n"],"names":["trim","require$$0","template","createLooseInterpolationRegExp","createStrictInterpolationRegExp","require$$1","invalidPatternsRegexes","authorizedKeys","matchAll","pattern","src","matches","match","regexPatternWithGlobal","RegExp","exec","group","push","isValidEmailTemplate","reg","test","interpolation","strict","loose","strictMatches","looseMatches","length","emailTemplate"],"mappings":";;;;;;;;IAEA,MAAM,EAAEA,IAAI,EAAE,GAAGC,UAAAA;IACjB,MAAM,EACJC,UAAU,EAAEC,8BAA8B,EAAEC,+BAA+B,EAAE,EAC9E,GAAGC,UAAAA;AAEJ,IAAA,MAAMC,sBAAAA,GAAyB;;AAE7B,QAAA,qBAAA;;AAEA,QAAA;AACD,KAAA;AAED,IAAA,MAAMC,cAAAA,GAAiB;AACrB,QAAA,KAAA;AACA,QAAA,WAAA;AACA,QAAA,YAAA;AACA,QAAA,MAAA;AACA,QAAA,MAAA;AACA,QAAA,YAAA;AACA,QAAA,eAAA;AACA,QAAA;AACD,KAAA;IAED,MAAMC,QAAAA,GAAW,CAACC,OAAAA,EAASC,GAAAA,GAAAA;AACzB,QAAA,MAAMC,UAAU,EAAE;QAClB,IAAIC,KAAAA;QAEJ,MAAMC,sBAAAA,GAAyBC,OAAOL,OAAAA,EAAS,GAAA,CAAA;;AAG/C,QAAA,MAAQG,KAAAA,GAAQC,sBAAAA,CAAuBE,IAAI,CAACL,GAAAA,CAAAA,CAAO;YACjD,MAAM,GAAGM,MAAM,GAAGJ,KAAAA;YAElBD,OAAAA,CAAQM,IAAI,CAACjB,IAAAA,CAAKgB,KAAAA,CAAAA,CAAAA;AACtB,QAAA;QAEE,OAAOL,OAAAA;AACT,IAAA,CAAA;AAEA,IAAA,MAAMO,uBAAuB,CAAChB,QAAAA,GAAAA;;QAE5B,KAAK,MAAMiB,OAAOb,sBAAAA,CAAwB;YACxC,IAAIa,GAAAA,CAAIC,IAAI,CAAClB,QAAAA,CAAAA,EAAW;gBACtB,OAAO,KAAA;AACb,YAAA;AACA,QAAA;AAEE,QAAA,MAAMmB,aAAAA,GAAgB;;AAEpBC,YAAAA,MAAAA,EAAQlB,+BAAAA,CAAgCG,cAAAA,CAAAA;;YAExCgB,KAAAA,EAAOpB,8BAAAA;AACX,SAAA;;AAGE,Q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