JOHN SILLVESTOR commited on
Commit
cade3fc
·
verified ·
1 Parent(s): b0d6bb2

Tighten stream blog gate and preview ranges

Browse files
Files changed (1) hide show
  1. server.js +9 -21
server.js CHANGED
@@ -15,7 +15,7 @@ const API_HASH = process.env.TELEGRAM_API_HASH || "";
15
  const STREAM_SECRET = process.env.STREAM_SECRET || "";
16
  const ROLE = (process.env.NODE_ROLE || "stream").toLowerCase();
17
  const HASH_LEN = 6;
18
- const TOKEN_TTL = 900;
19
  const MIN_ALIGN = 4096;
20
  const MB = 1024 * 1024;
21
  const GET_SIZES = [1048576, 524288, 262144, 131072, 65536, 32768, 16384, 8192, 4096];
@@ -165,7 +165,9 @@ function destOk(req, allow) {
165
 
166
  function streamDestOk(req) {
167
  const d = String(req.headers["sec-fetch-dest"] || "").toLowerCase();
168
- if (!d) return true;
 
 
169
  const block = [
170
  "document", "iframe", "frame", "fencedframe", "embed", "object",
171
  "script", "style", "manifest", "worker", "sharedworker", "serviceworker",
@@ -840,6 +842,7 @@ function applyPreview(rng, size, l) {
840
  if (!rng || !size) return rng;
841
  if (lvlPct(l) >= 100) {
842
  const end = Math.min(rng.end, size - 1);
 
843
  return {
844
  start: rng.start,
845
  end,
@@ -855,6 +858,7 @@ function applyPreview(rng, size, l) {
855
  }
856
  if (rng.start <= maxEnd) {
857
  const end = Math.min(rng.end, maxEnd);
 
858
  return {
859
  start: rng.start,
860
  end,
@@ -864,6 +868,7 @@ function applyPreview(rng, size, l) {
864
  if (tailStart < size && rng.end >= tailStart) {
865
  const start = Math.max(rng.start, tailStart);
866
  const end = Math.min(rng.end, size - 1);
 
867
  return { start, end, partial: true };
868
  }
869
  return null;
@@ -903,7 +908,7 @@ function parseRange(h, size) {
903
  }
904
  if (!Number.isFinite(start) || start < 0) start = 0;
905
  if (!Number.isFinite(end) || end >= size) end = size - 1;
906
- if (start > end) start = 0;
907
  return { start, end, partial: start > 0 || end < size - 1 };
908
  }
909
 
@@ -1219,27 +1224,10 @@ async function handleGenerate(req, res, url) {
1219
  }
1220
 
1221
  async function handleStream(req, res, url, msgId) {
1222
- if (!streamDestOk(req)) {
1223
  res.writeHead(403);
1224
  return res.end();
1225
  }
1226
- const o = String(req.headers.origin || "").trim();
1227
- if (o && o.toLowerCase() !== "null" && !isBlogOrigin(o)) {
1228
- res.writeHead(403);
1229
- return res.end();
1230
- }
1231
- const ref = String(req.headers.referer || "");
1232
- if (ref) {
1233
- try {
1234
- if (!isBlogOrigin(new URL(ref).origin)) {
1235
- res.writeHead(403);
1236
- return res.end();
1237
- }
1238
- } catch {
1239
- res.writeHead(403);
1240
- return res.end();
1241
- }
1242
- }
1243
  const k = url.searchParams.get("k") || "";
1244
  const ticket = openTicket(k);
1245
  if (!ticket || ticket.m !== msgId) {
 
15
  const STREAM_SECRET = process.env.STREAM_SECRET || "";
16
  const ROLE = (process.env.NODE_ROLE || "stream").toLowerCase();
17
  const HASH_LEN = 6;
18
+ const TOKEN_TTL = 600;
19
  const MIN_ALIGN = 4096;
20
  const MB = 1024 * 1024;
21
  const GET_SIZES = [1048576, 524288, 262144, 131072, 65536, 32768, 16384, 8192, 4096];
 
165
 
166
  function streamDestOk(req) {
167
  const d = String(req.headers["sec-fetch-dest"] || "").toLowerCase();
168
+ const site = String(req.headers["sec-fetch-site"] || "").toLowerCase();
169
+ if (site === "none") return false;
170
+ if (!d) return fromBlog(req);
171
  const block = [
172
  "document", "iframe", "frame", "fencedframe", "embed", "object",
173
  "script", "style", "manifest", "worker", "sharedworker", "serviceworker",
 
842
  if (!rng || !size) return rng;
843
  if (lvlPct(l) >= 100) {
844
  const end = Math.min(rng.end, size - 1);
845
+ if (rng.start > end) return null;
846
  return {
847
  start: rng.start,
848
  end,
 
858
  }
859
  if (rng.start <= maxEnd) {
860
  const end = Math.min(rng.end, maxEnd);
861
+ if (rng.start > end) return null;
862
  return {
863
  start: rng.start,
864
  end,
 
868
  if (tailStart < size && rng.end >= tailStart) {
869
  const start = Math.max(rng.start, tailStart);
870
  const end = Math.min(rng.end, size - 1);
871
+ if (start > end) return null;
872
  return { start, end, partial: true };
873
  }
874
  return null;
 
908
  }
909
  if (!Number.isFinite(start) || start < 0) start = 0;
910
  if (!Number.isFinite(end) || end >= size) end = size - 1;
911
+ if (start > end) return null;
912
  return { start, end, partial: start > 0 || end < size - 1 };
913
  }
914
 
 
1224
  }
1225
 
1226
  async function handleStream(req, res, url, msgId) {
1227
+ if (!streamDestOk(req) || !fromBlog(req)) {
1228
  res.writeHead(403);
1229
  return res.end();
1230
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1231
  const k = url.searchParams.get("k") || "";
1232
  const ticket = openTicket(k);
1233
  if (!ticket || ticket.m !== msgId) {