File size: 9,961 Bytes
2eb87e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/**
 * Structured HTML render of a disclosed VH/VulnForge report
 * (parsed report object preferred; raw yaml fallback).
 */
import { useMemo } from "react";
import { load as parseYaml } from "js-yaml";

type Anchor = { file_path?: string; line?: number | string; function?: string };
type FlowStep = { step?: number | string; location?: string; description?: string };
type ReportDoc = {
  metadata?: {
    title?: string;
    vuln_type?: string;
    cwe?: string;
    cvss_vector?: string;
    cvss_score?: number | string;
    ev_priority?: string;
    ev_score?: number | string;
    ev_rationale?: string;
    poc_status?: string;
    exp_status?: string;
    affected_versions?: string;
    anchors?: Anchor[];
  };
  description?: {
    background?: string;
    detailed_description?: string;
    summary?: string;
    attack_payload_description?: string;
    attack_description?: string;
    impact?: string;
    combined_impact?: string;
    remediation?: string;
    fix_suggestion?: string;
  };
  code?: {
    dataflow?: FlowStep[];
    data_flow?: FlowStep[];
    fix_patch?: string;
    patch?: string;
  };
  references?: unknown;
};

function asRecord(v: unknown): Record<string, unknown> {
  if (v && typeof v === "object" && !Array.isArray(v)) return v as Record<string, unknown>;
  return {};
}

function str(v: unknown): string {
  if (v == null) return "";
  if (typeof v === "string") return v.trim();
  if (typeof v === "number" || typeof v === "boolean") return String(v);
  return "";
}

function Section({ label, children }: { label: string; children: React.ReactNode }) {
  if (!children) return null;
  return (
    <section>
      <p className="flex items-center gap-1.5 font-mono text-[11px] uppercase tracking-wider text-ink-tertiary">
        <span className="h-3 w-[3px] rounded-full bg-accent-600" aria-hidden />
        {label}
      </p>
      <div className="mt-1.5">{children}</div>
    </section>
  );
}

function Prose({ text }: { text: string }) {
  if (!text) return null;
  return (
    <p className="whitespace-pre-wrap break-words text-sm leading-relaxed text-ink-secondary">
      {text}
    </p>
  );
}

function normalizeDoc(input: unknown): ReportDoc | null {
  if (!input || typeof input !== "object") return null;
  const r = input as Record<string, unknown>;
  return {
    metadata: asRecord(r.metadata) as ReportDoc["metadata"],
    description: asRecord(r.description) as ReportDoc["description"],
    code: asRecord(r.code) as ReportDoc["code"],
    references: r.references,
  };
}

export type StructuredReport = {
  metadata: Record<string, unknown>;
  description: Record<string, unknown>;
  code: Record<string, unknown>;
  references: unknown;
};

