Skip to content

Commit 62c8dd3

Browse files
authored
Merge pull request #2046 from LerianStudio/fix/uuid-assertions
fix: replace unsafe UUID type assertions with safe helper
2 parents 7c3431e + 8bdbb0f commit 62c8dd3

9 files changed

Lines changed: 421 additions & 127 deletions

File tree

components/crm/Makefile

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -246,16 +246,6 @@ sec:
246246
echo "$(YELLOW)No Go files found, skipping security checks$(NC)"; \
247247
fi
248248

249-
#-------------------------------------------------------
250-
# Clean
251-
#-------------------------------------------------------
252-
253-
.PHONY: clean
254-
clean:
255-
$(call title1,"Cleaning build artifacts")
256-
@rm -rf $(BIN_DIR)/* $(ARTIFACTS_DIR)/* $(TEST_REPORTS_DIR)/* coverage.tmp coverage.out coverage.html bin/
257-
@echo "$(GREEN)$(BOLD)[ok]$(NC) Artifacts cleaned successfully$(GREEN) ✔️$(NC)"
258-
259249
#-------------------------------------------------------
260250
# Docker
261251
#-------------------------------------------------------

components/ledger/Makefile

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -138,16 +138,6 @@ sec:
138138
echo "$(YELLOW)No Go files found, skipping security checks$(NC)"; \
139139
fi
140140

141-
#-------------------------------------------------------
142-
# Clean Commands
143-
#-------------------------------------------------------
144-
145-
.PHONY: clean
146-
clean:
147-
$(call title1,"Cleaning build artifacts")
148-
@rm -rf $(BIN_DIR)/* $(ARTIFACTS_DIR)/* $(TEST_REPORTS_DIR)/*
149-
@echo "$(GREEN)$(BOLD)[ok]$(NC) Artifacts cleaned successfully$(GREEN) ✔️$(NC)"
150-
151141
#-------------------------------------------------------
152142
# Docker Commands
153143
#-------------------------------------------------------

components/ledger/internal/adapters/http/in/account.go

Lines changed: 90 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,15 @@ func (handler *AccountHandler) CreateAccount(i any, c *fiber.Ctx) error {
5252

5353
logger, tracer, _, metricFactory := libCommons.NewTrackingFromContext(ctx)
5454

55-
organizationID := c.Locals("organization_id").(uuid.UUID)
56-
ledgerID := c.Locals("ledger_id").(uuid.UUID)
55+
organizationID, err := http.GetUUIDFromLocals(c, "organization_id")
56+
if err != nil {
57+
return http.WithError(c, err)
58+
}
59+
60+
ledgerID, err := http.GetUUIDFromLocals(c, "ledger_id")
61+
if err != nil {
62+
return http.WithError(c, err)
63+
}
5764

5865
payload := i.(*mmodel.CreateAccountInput)
5966
portfolioID := payload.PortfolioID
@@ -123,8 +130,15 @@ func (handler *AccountHandler) GetAllAccounts(c *fiber.Ctx) error {
123130
ctx, span := tracer.Start(ctx, "handler.get_all_accounts")
124131
defer span.End()
125132

126-
organizationID := c.Locals("organization_id").(uuid.UUID)
127-
ledgerID := c.Locals("ledger_id").(uuid.UUID)
133+
organizationID, err := http.GetUUIDFromLocals(c, "organization_id")
134+
if err != nil {
135+
return http.WithError(c, err)
136+
}
137+
138+
ledgerID, err := http.GetUUIDFromLocals(c, "ledger_id")
139+
if err != nil {
140+
return http.WithError(c, err)
141+
}
128142

129143
var (
130144
portfolioID *uuid.UUID
@@ -227,9 +241,20 @@ func (handler *AccountHandler) GetAccountByID(c *fiber.Ctx) error {
227241
ctx, span := tracer.Start(ctx, "handler.get_account_by_id")
228242
defer span.End()
229243

230-
organizationID := c.Locals("organization_id").(uuid.UUID)
231-
ledgerID := c.Locals("ledger_id").(uuid.UUID)
232-
id := c.Locals("id").(uuid.UUID)
244+
organizationID, err := http.GetUUIDFromLocals(c, "organization_id")
245+
if err != nil {
246+
return http.WithError(c, err)
247+
}
248+
249+
ledgerID, err := http.GetUUIDFromLocals(c, "ledger_id")
250+
if err != nil {
251+
return http.WithError(c, err)
252+
}
253+
254+
id, err := http.GetUUIDFromLocals(c, "id")
255+
if err != nil {
256+
return http.WithError(c, err)
257+
}
233258

234259
logger.Log(ctx, libLog.LevelInfo, fmt.Sprintf("Initiating retrieval of Account with Account ID: %s", id.String()))
235260

@@ -272,8 +297,16 @@ func (handler *AccountHandler) GetAccountExternalByCode(c *fiber.Ctx) error {
272297
ctx, span := tracer.Start(ctx, "handler.get_account_external_by_code")
273298
defer span.End()
274299

275-
organizationID := c.Locals("organization_id").(uuid.UUID)
276-
ledgerID := c.Locals("ledger_id").(uuid.UUID)
300+
organizationID, err := http.GetUUIDFromLocals(c, "organization_id")
301+
if err != nil {
302+
return http.WithError(c, err)
303+
}
304+
305+
ledgerID, err := http.GetUUIDFromLocals(c, "ledger_id")
306+
if err != nil {
307+
return http.WithError(c, err)
308+
}
309+
277310
code := c.Params("code")
278311

279312
alias := constant.DefaultExternalAccountAliasPrefix + code
@@ -319,8 +352,16 @@ func (handler *AccountHandler) GetAccountByAlias(c *fiber.Ctx) error {
319352
ctx, span := tracer.Start(ctx, "handler.get_account_by_alias")
320353
defer span.End()
321354

322-
organizationID := c.Locals("organization_id").(uuid.UUID)
323-
ledgerID := c.Locals("ledger_id").(uuid.UUID)
355+
organizationID, err := http.GetUUIDFromLocals(c, "organization_id")
356+
if err != nil {
357+
return http.WithError(c, err)
358+
}
359+
360+
ledgerID, err := http.GetUUIDFromLocals(c, "ledger_id")
361+
if err != nil {
362+
return http.WithError(c, err)
363+
}
364+
324365
alias := c.Params("alias")
325366

326367
logger.Log(ctx, libLog.LevelInfo, fmt.Sprintf("Initiating retrieval of Account with Account Alias: %s", alias))
@@ -368,9 +409,20 @@ func (handler *AccountHandler) UpdateAccount(i any, c *fiber.Ctx) error {
368409
ctx, span := tracer.Start(ctx, "handler.update_account")
369410
defer span.End()
370411

371-
organizationID := c.Locals("organization_id").(uuid.UUID)
372-
ledgerID := c.Locals("ledger_id").(uuid.UUID)
373-
id := c.Locals("id").(uuid.UUID)
412+
organizationID, err := http.GetUUIDFromLocals(c, "organization_id")
413+
if err != nil {
414+
return http.WithError(c, err)
415+
}
416+
417+
ledgerID, err := http.GetUUIDFromLocals(c, "ledger_id")
418+
if err != nil {
419+
return http.WithError(c, err)
420+
}
421+
422+
id, err := http.GetUUIDFromLocals(c, "id")
423+
if err != nil {
424+
return http.WithError(c, err)
425+
}
374426

375427
logger.Log(ctx, libLog.LevelInfo, fmt.Sprintf("Initiating update of Account with ID: %s", id.String()))
376428

@@ -426,9 +478,21 @@ func (handler *AccountHandler) DeleteAccountByID(c *fiber.Ctx) error {
426478
ctx, span := tracer.Start(ctx, "handler.delete_account_by_id")
427479
defer span.End()
428480

429-
organizationID := c.Locals("organization_id").(uuid.UUID)
430-
ledgerID := c.Locals("ledger_id").(uuid.UUID)
431-
id := c.Locals("id").(uuid.UUID)
481+
organizationID, err := http.GetUUIDFromLocals(c, "organization_id")
482+
if err != nil {
483+
return http.WithError(c, err)
484+
}
485+
486+
ledgerID, err := http.GetUUIDFromLocals(c, "ledger_id")
487+
if err != nil {
488+
return http.WithError(c, err)
489+
}
490+
491+
id, err := http.GetUUIDFromLocals(c, "id")
492+
if err != nil {
493+
return http.WithError(c, err)
494+
}
495+
432496
token := c.Get("Authorization")
433497

434498
logger.Log(ctx, libLog.LevelInfo, fmt.Sprintf("Initiating removal of Account with ID: %s", id.String()))
@@ -469,8 +533,15 @@ func (handler *AccountHandler) CountAccounts(c *fiber.Ctx) error {
469533
ctx, span := tracer.Start(ctx, "handler.count_accounts")
470534
defer span.End()
471535

472-
organizationID := c.Locals("organization_id").(uuid.UUID)
473-
ledgerID := c.Locals("ledger_id").(uuid.UUID)
536+
organizationID, err := http.GetUUIDFromLocals(c, "organization_id")
537+
if err != nil {
538+
return http.WithError(c, err)
539+
}
540+
541+
ledgerID, err := http.GetUUIDFromLocals(c, "ledger_id")
542+
if err != nil {
543+
return http.WithError(c, err)
544+
}
474545

475546
logger.Log(ctx, libLog.LevelInfo, fmt.Sprintf("Counting accounts for organization %s and ledger %s", organizationID, ledgerID))
476547

components/ledger/internal/adapters/http/in/accounttype.go

Lines changed: 60 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"github.com/LerianStudio/midaz/v3/pkg/mmodel"
1616
"github.com/LerianStudio/midaz/v3/pkg/net/http"
1717
"github.com/gofiber/fiber/v2"
18-
"github.com/google/uuid"
1918
"go.mongodb.org/mongo-driver/bson"
2019
)
2120

@@ -51,8 +50,15 @@ func (handler *AccountTypeHandler) CreateAccountType(i any, c *fiber.Ctx) error
5150
ctx, span := tracer.Start(ctx, "handler.create_account_type")
5251
defer span.End()
5352

54-
organizationID := c.Locals("organization_id").(uuid.UUID)
55-
ledgerID := c.Locals("ledger_id").(uuid.UUID)
53+
organizationID, err := http.GetUUIDFromLocals(c, "organization_id")
54+
if err != nil {
55+
return http.WithError(c, err)
56+
}
57+
58+
ledgerID, err := http.GetUUIDFromLocals(c, "ledger_id")
59+
if err != nil {
60+
return http.WithError(c, err)
61+
}
5662

5763
payload := i.(*mmodel.CreateAccountTypeInput)
5864

@@ -96,9 +102,20 @@ func (handler *AccountTypeHandler) GetAccountTypeByID(c *fiber.Ctx) error {
96102
ctx, span := tracer.Start(ctx, "handler.get_account_type_by_id")
97103
defer span.End()
98104

99-
organizationID := c.Locals("organization_id").(uuid.UUID)
100-
ledgerID := c.Locals("ledger_id").(uuid.UUID)
101-
id := c.Locals("id").(uuid.UUID)
105+
organizationID, err := http.GetUUIDFromLocals(c, "organization_id")
106+
if err != nil {
107+
return http.WithError(c, err)
108+
}
109+
110+
ledgerID, err := http.GetUUIDFromLocals(c, "ledger_id")
111+
if err != nil {
112+
return http.WithError(c, err)
113+
}
114+
115+
id, err := http.GetUUIDFromLocals(c, "id")
116+
if err != nil {
117+
return http.WithError(c, err)
118+
}
102119

103120
logger.Log(ctx, libLog.LevelInfo, fmt.Sprintf("Initiating retrieval of Account Type with ID: %s", id.String()))
104121

@@ -144,9 +161,20 @@ func (handler *AccountTypeHandler) UpdateAccountType(i any, c *fiber.Ctx) error
144161
ctx, span := tracer.Start(ctx, "handler.update_account_type")
145162
defer span.End()
146163

147-
organizationID := c.Locals("organization_id").(uuid.UUID)
148-
ledgerID := c.Locals("ledger_id").(uuid.UUID)
149-
id := c.Locals("id").(uuid.UUID)
164+
organizationID, err := http.GetUUIDFromLocals(c, "organization_id")
165+
if err != nil {
166+
return http.WithError(c, err)
167+
}
168+
169+
ledgerID, err := http.GetUUIDFromLocals(c, "ledger_id")
170+
if err != nil {
171+
return http.WithError(c, err)
172+
}
173+
174+
id, err := http.GetUUIDFromLocals(c, "id")
175+
if err != nil {
176+
return http.WithError(c, err)
177+
}
150178

151179
payload := i.(*mmodel.UpdateAccountTypeInput)
152180

@@ -199,9 +227,20 @@ func (handler *AccountTypeHandler) DeleteAccountTypeByID(c *fiber.Ctx) error {
199227
ctx, span := tracer.Start(ctx, "handler.delete_account_type_by_id")
200228
defer span.End()
201229

202-
organizationID := c.Locals("organization_id").(uuid.UUID)
203-
ledgerID := c.Locals("ledger_id").(uuid.UUID)
204-
id := c.Locals("id").(uuid.UUID)
230+
organizationID, err := http.GetUUIDFromLocals(c, "organization_id")
231+
if err != nil {
232+
return http.WithError(c, err)
233+
}
234+
235+
ledgerID, err := http.GetUUIDFromLocals(c, "ledger_id")
236+
if err != nil {
237+
return http.WithError(c, err)
238+
}
239+
240+
id, err := http.GetUUIDFromLocals(c, "id")
241+
if err != nil {
242+
return http.WithError(c, err)
243+
}
205244

206245
logger.Log(ctx, libLog.LevelInfo, fmt.Sprintf("Initiating deletion of Account Type with Account Type ID: %s", id.String()))
207246

@@ -250,8 +289,15 @@ func (handler *AccountTypeHandler) GetAllAccountTypes(c *fiber.Ctx) error {
250289
ctx, span := tracer.Start(ctx, "handler.get_all_account_types")
251290
defer span.End()
252291

253-
organizationID := c.Locals("organization_id").(uuid.UUID)
254-
ledgerID := c.Locals("ledger_id").(uuid.UUID)
292+
organizationID, err := http.GetUUIDFromLocals(c, "organization_id")
293+
if err != nil {
294+
return http.WithError(c, err)
295+
}
296+
297+
ledgerID, err := http.GetUUIDFromLocals(c, "ledger_id")
298+
if err != nil {
299+
return http.WithError(c, err)
300+
}
255301

256302
headerParams, err := http.ValidateParameters(c.Queries())
257303
if err != nil {

0 commit comments

Comments
 (0)