queryBuilder.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /**
  2. * Qsirch query builder.
  3. *
  4. * We build a Qsirch "q" string using documented operators like:
  5. * - path:"/Public"
  6. * - modified:"YYYY-MM-DD"
  7. * - modified:"YYYY-MM-DD..YYYY-MM-DD"
  8. * - comparison operators: modified:>=YYYY-MM-DD
  9. * - extension:"pdf"
  10. *
  11. * Note:
  12. * - Qsirch operator syntax must not include spaces between operator and value.
  13. * (Example: name:"QNAP" is correct, name: QNAP is incorrect.)
  14. *
  15. * Security:
  16. * - We treat user input as plain search terms.
  17. * - We strip characters that could turn user input into Qsirch operators.
  18. */
  19. /**
  20. * Normalize and sanitize user query so it cannot inject Qsirch operators.
  21. *
  22. * @param {string|null} raw
  23. * @returns {string|null}
  24. */
  25. export function sanitizeUserQuery(raw) {
  26. if (typeof raw !== "string") return null;
  27. let s = raw.trim();
  28. if (!s) return null;
  29. // Prevent operator injection:
  30. // - ":" is used by Qsirch operators (path:, modified:, extension:, ...)
  31. // - quotes can shape operator values
  32. s = s.replace(/[:"]/g, " ");
  33. // Prevent the user query from interfering with our own OR chaining:
  34. // We only remove the standalone token "OR" (case-sensitive),
  35. // so normal words like "order" or German "oder" remain unaffected.
  36. s = s.replace(/\bOR\b/g, " ");
  37. // Normalize whitespace
  38. s = s.replace(/\s+/g, " ").trim();
  39. return s || null;
  40. }
  41. function normalizePathPrefix(prefix) {
  42. let p = String(prefix || "").trim();
  43. if (!p) return "/";
  44. // Ensure leading slash and no trailing slash (unless it's just "/").
  45. if (!p.startsWith("/")) p = `/${p}`;
  46. if (p.length > 1) p = p.replace(/\/+$/, "");
  47. return p;
  48. }
  49. function buildDateClause(dateField, from, to) {
  50. const f = String(dateField || "modified").trim();
  51. // Range is the most explicit and is documented by QNAP for date ranges.
  52. if (from && to) return `${f}:"${from}..${to}"`;
  53. // QNAP documents comparison operators for dates/sizes as well.
  54. // Example: modified:<2015 (year)
  55. // We use ISO date strings here for determinism.
  56. if (from) return `${f}:>=${from}`;
  57. if (to) return `${f}:<=${to}`;
  58. return null;
  59. }
  60. function buildBranchClause({ pathPrefix, branch }) {
  61. const prefix = normalizePathPrefix(pathPrefix);
  62. return `path:"${prefix}/${branch}"`;
  63. }
  64. function buildGlobalClause({ pathPrefix }) {
  65. const prefix = normalizePathPrefix(pathPrefix);
  66. return `path:"${prefix}"`;
  67. }
  68. /**
  69. * Build the Qsirch "q" string from normalized inputs.
  70. *
  71. * @param {{
  72. * mode: "branch"|"multi"|"all",
  73. * branches: string[]|null,
  74. * q: string|null,
  75. * from: string|null,
  76. * to: string|null,
  77. * dateField: "modified"|"created",
  78. * pathPrefix: string
  79. * }} input
  80. * @returns {string}
  81. */
  82. export function buildQsirchQuery({
  83. mode,
  84. branches,
  85. q,
  86. from,
  87. to,
  88. dateField,
  89. pathPrefix,
  90. }) {
  91. const userTerms = sanitizeUserQuery(q);
  92. const dateClause = buildDateClause(dateField, from, to);
  93. const extClause = `extension:"pdf"`;
  94. // Base terms that should always apply within a clause.
  95. // Order does not matter for AND semantics, but keeping a stable ordering
  96. // makes debugging and tests easier.
  97. function assembleClause(pathClause) {
  98. const parts = [];
  99. if (userTerms) parts.push(userTerms);
  100. parts.push(pathClause);
  101. parts.push(extClause);
  102. if (dateClause) parts.push(dateClause);
  103. return parts.join(" ");
  104. }
  105. if (mode === "all") {
  106. return assembleClause(buildGlobalClause({ pathPrefix }));
  107. }
  108. if (mode === "branch") {
  109. const b = branches?.[0];
  110. return assembleClause(buildBranchClause({ pathPrefix, branch: b }));
  111. }
  112. // mode === "multi"
  113. // We replicate the full clause per branch and connect with OR.
  114. // This avoids precedence issues where a shared "extension:" or "modified:"
  115. // might only apply to the last OR segment.
  116. const clauses = (branches || []).map((b) =>
  117. assembleClause(buildBranchClause({ pathPrefix, branch: b }))
  118. );
  119. return clauses.join(" OR ");
  120. }