GeezerVison is showing github.com — fix(server): an HTML preview loads its files from the page's own fold… · patroza/t3code@bed9141
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: apps/server/src/assets/AssetAccess.test.ts +60-1Lines changed: 60 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -1172,7 +1172,8 @@ describe("AssetAccess", () => { | 1172 | 1172 | target: path.join(reportsDirectory, ".cache", "hidden.css"), | 1173 | 1173 | allowed: false, | 1174 | 1174 | }, | 1175 | | - { name: "shared.css", target: path.join(root, "shared.css"), allowed: true }, | | 1175 | + { name: "shared.css", target: path.join(root, "shared.css"), allowed: false }, | | 1176 | + { name: "local.css", target: path.join(reportsDirectory, "real.css"), allowed: true }, | 1176 | 1177 | ]; | 1177 | 1178 | for (const { name, target } of dependencies) { | 1178 | 1179 | yield* fs.writeFileString(target, "fixture"); | @@ -1199,6 +1200,64 @@ describe("AssetAccess", () => { | 1199 | 1200 | }).pipe(Effect.provide(layerTest)), | 1200 | 1201 | ); | 1201 | 1202 |
| | 1203 | + it.effect.skipIf(!symlinksSupported)( | | 1204 | + "serves an HTML preview's files from the page's own folder only", | | 1205 | + () => | | 1206 | + Effect.gen(function* () { | | 1207 | + const fs = yield* FileSystem.FileSystem; | | 1208 | + const path = yield* Path.Path; | | 1209 | + const root = yield* fs.makeTempDirectoryScoped(); | | 1210 | + const site = path.join(root, "site"); | | 1211 | + for (const directory of ["site/img", "src", "templates"]) { | | 1212 | + yield* fs.makeDirectory(path.join(root, directory), { recursive: true }); | | 1213 | + } | | 1214 | + for (const name of [ | | 1215 | + "site/page.html", | | 1216 | + "site/app.js", | | 1217 | + "site/img/logo.png", | | 1218 | + "src/main.js", | | 1219 | + "templates/page.html", | | 1220 | + ]) { | | 1221 | + yield* fs.writeFileString(path.join(root, name), "fixture"); | | 1222 | + } | | 1223 | + yield* fs.symlink(path.join(root, "src", "main.js"), path.join(site, "main.js")); | | 1224 | + yield* fs.symlink(path.join(root, "src"), path.join(site, "vendor")); | | 1225 | + yield* fs.symlink(path.join(site, "app.js"), path.join(site, "alias.js")); | | 1226 | + yield* fs.symlink( | | 1227 | + path.join(root, "templates", "page.html"), | | 1228 | + path.join(site, "linked.html"), | | 1229 | + ); | | 1230 | + | | 1231 | + const tokenFor = Effect.fn(function* (entry: string) { | | 1232 | + const result = yield* issueAssetUrl({ | | 1233 | + resource: { | | 1234 | + _tag: "workspace-file", | | 1235 | + threadId: ThreadId.make("thread-1"), | | 1236 | + path: path.join(site, entry), | | 1237 | + }, | | 1238 | + workspaceRoot: root, | | 1239 | + }); | | 1240 | + const suffix = result.relativeUrl.slice(ASSET_ROUTE_PREFIX.length + 1); | | 1241 | + return suffix.slice(0, suffix.indexOf("/")); | | 1242 | + }); | | 1243 | + const token = yield* tokenFor("page.html"); | | 1244 | + for (const name of ["page.html", "app.js", "alias.js", "img/logo.png"]) { | | 1245 | + expect(yield* resolveAsset(token, name), name).toMatchObject({ kind: "file" }); | | 1246 | + } | | 1247 | + for (const name of ["main.js", "vendor/main.js", "linked.html"]) { | | 1248 | + expect(yield* resolveAsset(token, name), name).toBeNull(); | | 1249 | + } | | 1250 | + | | 1251 | + // The opened page may itself be a link; the files it loads still come from its folder. | | 1252 | + const linkedToken = yield* tokenFor("linked.html"); | | 1253 | + expect(yield* resolveAsset(linkedToken, "linked.html")).toMatchObject({ | | 1254 | + path: yield* fs.realPath(path.join(root, "templates", "page.html")), | | 1255 | + }); | | 1256 | + expect(yield* resolveAsset(linkedToken, "app.js")).toMatchObject({ kind: "file" }); | | 1257 | + expect(yield* resolveAsset(linkedToken, "main.js")).toBeNull(); | | 1258 | + }).pipe(Effect.provide(layerTest)), | | 1259 | + ); | | 1260 | + | 1202 | 1261 | it.effect("issues workspace URLs that resolve the entry file and sibling assets", () => | 1203 | 1262 | Effect.gen(function* () { | 1204 | 1263 | const fileSystem = yield* FileSystem.FileSystem; | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/assets/AssetAccess.ts +9-4Lines changed: 9 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -332,6 +332,8 @@ const resolveCanonicalWorkspaceFile = Effect.fn("AssetAccess.resolveCanonicalWor | 332 | 332 | readonly relativePath: string; | 333 | 333 | /** The HTML entry's folder, relative to the root, when serving one of its dependencies. */ | 334 | 334 | readonly previewBaseRelativePath?: string; | | 335 | + /** The request is for the opened page itself, which may be a link to elsewhere in the root. */ | | 336 | + readonly previewEntry?: boolean; | 335 | 337 | }) { | 336 | 338 | const fileSystem = yield* FileSystem.FileSystem; | 337 | 339 | const workspacePaths = yield* WorkspacePaths.WorkspacePaths; | @@ -354,16 +356,17 @@ const resolveCanonicalWorkspaceFile = Effect.fn("AssetAccess.resolveCanonicalWor | 354 | 356 | if (relative === "" || relative.startsWith("..") || path.isAbsolute(relative)) return null; | 355 | 357 |
| 356 | 358 | if (input.previewBaseRelativePath !== undefined) { | 357 | | - // The opened entry's own folder may be hidden; nothing below it, or reached outside it, may. | | 359 | + // The opened entry's own folder may be hidden; nothing below it may. | 358 | 360 | const canonicalBase = yield* ProjectFaviconResolver.optionOnNotFound( | 359 | 361 | fileSystem.realPath(path.join(canonicalRoot.value, input.previewBaseRelativePath)), | 360 | 362 | ); | 361 | 363 | const belowBase = Option.isSome(canonicalBase) | 362 | 364 | ? path.relative(canonicalBase.value, canonicalFile.value) | 363 | 365 | : ".."; | 364 | | - const dependency = | 365 | | - belowBase.startsWith("..") || path.isAbsolute(belowBase) ? relative : belowBase; | 366 | | - if (!isPreviewDependencyPath(dependency, path)) return null; | | 366 | + const outsideBase = belowBase.startsWith("..") || path.isAbsolute(belowBase); | | 367 | + // Only the opened page may resolve outside its folder; the files it loads may not. | | 368 | + if (outsideBase && input.previewEntry !== true) return null; | | 369 | + if (!isPreviewDependencyPath(outsideBase ? relative : belowBase, path)) return null; | 367 | 370 | } | 368 | 371 |
| 369 | 372 | const info = yield* ProjectFaviconResolver.optionOnNotFound( | @@ -379,6 +382,7 @@ const resolveCanonicalWorkspaceFileForRequest = (input: { | 379 | 382 | readonly workspaceRoot: string; | 380 | 383 | readonly relativePath: string; | 381 | 384 | readonly previewBaseRelativePath?: string; | | 385 | + readonly previewEntry?: boolean; | 382 | 386 | }) => | 383 | 387 | resolveCanonicalWorkspaceFile(input).pipe( | 384 | 388 | Effect.tapError((cause) => | @@ -1153,6 +1157,7 @@ export const resolveAsset = Effect.fn("AssetAccess.resolveAsset")(function* ( | 1153 | 1157 | workspaceRoot: claims.workspaceRoot, | 1154 | 1158 | relativePath: joinedRelativePath, | 1155 | 1159 | previewBaseRelativePath: claims.baseRelativePath, | | 1160 | + previewEntry: decodedPath === claims.entryFileName, | 1156 | 1161 | }); | 1157 | 1162 | return workspaceFile | 1158 | 1163 | ? ({ kind: "file", path: workspaceFile, renderIntent: "file-preview" } satisfies ResolvedAsset) | |
Collapse file Copy file name to clipboardExpand all lines: docs/internals/04_environment-auth.md +5-2Lines changed: 5 additions & 2 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -151,5 +151,8 @@ relative paths and writes remain constrained by | 151 | 151 |
| 152 | 152 | Signed asset URLs are bearer credentials for a single canonical file, rechecked by device/inode | 153 | 153 | identity on serve ([`AssetAccess.ts`](../../apps/server/src/assets/AssetAccess.ts)). They never | 154 | | -grant access to sibling files, and because `filesystem:read` is checked only at mint time, an issued | 155 | | -URL remains valid until it expires even if the session or grant is revoked. | | 154 | +grant access to sibling files, with one exception: the URL of an HTML page opened from a workspace | | 155 | +also serves the files that page loads (styles, scripts, images, media, fonts and other pages) from | | 156 | +the page's own folder and the folders below it. It serves no hidden file, and no file that a link | | 157 | +in that folder points to outside it. Because `filesystem:read` is checked only at mint time, an | | 158 | +issued URL remains valid until it expires even if the session or grant is revoked. |
|