Skip to content

fix: use backend-provided AWS credentials for S3 upload - #14

Merged
neuromechanist merged 4 commits into
mainfrom
fix/backend-provided-aws-credentials
Jan 16, 2026
Merged

neuromechanist merged 4 commits into
mainfrom
fix/backend-provided-aws-credentials

Conversation

@neuromechanist

Copy link
Copy Markdown
Contributor

Summary

  • Users no longer need AWS credentials in environment variables
  • Backend now returns AWS credentials in the POST /datasets response
  • CLI extracts and uses credentials from API response for git-annex S3 operations

Changes

Backend (backend/src/routes/datasets.ts):

  • Added aws_credentials to create dataset response with accessKeyId, secretAccessKey, region, bucket

CLI:

  • src/lib/api.ts: Added aws_credentials to CreateDatasetResponse interface
  • src/lib/datalad.ts: Removed awsCredentials from PrerequisitesResult, removed check from checkPrerequisites()
  • src/commands/dataset.ts: Use credentials from API response instead of environment variables

Tests:

  • test/e2e-upload.test.ts: Removed AWS credentials from prerequisites check

Test plan

  • Build passes (bun run build)
  • Lint passes (bun run lint)
  • Tests pass (109 tests)
  • Backend deployed to production

Fixes #13

Users no longer need AWS credentials in environment variables.
The backend now returns AWS credentials in the create dataset response,
which the CLI uses for git-annex S3 operations.

Changes:
- Backend returns aws_credentials in POST /datasets response
- CLI extracts credentials from API response instead of env vars
- Removed checkAWSCredentials() from upload prerequisites
- Updated tests to reflect new credential flow

Fixes #13
@neuromechanist

Copy link
Copy Markdown
Contributor Author

PR Review Summary - Security Analysis

Critical Issues (Must Fix Before Merge)

1. Shared AWS Credentials Expose Global Access

Location: backend/src/routes/datasets.ts:141-148

The backend returns the same AWS credentials to ALL authenticated users. Any user could access/modify ANY dataset in the bucket.

CLAUDE.md Violation: States "API tokens tied to GitHub PAT and S3 credentials per user"

Fix Required: Implement per-user scoped credentials (see below)

2. No Backend Validation of Environment Variables

Location: backend/src/routes/datasets.ts:141-148

Backend returns c.env.AWS_* without checking if they exist. If env vars are undefined, users get cryptic errors later.

// Add validation before returning
if (!c.env.AWS_ACCESS_KEY_ID || !c.env.AWS_SECRET_ACCESS_KEY) {
  return c.json({ error: "Server configuration error" }, 500);
}

3. No CLI Validation of API Response

Location: src/commands/dataset.ts:382-387

CLI blindly trusts response.aws_credentials exists. Should validate before use.

if (!response.aws_credentials?.accessKeyId || !response.aws_credentials?.secretAccessKey) {
  spinner.fail("Backend did not return valid AWS credentials");
  process.exit(1);
}

Important Issues

Issue Location Description
Unsafe TypeScript cast api.ts:85 return data as T bypasses runtime validation
Non-actionable errors dataset.ts:442-446 S3 errors don't indicate credentials came from backend
Type duplication Multiple files Same credential structure in 3 places

Recommendation

Do not merge as-is. Need to:

  1. Implement per-user credential scoping (STS temporary credentials or per-user IAM)
  2. Add validation in both backend and CLI
  3. Improve error messages

Generated by pr-review-toolkit

Security enhancement: users no longer receive shared AWS credentials.
Instead, each user gets their own IAM user with credentials scoped
to their dataset prefixes via IAM policies.

Backend changes:
- Add user_s3_permissions table and IAM credential columns (migration)
- Add IAM service for managing per-user IAM users and policies
- Add encryption service for secure credential storage
- Update admin approve to create per-user IAM user and store encrypted credentials
- Update admin revoke to delete IAM user and clear credentials
- Update dataset creation to use per-user credentials for presigned URLs
- Update dataset creation to update user's IAM policy for new dataset prefix
- Remove aws_credentials from API response (security fix)

New secrets required:
- ENCRYPTION_KEY: For encrypting stored credentials

Note: CLI changes to use presigned URLs still pending.
Foundation for secure uploads without exposing AWS credentials:
- Update CreateDatasetResponse to use s3_config instead of aws_credentials
- Add FileInfo interface for file manifest requests
- Add requestUploadUrls API function for additional file uploads
- Add uploadFileWithPresignedUrl for single file HTTP PUT upload
- Add uploadFilesWithPresignedUrls for parallel batch uploads
- Add registerUrlWithGitAnnex to track uploaded files in git-annex
- Add registerUrlsWithGitAnnex for batch URL registration
- Add configureWebRemote for git-annex web remote setup

Note: Upload command rewrite to use this flow still pending.
@neuromechanist

Copy link
Copy Markdown
Contributor Author

Progress Update - Per-User Scoped Credentials

Completed

Backend (3 commits):

  1. Database migration for per-user AWS credentials storage
  2. IAM service for creating/managing per-user IAM users
  3. Encryption service for secure credential storage
  4. Admin approval creates IAM user with encrypted credentials
  5. Admin revoke deletes IAM user and clears credentials
  6. Dataset creation uses per-user credentials for presigned URLs
  7. IAM policy updated per-dataset to scope S3 access
  8. Removed aws_credentials from API response

CLI (1 commit):

  1. Updated CreateDatasetResponse interface (s3_config instead of aws_credentials)
  2. Added requestUploadUrls API function
  3. Added presigned URL upload functions (uploadFilesWithPresignedUrls)
  4. Added git-annex URL registration (registerUrlsWithGitAnnex)

Still Needed

CLI upload command rewrite:
The upload command needs to be refactored to use the new flow:

  1. Collect file manifest (data files)
  2. Request presigned URLs from backend
  3. Upload files via HTTP PUT to presigned URLs
  4. Register S3 URLs with git-annex using registerurl
  5. Push git metadata to GitHub

New secrets required:

  • ENCRYPTION_KEY - for encrypting stored IAM credentials

Database migration:
Run wrangler d1 migrations apply nemar-db --remote to apply the new migration.

Security Model

User Approval → Create IAM user → Store encrypted credentials
                     ↓
Dataset Create → Update IAM policy for new prefix → Generate presigned URLs
                     ↓
CLI Upload → Upload via presigned URLs → Register URLs with git-annex
                     ↓
User Revoke → Delete IAM user → Clear credentials → Revoke S3 access

Each user's credentials can only access their own dataset prefixes. Credentials are never exposed to the CLI.

- encryption.ts: use random salt per encryption, add proper error handling
- admin.ts: block revocation if IAM cleanup fails (security critical)
- datasets.ts: use user credentials for upload-urls endpoint
- datasets.ts: return error on IAM policy update failure
- datasets.ts: add ENCRYPTION_KEY validation, wrap presigned URL generation
- dataset.ts: rewrite upload command to use presigned URL flow
- datalad.ts: add file manifest collection and presigned URL upload functions
@neuromechanist
neuromechanist merged commit 533dc96 into main Jan 16, 2026
@neuromechanist
neuromechanist deleted the fix/backend-provided-aws-credentials branch January 16, 2026 17:20
@neuromechanist neuromechanist mentioned this pull request Jan 17, 2026
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.

Upload should use backend-provided S3 credentials, not user environment variables

1 participant