abinazebinoy commited on
Commit
a1ddfee
·
unverified ·
2 Parent(s): 2b31b6b7650d33

Merge pull request #93 from abinaze/fix/desktop-mode-dossier-output

Browse files

fix: desktop-mode-adapt dossier output; unify 3 hardcoded copies of its path

ai/dossier_generator.py CHANGED
@@ -165,15 +165,23 @@ class DossierGenerator:
165
  return False
166
 
167
  def generate(self, entity_id: str, multi_report: dict = None,
168
- output_dir: str = "data/processed") -> dict:
169
- # H-07 / CodeQL fix: use absolute path from this file, not CWD-relative
170
- _this_dir = os.path.dirname(os.path.abspath(__file__))
171
- _project_root = os.path.dirname(_this_dir)
172
- allowed_base = os.path.realpath(os.path.join(_project_root, "data"))
 
 
 
 
 
 
 
173
  real_out_dir = os.path.realpath(
174
- os.path.join(_project_root, output_dir) if not os.path.isabs(output_dir) else output_dir
 
175
  )
176
- if not real_out_dir.startswith(allowed_base + os.sep):
177
  raise ValueError(f"Output directory outside allowed path: {output_dir}")
178
  # CodeQL #12-15 FIX: use validated real_out_dir, not original output_dir
179
  os.makedirs(real_out_dir, exist_ok=True)
 
165
  return False
166
 
167
  def generate(self, entity_id: str, multi_report: dict = None,
168
+ output_dir: str = None) -> dict:
169
+ # P-1 fix: allowed_base and the default output_dir now both come
170
+ # from the single centralized get_dossier_output_dir() -- see
171
+ # that function's own docstring for why 3 independently-
172
+ # hardcoded copies of this path was a real, latent-inconsistency
173
+ # risk, not just harmless duplication. H-07 / CodeQL fix
174
+ # preserved: still an absolute-path allowlist check, not
175
+ # CWD-relative.
176
+ from config.settings import get_dossier_output_dir
177
+ allowed_base = os.path.realpath(get_dossier_output_dir())
178
+ if output_dir is None:
179
+ output_dir = allowed_base
180
  real_out_dir = os.path.realpath(
181
+ output_dir if os.path.isabs(output_dir)
182
+ else os.path.join(allowed_base, output_dir)
183
  )
184
+ if real_out_dir != allowed_base and not real_out_dir.startswith(allowed_base + os.sep):
185
  raise ValueError(f"Output directory outside allowed path: {output_dir}")
186
  # CodeQL #12-15 FIX: use validated real_out_dir, not original output_dir
187
  os.makedirs(real_out_dir, exist_ok=True)
api/routes/export.py CHANGED
@@ -51,17 +51,19 @@ def export_pdf(entity_id: str, driver=Depends(get_db)):
51
  result = generator.generate(
52
  entity_id,
53
  multi_report=multi_report,
54
- output_dir="data/processed",
55
  )
56
 
57
  output_path = result.get("pdf_path") or result.get("html_path")
58
  if not output_path or not os.path.exists(output_path):
59
  raise HTTPException(status_code=500,
60
  detail="Dossier generation failed")
61
- # H-07 / CodeQL fix: use file-anchored absolute path
62
- _routes_dir = os.path.dirname(os.path.abspath(__file__))
63
- _project_root = os.path.dirname(os.path.dirname(_routes_dir))
64
- allowed_dir = os.path.realpath(os.path.join(_project_root, "data", "processed"))
 
 
 
65
  real_out = os.path.realpath(output_path)
66
  if not real_out.startswith(allowed_dir + os.sep):
67
  raise HTTPException(status_code=400, detail="Invalid output path")
 
51
  result = generator.generate(
52
  entity_id,
53
  multi_report=multi_report,
 
54
  )
55
 
56
  output_path = result.get("pdf_path") or result.get("html_path")
57
  if not output_path or not os.path.exists(output_path):
58
  raise HTTPException(status_code=500,
59
  detail="Dossier generation failed")
60
+ # H-07 / CodeQL fix: use file-anchored absolute path.
61
+ # P-1 fix: was its own separate hardcoded <project_root>/data/processed
62
+ # computation, independent of (and narrower than) dossier_generator.py's
63
+ # own guard -- see get_dossier_output_dir()'s docstring in
64
+ # config/settings.py for why that latent inconsistency mattered.
65
+ from config.settings import get_dossier_output_dir
66
+ allowed_dir = os.path.realpath(get_dossier_output_dir())
67
  real_out = os.path.realpath(output_path)
