In chapter 4.5 of Jakob Schwichtenberg's Physics from Symmetry, he expresses the variation of the Lagrangian $L = L\left ( q, \dot{q}, t \right )$ with respect to the generalized coordinate $q$ as
$$\delta L = L\left ( q, \dot{q}, t \right ) - L \left ( q + \delta q, \dot{q} + \frac{d\delta q}{dt}, t \right )$$
He then says that letting $\delta L \neq 0$ is fine as long as the action is invariant under the variation:
$$\delta S = \int dt \delta L = 0 \tag{1}$$
He claims that $L$ can differ from another Lagrangian, say, $L^\prime$, by the time derivative of a function $G = G\left ( q, t \right )$. Let's say that
$$L^\prime = L + \frac{dG}{dt}$$
He then goes on to prove that the variation with respect to the new Lagrangian is equal to the variation of the action of the original Lagrangian, namely:
$$\delta S^\prime = \int dt \delta L^\prime = \int dt \delta L + \int dt \frac{d}{dt}\delta G = \delta S + \frac{\partial G}{\partial q}\delta q \Bigg|_\text{(endpoints)} = \delta S \tag{2}$$
where $\frac{\partial G}{\partial q}\delta q \Bigg|_\text{(endpoints)}$ is zero due to the vanishing variation $\delta q$ at the bounds of integration. This proves that $L$ and $L^\prime$ yield the same equations of motion. From this, he posits that the variation in $L$ can be expressed as
$$\delta L = \frac{dG}{dt}\tag{3}$$
implying that the Lagrangian can vary in this fashion while keeping $S$ invariant. However, inserting this expression into $\left ( 1 \right )$ yields
$$\delta S = \int dt \frac{dG}{dt} = G \Bigg|_{\text{(endpoints)}}\tag{4}$$
which is not necessarily zero since there was never any restriction imposed on $G$ to either vanish or have the same value at both integration bounds. Should the variation in $L$ be more accurately expressed as
$$\delta L = \frac{d}{dt}\delta G$$
since it was readily shown in $\left ( 2 \right )$ that the above vanishes when integrated? I can only reason the original form in $\left ( \text{3} \right )$ if I literally interpret $\delta L$ as $\delta L = L^\prime - L$ which I have a hard time justifying since $\frac{d G}{d t}$ is not an infinitesimal, on top of what was shown in $\left ( 4 \right )$.
For what it's worth, I believe the form of $\left ( \text{3} \right )$ due to how it's used immediately after in the book, I just can't seem to justify its form. The only other reference I could find that has the variation of $L$ in this form is this webpage which likewise doesn't explain the form of $\delta L$.