The problem we need to solve is that building a Lean project allows for arbitrary code execution, just like many other programming languages. Thus, building a Lean project might interfere with your proof checking infrastructure and fool it into accepting an incorrect proof.
"Building" a Lean project actually consists of two steps that can be separated: compiling the high-level Lean document to the simple expression format of the Lean kernel (this part is called "elaboration") and then checking that this expression consistutes a valid proof of the claimed statement (this part is called "kernel checking"). Only the first part potentially involves arbitrary code execution.
Lean ships with a component called `lake comparator` [1]. It works like this: a trusted challenge (formal statement) and the potential proof are elaborated separately in sandboxed environments so that code execution during the build of the proof cannot interfere with the checker or the trusted statement. Then the statement and proof are both exported into a simple JSON-format [2] that represents the relevant terms in the expression format of the Lean kernel, and then the checkers check this JSON.
If this process succeeds, then you know that you definitely have a proof of the relevant statement, with the JSON as a witness of the proof.
[1]: https://lean-lang.org/doc/reference/latest/ValidatingProofs/... [2]: https://github.com/leanprover/lean4export