git-ssh-url.test.ts 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import { describe, expect, it } from "vitest";
  2. import { parseGitUrl } from "../src/utils/git.ts";
  3. describe("Git URL Parsing", () => {
  4. describe("protocol URLs (accepted without git: prefix)", () => {
  5. it("should parse HTTPS URL", () => {
  6. const result = parseGitUrl("https://github.com/user/repo");
  7. expect(result).toMatchObject({
  8. host: "github.com",
  9. path: "user/repo",
  10. repo: "https://github.com/user/repo",
  11. });
  12. });
  13. it("should parse ssh:// URL", () => {
  14. const result = parseGitUrl("ssh://git@github.com/user/repo");
  15. expect(result).toMatchObject({
  16. host: "github.com",
  17. path: "user/repo",
  18. repo: "ssh://git@github.com/user/repo",
  19. });
  20. });
  21. it("should parse protocol URL with ref", () => {
  22. const result = parseGitUrl("https://github.com/user/repo@v1.0.0");
  23. expect(result).toMatchObject({
  24. host: "github.com",
  25. path: "user/repo",
  26. ref: "v1.0.0",
  27. repo: "https://github.com/user/repo",
  28. });
  29. });
  30. });
  31. describe("shorthand URLs (accepted only with git: prefix)", () => {
  32. it("should parse git@host:path with git: prefix", () => {
  33. const result = parseGitUrl("git:git@github.com:user/repo");
  34. expect(result).toMatchObject({
  35. host: "github.com",
  36. path: "user/repo",
  37. repo: "git@github.com:user/repo",
  38. });
  39. });
  40. it("should parse host/path shorthand with git: prefix", () => {
  41. const result = parseGitUrl("git:github.com/user/repo");
  42. expect(result).toMatchObject({
  43. host: "github.com",
  44. path: "user/repo",
  45. repo: "https://github.com/user/repo",
  46. });
  47. });
  48. it("should parse shorthand with ref and git: prefix", () => {
  49. const result = parseGitUrl("git:git@github.com:user/repo@v1.0.0");
  50. expect(result).toMatchObject({
  51. host: "github.com",
  52. path: "user/repo",
  53. ref: "v1.0.0",
  54. repo: "git@github.com:user/repo",
  55. });
  56. });
  57. });
  58. it("should reject unsafe git install path inputs", () => {
  59. for (const source of [
  60. "git:git@evil.example:../../victim/repo",
  61. "https://evil.example/..%2F..%2Fvictim/repo",
  62. "https://evil.example/..%2F..%2Fvictim/repo%",
  63. "git:git@evil.example:/absolute/repo",
  64. "git:git@evil.example:user\\repo/name",
  65. "git:git@evil.example:user/repo\0name",
  66. ]) {
  67. expect(parseGitUrl(source)).toBeNull();
  68. }
  69. });
  70. describe("unsupported without git: prefix", () => {
  71. it("should reject git@host:path without git: prefix", () => {
  72. expect(parseGitUrl("git@github.com:user/repo")).toBeNull();
  73. });
  74. it("should reject host/path shorthand without git: prefix", () => {
  75. expect(parseGitUrl("github.com/user/repo")).toBeNull();
  76. });
  77. it("should reject user/repo shorthand", () => {
  78. expect(parseGitUrl("user/repo")).toBeNull();
  79. });
  80. });
  81. });