Skip to content

Add client-side import/export for saved recipes - #36

Merged
m4sh-wacker merged 10 commits into
OWASP:mainfrom
mannubaveja007:fix/issue-35-recipe-import-export
Nov 25, 2026
Merged

m4sh-wacker merged 10 commits into
OWASP:mainfrom
mannubaveja007:fix/issue-35-recipe-import-export

Conversation

@mannubaveja007

Copy link
Copy Markdown
Contributor

Fixes #35

Summary

Adds export/import for saved recipes, entirely client-side.

Implementation

  • exportRecipes() serializes saved recipes to JSON.
  • importRecipes() validates entries, skips duplicates and invalid data, respects the 100-recipe limit, and merges without overwriting.
  • Recipe library UI adds Export/Import buttons and a hidden file input, with feedback messages.

Tests

  • Added src/lib/recipes.test.ts covering round-trip import/export, invalid JSON, and duplicate-id handling.
  • Typecheck, lint, and tests pass.

@m4sh-wacker

m4sh-wacker commented Nov 23, 2026 •

Copy link
Copy Markdown
Collaborator

Thanks for the contribution, @mannubaveja007! Great work getting this implemented so quickly. The overall approach looks good, and all CI checks are passing.

I reviewed the changes and found a few edge cases I'd like you to address before merging:

  1. Validate the export format version and reject unsupported versions.
  2. Handle localStorage persistence failures so the UI never reports a successful import when the recipes weren't actually saved.
  3. Add reasonable file-size and import limits, and handle file-reading errors gracefully.

Please also add regression tests covering these cases.

Thanks again for the work! Looking forward to the updated PR.

@mannubaveja007
mannubaveja007 force-pushed the fix/issue-35-recipe-import-export branch from 67d990e to 64a6947 Compare November 23, 2026 21:07
@mannubaveja007

Copy link
Copy Markdown
Contributor Author

Updated per review:

  • Version validation: unsupported/missing versions are rejected.
  • Persist failures now return an error instead of reporting success.
  • Added file-size (1 MB) and import (1000 entries) limits, plus read-error handling.
  • Added regression tests for these cases.

All checks/tests pass.

@m4sh-wacker

Copy link
Copy Markdown
Collaborator

Thanks for the updates, @mannubaveja007! Great work addressing the previous feedback. CI and CodeQL are passing.

I found a couple of remaining edge cases before we merge:

  1. readArg() currently trusts imported argument metadata. Properties like options and toggleValues are not validated, which could cause runtime errors when loading a crafted recipe. Please validate arguments against the actual operation definitions and only restore valid values.

  2. Version validation still accepts payloads without a version, as well as raw arrays. Please make sure this behavior is intentional and consistent with the versioning policy.

Please add regression tests for these cases as well.

Once these are addressed, I'll review the updated changes again.

Thanks for your continued work on DecodeBox!

@mannubaveja007

Copy link
Copy Markdown
Contributor Author

Updated:

  • Imported args are now validated against operation definitions; only valid values are restored.
  • Export version is now required (version 1); unversioned/raw-array payloads are rejected.
  • Tests pass.

Ready for re-review.

@mannubaveja007

Copy link
Copy Markdown
Contributor Author

Fixed the failing test: malformed steps still drop the whole recipe, while valid imported args are sanitized against operation definitions. Pushed.

@m4sh-wacker

Copy link
Copy Markdown
Collaborator

Thanks for the latest update! I reviewed commit 3f9a48c and confirmed that the version validation now correctly rejects missing versions, unsupported versions, and raw-array payloads. The additional malformed-argument check is also a good improvement.

However, I found a few remaining issues:

1. toggleString values are still restored incorrectly

In readStep(), the imported value is compared against def.toggleValues:

if (def.type === 'toggleString' && def.toggleValues &&
    !def.toggleValues.includes(String(value))) {
  value = def.value;
}

toggleValues defines the available format selections, while value contains the actual user-provided data.

