Let $X$ be a smooth projective variery over $\mathbb C$, and $U$ be an open subset of $X$ such that the complement of $U$ has (complex) codimension $2$. Let $L$ be a line bundle on $X$. Is the following statement true?
The natural map $$H^0(X,L)\to H^0(U,L|_U)$$ is an isomorphsim.
It is easy to see the injectivity, as varieties are irreducible, hence $U$ is dominant. For surjectivity I am not sure if it's true. But if it is, I think it should be something like Hartogs' theorem.
My attampt 1:
Any section $s\in H^0(U,L|_U)$ can be viewed as a rational function on $U$, hence is automatically a rational function $\tilde s$ on $X$. So remains to check the conditions on valuations are the same, that is, for any irreducible divisor $Z$ on $X$, $ord_Z(\tilde s)$ in $X$ should be consistent with $ord_{Z\cap U}(s)$ in $U$. But I have no idea how to do it then.
My attampt 2:
We can find a set of open balls $B_i$ which covers $X$. Then for every section $s\in H^0(U,L|_U)$, the restriction $s_i=s|_{B_i\cap U}$ can be viewed as a holomorphic function, by Hartogs' extension theorem we can extend it to $\tilde s_i$ on $B_i$, and then glue to a section on $X$. It seems this is done?
Let $X = \cup U_i$ be a finite open affine covering of $X$. We have the following commutative diagram which is left exact.
$\require{AMScd} \begin{CD} 0 @> >> H^0(X,L) @> >> \prod^i H^0(U_i,L_{U_i}) @> >> \prod^{ij}H^0(U_{ij}, L_{U_{ij}})\\ @VVV @VVV @VVV @VVV\\ 0 @> >> H^0(U,L) @> >> H^0(U\cap U_i,L_{U_i}) @> >> H^0(U \cap U_{ij}, L_{U_{ij}}); \end{CD}$
Now, as already pointed out in the question the first map is injective. A similar argument, would say that the second and third vertical maps are also injective. Also note that since second and third vertical maps are surjective as well. This is a corollary of the fact that $L_{U_i}$ and $L_{U_{ij}}$ are trivial and sections over open sets with complement of codimension 2 can always be extended to all of space when thn ambient space is normal.
This argument could also be written in more cohomological language as following: Let $Z = X\setminus U$. Consider the following long exact sequence
$0 \rightarrow H^0_Z(X,L) \rightarrow H^0(X,L) \rightarrow H^0(U,L) \rightarrow H^1_Z(X,L) \rightarrow \dots$
Since $Z$ is of codimension 2 in a smooth projective variety, then $depth_{I(Z)}L \geq 2$. It follows from computation from local cohomology that $H^i_Z(X,L) = 0$, for $i = 0,1$. Thus it follows that the restriction map
$H^0(X,L) \rightarrow H^0(U,L_{U})$
is an isomorphism.