List and watch directories through links that leave the workspace root
经离开工作区根的链接列举和监听目录
`list` and `changes` judged a directory only by its resolved target: `ctx.fs.contains(root, target)` had to hold. A directory link in the workspace that resolves outside the root therefore appeared in its parent's listing as a directory and failed with `workspace-file/outside-workspace` when opened. The files under it stayed readable: `read`, `readBytes`, and `stat` follow the Session filesystem backend's read author
English
Problem
list and changes judged a directory only by its resolved target: ctx.fs.contains(root, target) had to hold. A directory link in the workspace that resolves outside the root therefore appeared in its parent's listing as a directory and failed with workspace-file/outside-workspace when opened. The files under it stayed readable: read, readBytes, and stat follow the Session filesystem backend's read authority, which the workspace file read authority does not limit to the workspace. The refusal withheld only the names under the link, and the reader had to ask the Agent for a link to each file.
Decision
A directory passes the workspace check of list and changes in either of two ways: its resolved target is inside the workspace root, or the path the filesystem backend resolved descends from the root by child names. workspaceDescent in src/descent.ts decides the second from two spellings the backend returns as FsTarget.displayPath, one for the root and one for the directory: each is absolute, has a home prefix expanded, and follows no link. The directory qualifies when its spelling is the root's spelling followed by child names. A .. segment, a spelling above or beside the root, another Windows drive, and a root spelling that is not absolute do not qualify. A .. segment is refused because after a link it names the parent of the link's target, and the workspace check cannot tell whether any link in the workspace names that directory.
A listing names a directory inside the root by its resolved position, and a directory outside the root by its child names below the root. An absolute directory outside the root, the home directory the backend expands from ~, and a .. traversal that resolves outside the root remain workspace-file/outside-workspace. Search does not follow links, and the file methods are unchanged.
This supersedes two earlier choices. The Workspace Files service and the workspace file read authority had list and changes judge a directory by resolved containment alone, and the archived directory link listing note refused a link that resolves outside the workspace.
Alternatives considered
Keep resolved containment as the only check. Rejected: the Session backend already serves the content of those files to the same caller, so the refusal withheld only directory entry names, while the tree presented a directory it could not open.
Remove the workspace check from list and changes. Rejected: a caller could then enumerate any Host directory by absolute path. Under the chosen rule, reaching a directory outside the root requires a link placed in the workspace.
Require a separate grant for each outside target. Rejected: the backend's read authority already covers the target, so a second approval would restrict browsing more than reading.
Judge the request string. Rejected: the backend reads the same string by its own rules. It expands a leading ~ to the Host user's home directory and applies its platform's drive and UNC rules, so a check that parsed the string separately accepted ~, and a path on another drive under a root spelled //server/share, with no link in the workspace. The backend's unresolved spelling already carries those rules.
Resolve each ancestor of the path until one equals the root. Rejected: it costs one backend resolution per path segment on every listing, and each is a round trip on a remote backend. Comparing the two spellings that list and changes already hold costs none.
Consequences
The Sidebar file tree expands a directory link that resolves outside the workspace, lists the directories below it, and refreshes them from watches. A link to a broad directory, such as the filesystem root, shows that directory's entry names to the Session's owner, whose file reads already reach it. Every workspaceFiles method stays owner-only in Liveshare. Link cycles are not detected: list and a directory watch each cover one level, so a link that leads back to an ancestor lists again at each level until the platform refuses the path.
A path spelled against a root other than WorkspaceFileScope.workspaceRoot, such as the canonical spelling of a Session directory that is itself reached through a link, passes only by resolved containment, so a link resolving outside the workspace stays refused for that spelling; under a Windows root the spellings compare exactly, so another letter case is refused the same way. The check relies on displayPath being the absolute spelling the backend resolved the target from, which the local backend returns and the SSH backend relays from the remote host; a backend that returns a workspace-relative path or a URI keeps such a link refused. Search results omit entries below directory links.
中文
Problem
list 与 changes 只按解析后的目标判定目录:ctx.fs.contains(root, target) 必须成立。因此,工作区内解析到工作区根外的目录链接在其父目录的列举结果中显示为目录,打开时却以 workspace-file/outside-workspace 失败。其下的文件仍然可读:read、readBytes 与 stat 沿用 Session 文件系统后端的读取权限,工作区文件读取权限不把它限制在工作区内。这一拒绝只隐藏了链接之下的名称,读者只能让 Agent 逐个给出文件链接。
Decision
目录以两种方式之一通过 list 与 changes 的工作区检查:其解析后的目标在工作区根内,或文件系统后端所解析的路径由工作区根逐级经子项名称向下写出。src/descent.ts 中的 workspaceDescent 依据后端以 FsTarget.displayPath 返回的两个写法判定第二种,一个属于根,一个属于目录:两者都是绝对路径,已展开主目录前缀,且未跟随任何链接。目录的写法等于根的写法后接子项名称时符合条件。含 .. 段的写法、位于根旁或根之上的写法、另一个 Windows 盘符,以及非绝对的根写法都不符合。拒绝 .. 段,是因为链接之后的 .. 段指向链接目标的父目录,而这项检查无法确认工作区内有链接指向该目录。
列举结果以解析后的位置命名工作区根内的目录,以其在根下的子项名称命名工作区根外的目录。工作区根外的绝对目录、后端由 ~ 展开的主目录,以及解析到根外的 .. 路径仍是 workspace-file/outside-workspace。搜索不跟随链接,文件方法保持不变。
本记录取代此前的两项选择。Workspace Files 服务与工作区文件读取权限让 list 与 changes 只按解析后的包含关系判定目录,已归档的目录链接列举记录拒绝解析到工作区外的链接。
Alternatives considered
只保留解析后的包含检查。 否决:Session 后端已经向同一调用方提供这些文件的内容,因此这一拒绝只隐藏了目录条目名称,而文件树展示了一个打不开的目录。
移除 list 与 changes 的工作区检查。 否决:调用方将可以按绝对路径枚举 Host 上的任意目录。按所选规则,到达根外目录需要工作区内存在一个链接。
要求为每个根外目标单独授权。 否决:后端的读取权限已经覆盖该目标,第二次批准会使浏览比读取受到更多限制。
按请求字符串判定。 否决:后端按自己的规则解读同一个字符串。它把开头的 ~ 展开为 Host 用户的主目录,并应用所在平台的盘符与 UNC 规则,因此单独解析该字符串的检查在工作区内没有任何链接时接受了 ~,以及根写作 //server/share 时另一个盘符上的路径。后端的未解析写法已经包含这些规则。
逐级解析路径的每个祖先,直到其中一个等于根。 否决:每次列举都要为每个路径段做一次后端解析,在远程后端上每次解析都是一次往返。比较 list 与 changes 已经持有的两个写法不需要任何解析。
Consequences
侧栏文件树可以展开解析到工作区外的目录链接,列出其下的目录,并通过监听刷新它们。指向范围很大的目录(例如文件系统根)的链接,会向 Session 的所有者显示该目录的条目名称,而所有者的文件读取本来就能到达那里。workspaceFiles 的每个方法在 Liveshare 中仍仅限所有者。不检测链接成环:list 与目录监听各自只覆盖一层,因此指回某个祖先的链接在每一层都会再次列出,直到平台拒绝该路径。
按 WorkspaceFileScope.workspaceRoot 以外的根写出的路径,例如本身经链接到达的 Session 目录的规范写法,只能通过解析后的包含检查,因此对这种写法,解析到工作区外的链接仍被拒绝;Windows 根下两个写法按原样比较,因此大小写不同的写法同样被拒绝。这项检查依赖 displayPath 是后端解析目标时所用的绝对写法:本地后端返回该写法,SSH 后端从远端主机转交它;返回工作区相对路径或 URI 的后端会继续拒绝这类链接。搜索结果不包含目录链接之下的条目。