As a result, custom values such as AES keys, IVs, and salts can be replaced with their defaults during import. The imported toggleValue is also never restored.

Please validate and restore toggleValue separately from value, preserving valid custom values.

2. Numeric arguments still lack operation-defined range validation

The current implementation checks whether a numeric value is a number, but does not enforce the operation's min and max constraints.

For example, Argon2 defines memory and iteration limits, but a crafted recipe can bypass these UI-level constraints and supply excessive values.

Please enforce finite-number validation and the actual operation-defined boundaries before restoring imported values. This is particularly important for memory- and CPU-intensive operations such as Argon2 and scrypt.

3. Potential regression in lazy loading

The static import of getOperation from ../engine/operations in recipes.ts still pulls the operation registry into the recipe library's dependency graph.

Since the engine is intentionally lazy-loaded, please verify the production bundle output and consider a lightweight validation registry that preserves the existing loading architecture.

4. Additional regression tests

Please add tests covering:

  • Round-trip import/export with real operation steps and custom arguments.
  • Preservation of toggleString values and format selections.
  • Rejection or sanitization of numeric values outside operation-defined boundaries.
  • Missing-version and raw-array payload rejection.
  • Unknown operation IDs and malformed argument definitions.

The latest changes are moving in the right direction, and the versioning issue is now resolved. I'd like to see these remaining edge cases covered before approving the PR.

Thanks again for your continued work!

@m4sh-wacker

Copy link
Copy Markdown
Collaborator

Thanks for the update! The lazy-loading improvement and numeric validation changes look good. I reviewed the latest commit, and there are still two important issues I'd like to address before approving this PR.

1. toggleString value validation is still incorrect

File: src/lib/recipes.ts (around line 84)

The current validation still compares the actual argument value against toggleValues:

if (def.type === 'toggleString' && def.toggleValues &&
    !def.toggleValues.includes(String(value))) value = def.value;

This is incorrect because value contains the user's actual input, while toggleValues defines the available format selections.

For example:

  • value: "my-secret-key"
  • toggleValue: "Base64"

The current logic may replace a perfectly valid user-provided key with the default value simply because it isn't one of the toggle options.

Expected behavior:

  • Preserve the actual value regardless of its contents.
  • Validate toggleValue separately against def.toggleValues.
  • Only reset toggleValue to its default when the selected format is invalid.

Please also update the tests to explicitly verify that both the actual value and the selected toggle format survive a round-trip import.

2. Operation catalogue validation is bypassed in the actual import flow

File: src/store/useStore.ts (around line 725)

Currently, the store calls:

const result = importRecipesLib(raw);

However, importRecipes() accepts an optional operations argument, and defaults it to an empty array.

As a result, the validation in readStep():

if (operations.length > 0 && !op) return null;

does not reject unknown operation IDs when the actual application import flow is used.

The tests pass an operation catalogue explicitly, but the production path does not.

Expected behavior:

  • Pass the loaded operation catalogue to importRecipesLib().
  • Ensure imported recipes are validated against actual operation definitions.
  • Ensure persisted recipes loaded from localStorage are also validated once the operation catalogue is available.
  • Do not silently accept unknown operations simply because the catalogue hasn't been provided.

3. Additional regression tests

Please add tests covering:

  1. Preserving arbitrary toggleString values such as "my-secret-key".
  2. Preserving valid toggle selections such as Base64.
  3. Resetting invalid toggle selections without modifying the actual user value.
  4. Rejecting unknown operation IDs through the actual store import flow.
  5. Ensuring imported argument values are normalized against the corresponding operation definitions.

The current CI and CodeQL results are encouraging, and I appreciate the improvements in this commit.

Once these two main issues are addressed and the regression tests are added, I'll be happy to review the updated commit again.

Thanks!

@m4sh-wacker

Copy link
Copy Markdown
Collaborator

Thanks for the latest update, Mannu!