/** Prefer structured `report`; raw yaml only as fallback. */
export function ReportBody({
  report,
  yaml,
}: {
  report?: StructuredReport | null;
  yaml?: string | null;
}) {
  const doc = useMemo<ReportDoc | null>(() => {
    if (report) return normalizeDoc(report);
    if (yaml?.trim()) {
      try {
        return normalizeDoc(parseYaml(yaml));
      } catch {
        return null;
      }
    }
    return null;
  }, [report, yaml]);

  if (!doc) {
    if (yaml?.trim()) {
      return (
        <pre className="mt-4 max-h-[32rem] overflow-auto whitespace-pre-wrap break-words rounded-md border border-line bg-surface px-4 py-3 font-mono text-[12px] leading-relaxed text-ink">
          {yaml}
        </pre>
      );
    }
    return (
      <p className="mt-4 text-sm text-ink-tertiary">
        Structured report is not available for this disclosure.
      </p>
    );
  }

  const meta = doc.metadata ?? {};
  const fullTitle = typeof meta === "object" && "title" in meta ? (meta as { title?: string }).title : undefined;
  const desc = doc.description ?? {};
  const anchors = (meta.anchors ?? []).filter((a) => a && (a.file_path || a.function));
  const dataflow = (doc.code?.dataflow ?? doc.code?.data_flow ?? []).filter(Boolean);
  const refList = Array.isArray(doc.references)
    ? doc.references.filter((r) => typeof r === "string" && (r as string).trim())
    : [];

  return (
    <div className="mt-4 space-y-5">
      {fullTitle && (
        <h4 className="font-display text-[15px] font-semibold leading-snug text-ink">{fullTitle}</h4>
      )}
      <dl className="grid grid-cols-2 gap-x-6 gap-y-2 text-[13px] sm:grid-cols-4">
        {meta.vuln_type && (
          <div>
            <dt className="text-ink-tertiary">Type</dt>
            <dd className="mt-0.5 font-medium text-ink">{meta.vuln_type}</dd>
          </div>
        )}
        {meta.cvss_score != null && (
          <div>
            <dt className="text-ink-tertiary">CVSS</dt>
            <dd className="mt-0.5 font-medium text-ink">{meta.cvss_score}</dd>
          </div>
        )}
        {meta.ev_priority && (
          <div>
            <dt className="text-ink-tertiary">EV priority</dt>
            <dd className="mt-0.5 font-medium text-ink">{meta.ev_priority}</dd>
          </div>
        )}
        {meta.poc_status && (
          <div>
            <dt className="text-ink-tertiary">PoC status</dt>
            <dd className="mt-0.5 font-medium text-ink">{meta.poc_status}</dd>
          </div>
        )}
        {meta.exp_status && (
          <div>
            <dt className="text-ink-tertiary">EXP status</dt>
            <dd className="mt-0.5 font-medium text-ink">{meta.exp_status}</dd>
          </div>
        )}
        {meta.affected_versions && (
          <div className="col-span-2 sm:col-span-2">
            <dt className="text-ink-tertiary">Affected</dt>
            <dd className="mt-0.5 font-medium text-ink">{meta.affected_versions}</dd>
          </div>
        )}
        {meta.cvss_vector && (
          <div className="col-span-2 sm:col-span-4">
            <dt className="text-ink-tertiary">Vector</dt>
            <dd className="mt-0.5 font-mono text-[12px] text-ink-secondary">{meta.cvss_vector}</dd>
          </div>
        )}
      </dl>

      {str(meta.ev_rationale) && (
        <Section label="Exploitability">
          <Prose text={str(meta.ev_rationale)} />
        </Section>
      )}

      {anchors.length > 0 && (
        <Section label="Anchors">
          <ul className="space-y-1.5">
            {anchors.map((a, i) => (
              <li key={i} className="flex flex-wrap items-baseline gap-x-2.5 text-[13px]">
                <span className="shrink-0 font-mono font-medium text-ink">
                  {a.file_path}
                  {a.line != null && `:${a.line}`}
                </span>
                {a.function && <span className="text-ink-secondary">{a.function}</span>}
              </li>
            ))}
          </ul>
        </Section>
      )}

      {str(desc.background) && (
        <Section label="Background">
          <Prose text={str(desc.background)} />
        </Section>
      )}
      {(str(desc.detailed_description) || str(desc.summary)) && (
        <Section label="Detailed analysis">
          <Prose text={str(desc.detailed_description) || str(desc.summary)} />
        </Section>
      )}
      {(str(desc.attack_payload_description) || str(desc.attack_description)) && (
        <Section label="Attack">
          {str(desc.attack_description) && <Prose text={str(desc.attack_description)} />}
          {str(desc.attack_payload_description) && (
            <Prose text={str(desc.attack_payload_description)} />
          )}
        </Section>
      )}
      {(str(desc.impact) || str(desc.combined_impact)) && (
        <Section label="Impact">
          <Prose text={str(desc.impact) || str(desc.combined_impact)} />
        </Section>
      )}
      {(str(desc.remediation) || str(desc.fix_suggestion)) && (
        <Section label="Remediation">
          <Prose text={str(desc.remediation) || str(desc.fix_suggestion)} />
        </Section>
      )}

      {dataflow.length > 0 && (
        <Section label="Dataflow">
          <ol className="space-y-2.5">
            {dataflow.map((st, i) => (
              <li key={i} className="flex gap-3 text-[13px]">
                <span className="flex h-5 w-5 shrink-0 items-center justify-center rounded-full bg-surface-sunken font-mono text-[11px] text-ink-secondary">
                  {st.step ?? i + 1}
                </span>
                <div className="min-w-0">
                  {st.location && (
                    <p className="font-mono text-[12px] font-medium text-ink">{st.location}</p>
                  )}
                  {st.description && (
                    <p className="mt-0.5 whitespace-pre-wrap break-words leading-relaxed text-ink-secondary">
                      {st.description}
                    </p>
                  )}
                </div>
              </li>
            ))}
          </ol>
        </Section>
      )}

      {(str(doc.code?.fix_patch) || str(doc.code?.patch)) && (
        <Section label="Fix patch">
          <pre className="overflow-x-auto whitespace-pre-wrap break-words rounded-md border border-line bg-surface-sunken px-4 py-3 font-mono text-[12px] leading-relaxed text-ink">
            {str(doc.code?.fix_patch) || str(doc.code?.patch)}
          </pre>
        </Section>
      )}

      {refList.length > 0 && (
        <Section label="References">
          <ul className="space-y-1 text-[13px]">
            {(refList as string[]).map((r, i) =>
              /^https?:\/\//.test(r) ? (
                <li key={i}>
                  <a
                    href={r}
                    target="_blank"
                    rel="noreferrer"
                    className="break-all font-mono text-[12px] text-accent-600 hover:underline"
                  >
                    {r}
                  </a>
                </li>
              ) : (
                <li key={i} className="text-ink-secondary">
                  {r}
                </li>
              ),
            )}
          </ul>
        </Section>
      )}
    </div>
  );
}