We use a boundary perturbation technique based on the calculus of moving surfaces to compute the gravitational potential for near-spherical geometries with piecewise constant densities. The perturbation analysis is carried out to third order in the small parameter. The presented technique can be adapted to a broad range of potential problems including geometries with variable densities and surface density distributions that arise in electrostatics. The technique is applicable to arbitrary small perturbations of a spherically symmetric configuration and, in principle, to arbitrary initial domains. However, the Laplace equation for an arbitrary domain can usually be solved only numerically. We therefore concentrate on spherical domains which yield a number of geophysical applications. As an illustration, we apply our analysis to the case of a near spherical triaxial ellipsoid and show that third order estimates for ellipticities such as that of the Earth are accurate to ten digits. We include an appendix that contains a concise, but complete, exposition of the tensor calculus of moving interfaces.