{"version":3,"file":"safe-href.mjs","names":[],"sources":["../../../../src/ai/response/safe-href.ts"],"sourcesContent":["/*!\n *\n *  Copyright (c) \"Neo4j\"\n *  Neo4j Sweden AB [http://neo4j.com]\n *\n *  This file is part of Neo4j.\n *\n *  Neo4j is free software: you can redistribute it and/or modify\n *  it under the terms of the GNU General Public License as published by\n *  the Free Software Foundation, either version 3 of the License, or\n *  (at your option) any later version.\n *\n *  This program is distributed in the hope that it will be useful,\n *  but WITHOUT ANY WARRANTY; without even the implied warranty of\n *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n *  GNU General Public License for more details.\n *\n *  You should have received a copy of the GNU General Public License\n *  along with this program.  If not, see <http://www.gnu.org/licenses/>.\n */\n\nconst SAFE_PROTOCOLS = new Set(['http:', 'https:', 'mailto:']);\n/** Dummy https origin used only to classify relative vs absolute URLs. */\nconst DUMMY_BASE = 'https://safe.invalid/';\n\n/** True when the string contains C0 controls or DEL (can obfuscate schemes). */\nconst hasControlCharacter = (value: string): boolean => {\n  for (let index = 0; index < value.length; index++) {\n    const code = value.charCodeAt(index);\n    if (code <= 0x1f || code === 0x7f) {\n      return true;\n    }\n  }\n  return false;\n};\n\n/** Scheme / path / host only — excludes query and fragment. */\nconst structuralPrefix = (value: string): string => {\n  const queryIndex = value.indexOf('?');\n  const fragmentIndex = value.indexOf('#');\n  let end = value.length;\n  if (queryIndex !== -1) {\n    end = Math.min(end, queryIndex);\n  }\n  if (fragmentIndex !== -1) {\n    end = Math.min(end, fragmentIndex);\n  }\n  return value.slice(0, end);\n};\n\nconst tryDecode = (value: string): string => {\n  try {\n    return decodeURIComponent(value);\n  } catch {\n    return value;\n  }\n};\n\n/**\n * Returns a URL safe to use as an anchor `href` or image `src`, or `undefined`\n * when the value must not be rendered as a navigable/fetchable URL.\n */\nexport const safeHref = (href?: string): string | undefined => {\n  if (!href) {\n    return undefined;\n  }\n  const trimmed = href.trim();\n  if (!trimmed) {\n    return undefined;\n  }\n\n  // Control characters can obfuscate schemes (e.g. java\\nscript:).\n  if (hasControlCharacter(trimmed)) {\n    return undefined;\n  }\n\n  // Decode only the structural prefix so query/body percent-encoding (e.g.\n  // mailto ?body=…%0D%0A… or ?q=%5C) is not treated as URL structure.\n  const structure = structuralPrefix(trimmed);\n  const decodedStructure = tryDecode(structure);\n  if (hasControlCharacter(decodedStructure)) {\n    return undefined;\n  }\n\n  // Network-path / backslash forms resolve or normalize cross-origin.\n  if (\n    /^[/\\\\]{2}/.test(trimmed) ||\n    /^[/\\\\]{2}/.test(structure) ||\n    /^[/\\\\]{2}/.test(decodedStructure) ||\n    structure.includes('\\\\') ||\n    decodedStructure.includes('\\\\')\n  ) {\n    return undefined;\n  }\n\n  let parsed: URL;\n  try {\n    parsed = new URL(trimmed, DUMMY_BASE);\n  } catch {\n    return undefined;\n  }\n\n  const hasExplicitScheme = /^[a-zA-Z][a-zA-Z0-9+.-]*:/.test(trimmed);\n\n  if (hasExplicitScheme) {\n    return SAFE_PROTOCOLS.has(parsed.protocol) ? trimmed : undefined;\n  }\n\n  // Path-/query-/fragment-relative (including bare paths like docs/page):\n  // must resolve against the dummy origin, not a different host.\n  if (parsed.origin !== new URL(DUMMY_BASE).origin) {\n    return undefined;\n  }\n\n  return trimmed;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAqBA,MAAM,iCAAiB,IAAI,IAAI;CAAC;CAAS;CAAU;AAAS,CAAC;;AAE7D,MAAM,aAAa;;AAGnB,MAAM,uBAAuB,UAA2B;CACtD,KAAK,IAAI,QAAQ,GAAG,QAAQ,MAAM,QAAQ,SAAS;EACjD,MAAM,OAAO,MAAM,WAAW,KAAK;EACnC,IAAI,QAAQ,MAAQ,SAAS,KAC3B,OAAO;CAEX;CACA,OAAO;AACT;;AAGA,MAAM,oBAAoB,UAA0B;CAClD,MAAM,aAAa,MAAM,QAAQ,GAAG;CACpC,MAAM,gBAAgB,MAAM,QAAQ,GAAG;CACvC,IAAI,MAAM,MAAM;CAChB,IAA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