location-mutation.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. export * as LocationMutation from "./location-mutation"
  2. import path from "path"
  3. import { Context, Effect, Layer, Schema } from "effect"
  4. import { FSUtil } from "./fs-util"
  5. import { Location } from "./location"
  6. export const Kind = Schema.Literals(["file", "directory"])
  7. export type Kind = typeof Kind.Type
  8. /**
  9. * Mutation paths do not accept project references. Relative paths must stay
  10. * inside the active Location. Absolute paths outside it require separate
  11. * `external_directory` approval.
  12. */
  13. export const ResolveInput = Schema.Struct({
  14. path: Schema.String,
  15. /** Selects the external approval boundary; it does not validate the target type. */
  16. kind: Kind.pipe(Schema.optional),
  17. })
  18. export type ResolveInput = typeof ResolveInput.Type
  19. export class PathError extends Schema.TaggedErrorClass<PathError>()("LocationMutation.PathError", {
  20. path: Schema.String,
  21. reason: Schema.Literals(["relative_escape", "location_escape", "non_directory_ancestor"]),
  22. }) {}
  23. export interface ExternalDirectoryAuthorization {
  24. readonly action: "external_directory"
  25. /** Canonical existing directory used as the external approval boundary. */
  26. readonly directory: string
  27. /** `external_directory` permission resource. */
  28. readonly resource: string
  29. readonly save: string
  30. }
  31. export const externalDirectoryPermission = (input: ExternalDirectoryAuthorization) => ({
  32. action: input.action,
  33. resources: [input.resource],
  34. save: [input.save],
  35. })
  36. export interface Target {
  37. /** Canonical existing path, or missing path below a canonical directory. */
  38. readonly canonical: string
  39. /** Permission resource: Location-relative for internal paths, canonical for external paths. */
  40. readonly resource: string
  41. readonly externalDirectory?: ExternalDirectoryAuthorization
  42. }
  43. export interface Interface {
  44. /**
  45. * Resolve a path and derive its permission resources. Relative paths must
  46. * stay inside the Location. Absolute paths outside it require separate
  47. * `external_directory` approval. This does not approve the mutation.
  48. */
  49. readonly resolve: (input: ResolveInput) => Effect.Effect<Target, PathError | FSUtil.Error>
  50. }
  51. export class Service extends Context.Service<Service, Interface>()("@opencode/v2/LocationMutation") {}
  52. interface ResolvedPath {
  53. readonly canonical: string
  54. readonly type?:
  55. | "File"
  56. | "Directory"
  57. | "SymbolicLink"
  58. | "BlockDevice"
  59. | "CharacterDevice"
  60. | "FIFO"
  61. | "Socket"
  62. | "Unknown"
  63. readonly directory: string
  64. }
  65. const slash = (value: string) => value.replaceAll("\\", "/")
  66. export const layer = Layer.effect(
  67. Service,
  68. Effect.gen(function* () {
  69. const fs = yield* FSUtil.Service
  70. const location = yield* Location.Service
  71. const locationRoot = yield* fs.realPath(location.directory)
  72. function notFound<A>(effect: Effect.Effect<A, FSUtil.Error>) {
  73. return effect.pipe(Effect.catchReason("PlatformError", "NotFound", () => Effect.succeed(undefined)))
  74. }
  75. const resolvePath = Effect.fnUntraced(function* (absolute: string) {
  76. const existing = yield* notFound(fs.realPath(absolute))
  77. if (existing !== undefined) {
  78. const info = yield* fs.stat(existing)
  79. return {
  80. canonical: existing,
  81. type: info.type,
  82. directory: info.type === "Directory" ? existing : path.dirname(existing),
  83. } satisfies ResolvedPath
  84. }
  85. let anchor = path.dirname(absolute)
  86. while (true) {
  87. const canonical = yield* notFound(fs.realPath(anchor))
  88. if (canonical !== undefined) {
  89. const info = yield* fs.stat(canonical)
  90. if (info.type !== "Directory") {
  91. return yield* new PathError({ path: absolute, reason: "non_directory_ancestor" })
  92. }
  93. return {
  94. canonical: path.resolve(canonical, path.relative(anchor, absolute)),
  95. directory: canonical,
  96. } satisfies ResolvedPath
  97. }
  98. const parent = path.dirname(anchor)
  99. if (parent === anchor) return yield* new PathError({ path: absolute, reason: "non_directory_ancestor" })
  100. anchor = parent
  101. }
  102. })
  103. const resolve = Effect.fn("LocationMutation.resolve")(function* (input: ResolveInput) {
  104. const relative = !path.isAbsolute(input.path)
  105. const absolute = path.resolve(location.directory, input.path)
  106. const lexicallyInternal = FSUtil.contains(location.directory, absolute)
  107. if (relative && !lexicallyInternal) return yield* new PathError({ path: input.path, reason: "relative_escape" })
  108. const resolved = yield* resolvePath(absolute)
  109. if (lexicallyInternal && !FSUtil.contains(locationRoot, resolved.canonical)) {
  110. return yield* new PathError({ path: input.path, reason: "location_escape" })
  111. }
  112. const external = !lexicallyInternal
  113. const resource = external
  114. ? slash(resolved.canonical)
  115. : slash(path.relative(locationRoot, resolved.canonical) || ".")
  116. const externalDirectory =
  117. input.kind === "directory" && resolved.type === "Directory" ? resolved.canonical : resolved.directory
  118. const externalResource = slash(path.join(externalDirectory, "*"))
  119. return {
  120. canonical: resolved.canonical,
  121. resource,
  122. externalDirectory: external
  123. ? {
  124. action: "external_directory",
  125. directory: externalDirectory,
  126. resource: externalResource,
  127. save: externalResource,
  128. }
  129. : undefined,
  130. } satisfies Target
  131. })
  132. return Service.of({ resolve })
  133. }),
  134. )
  135. export const locationLayer = layer