Skip to content

#406 Security vulnerabilities and runtime bugs fixes in upgrade scripts#407

Merged
Prafulrakhade merged 3 commits into
mosip:developfrom
GOKULRAJ136:dev-python
Jun 15, 2026
Merged

#406 Security vulnerabilities and runtime bugs fixes in upgrade scripts#407
Prafulrakhade merged 3 commits into
mosip:developfrom
GOKULRAJ136:dev-python

Conversation

@GOKULRAJ136

@GOKULRAJ136 GOKULRAJ136 commented Jun 10, 2026

Copy link
Copy Markdown
Contributor

Summary by CodeRabbit

  • Bug Fixes

    • Improved ID seeding and series validation to avoid incorrect or missing numeric values during data migrations.
    • Adjusted migration flow to be more robust when recent IDs are absent.
  • Security

    • Replaced unsafe string-based query and variable handling with safer, validated construction and assignment to reduce injection and misassignment risks.

Signed-off-by: GOKULRAJ136 <110164849+GOKULRAJ136@users.noreply.github.com>
@coderabbitai

coderabbitai Bot commented Jun 10, 2026

Copy link
Copy Markdown

Review Change Stack

Walkthrough

Hardens migration scripts by using psycopg2.sql for identifier-safe queries, replacing eval with validated printf-based property assignment, refining seed ID discovery and fill_series validation, and simplifying bioAttributes extraction.

Changes

Data Upgrade Script Reliability and Security

Layer / File(s) Summary
SQL injection prevention via parameterized queries
mosip_master/data_upgrade/1.1.5.5_to_1.2.0.1/data-uploader.py, mosip_master/data_upgrade/1.2.0.1_to_1.3.0/data-uploader.py
SELECT query construction now uses psycopg2.sql.SQL(...).format(sql.Identifier(args.table)); retrieved ids are validated with str(id_value).isdigit() and a numeric seed is chosen or falls back to 1000.
Unsafe eval removal and validated property assignment
mosip_master/data_upgrade/1.1.5.5_to_1.2.0.1/upgrade.sh, mosip_master/data_upgrade/1.2.0.1_to_1.3.0/upgrade.sh
Property keys are normalized (dots→underscores), validated against a shell identifier regex, and assigned via printf -v when valid; invalid keys are warned and skipped instead of using eval.
ID series generation and validation refinement
mosip_master/data_upgrade/1.2.0.1_to_1.3.0/data-uploader.py
get_seed_value() initializes seed_value=None, scans recent ids via identifier-safe query for the first numeric id, and falls back to 1000; fill_series() early-exits when end_row is None.
Simplified bioAttributes extraction
mosip_master/data_upgrade/1.1.5.5_to_1.2.0.1/migration-ui_spec.py
getSupportedAgeGroups() reads bioAttributes directly and normalizes None to [], removing the prior retry loop.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related issues

Poem

🐇 I hopped through scripts with a careful paw,
Replaced loose strings with identifiers law,
Eval went away, printf now takes the stage,
Seeds find numbers, or start at a steady page,
Safe upgrades hop forward—hooray! 🎉

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main objective: fixing security vulnerabilities and runtime bugs across multiple upgrade scripts (data-uploader.py and upgrade.sh), addressing issues like SQL injection prevention, eval usage elimination, and numeric ID validation.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
mosip_master/data_upgrade/1.2.0.1_to_1.3.0/data-uploader.py (1)

103-104: ⚡ Quick win

Remove the unused loop variable flagged by Ruff (B007).

value is never used in the loop body; rename it to _ to clear the warning.

Suggested fix
-    for i, value in enumerate(range(start_row, end_row + 1), start=1):
+    for i, _ in enumerate(range(start_row, end_row + 1), start=1):
         column[i].value = int(seed_value) + i
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@mosip_master/data_upgrade/1.2.0.1_to_1.3.0/data-uploader.py` around lines 103
- 104, The loop in the data-uploader uses an unused loop variable "value" in the
line "for i, value in enumerate(range(start_row, end_row + 1), start=1):" which
triggers Ruff B007; update the loop to use a throwaway variable "_" (e.g., "for
i, _ in enumerate(...):") so only "i" is used, leaving the assignment in
column[i].value = int(seed_value) + i unchanged.

Source: Linters/SAST tools

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@mosip_master/data_upgrade/1.2.0.1_to_1.3.0/data-uploader.py`:
- Line 51: The SQL builds an identifier by concatenating args.table in
get_seed_value; change the query to use psycopg2.sql composition with
psycopg2.sql.Identifier (e.g., build the query via psycopg2.sql.SQL("select id
from master.{} order by id desc limit 20").format(Identifier(args.table))) and
pass that composed SQL to cursor.execute to avoid SQL injection, and in the loop
where you do for i, value in enumerate(...): (inside get_seed_value or nearby)
rename the unused value to _ (for i, _ in enumerate(...)) to address the Ruff
B007 warning.

---

Nitpick comments:
In `@mosip_master/data_upgrade/1.2.0.1_to_1.3.0/data-uploader.py`:
- Around line 103-104: The loop in the data-uploader uses an unused loop
variable "value" in the line "for i, value in enumerate(range(start_row, end_row
+ 1), start=1):" which triggers Ruff B007; update the loop to use a throwaway
variable "_" (e.g., "for i, _ in enumerate(...):") so only "i" is used, leaving
the assignment in column[i].value = int(seed_value) + i unchanged.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 073f8348-6f8e-4086-a9c3-312a26db1914

