You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
3.2 KiB

11 months ago
  1. const { CommunityHub } = require("../../models/communityHub");
  2. const { reqBody } = require("../http");
  3. /**
  4. * ### Must be called after `communityHubItem`
  5. * Checks if community hub bundle downloads are enabled. The reason this functionality is disabled
  6. * by default is that since AgentSkills, Workspaces, and DataConnectors are all imported from the
  7. * community hub via unzipping a bundle - it would be possible for a malicious user to craft and
  8. * download a malicious bundle and import it into their own hosted instance. To avoid this, this
  9. * functionality is disabled by default and must be enabled manually by the system administrator.
  10. *
  11. * On hosted systems, this would not be an issue since the user cannot modify this setting, but those
  12. * who self-host can still unlock this feature manually by setting the environment variable
  13. * which would require someone who likely has the capacity to understand the risks and the
  14. * implications of importing unverified items that can run code on their system, container, or instance.
  15. * @see {@link https://docs.anythingllm.com/docs/community-hub/import}
  16. * @param {import("express").Request} request
  17. * @param {import("express").Response} response
  18. * @param {import("express").NextFunction} next
  19. * @returns {void}
  20. */
  21. function communityHubDownloadsEnabled(request, response, next) {
  22. if (!("COMMUNITY_HUB_BUNDLE_DOWNLOADS_ENABLED" in process.env)) {
  23. return response.status(422).json({
  24. error:
  25. "Community Hub bundle downloads are not enabled. The system administrator must enable this feature manually to allow this instance to download these types of items. See https://docs.anythingllm.com/configuration#anythingllm-hub-agent-skills",
  26. });
  27. }
  28. // If the admin specifically did not set the system to `allow_all` then downloads are limited to verified items or private items only.
  29. // This is to prevent users from downloading unverified items and importing them into their own instance without understanding the risks.
  30. const item = response.locals.bundleItem;
  31. if (
  32. !item.verified &&
  33. item.visibility !== "private" &&
  34. process.env.COMMUNITY_HUB_BUNDLE_DOWNLOADS_ENABLED !== "allow_all"
  35. ) {
  36. return response.status(422).json({
  37. error:
  38. "Community hub bundle downloads are limited to verified public items or private team items only. Please contact the system administrator to review or modify this setting. See https://docs.anythingllm.com/configuration#anythingllm-hub-agent-skills",
  39. });
  40. }
  41. next();
  42. }
  43. /**
  44. * Fetch the bundle item from the community hub.
  45. * Sets `response.locals.bundleItem` and `response.locals.bundleUrl`.
  46. */
  47. async function communityHubItem(request, response, next) {
  48. const { importId } = reqBody(request);
  49. if (!importId)
  50. return response.status(500).json({
  51. success: false,
  52. error: "Import ID is required",
  53. });
  54. const {
  55. url,
  56. item,
  57. error: fetchError,
  58. } = await CommunityHub.getBundleItem(importId);
  59. if (fetchError)
  60. return response.status(500).json({
  61. success: false,
  62. error: fetchError,
  63. });
  64. response.locals.bundleItem = item;
  65. response.locals.bundleUrl = url;
  66. next();
  67. }
  68. module.exports = {
  69. communityHubItem,
  70. communityHubDownloadsEnabled,
  71. };