index.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. var __defProp = Object.defineProperty;
  2. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  3. var __getOwnPropNames = Object.getOwnPropertyNames;
  4. var __hasOwnProp = Object.prototype.hasOwnProperty;
  5. var __export = (target, all) => {
  6. for (var name in all)
  7. __defProp(target, name, { get: all[name], enumerable: true });
  8. };
  9. var __copyProps = (to, from, except, desc) => {
  10. if (from && typeof from === "object" || typeof from === "function") {
  11. for (let key of __getOwnPropNames(from))
  12. if (!__hasOwnProp.call(to, key) && key !== except)
  13. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  14. }
  15. return to;
  16. };
  17. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  18. var csrf_exports = {};
  19. __export(csrf_exports, {
  20. csrf: () => csrf
  21. });
  22. module.exports = __toCommonJS(csrf_exports);
  23. var import_http_exception = require("../../http-exception");
  24. const secFetchSiteValues = ["same-origin", "same-site", "none", "cross-site"];
  25. const isSecFetchSite = (value) => secFetchSiteValues.includes(value);
  26. const isSafeMethodRe = /^(GET|HEAD)$/;
  27. const isRequestedByFormElementRe = /^\b(application\/x-www-form-urlencoded|multipart\/form-data|text\/plain)\b/i;
  28. const csrf = (options) => {
  29. const originHandler = ((optsOrigin) => {
  30. if (!optsOrigin) {
  31. return (origin, c) => origin === new URL(c.req.url).origin;
  32. } else if (typeof optsOrigin === "string") {
  33. return (origin) => origin === optsOrigin;
  34. } else if (typeof optsOrigin === "function") {
  35. return optsOrigin;
  36. } else {
  37. return (origin) => optsOrigin.includes(origin);
  38. }
  39. })(options?.origin);
  40. const isAllowedOrigin = async (origin, c) => {
  41. if (origin === void 0) {
  42. return false;
  43. }
  44. return await originHandler(origin, c);
  45. };
  46. const secFetchSiteHandler = ((optsSecFetchSite) => {
  47. if (!optsSecFetchSite) {
  48. return (secFetchSite) => secFetchSite === "same-origin";
  49. } else if (typeof optsSecFetchSite === "string") {
  50. return (secFetchSite) => secFetchSite === optsSecFetchSite;
  51. } else if (typeof optsSecFetchSite === "function") {
  52. return optsSecFetchSite;
  53. } else {
  54. return (secFetchSite) => optsSecFetchSite.includes(secFetchSite);
  55. }
  56. })(options?.secFetchSite);
  57. const isAllowedSecFetchSite = async (secFetchSite, c) => {
  58. if (secFetchSite === void 0) {
  59. return false;
  60. }
  61. if (!isSecFetchSite(secFetchSite)) {
  62. return false;
  63. }
  64. return await secFetchSiteHandler(secFetchSite, c);
  65. };
  66. return async function csrf2(c, next) {
  67. if (!isSafeMethodRe.test(c.req.method) && isRequestedByFormElementRe.test(c.req.header("content-type") || "text/plain") && !await isAllowedSecFetchSite(c.req.header("sec-fetch-site"), c) && !await isAllowedOrigin(c.req.header("origin"), c)) {
  68. const res = new Response("Forbidden", { status: 403 });
  69. throw new import_http_exception.HTTPException(403, { res });
  70. }
  71. await next();
  72. };
  73. };
  74. // Annotate the CommonJS export names for ESM import in node:
  75. 0 && (module.exports = {
  76. csrf
  77. });