localMediaPreviewProtocol.test.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. import { describe, expect, it, vi } from "vitest";
  2. import {
  3. buildLocalMediaPreviewUrl,
  4. createLocalMediaPreviewPathRegistry,
  5. installLocalMediaPreviewProtocol,
  6. registerLocalMediaPreviewScheme,
  7. } from "../src/main/localMediaPreviewProtocol.js";
  8. describe("localMediaPreviewProtocol", () => {
  9. it("registers a standard streaming secure scheme before app ready", () => {
  10. const protocol = { registerSchemesAsPrivileged: vi.fn() };
  11. registerLocalMediaPreviewScheme(protocol);
  12. expect(protocol.registerSchemesAsPrivileged).toHaveBeenCalledWith([
  13. {
  14. scheme: "zcode-media",
  15. privileges: { standard: true, secure: true, stream: true },
  16. },
  17. ]);
  18. });
  19. it.each([
  20. "/Users/test/.zcode/cli/video-cache/session/demo.mov",
  21. "C:\\Users\\test\\.zcode\\cli\\video-cache\\session\\demo.mp4",
  22. "\\\\server\\share\\demo.avi",
  23. ])("passes an exactly authorized media path to Electron's file loader: %s", async (path) => {
  24. let handler:
  25. | ((request: { url: string }, callback: (response: unknown) => void) => void)
  26. | undefined;
  27. const protocol = {
  28. registerFileProtocol: vi.fn(
  29. (
  30. _scheme: string,
  31. next: (request: { url: string }, callback: (response: unknown) => void) => void,
  32. ) => {
  33. handler = next;
  34. return true;
  35. },
  36. ),
  37. };
  38. const registry = createLocalMediaPreviewPathRegistry({
  39. isAbsolutePath: () => true,
  40. realpath: async (value) => value,
  41. realpathSync: (value) => value,
  42. isRegularFileSync: () => true,
  43. });
  44. const authorizedPath = await registry.authorize(path);
  45. installLocalMediaPreviewProtocol(protocol, {
  46. isPathAuthorized: registry.isAuthorized,
  47. });
  48. const callback = vi.fn();
  49. handler?.({ url: buildLocalMediaPreviewUrl(authorizedPath) }, callback);
  50. expect(callback).toHaveBeenCalledWith(authorizedPath);
  51. });
  52. it("canonicalizes a Host-authorized path and rejects the original alias", async () => {
  53. const registry = createLocalMediaPreviewPathRegistry({
  54. isAbsolutePath: () => true,
  55. realpath: async () => "/real/video.mp4",
  56. realpathSync: () => "/real/video.mp4",
  57. isRegularFileSync: () => true,
  58. });
  59. await expect(registry.authorize("/linked/video.mp4")).resolves.toBe("/real/video.mp4");
  60. expect(registry.isAuthorized("/real/video.mp4")).toBe(true);
  61. expect(registry.isAuthorized("/linked/video.mp4")).toBe(false);
  62. });
  63. it("rejects malformed and unauthorized absolute preview URLs", () => {
  64. let handler:
  65. | ((request: { url: string }, callback: (response: unknown) => void) => void)
  66. | undefined;
  67. const protocol = {
  68. registerFileProtocol: vi.fn(
  69. (
  70. _scheme: string,
  71. next: (request: { url: string }, callback: (response: unknown) => void) => void,
  72. ) => {
  73. handler = next;
  74. return true;
  75. },
  76. ),
  77. };
  78. installLocalMediaPreviewProtocol(protocol, { isPathAuthorized: () => false });
  79. for (const url of [
  80. "zcode-media://local/not-absolute",
  81. buildLocalMediaPreviewUrl("/etc/passwd"),
  82. "zcode-media://other/value",
  83. ]) {
  84. const callback = vi.fn();
  85. handler?.({ url }, callback);
  86. expect(callback).toHaveBeenCalledWith({ error: -300 });
  87. }
  88. });
  89. it("rejects relative paths before registering them", async () => {
  90. const registry = createLocalMediaPreviewPathRegistry({
  91. isAbsolutePath: (path) => path.startsWith("/"),
  92. realpath: async (path) => path,
  93. });
  94. await expect(registry.authorize("relative/demo.mp4")).rejects.toThrow(
  95. "Local media preview path must be absolute",
  96. );
  97. });
  98. it("expires authorized paths and supports explicit clear", async () => {
  99. let now = 1_000;
  100. const registry = createLocalMediaPreviewPathRegistry({
  101. isAbsolutePath: () => true,
  102. now: () => now,
  103. ttlMs: 100,
  104. realpath: async (path) => path,
  105. realpathSync: (path) => path,
  106. isRegularFileSync: () => true,
  107. });
  108. await registry.authorize("/workspace/video.mp4");
  109. expect(registry.isAuthorized("/workspace/video.mp4")).toBe(true);
  110. now += 90;
  111. await registry.authorize("/workspace/video.mp4");
  112. now += 20;
  113. expect(registry.isAuthorized("/workspace/video.mp4")).toBe(true);
  114. now += 81;
  115. expect(registry.isAuthorized("/workspace/video.mp4")).toBe(false);
  116. await registry.authorize("/workspace/video.mp4");
  117. registry.clear();
  118. expect(registry.isAuthorized("/workspace/video.mp4")).toBe(false);
  119. });
  120. it("evicts the least recently used authorization at capacity", async () => {
  121. let now = 1_000;
  122. const registry = createLocalMediaPreviewPathRegistry({
  123. isAbsolutePath: () => true,
  124. now: () => now,
  125. ttlMs: 10_000,
  126. maxEntries: 2,
  127. realpath: async (path) => path,
  128. realpathSync: (path) => path,
  129. isRegularFileSync: () => true,
  130. });
  131. await registry.authorize("/workspace/a.mp4");
  132. now += 1;
  133. await registry.authorize("/workspace/b.mp4");
  134. now += 1;
  135. expect(registry.isAuthorized("/workspace/a.mp4")).toBe(true);
  136. now += 1;
  137. await registry.authorize("/workspace/c.mp4");
  138. expect(registry.isAuthorized("/workspace/a.mp4")).toBe(true);
  139. expect(registry.isAuthorized("/workspace/b.mp4")).toBe(false);
  140. expect(registry.isAuthorized("/workspace/c.mp4")).toBe(true);
  141. });
  142. it("rejects an authorized path whose real target or file type changed", async () => {
  143. let currentRealpath = "/workspace/video.mp4";
  144. let regularFile = true;
  145. const registry = createLocalMediaPreviewPathRegistry({
  146. isAbsolutePath: () => true,
  147. realpath: async () => "/workspace/video.mp4",
  148. realpathSync: () => currentRealpath,
  149. isRegularFileSync: () => regularFile,
  150. });
  151. await registry.authorize("/workspace/video.mp4");
  152. expect(registry.isAuthorized("/workspace/video.mp4")).toBe(true);
  153. currentRealpath = "/private/sensitive.txt";
  154. expect(registry.isAuthorized("/workspace/video.mp4")).toBe(false);
  155. currentRealpath = "/workspace/video.mp4";
  156. regularFile = false;
  157. await registry.authorize("/workspace/video.mp4");
  158. expect(registry.isAuthorized("/workspace/video.mp4")).toBe(false);
  159. });
  160. });