68
  if not real_out.startswith(allowed_dir + os.sep):
69
  raise HTTPException(status_code=400, detail="Invalid output path")
config/settings.py CHANGED
@@ -42,6 +42,74 @@ def get_app_data_dir() -> Path:
42
  return Path(base) / "BharatGraph"
43
 
44
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
  def _local_config_path() -> Path:
46
  return get_app_data_dir() / "config.json"
47
 
 
42
  return Path(base) / "BharatGraph"
43
 
44
 
45
+ def get_dossier_output_dir() -> str:
46
+ """Absolute base directory generated dossiers/reports are written to
47
+ and may be served from.
48
+
49
+ P-1 fix, centralized here because this exact path was previously
50
+ hardcoded/recomputed independently in THREE places that all need to
51
+ agree for the export flow to work at all:
52
+ - ai/dossier_generator.py's generate() computed its own
53
+ <project_root>/data as a path-traversal allowlist (H-07/CodeQL
54
+ fix) and joined output_dir onto <project_root> directly.
55
+ - api/routes/export.py passed the literal string "data/processed"
56
+ as that function's output_dir argument.
57
+ - api/routes/export.py ALSO independently recomputed
58
+ <project_root>/data/processed as ITS OWN separate, independent
59
+ path-traversal allowlist (a second H-07/CodeQL fix) to validate
60
+ the path generate() handed back, before serving the file.
61
+ These three were already subtly inconsistent with each other before
62
+ any desktop-mode change: the first allowed anything under
63
+ <project_root>/data/ (e.g. data/samples), while the second allowed
64
+ only <project_root>/data/processed specifically -- benign only
65
+ because output_dir is never actually varied by any real caller
66
+ today. Unifying all three onto this single function fixes that
67
+ latent inconsistency and means a future desktop-mode change to
68
+ where dossiers live can never desync between the two independent
69
+ security guards that gate the export endpoint.
70
+ Web-deployment behavior is unchanged when BHARATGRAPH_DESKTOP_MODE
71
+ is unset -- same exact <project_root>/data/processed as before.
72
+ """
73
+ if os.getenv("BHARATGRAPH_DESKTOP_MODE", "").lower() == "true":
74
+ return str(get_app_data_dir() / "reports")
75
+ _project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
76
+ return os.path.join(_project_root, "data", "processed")
77
+
78
+
79
+ def get_dossier_output_dir() -> str:
80
+ """Absolute base directory generated dossiers/reports are written to
81
+ and may be served from.
82
+
83
+ P-1 fix, centralized here because this exact path was previously
84
+ hardcoded/recomputed independently in THREE places that all need to
85
+ agree for the export flow to work at all:
86
+ - ai/dossier_generator.py's generate() computed its own
87
+ <project_root>/data as a path-traversal allowlist (H-07/CodeQL
88
+ fix) and joined output_dir onto <project_root> directly.
89
+ - api/routes/export.py passed the literal string "data/processed"
90
+ as that function's output_dir argument.
91
+ - api/routes/export.py ALSO independently recomputed
92
+ <project_root>/data/processed as ITS OWN separate, independent
93
+ path-traversal allowlist (a second H-07/CodeQL fix) to validate
94
+ the path generate() handed back, before serving the file.
95
+ These three were already subtly inconsistent with each other before
96
+ any desktop-mode change: the first allowed anything under
97
+ <project_root>/data/ (e.g. data/samples), while the second allowed
98
+ only <project_root>/data/processed specifically -- benign only
99
+ because output_dir is never actually varied by any real caller
100
+ today. Unifying all three onto this single function fixes that
101
+ latent inconsistency and means a future desktop-mode change to
102
+ where dossiers live can never desync between the two independent
103
+ security guards that gate the export endpoint.
104
+ Web-deployment behavior is unchanged when BHARATGRAPH_DESKTOP_MODE
105
+ is unset -- same exact <project_root>/data/processed as before.
106
+ """
107
+ if os.getenv("BHARATGRAPH_DESKTOP_MODE", "").lower() == "true":
108
+ return str(get_app_data_dir() / "reports")
109
+ _project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
110
+ return os.path.join(_project_root, "data", "processed")
111
+
112
+
113
  def _local_config_path() -> Path:
114
  return get_app_data_dir() / "config.json"
115