About 10 per cent of intermediate- and high-mass dwarf stars are observed to host a strong large-scale magnetic field at their surface, which is thought to be of fossil field origin. However, there are few inferences as to the magnetic field strength and geometry within the deep interiors of stars. Considering that massive stars harbour a convective core whilst on the main sequence, asteroseismology of gravity (g) modes is able to provide constraints on their core masses, yet it has so far not been used to probe the strength of their interior magnetic fields. Here we use asteroseismology to constrain an upper limit for the magnetic field strength in the near-core region of the pulsating and magnetic B star HD 43317, based on the expected interaction of a magnetic field and its g modes. We find a magnetic field strength of order $5 \times 10^5$ G is sufficient to suppress high-radial order g modes and reproduce the observed frequency spectrum of HD 43317, which contains only high-frequency g modes. This result is the first inference of the magnetic field strength inside a main-sequence star. Comment: Accepted to MNRAS Letters