@tangen/porter 0.1.4 → 0.1.6
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/index.d.ts +1 -0
- package/dist/index.js +31 -2
- package/dist/safe-key.d.ts +38 -0
- package/package.json +2 -2
- package/src/bundle.ts +5 -0
- package/src/index.ts +1 -0
- package/src/safe-key.ts +57 -0
- package/src/tar.ts +6 -1
package/dist/index.d.ts
CHANGED
|
@@ -9,3 +9,4 @@
|
|
|
9
9
|
*/
|
|
10
10
|
export { createBundleWriter, openBundleReader, assertSupportedFormat, BUNDLE_FORMAT_VERSION, type BundleManifest, type BundleWriter, type BundleReader, type BundleSectionWriter, } from './bundle';
|
|
11
11
|
export { bundleKeys, tarGzBundle, untarGz, importBundleFromTarGz, type TarEntry } from './tar';
|
|
12
|
+
export { isSafeBundleFilename, isSafeBundleId, assertSafeMediaRef } from './safe-key';
|
package/dist/index.js
CHANGED
|
@@ -1,3 +1,25 @@
|
|
|
1
|
+
// src/safe-key.ts
|
|
2
|
+
function isSafeBundleFilename(name) {
|
|
3
|
+
if (typeof name !== "string" || name === "")
|
|
4
|
+
return false;
|
|
5
|
+
if (name === "." || name === "..")
|
|
6
|
+
return false;
|
|
7
|
+
if (name.includes("/") || name.includes("\\") || name.includes("\x00"))
|
|
8
|
+
return false;
|
|
9
|
+
return true;
|
|
10
|
+
}
|
|
11
|
+
function isSafeBundleId(id) {
|
|
12
|
+
return typeof id === "string" && /^[A-Za-z0-9_.-]{1,128}$/.test(id) && !id.includes("..");
|
|
13
|
+
}
|
|
14
|
+
function assertSafeMediaRef(assetId, filename) {
|
|
15
|
+
if (!isSafeBundleId(assetId)) {
|
|
16
|
+
throw new Error(`bundle media entry has an unusable asset id: ${JSON.stringify(assetId)}`);
|
|
17
|
+
}
|
|
18
|
+
if (!isSafeBundleFilename(filename)) {
|
|
19
|
+
throw new Error(`bundle media entry "${String(assetId)}" names a file that is not a plain filename: ${JSON.stringify(filename)}`);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
1
23
|
// src/bundle.ts
|
|
2
24
|
var BUNDLE_FORMAT_VERSION = "1.0.0";
|
|
3
25
|
var prefix = (bundleId) => `porter/bundles/${bundleId}`;
|
|
@@ -43,6 +65,7 @@ function createBundleWriter(storage, bundleId, opts = {}) {
|
|
|
43
65
|
return {
|
|
44
66
|
section,
|
|
45
67
|
async writeMediaFile(assetId, filename, bytes, contentType) {
|
|
68
|
+
assertSafeMediaRef(assetId, filename);
|
|
46
69
|
await storage.write(mediaFileKey(bundleId, assetId, filename), bytes, { contentType });
|
|
47
70
|
},
|
|
48
71
|
async seal(manifest) {
|
|
@@ -95,6 +118,7 @@ async function openBundleReader(storage, bundleId) {
|
|
|
95
118
|
}
|
|
96
119
|
},
|
|
97
120
|
readMediaFile(assetId, filename) {
|
|
121
|
+
assertSafeMediaRef(assetId, filename);
|
|
98
122
|
return storage.read(mediaFileKey(bundleId, assetId, filename));
|
|
99
123
|
}
|
|
100
124
|
};
|
|
@@ -112,8 +136,10 @@ async function bundleKeys(storage, bundleId) {
|
|
|
112
136
|
for await (const entry of reader.readSection("media/index")) {
|
|
113
137
|
const asset = entry.asset;
|
|
114
138
|
const filename = entry.filename;
|
|
115
|
-
if (asset?.id && filename)
|
|
116
|
-
|
|
139
|
+
if (!asset?.id && !filename)
|
|
140
|
+
continue;
|
|
141
|
+
assertSafeMediaRef(asset?.id, filename);
|
|
142
|
+
keys.push(`media/files/${asset.id}/${filename}`);
|
|
117
143
|
}
|
|
118
144
|
return keys;
|
|
119
145
|
}
|
|
@@ -217,9 +243,12 @@ export {
|
|
|
217
243
|
untarGz,
|
|
218
244
|
tarGzBundle,
|
|
219
245
|
openBundleReader,
|
|
246
|
+
isSafeBundleId,
|
|
247
|
+
isSafeBundleFilename,
|
|
220
248
|
importBundleFromTarGz,
|
|
221
249
|
createBundleWriter,
|
|
222
250
|
bundleKeys,
|
|
223
251
|
assertSupportedFormat,
|
|
252
|
+
assertSafeMediaRef,
|
|
224
253
|
BUNDLE_FORMAT_VERSION
|
|
225
254
|
};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* File Name: safe-key.ts
|
|
3
|
+
* Purpose: A bundle names its own media files, and a bundle is UNTRUSTED
|
|
4
|
+
* input. These are the two guards that stop one of those names
|
|
5
|
+
* becoming a storage path somewhere it was never meant to reach.
|
|
6
|
+
*
|
|
7
|
+
* Author: Raghunath J
|
|
8
|
+
* License: MIT
|
|
9
|
+
*
|
|
10
|
+
* Dependencies/Notes: Found in the second audit pass over Porter's import
|
|
11
|
+
* paths, 2026-10-23, which the 2026-10-12 audit named as one of the
|
|
12
|
+
* three areas it had given least attention.
|
|
13
|
+
*
|
|
14
|
+
* The chain, traced through source: `media/index` in a bundle carries
|
|
15
|
+
* a `filename` and an `asset.id`; `porter-import.ts` built
|
|
16
|
+
* `${workspaceId}/${targetId}/${filename}` from it and wrote there.
|
|
17
|
+
* The local storage adapter's own `safePath` refuses to escape the
|
|
18
|
+
* media ROOT, which is why this is not a read of `/etc/passwd` - but
|
|
19
|
+
* it does not refuse to escape the WORKSPACE prefix, and a workspace
|
|
20
|
+
* is this platform's tenancy boundary. A `filename` of
|
|
21
|
+
* `../../<other-workspace>/<asset>/logo.png` therefore put
|
|
22
|
+
* bundle-chosen bytes into another workspace's media.
|
|
23
|
+
*
|
|
24
|
+
* Refused, never rewritten. Quietly reducing a traversing name to its
|
|
25
|
+
* basename would import the file under a name nobody asked for and
|
|
26
|
+
* leave the bundle looking valid; the same reasoning as the content
|
|
27
|
+
* validator, which refuses a malformed document and names the node
|
|
28
|
+
* rather than reshaping it.
|
|
29
|
+
*/
|
|
30
|
+
/** A leaf filename: no separator, no traversal, no NUL, not empty. */
|
|
31
|
+
export declare function isSafeBundleFilename(name: unknown): name is string;
|
|
32
|
+
/** An id that is safe to put in a storage path: the shape every Tangen id has. */
|
|
33
|
+
export declare function isSafeBundleId(id: unknown): id is string;
|
|
34
|
+
/**
|
|
35
|
+
* The guard both the read side and the write side call, so a name can never be
|
|
36
|
+
* checked on one path and trusted on the other.
|
|
37
|
+
*/
|
|
38
|
+
export declare function assertSafeMediaRef(assetId: unknown, filename: unknown): asserts filename is string;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tangen/porter",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.6",
|
|
4
4
|
"description": "Porter - data movement for Tangen CMS: promote, export/import bundles, remote transfer. A porter carries your goods safely between places.",
|
|
5
5
|
"homepage": "https://github.com/raghunathj/tangen#readme",
|
|
6
6
|
"bugs": {
|
|
@@ -40,7 +40,7 @@
|
|
|
40
40
|
"test": "bun test"
|
|
41
41
|
},
|
|
42
42
|
"dependencies": {
|
|
43
|
-
"@tangen/types": "^0.
|
|
43
|
+
"@tangen/types": "^0.5.0"
|
|
44
44
|
},
|
|
45
45
|
"exports": {
|
|
46
46
|
".": {
|
package/src/bundle.ts
CHANGED
|
@@ -19,6 +19,7 @@
|
|
|
19
19
|
* media/files/<assetId>/<file> raw bytes
|
|
20
20
|
*/
|
|
21
21
|
import type { StorageAdapter } from '@tangen/types'
|
|
22
|
+
import { assertSafeMediaRef } from './safe-key'
|
|
22
23
|
|
|
23
24
|
export const BUNDLE_FORMAT_VERSION = '1.0.0'
|
|
24
25
|
|
|
@@ -106,6 +107,7 @@ export function createBundleWriter(
|
|
|
106
107
|
return {
|
|
107
108
|
section,
|
|
108
109
|
async writeMediaFile(assetId, filename, bytes, contentType) {
|
|
110
|
+
assertSafeMediaRef(assetId, filename)
|
|
109
111
|
await storage.write(mediaFileKey(bundleId, assetId, filename), bytes, { contentType })
|
|
110
112
|
},
|
|
111
113
|
async seal(manifest) {
|
|
@@ -172,6 +174,9 @@ export async function openBundleReader(storage: StorageAdapter, bundleId: string
|
|
|
172
174
|
}
|
|
173
175
|
},
|
|
174
176
|
readMediaFile(assetId, filename) {
|
|
177
|
+
// The read side too: a bundle names this, and the write side refusing a
|
|
178
|
+
// traversal is no use if the read side builds the same path anyway.
|
|
179
|
+
assertSafeMediaRef(assetId, filename)
|
|
175
180
|
return storage.read(mediaFileKey(bundleId, assetId, filename))
|
|
176
181
|
},
|
|
177
182
|
}
|
package/src/index.ts
CHANGED
|
@@ -12,3 +12,4 @@ export {
|
|
|
12
12
|
type BundleManifest, type BundleWriter, type BundleReader, type BundleSectionWriter,
|
|
13
13
|
} from './bundle'
|
|
14
14
|
export { bundleKeys, tarGzBundle, untarGz, importBundleFromTarGz, type TarEntry } from './tar'
|
|
15
|
+
export { isSafeBundleFilename, isSafeBundleId, assertSafeMediaRef } from './safe-key'
|
package/src/safe-key.ts
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* File Name: safe-key.ts
|
|
3
|
+
* Purpose: A bundle names its own media files, and a bundle is UNTRUSTED
|
|
4
|
+
* input. These are the two guards that stop one of those names
|
|
5
|
+
* becoming a storage path somewhere it was never meant to reach.
|
|
6
|
+
*
|
|
7
|
+
* Author: Raghunath J
|
|
8
|
+
* License: MIT
|
|
9
|
+
*
|
|
10
|
+
* Dependencies/Notes: Found in the second audit pass over Porter's import
|
|
11
|
+
* paths, 2026-10-23, which the 2026-10-12 audit named as one of the
|
|
12
|
+
* three areas it had given least attention.
|
|
13
|
+
*
|
|
14
|
+
* The chain, traced through source: `media/index` in a bundle carries
|
|
15
|
+
* a `filename` and an `asset.id`; `porter-import.ts` built
|
|
16
|
+
* `${workspaceId}/${targetId}/${filename}` from it and wrote there.
|
|
17
|
+
* The local storage adapter's own `safePath` refuses to escape the
|
|
18
|
+
* media ROOT, which is why this is not a read of `/etc/passwd` - but
|
|
19
|
+
* it does not refuse to escape the WORKSPACE prefix, and a workspace
|
|
20
|
+
* is this platform's tenancy boundary. A `filename` of
|
|
21
|
+
* `../../<other-workspace>/<asset>/logo.png` therefore put
|
|
22
|
+
* bundle-chosen bytes into another workspace's media.
|
|
23
|
+
*
|
|
24
|
+
* Refused, never rewritten. Quietly reducing a traversing name to its
|
|
25
|
+
* basename would import the file under a name nobody asked for and
|
|
26
|
+
* leave the bundle looking valid; the same reasoning as the content
|
|
27
|
+
* validator, which refuses a malformed document and names the node
|
|
28
|
+
* rather than reshaping it.
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
/** A leaf filename: no separator, no traversal, no NUL, not empty. */
|
|
32
|
+
export function isSafeBundleFilename(name: unknown): name is string {
|
|
33
|
+
if (typeof name !== 'string' || name === '') return false
|
|
34
|
+
if (name === '.' || name === '..') return false
|
|
35
|
+
if (name.includes('/') || name.includes('\\') || name.includes('\0')) return false
|
|
36
|
+
return true
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** An id that is safe to put in a storage path: the shape every Tangen id has. */
|
|
40
|
+
export function isSafeBundleId(id: unknown): id is string {
|
|
41
|
+
return typeof id === 'string' && /^[A-Za-z0-9_.-]{1,128}$/.test(id) && !id.includes('..')
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* The guard both the read side and the write side call, so a name can never be
|
|
46
|
+
* checked on one path and trusted on the other.
|
|
47
|
+
*/
|
|
48
|
+
export function assertSafeMediaRef(assetId: unknown, filename: unknown): asserts filename is string {
|
|
49
|
+
if (!isSafeBundleId(assetId)) {
|
|
50
|
+
throw new Error(`bundle media entry has an unusable asset id: ${JSON.stringify(assetId)}`)
|
|
51
|
+
}
|
|
52
|
+
if (!isSafeBundleFilename(filename)) {
|
|
53
|
+
throw new Error(
|
|
54
|
+
`bundle media entry "${String(assetId)}" names a file that is not a plain filename: ${JSON.stringify(filename)}`,
|
|
55
|
+
)
|
|
56
|
+
}
|
|
57
|
+
}
|
package/src/tar.ts
CHANGED
|
@@ -17,6 +17,7 @@
|
|
|
17
17
|
import { gzipSync, gunzipSync } from 'node:zlib'
|
|
18
18
|
import type { StorageAdapter } from '@tangen/types'
|
|
19
19
|
import { openBundleReader } from './bundle'
|
|
20
|
+
import { assertSafeMediaRef } from './safe-key'
|
|
20
21
|
|
|
21
22
|
const prefix = (bundleId: string) => `porter/bundles/${bundleId}`
|
|
22
23
|
|
|
@@ -33,7 +34,11 @@ export async function bundleKeys(storage: StorageAdapter, bundleId: string): Pro
|
|
|
33
34
|
for await (const entry of reader.readSection('media/index')) {
|
|
34
35
|
const asset = (entry as any).asset
|
|
35
36
|
const filename = (entry as any).filename
|
|
36
|
-
if (asset?.id && filename)
|
|
37
|
+
if (!asset?.id && !filename) continue
|
|
38
|
+
// Packaging reads each of these keys, so a traversing name here would read
|
|
39
|
+
// another bundle's bytes into the archive. Same guard as the writer.
|
|
40
|
+
assertSafeMediaRef(asset?.id, filename)
|
|
41
|
+
keys.push(`media/files/${asset.id}/${filename}`)
|
|
37
42
|
}
|
|
38
43
|
return keys
|
|
39
44
|
}
|