Fix space on end of page name creates two pages, one shadowed #615 (#817)

pull/829/head
Florent 2024-03-23 20:02:16 +01:00 committed by GitHub
parent 8a3782e946
commit 5a6d7ecae1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 31 additions and 10 deletions

View File

@ -34,14 +34,18 @@ export class HttpSpacePrimitives implements SpacePrimitives {
throw new Error("Offline"); throw new Error("Offline");
} }
if (result.redirected) { if (result.redirected) {
// Got a redirect, we'll assume this is due to invalid credentials and redirecting to an auth page if (result.status === 401) {
console.log( console.log(
"Got a redirect via the API so will redirect to URL", "Received unauthorized status and got a redirect via the API so will redirect to URL",
result.url, result.url,
); );
alert("You are not authenticated, redirecting to login page..."); alert("You are not authenticated, redirecting to login page...");
location.href = result.url; location.href = result.url;
throw new Error("Not authenticated"); throw new Error("Not authenticated");
} else {
location.href = result.url;
throw new Error("Redirected");
}
} }
if (result.status === 401) { if (result.status === 401) {
location.reload(); location.reload();

View File

@ -12,6 +12,7 @@ import { parse } from "$common/markdown_parser/parse_tree.ts";
import { renderMarkdownToHtml } from "../plugs/markdown/markdown_render.ts"; import { renderMarkdownToHtml } from "../plugs/markdown/markdown_render.ts";
import { parsePageRef } from "$sb/lib/page_ref.ts"; import { parsePageRef } from "$sb/lib/page_ref.ts";
import { base64Encode } from "$lib/crypto.ts"; import { base64Encode } from "$lib/crypto.ts";
import * as path from "$std/path/mod.ts";
const authenticationExpirySeconds = 60 * 60 * 24 * 7; // 1 week const authenticationExpirySeconds = 60 * 60 * 24 * 7; // 1 week
@ -462,6 +463,7 @@ export class HttpServer {
}); });
const filePathRegex = "/:path{[^!].*\\.[a-zA-Z]+}"; const filePathRegex = "/:path{[^!].*\\.[a-zA-Z]+}";
const mdExt = ".md";
this.app.get( this.app.get(
filePathRegex, filePathRegex,
@ -474,7 +476,7 @@ export class HttpServer {
name, name,
); );
if ( if (
name.endsWith(".md") && name.endsWith(mdExt) &&
// This header signififies the requests comes directly from the http_space_primitives client (not the browser) // This header signififies the requests comes directly from the http_space_primitives client (not the browser)
!req.header("X-Sync-Mode") && !req.header("X-Sync-Mode") &&
// This Accept header is used by federation to still work with CORS // This Accept header is used by federation to still work with CORS
@ -486,7 +488,7 @@ export class HttpServer {
console.warn( console.warn(
"Request was without X-Sync-Mode nor a CORS request, redirecting to page", "Request was without X-Sync-Mode nor a CORS request, redirecting to page",
); );
return c.redirect(`/${name.slice(0, -3)}`); return c.redirect(`/${name.slice(0, -mdExt.length)}`, 401);
} }
if (name.startsWith(".")) { if (name.startsWith(".")) {
// Don't expose hidden files // Don't expose hidden files
@ -518,6 +520,16 @@ export class HttpServer {
return c.text(e.message, 500); return c.text(e.message, 500);
} }
} }
const filename = path.posix.basename(name, mdExt);
if (filename.trim() !== filename) {
const newName = path.posix.join(
path.posix.dirname(name),
filename.trim(),
);
return c.redirect(`/${newName}`);
}
try { try {
if (req.header("X-Get-Meta")) { if (req.header("X-Get-Meta")) {
// Getting meta via GET request // Getting meta via GET request
@ -557,6 +569,11 @@ export class HttpServer {
return c.text("Forbidden", 403); return c.text("Forbidden", 403);
} }
const filename = path.posix.basename(name, mdExt);
if (filename.trim() !== filename) {
return c.text("Malformed filename", 400);
}
const body = await req.arrayBuffer(); const body = await req.arrayBuffer();
try { try {