Allan Vargas Torres
ENES

All projects

E-invoicing SaaS

Wrote the pre-launch audit of a multi-tenant e-invoicing platform and fixed all 17 findings.

In short

  • Wrote the pre-launch audit and fixed all 17 findings
  • Tenant isolation with PostgreSQL row-level security, tested on all 20 tables in CI
  • Idempotent invoicing: a retry never issues the same invoice twice
Role
Software development intern (full stack)
Period
Jul 2026 – Aug 2026
Team
Product team at Argana
Status
Internship
Stack
TypeScript, NestJS, React, PostgreSQL (row-level security), XAdES-EPES, AES-256-GCM, Argon2id, JWT and API keys, Webhooks, pnpm + Turborepo

The code is private, so this case study describes the work instead of linking to it.

The problem

In Costa Rica every invoice is an electronic document: it is signed, sent to the Ministry of Finance (Hacienda) and accepted or rejected. The platform already did that, through a web app and an API for other systems.

Before launch it had to hold up in two ways:

  • In the cases real businesses hit: concurrent requests, foreign currencies, documents that need to be voided, and a tax authority that does not always answer.
  • With many companies in one database, each seeing all of its own data and nothing from anyone else.

What I did

  • Wrote the full-repository audit and fixed all 17 findings: 8 tax-compliance defects and 9 business-logic bugs.
  • Fixed row-level security policies that locked each company out of its own data, and proved tenant isolation on all 20 tables with tests that run in CI.
  • Hardened file uploads and outbound requests. Uploaded files are checked by their content signature, not by the type they claim to be.
  • Made invoice creation idempotent: a fingerprint per request means a retried call never issues the same invoice twice.
  • Account safety: role changes can no longer leave a company without an administrator; new users need a confirmed email and join by invitation link instead of a shared password.
  • API and webhooks: pagination on every collection, more useful events, and end-to-end tests for API keys.
  • Subscription payments by bank transfer (SINPE): the customer uploads the transfer receipt and the platform’s staff review it from a back office.
  • A clean start: the repository builds and runs from a fresh checkout, with demo data and one account per role, and the end-to-end conformance gate really runs in CI.
  • Dependencies: no known high or critical vulnerabilities by the end of my internship.

How it works now

Three parts of the platform I worked on, and why they are built that way.

  1. Create the invoiceIts number comes from an atomic counter; a retried request gets the same invoice back
  2. Sign itXAdES-EPES digital signature
  3. Send it to the tax authorityRetries with backoff; the signed XML is kept in permanent storage
  4. Accepted or rejectedNo answer? Ask for the status by its key; never send it again
Every step runs inside the company’s own session, isolated by row-level security.

Invoice numbers from an atomic counter

Each invoice needs a consecutive number per company, branch, terminal and document type, with no gaps and no duplicates. Counting the existing invoices and adding one breaks as soon as two requests arrive at the same time: both see the same count.

The number comes from a counter row that is locked and incremented atomically, inside the same transaction that creates the invoice. If the invoice fails, the transaction rolls back and the number is not used up. I moved every invoice number onto this counter, which removed any chance of a duplicate number reaching the tax authority.

When the tax authority does not answer

Each invoice moves through explicit states, from signing to the tax authority’s final answer. If the tax authority times out, the platform does not send the invoice again; it asks for the status of the document by its key, with retries and backoff. I built those retries and the status query, plus a recovery job for invoices that would otherwise stay pending when no answer ever comes. And if the connection drops while an invoice is being issued, retrying returns the same invoice instead of a new number.

Tenant isolation in the database

Isolation does not depend on every query remembering a WHERE company_id = …. The company is set on the PostgreSQL session with SET LOCAL inside each transaction, so it never leaks to the next request that reuses the pooled connection, and the tables use FORCE ROW LEVEL SECURITY, so not even the table owner skips the policies. I wrote the CI test that checks every table in both directions: each company sees all of its data and none of anyone else’s.

Result

  • The platform was ready to launch with no known open tax-compliance defects.
  • No known high or critical security findings or dependency vulnerabilities by the end of my internship.

What I took from it

An audit is only as good as what closes it. Each finding ended with a fix and a test that would fail if the problem came back, so the next person does not have to trust my report: they can run it.