mirror of
https://github.com/PR0M3TH3AN/bitvid.git
synced 2026-11-03 03:12:31 +00:00
security: fix magnet parameter injection and data loss
- Fix magnet parameter injection vulnerability in `js/magnetShared.js` by ensuring all parameter values are decoded during normalization and re-encoded during URI construction.
- Fix a data loss bug in `normalizeMagnetInput` where parameter values containing `=` were truncated due to `split("=", 2)`.
- Consistently decode all magnet parameter values to ensure a clean internal representation.
- Use `encodeURIComponent` in `buildMagnetUri` to safely reconstruct the magnet URI.
Co-authored-by: PR0M3TH3AN <[email protected]>
This commit is contained in:
1 parent
f8f3c587ff
commit
be66e39ee5
1 file changed
+17
-11
+17
-11
@@ -223,19 +223,25 @@ export function normalizeMagnetInput(rawValue) {
|
||||
|
||||
const params = [];
|
||||
for (const rawParam of rawParams) {
|
||||
const [rawKey, rawVal = ""] = rawParam.split("=", 2);
|
||||
const key = rawKey.trim();
|
||||
const eqIndex = rawParam.indexOf("=");
|
||||
let key, rawVal;
|
||||
if (eqIndex === -1) {
|
||||
key = rawParam;
|
||||
rawVal = "";
|
||||
} else {
|
||||
key = rawParam.slice(0, eqIndex);
|
||||
rawVal = rawParam.slice(eqIndex + 1);
|
||||
}
|
||||
|
||||
key = key.trim();
|
||||
if (!key) {
|
||||
continue;
|
||||
}
|
||||
const lowerKey = key.toLowerCase();
|
||||
let value = rawVal.trim();
|
||||
if (lowerKey === "xt" && value) {
|
||||
const decoded = safeDecodeURIComponentLoose(value);
|
||||
if (decoded && decoded !== value) {
|
||||
value = decoded;
|
||||
didMutate = true;
|
||||
}
|
||||
|
||||
const trimmedRawVal = rawVal.trim();
|
||||
const value = safeDecodeURIComponentLoose(trimmedRawVal);
|
||||
if (value !== trimmedRawVal) {
|
||||
didMutate = true;
|
||||
}
|
||||
params.push(createParam(key, value));
|
||||
}
|
||||
@@ -253,7 +259,7 @@ export function normalizeMagnetInput(rawValue) {
|
||||
|
||||
export function buildMagnetUri(normalizedScheme, params, fragment) {
|
||||
const queryString = params
|
||||
.map(({ key, value }) => (value ? `${key}=${value}` : key))
|
||||
.map(({ key, value }) => (value ? `${key}=${encodeURIComponent(value)}` : key))
|
||||
.join("&");
|
||||
return `${normalizedScheme}${queryString ? `?${queryString}` : ""}${fragment || ""}`;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user