I reviewed the latest commit (1cc1f10) and can confirm that the previous issues have been addressed. The toggleString handling, operation-aware validation, and lazy-loading approach are all moving in the right direction.

I also checked the latest CI and CodeQL results, and everything is passing. Great work!

I found three remaining edge cases that I'd like you to address before we can approve and merge this PR.

1. Fail-open behavior when the operation catalogue is unavailable

File: src/lib/recipes.ts

Currently:

if (operations.length > 0 && !op) return null;

When operations is an empty array, unknown operation IDs are accepted.

In the actual application flow, operations are loaded asynchronously. If a user imports recipes before the catalogue is available, validation can be bypassed.

Expected behavior:

  • Do not accept imported recipes when the operation catalogue is unavailable.
  • Ensure importing is performed only after operations have successfully loaded.
  • Reject unknown operation IDs rather than silently accepting them.

2. Argument type validation

File: src/lib/recipes.ts

safeArgValue() currently accepts strings, numbers, and booleans regardless of the expected argument type.

For example, an argument defined as string or textarea could still receive a Boolean or Number from a crafted import file.

Expected behavior:

  • Validate each imported argument against its actual OperationArg.type.
  • Ensure string, textarea, and toggleString only accept strings.
  • Ensure number only accepts finite numbers.
  • Ensure boolean only accepts booleans.
  • Fall back to the operation's default value when the imported type is invalid.

3. Persist normalized recipes correctly

File: src/lib/recipes.ts

In normalizeRecipes():

if (valid.length !== recipes.length) persist(valid);

This only persists changes when the number of recipes changes.

However, a recipe can remain valid while some of its arguments are normalized or corrected. In that case, the updated version is returned to the store but is not persisted to localStorage.

Expected behavior:

  • Persist normalized recipes whenever their actual content changes, not just when the recipe count changes.
  • Ensure the normalized data in the store and localStorage remains consistent.

4. Additional regression tests

Please add tests covering:

  1. Import attempts before the operation catalogue is loaded.
  2. Unknown operation IDs when the catalogue is unavailable.
  3. Incorrect primitive types for string, textarea, toggleString, number, and boolean arguments.
  4. Persistence of normalized arguments when the recipe count remains unchanged.
  5. A complete import/export round-trip with real operations and custom argument values.

I appreciate your effort and the improvements you've made so far.

Please address these remaining cases and push an updated commit. Once they're covered and the tests pass, I'll review the PR again.

Thanks for your patience and continued contributions!

@mannubaveja007

Copy link
Copy Markdown
Contributor Author

Addressed the latest review: imports now reject when the operation catalogue is unavailable, args are validated per type, normalized recipes persist on content changes, and tests cover these cases.

@mannubaveja007

Copy link
Copy Markdown
Contributor Author

Fixed both gaps: normalizeRecipes now fails closed on an empty catalogue (and loadOperations skips normalization), and normalization uses the persist result so Zustand stays in sync with localStorage, including the 100-recipe trim. Added tests.

@m4sh-wacker

Copy link
Copy Markdown
Collaborator

Thanks for the latest updates, Mannu! I reviewed the new commits and the normalization changes look good.

I noticed one remaining startup edge case: loadSavedRecipe() can still restore a saved recipe before the operation catalogue has finished loading and validation has completed.

Please add a guard inside loadSavedRecipe() and ensure the saved recipe entries cannot be loaded until normalization is complete. Also, please add a regression test covering this scenario.

Once this is addressed, I'm happy to approve and merge the PR.

Thanks again for your patience and excellent work!

@m4sh-wacker

Copy link
Copy Markdown
Collaborator

Thanks a lot, Mannu, for your patience and dedication throughout this PR. I really appreciate your hard work, responsiveness, and willingness to address all the feedback. Great contribution to OWASP DecodeBox — looking forward to more collaborations!

@m4sh-wacker
m4sh-wacker merged commit 05f0435 into OWASP:main Nov 25, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add Import/Export Support for Saved Recipes

2 participants