generated from interop-alliance/isomorphic-lib-template
-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add validation for exported ActivityPub tarballs #7
Draft
0marSalah
wants to merge
8
commits into
main
Choose a base branch
from
22-implement-validateexportstream-function
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+200
−28
Draft
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e91222a
Add validation for exported ActivityPub tarballs
0marSalah ac6fb51
Add validateExportStream to ESM build script
0marSalah 6b5266f
Export verify module from index.ts
0marSalah e7ef58f
Refactor importActorProfile to use ReadableStream and improve error h…
0marSalah c869227
Refactor validateExportStream to accept ReadableStream and improve fi…
0marSalah 6afa20d
Add ReadableStream support for importActorProfile and validateExportS…
0marSalah 7bfa145
Remove eslint-plugin-n dependency from package.json
0marSalah 3a13b9e
Remove debug logging from validateExportStream function
0marSalah File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import * as tar from 'tar-stream' | ||
import { type Readable } from 'stream' | ||
import YAML from 'yaml' | ||
|
||
/** | ||
* Validates the structure and content of an exported ActivityPub tarball. | ||
* @param tarStream - A ReadableStream containing the .tar archive. | ||
* @returns A promise that resolves to an object with `valid` (boolean) and `errors` (string[]). | ||
*/ | ||
export async function validateExportStream( | ||
tarStream: Readable | ||
): Promise<{ valid: boolean; errors: string[] }> { | ||
console.log('Validating export stream...') | ||
const extract = tar.extract() | ||
const errors: string[] = [] | ||
const requiredFiles = [ | ||
'manifest.yaml', // or 'manifest.yml' | ||
'activitypub/actor.json', | ||
'activitypub/outbox.json' | ||
].map((file) => file.toLowerCase()) // Normalize to lowercase for consistent comparison | ||
const foundFiles = new Set<string>() | ||
|
||
return await new Promise((resolve) => { | ||
extract.on('entry', (header, stream, next) => { | ||
const fileName = header.name.toLowerCase() // Normalize file name | ||
foundFiles.add(fileName) | ||
|
||
let content = '' | ||
stream.on('data', (chunk) => { | ||
content += chunk.toString() | ||
}) | ||
|
||
stream.on('end', () => { | ||
try { | ||
// Validate JSON files | ||
if (fileName.endsWith('.json')) { | ||
JSON.parse(content) // Throws an error if content is not valid JSON | ||
} | ||
|
||
// Validate manifest file | ||
if (fileName === 'manifest.yaml' || fileName === 'manifest.yml') { | ||
const manifest = YAML.parse(content) | ||
if (!manifest['ubc-version']) { | ||
errors.push('Manifest is missing required field: ubc-version') | ||
} | ||
if (!manifest.contents?.activitypub) { | ||
errors.push( | ||
'Manifest is missing required field: contents.activitypub' | ||
) | ||
} | ||
} | ||
} catch (error: any) { | ||
errors.push(`Error processing file ${fileName}: ${error.message}`) | ||
} | ||
next() | ||
}) | ||
|
||
stream.on('error', (error) => { | ||
errors.push(`Stream error on file ${fileName}: ${error.message}`) | ||
next() | ||
}) | ||
}) | ||
|
||
extract.on('finish', () => { | ||
// Check if all required files are present | ||
for (const file of requiredFiles) { | ||
if (!foundFiles.has(file)) { | ||
errors.push(`Missing required file: ${file}`) | ||
} | ||
} | ||
|
||
resolve({ | ||
valid: errors.length === 0, | ||
errors | ||
}) | ||
}) | ||
|
||
extract.on('error', (error) => { | ||
errors.push(`Error during extraction: ${error.message}`) | ||
resolve({ | ||
valid: false, | ||
errors | ||
}) | ||
}) | ||
|
||
// Pipe the ReadableStream into the extractor | ||
tarStream.pipe(extract) | ||
}) | ||
} |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { expect } from 'chai' | ||
import { readFileSync } from 'fs' | ||
import { validateExportStream } from '../dist' | ||
import { Readable } from 'stream' | ||
|
||
describe('validateExportStream', () => { | ||
it('should validate a valid tarball', async () => { | ||
// Load a valid tarball (e.g., exported-profile-valid.tar) | ||
const tarBuffer = readFileSync( | ||
'test/fixtures/tarball-samples/valid-export.tar' | ||
) | ||
const tarStream = Readable.from(tarBuffer) | ||
const result = await validateExportStream(tarStream) | ||
console.log('🚀 ~ it ~ valid result:', result) | ||
|
||
expect(result.valid).to.be.true | ||
expect(result.errors).to.be.an('array').that.is.empty | ||
}) | ||
|
||
it('should fail if manifest.yaml is missing', async () => { | ||
// Load a tarball with missing manifest.yaml | ||
const tarBuffer = readFileSync( | ||
'test/fixtures/tarball-samples/missing-manifest.tar' | ||
) | ||
const tarStream = Readable.from(tarBuffer) | ||
const result = await validateExportStream(tarStream) | ||
console.log('🚀 ~ it ~ miss mani result:', result) | ||
|
||
expect(result.valid).to.be.false | ||
}) | ||
|
||
it('should fail if actor.json is missing', async () => { | ||
// Load a tarball with missing actor.json | ||
const tarBuffer = readFileSync( | ||
'test/fixtures/tarball-samples/missing-actor.tar' | ||
) | ||
const tarStream = Readable.from(tarBuffer) | ||
const result = await validateExportStream(tarStream) | ||
|
||
expect(result.valid).to.be.false | ||
console.log(JSON.stringify(result.errors)) | ||
}) | ||
|
||
it('should fail if outbox.json is missing', async () => { | ||
// Load a tarball with missing outbox.json | ||
const tarBuffer = readFileSync( | ||
'test/fixtures/tarball-samples/missing-outbox.tar' | ||
) | ||
const tarStream = Readable.from(tarBuffer) | ||
const result = await validateExportStream(tarStream) | ||
|
||
expect(result.valid).to.be.false | ||
}) | ||
|
||
it('should fail if actor.json contains invalid JSON', async () => { | ||
// Load a tarball with invalid JSON in actor.json | ||
const tarBuffer = readFileSync( | ||
'test/fixtures/tarball-samples/invalid-actor.tar' | ||
) | ||
const tarStream = Readable.from(tarBuffer) | ||
const result = await validateExportStream(tarStream) | ||
|
||
expect(result.valid).to.be.false | ||
}) | ||
|
||
it('should fail if manifest.yaml is invalid', async () => { | ||
// Load a tarball with invalid manifest.yaml | ||
const tarBuffer = readFileSync( | ||
'test/fixtures/tarball-samples/invalid-manifest.tar' | ||
) | ||
const tarStream = Readable.from(tarBuffer) | ||
const result = await validateExportStream(tarStream) | ||
|
||
expect(result.valid).to.be.false | ||
}) | ||
}) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will you please make it so
tarBuffer
can be a ReadableStream? That way, if the export is really big and the tar is really big, it doesn't have to be buffered in memory all at once.I think you should be able have tar-stream parse the stream, async iterate through the tar entries, and ensure each entry is valid, all without every buffering all the entries in memory
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you r abs right, i should consider that, thanks