nuxt-skew-protection 2.0.4 → 2.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/module.json +1 -1
- package/dist/module.mjs +75 -25
- package/package.json +1 -1
package/dist/module.json
CHANGED
package/dist/module.mjs
CHANGED
|
@@ -469,8 +469,24 @@ function withCloudflareBuildAssetRouting(current, buildAssetsDir) {
|
|
|
469
469
|
|
|
470
470
|
const RE_ENOTFOUND = /ENOTFOUND\s+(\S+)/;
|
|
471
471
|
const RE_GETADDRINFO = /getaddrinfo\s+\S+\s+(\S+)/;
|
|
472
|
+
const RE_HTTP_STATUS = /(?:^|:\s+|\b(?:HTTP(?: error)?|status(?: code)?)\s+)([1-5]\d{2})(?=\s|$)/i;
|
|
472
473
|
const PENDING_UPLOADS_KEY = "pending-uploads.json";
|
|
473
474
|
const RE_BLOB_KEY = /^blobs\/[a-f0-9]{64}(?:[-/][a-f0-9-]{36})?$/;
|
|
475
|
+
const RE_VERSION_PREFIX = /^[\w-][\w.-]*$/;
|
|
476
|
+
const RE_UNSAFE_STORAGE_KEY = /[\\:%?#\s\p{Cc}]/u;
|
|
477
|
+
const RESERVED_STORAGE_PREFIXES = /* @__PURE__ */ new Set([
|
|
478
|
+
"blobs",
|
|
479
|
+
"asset-operations",
|
|
480
|
+
"asset-operation-completions",
|
|
481
|
+
PENDING_UPLOADS_KEY,
|
|
482
|
+
"version-manifest.json"
|
|
483
|
+
]);
|
|
484
|
+
function isRecoverableAssetKey(key) {
|
|
485
|
+
if (RE_BLOB_KEY.test(key))
|
|
486
|
+
return true;
|
|
487
|
+
const [version, ...segments] = key.split("/");
|
|
488
|
+
return !!version && RE_VERSION_PREFIX.test(version) && !RESERVED_STORAGE_PREFIXES.has(version.toLowerCase()) && !RE_UNSAFE_STORAGE_KEY.test(key) && segments.length > 0 && segments.every((segment) => segment.length > 0 && segment !== "." && segment !== "..");
|
|
489
|
+
}
|
|
474
490
|
function formatStorageError(error, operation) {
|
|
475
491
|
const cause = error instanceof Error ? error.cause : void 0;
|
|
476
492
|
const message = error instanceof Error ? error.message : String(error);
|
|
@@ -483,7 +499,8 @@ function formatStorageError(error, operation) {
|
|
|
483
499
|
if (causeMessage.includes("ECONNREFUSED") || message.includes("ECONNREFUSED")) {
|
|
484
500
|
return new Error(`Storage ${operation} failed: Connection refused. Is the storage server running and accessible?`);
|
|
485
501
|
}
|
|
486
|
-
|
|
502
|
+
const httpStatus = message.match(RE_HTTP_STATUS)?.[1];
|
|
503
|
+
if (httpStatus === "401" || !httpStatus && (message.includes("Unauthorized") || message.includes("WRONGPASS"))) {
|
|
487
504
|
return new Error(`Storage ${operation} failed: Authentication failed. Check your storage credentials.`);
|
|
488
505
|
}
|
|
489
506
|
if (message.includes("timeout") || message.includes("ETIMEDOUT")) {
|
|
@@ -615,15 +632,30 @@ function createAssetManager(options) {
|
|
|
615
632
|
function safetyKey(key) {
|
|
616
633
|
return `asset-operations/${createHash("sha256").update(key).digest("hex")}`;
|
|
617
634
|
}
|
|
618
|
-
|
|
619
|
-
const
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
635
|
+
function createOperationReader() {
|
|
636
|
+
const operations = /* @__PURE__ */ new Map();
|
|
637
|
+
const completions = /* @__PURE__ */ new Map();
|
|
638
|
+
return (key) => {
|
|
639
|
+
const existing = operations.get(key);
|
|
640
|
+
if (existing)
|
|
641
|
+
return existing;
|
|
642
|
+
const read = storage.getItem(safetyKey(key)).then((operation) => {
|
|
643
|
+
if (operation === null)
|
|
644
|
+
return false;
|
|
645
|
+
if (typeof operation !== "string")
|
|
646
|
+
throw new TypeError("Invalid pending asset uploads. Expected content-addressed object keys.");
|
|
647
|
+
let complete = completions.get(operation);
|
|
648
|
+
if (!complete) {
|
|
649
|
+
complete = storage.getItem(`asset-operation-completions/${operation}`).then((value) => value === true);
|
|
650
|
+
completions.set(operation, complete);
|
|
651
|
+
}
|
|
652
|
+
return complete.then((value) => !value);
|
|
653
|
+
});
|
|
654
|
+
operations.set(key, read);
|
|
655
|
+
return read;
|
|
656
|
+
};
|
|
625
657
|
}
|
|
626
|
-
async function protectAssetOperation(keys) {
|
|
658
|
+
async function protectAssetOperation(keys, hasUncertainOperation = createOperationReader()) {
|
|
627
659
|
const operation = randomUUID();
|
|
628
660
|
const uniqueKeys = [
|
|
629
661
|
...new Set(keys)
|
|
@@ -655,6 +687,7 @@ function createAssetManager(options) {
|
|
|
655
687
|
const candidates = [
|
|
656
688
|
...new Set(keys)
|
|
657
689
|
].filter((key) => !retainedKeys.has(key));
|
|
690
|
+
const hasUncertainOperation = createOperationReader();
|
|
658
691
|
await processBatch(candidates, 50, async (key) => {
|
|
659
692
|
if (await hasUncertainOperation(key))
|
|
660
693
|
uncertain.add(key);
|
|
@@ -680,7 +713,7 @@ function createAssetManager(options) {
|
|
|
680
713
|
const results = await processBatch(candidates, 50, async (key) => {
|
|
681
714
|
if (uncertain.has(key) || RE_BLOB_KEY.test(key) && !deletable.has(key))
|
|
682
715
|
return key;
|
|
683
|
-
const complete = await protectAssetOperation([key]);
|
|
716
|
+
const complete = await protectAssetOperation([key], hasUncertainOperation);
|
|
684
717
|
return storage.removeItem(key).then(async () => {
|
|
685
718
|
await complete();
|
|
686
719
|
return void 0;
|
|
@@ -706,13 +739,13 @@ function createAssetManager(options) {
|
|
|
706
739
|
const value = await storage.getItem(PENDING_UPLOADS_KEY);
|
|
707
740
|
if (value === null)
|
|
708
741
|
return { _tag: "PendingUploads", keys: [], uncertainKeys: [] };
|
|
709
|
-
if (typeof value !== "object" || value === null || !("_tag" in value) || value._tag !== "PendingUploads" || !("keys" in value) || !Array.isArray(value.keys) || !value.keys.every((key) => typeof key === "string" &&
|
|
710
|
-
throw new TypeError("Invalid pending asset uploads. Expected
|
|
742
|
+
if (typeof value !== "object" || value === null || !("_tag" in value) || value._tag !== "PendingUploads" || !("keys" in value) || !Array.isArray(value.keys) || !value.keys.every((key) => typeof key === "string" && isRecoverableAssetKey(key))) {
|
|
743
|
+
throw new TypeError("Invalid pending asset uploads. Expected stored asset keys.");
|
|
711
744
|
}
|
|
712
745
|
const keys = value.keys;
|
|
713
746
|
const uncertainKeys = "uncertainKeys" in value ? value.uncertainKeys : keys;
|
|
714
747
|
if (!Array.isArray(uncertainKeys) || !uncertainKeys.every((key) => typeof key === "string" && key.length > 0)) {
|
|
715
|
-
throw new TypeError("Invalid pending asset uploads. Expected
|
|
748
|
+
throw new TypeError("Invalid pending asset uploads. Expected stored asset keys.");
|
|
716
749
|
}
|
|
717
750
|
return {
|
|
718
751
|
_tag: "PendingUploads",
|
|
@@ -721,7 +754,7 @@ function createAssetManager(options) {
|
|
|
721
754
|
};
|
|
722
755
|
}
|
|
723
756
|
async function recordPendingUploads(keys) {
|
|
724
|
-
const additions = [...keys].filter(
|
|
757
|
+
const additions = [...keys].filter(isRecoverableAssetKey);
|
|
725
758
|
if (additions.length === 0)
|
|
726
759
|
return;
|
|
727
760
|
const previous = await getPendingUploads();
|
|
@@ -741,7 +774,7 @@ function createAssetManager(options) {
|
|
|
741
774
|
const keys = [
|
|
742
775
|
.../* @__PURE__ */ new Set([
|
|
743
776
|
...previous.keys,
|
|
744
|
-
...references.filter(
|
|
777
|
+
...references.filter(isRecoverableAssetKey)
|
|
745
778
|
])
|
|
746
779
|
];
|
|
747
780
|
if (references.length > 0) {
|
|
@@ -782,7 +815,7 @@ function createAssetManager(options) {
|
|
|
782
815
|
keys: [
|
|
783
816
|
.../* @__PURE__ */ new Set([
|
|
784
817
|
...remaining,
|
|
785
|
-
...current.uncertainKeys.filter(
|
|
818
|
+
...current.uncertainKeys.filter(isRecoverableAssetKey)
|
|
786
819
|
])
|
|
787
820
|
]
|
|
788
821
|
});
|
|
@@ -824,7 +857,14 @@ function createAssetManager(options) {
|
|
|
824
857
|
const manifestStart = Date.now();
|
|
825
858
|
const manifest = await getVersionManifest(storage);
|
|
826
859
|
logger.debug(`updateVersionsManifest: loaded manifest in ${formatDuration(Date.now() - manifestStart)} (${Object.keys(manifest.versions).length} versions)`);
|
|
827
|
-
const
|
|
860
|
+
const existingVersion = manifest.versions[buildId];
|
|
861
|
+
const isExistingVersion = !!existingVersion;
|
|
862
|
+
const previousStorageKeys = existingVersion ? Object.fromEntries(existingVersion.assets.map((asset) => [
|
|
863
|
+
asset,
|
|
864
|
+
assetStorageKey(buildId, existingVersion, asset)
|
|
865
|
+
])) : void 0;
|
|
866
|
+
if (previousStorageKeys)
|
|
867
|
+
await recordPendingUploads(Object.values(previousStorageKeys));
|
|
828
868
|
const previousVersionId = manifest.current && manifest.current !== buildId ? manifest.current : getPreviousVersion(manifest, buildId);
|
|
829
869
|
const previousVersion = previousVersionId ? manifest.versions[previousVersionId] : void 0;
|
|
830
870
|
const previousAssets = previousVersion?.originalAssets || previousVersion?.assets || [];
|
|
@@ -833,6 +873,7 @@ function createAssetManager(options) {
|
|
|
833
873
|
timestamp: now.toISOString(),
|
|
834
874
|
expires: expires.toISOString(),
|
|
835
875
|
assets: persistAssets ? [...assets] : [],
|
|
876
|
+
...previousStorageKeys ? { storageKeys: previousStorageKeys } : {},
|
|
836
877
|
// Store original assets for deletedChunks calculation (assets array gets modified by deduplication)
|
|
837
878
|
originalAssets: [...assets],
|
|
838
879
|
deletedChunks: calculateDeletedChunks(assets, previousAssets)
|
|
@@ -932,18 +973,23 @@ function createAssetManager(options) {
|
|
|
932
973
|
let deduplicatedCount = 0;
|
|
933
974
|
let totalBytes = 0;
|
|
934
975
|
let skippedCount = 0;
|
|
935
|
-
const supersededKeys = [];
|
|
936
976
|
const version = manifest.versions[buildId];
|
|
937
|
-
version.storageKeys
|
|
977
|
+
const supersededKeys = Object.values(version.storageKeys || {});
|
|
978
|
+
await recordPendingUploads(supersededKeys);
|
|
938
979
|
const uploads = /* @__PURE__ */ new Map();
|
|
939
980
|
const uncertainObjectKeys = new Set((await getPendingUploads()).uncertainKeys);
|
|
981
|
+
const hasUncertainOperation = createOperationReader();
|
|
982
|
+
const retainedKeys = Object.values(manifest.versions).flatMap((data) => Object.values(data.storageKeys || {})).filter((key) => RE_BLOB_KEY.test(key) && !uncertainObjectKeys.has(key));
|
|
983
|
+
const checkedKeys = await processBatch([
|
|
984
|
+
...new Set(retainedKeys)
|
|
985
|
+
], 50, async (key) => await hasUncertainOperation(key) ? void 0 : key);
|
|
986
|
+
const safeKeys = new Set(checkedKeys.filter((key) => key !== void 0));
|
|
940
987
|
const reusableKeys = /* @__PURE__ */ new Map();
|
|
941
|
-
for (const
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
reusableKeys.set(key.slice("blobs/".length, "blobs/".length + 64), key);
|
|
945
|
-
}
|
|
988
|
+
for (const key of retainedKeys) {
|
|
989
|
+
if (safeKeys.has(key))
|
|
990
|
+
reusableKeys.set(key.slice("blobs/".length, "blobs/".length + 64), key);
|
|
946
991
|
}
|
|
992
|
+
version.storageKeys = {};
|
|
947
993
|
const storeStart = Date.now();
|
|
948
994
|
for (let index = 0; index < assets.length; index += 25) {
|
|
949
995
|
const batch = await processBatch(assets.slice(index, index + 25), 25, async (asset) => {
|
|
@@ -1174,7 +1220,11 @@ function createAssetManager(options) {
|
|
|
1174
1220
|
await restoreOldAssetsToPublic(buildId, publicDir, storedAssets);
|
|
1175
1221
|
await storeAssetsInStorage(buildId, publicDir, storedAssets, manifest);
|
|
1176
1222
|
} else {
|
|
1177
|
-
const removedKeys =
|
|
1223
|
+
const removedKeys = [
|
|
1224
|
+
...Object.values(manifest.versions[buildId].storageKeys || {}),
|
|
1225
|
+
...limitVersionCount(manifest, buildId)
|
|
1226
|
+
];
|
|
1227
|
+
delete manifest.versions[buildId].storageKeys;
|
|
1178
1228
|
await recordPendingUploads(removedKeys);
|
|
1179
1229
|
await commitVersionManifest(manifest);
|
|
1180
1230
|
await finishAssetCleanup(manifest, removedKeys);
|