fix(upload): key R2 upload folder by immutable companyId instead of mutable companyName

Open Beginner friendly
#60 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
82/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
cloud, typescript
Domain
backend, cloud

Research direction

Read apps/api/src/upload/upload.service.ts, especially uploadFile around lines 128-135, and trace how the R2 key prefix is built. Replace the company name based prefix with the company ID or user ID fallback described in the issue, and remove companyName from key generation. Done when new uploads use the immutable prefix and existing stored URLs remain unaffected.

Written by the indexing model from the issue text.

Description

backend good first issue
Problem Description

In apps/api/src/upload/upload.service.ts (uploadFile, lines 128-135), the R2 object path prefix companySegment is derived from sanitizeSegment(userCompany.name) or from the client-supplied companyName when userCompany is null.

This creates three issues at 1k-10k+ multi-tenant scale:

  1. Prefix collisions: Two different companies whose names sanitize to the same slug (e.g., "Acme Co" and "Acme! Co") write into the same R2 folder prefix.
  2. Client-supplied folder spoofing: A user with userCompany === null can pass another company's name in companyName and write files into that tenant's R2 folder.
  3. Mutable path: Renaming a company changes its future upload folder prefix.

Expected Behavior
  1. New uploads must always be scoped under an immutable, collision-free folder prefix (userCompany.id, or user-${userId} when the user has no company).
  2. Client-supplied companyName must be ignored for R2 key generation.
  3. Existing uploaded file URLs already stored in the database continue to resolve normally.

Where to Look
  • apps/api/src/upload/upload.service.ts (uploadFile, lines 128-135)

Solution / Implementation Guide
  1. In apps/api/src/upload/upload.service.ts, replace the companySegment derivation in uploadFile with:
    const companySegment = userCompany?.id
      ? sanitizeSegment(userCompany.id)
      : `user-${sanitizeSegment(userId)}`;
    
  2. Remove usage of the client-supplied companyName parameter for constructing the R2 storage key.
Dominant language
TypeScript
Stars
2
Forks
1
Avg merge
6m
Merged PRs (30d)
27

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from sumamakhan761/tryinstance

All issues in sumamakhan761/tryinstance

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.