📥 Commits

Reviewing files that changed from the base of the PR and between 39455d5 and a8c04fc.

📒 Files selected for processing (5)
  • mosip_master/data_upgrade/1.1.5.5_to_1.2.0.1/data-uploader.py
  • mosip_master/data_upgrade/1.1.5.5_to_1.2.0.1/migration-ui_spec.py
  • mosip_master/data_upgrade/1.1.5.5_to_1.2.0.1/upgrade.sh
  • mosip_master/data_upgrade/1.2.0.1_to_1.3.0/data-uploader.py
  • mosip_master/data_upgrade/1.2.0.1_to_1.3.0/upgrade.sh

Comment thread mosip_master/data_upgrade/1.2.0.1_to_1.3.0/data-uploader.py Outdated
Signed-off-by: GOKULRAJ136 <110164849+GOKULRAJ136@users.noreply.github.com>
Comment thread mosip_master/data_upgrade/1.2.0.1_to_1.3.0/data-uploader.py Outdated
Signed-off-by: GOKULRAJ136 <110164849+GOKULRAJ136@users.noreply.github.com>

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
mosip_master/data_upgrade/1.2.0.1_to_1.3.0/data-uploader.py (1)

48-67: ⚡ Quick win

Consider using context managers for database resources.

The connection and cursor are opened but never explicitly closed. While the OS will clean up when the script exits, using context managers is a best practice that ensures proper resource cleanup.

♻️ Refactor with context managers
 def get_seed_value():
   seed_value = None
-  conn = psycopg2.connect(database="mosip_master", user = args.dbusername, password = args.dbpassword, host = args.dbhost, port = args.dbport)
-  cursor = conn.cursor()
-  cursor.execute(
-      sql.SQL("select id from master.{} order by id desc limit 20")
-      .format(sql.Identifier(args.table))
-  )
-  for row in cursor.fetchall():
-    id_value = row[0]
-    if id_value is None:
-      seed_value = 1000
-      break
-    if str(id_value).isdigit():
-      seed_value = id_value
-      break
+  with psycopg2.connect(database="mosip_master", user=args.dbusername, password=args.dbpassword, host=args.dbhost, port=args.dbport) as conn:
+    with conn.cursor() as cursor:
+      cursor.execute(
+          sql.SQL("select id from master.{} order by id desc limit 20")
+          .format(sql.Identifier(args.table))
+      )
+      for row in cursor.fetchall():
+        id_value = row[0]
+        if id_value is None:
+          seed_value = 1000
+          break
+        if str(id_value).isdigit():
+          seed_value = id_value
+          break
 
   if seed_value is None:
     seed_value = 1000
   return seed_value
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@mosip_master/data_upgrade/1.2.0.1_to_1.3.0/data-uploader.py` around lines 48
- 67, The get_seed_value function opens a psycopg2 connection and cursor but
never closes them; refactor to use context managers so resources are always
cleaned up: wrap the connection creation in a with psycopg2.connect(...) as conn
and obtain the cursor via with conn.cursor() as cursor, move the existing sql
execution and fetch logic inside those blocks, and return seed_value as before
(ensure any required transaction/autocommit behavior is preserved if needed).
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@mosip_master/data_upgrade/1.2.0.1_to_1.3.0/data-uploader.py`:
- Around line 48-67: The get_seed_value function opens a psycopg2 connection and
cursor but never closes them; refactor to use context managers so resources are
always cleaned up: wrap the connection creation in a with psycopg2.connect(...)
as conn and obtain the cursor via with conn.cursor() as cursor, move the
existing sql execution and fetch logic inside those blocks, and return
seed_value as before (ensure any required transaction/autocommit behavior is
preserved if needed).

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 2abf112f-0dcb-4e0d-9c52-b798e0d65c97

📥 Commits

Reviewing files that changed from the base of the PR and between a8c04fc and 4cb958d.

📒 Files selected for processing (1)
  • mosip_master/data_upgrade/1.2.0.1_to_1.3.0/data-uploader.py

@Prafulrakhade Prafulrakhade merged commit b053ee1 into mosip:develop Jun 15, 2026
4 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.

3 participants