fix(editor): bound dangerous SQL preview size
This commit is contained in:
1 parent
1cf594eac3
commit
f6bba1cb6b
12 files changed
+287
-6
No files matched your search
@@ -0,0 +1,31 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { createBoundedTextPreview } from "@/lib/common/boundedTextPreview";
|
||||
|
||||
describe("createBoundedTextPreview", () => {
|
||||
it("keeps short text unchanged", () => {
|
||||
const sql = "DROP TABLE IF EXISTS users;\nSELECT 1;";
|
||||
|
||||
expect(createBoundedTextPreview(sql, { maxCharacters: 8192, maxLines: 200 })).toEqual({
|
||||
head: sql,
|
||||
tail: "",
|
||||
truncated: false,
|
||||
omittedCharacters: 0,
|
||||
omittedLines: 0,
|
||||
totalCharacters: sql.length,
|
||||
totalLines: 2,
|
||||
});
|
||||
});
|
||||
|
||||
it("bounds a 40k-line preview by both characters and lines", () => {
|
||||
const sql = Array.from({ length: 40_000 }, (_, index) => `INSERT INTO t VALUES (${index});`).join("\n");
|
||||
const preview = createBoundedTextPreview(sql, { maxCharacters: 8192, maxLines: 200 });
|
||||
|
||||
expect(preview.truncated).toBe(true);
|
||||
expect(preview.head.length + preview.tail.length).toBeLessThanOrEqual(8192);
|
||||
expect(preview.head.split("\n").length + preview.tail.split("\n").length).toBeLessThanOrEqual(200);
|
||||
expect(preview.head).toContain("VALUES (0)");
|
||||
expect(preview.tail).toContain("VALUES (39999)");
|
||||
expect(preview.omittedCharacters).toBeGreaterThan(1_000_000);
|
||||
expect(preview.omittedLines).toBeGreaterThanOrEqual(39_800);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,92 @@
|
||||
export interface BoundedTextPreviewOptions {
|
||||
maxCharacters: number;
|
||||
maxLines: number;
|
||||
}
|
||||
|
||||
export interface BoundedTextPreview {
|
||||
head: string;
|
||||
tail: string;
|
||||
truncated: boolean;
|
||||
omittedCharacters: number;
|
||||
omittedLines: number;
|
||||
totalCharacters: number;
|
||||
totalLines: number;
|
||||
}
|
||||
|
||||
function countLines(text: string): number {
|
||||
let lines = 1;
|
||||
for (let index = 0; index < text.length; index += 1) {
|
||||
if (text.charCodeAt(index) === 10) lines += 1;
|
||||
}
|
||||
return lines;
|
||||
}
|
||||
|
||||
function clampCodePointEnd(text: string, end: number): number {
|
||||
if (end <= 0 || end >= text.length) return end;
|
||||
const previous = text.charCodeAt(end - 1);
|
||||
const current = text.charCodeAt(end);
|
||||
return previous >= 0xd800 && previous <= 0xdbff && current >= 0xdc00 && current <= 0xdfff ? end - 1 : end;
|
||||
}
|
||||
|
||||
function clampCodePointStart(text: string, start: number): number {
|
||||
if (start <= 0 || start >= text.length) return start;
|
||||
const previous = text.charCodeAt(start - 1);
|
||||
const current = text.charCodeAt(start);
|
||||
return previous >= 0xd800 && previous <= 0xdbff && current >= 0xdc00 && current <= 0xdfff ? start + 1 : start;
|
||||
}
|
||||
|
||||
function headBoundary(text: string, maxCharacters: number, maxLines: number): number {
|
||||
const characterBoundary = Math.min(text.length, maxCharacters);
|
||||
let lines = 1;
|
||||
for (let index = 0; index < characterBoundary; index += 1) {
|
||||
if (text.charCodeAt(index) !== 10) continue;
|
||||
lines += 1;
|
||||
if (lines > maxLines) return index;
|
||||
}
|
||||
return clampCodePointEnd(text, characterBoundary);
|
||||
}
|
||||
|
||||
function tailBoundary(text: string, maxCharacters: number, maxLines: number): number {
|
||||
const characterBoundary = Math.max(0, text.length - maxCharacters);
|
||||
let lines = 1;
|
||||
for (let index = text.length - 1; index >= characterBoundary; index -= 1) {
|
||||
if (text.charCodeAt(index) !== 10) continue;
|
||||
lines += 1;
|
||||
if (lines > maxLines) return index + 1;
|
||||
}
|
||||
return clampCodePointStart(text, characterBoundary);
|
||||
}
|
||||
|
||||
export function createBoundedTextPreview(text: string, options: BoundedTextPreviewOptions): BoundedTextPreview {
|
||||
const maxCharacters = Math.max(2, Math.floor(options.maxCharacters));
|
||||
const maxLines = Math.max(2, Math.floor(options.maxLines));
|
||||
const totalLines = countLines(text);
|
||||
|
||||
if (text.length <= maxCharacters && totalLines <= maxLines) {
|
||||
return {
|
||||
head: text,
|
||||
tail: "",
|
||||
truncated: false,
|
||||
omittedCharacters: 0,
|
||||
omittedLines: 0,
|
||||
totalCharacters: text.length,
|
||||
totalLines,
|
||||
};
|
||||
}
|
||||
|
||||
const headEnd = headBoundary(text, Math.ceil(maxCharacters / 2), Math.ceil(maxLines / 2));
|
||||
const tailStart = Math.max(headEnd, tailBoundary(text, Math.floor(maxCharacters / 2), Math.floor(maxLines / 2)));
|
||||
const head = text.slice(0, headEnd);
|
||||
const tail = text.slice(tailStart);
|
||||
const visibleLines = countLines(head) + (tail ? countLines(tail) : 0);
|
||||
|
||||
return {
|
||||
head,
|
||||
tail,
|
||||
truncated: true,
|
||||
omittedCharacters: tailStart - headEnd,
|
||||
omittedLines: Math.max(0, totalLines - visibleLines),
|
||||
totalCharacters: text.length,
|
||||
totalLines,
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user