preventCsrf.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. exports.preventCsrf = exports.recommendedCsrfPreventionRequestHeaders = void 0;
  7. const whatwg_mimetype_1 = __importDefault(require("whatwg-mimetype"));
  8. const internalErrorClasses_js_1 = require("./internalErrorClasses.js");
  9. exports.recommendedCsrfPreventionRequestHeaders = [
  10. 'x-apollo-operation-name',
  11. 'apollo-require-preflight',
  12. ];
  13. const NON_PREFLIGHTED_CONTENT_TYPES = [
  14. 'application/x-www-form-urlencoded',
  15. 'multipart/form-data',
  16. 'text/plain',
  17. ];
  18. function preventCsrf(headers, csrfPreventionRequestHeaders) {
  19. const contentType = headers.get('content-type');
  20. if (contentType !== undefined) {
  21. const contentTypeParsed = whatwg_mimetype_1.default.parse(contentType);
  22. if (contentTypeParsed === null) {
  23. return;
  24. }
  25. if (!NON_PREFLIGHTED_CONTENT_TYPES.includes(contentTypeParsed.essence)) {
  26. return;
  27. }
  28. }
  29. if (csrfPreventionRequestHeaders.some((header) => {
  30. const value = headers.get(header);
  31. return value !== undefined && value.length > 0;
  32. })) {
  33. return;
  34. }
  35. throw new internalErrorClasses_js_1.BadRequestError(`This operation has been blocked as a potential Cross-Site Request Forgery ` +
  36. `(CSRF). Please either specify a 'content-type' header (with a type that ` +
  37. `is not one of ${NON_PREFLIGHTED_CONTENT_TYPES.join(', ')}) or provide ` +
  38. `a non-empty value for one of the following headers: ${csrfPreventionRequestHeaders.join(', ')}\n`);
  39. }
  40. exports.preventCsrf = preventCsrf;
  41. //# sourceMappingURL=preventCsrf.js.map