The ex post facto clause in the constitution (Article 1, Section 9, Clause 3) has been interpreted by the Supreme Court to only apply to legislation that would penalize past behavior in a criminal sense. As taxes aren't a criminal punishment, their retroactive application doesn't fall under this definition. The Congressional Research Service has published an explainer entitled Retroactive Legislation: A Primer for Congress which states:
While the Ex Post Facto Clause on its face might appear to bar all
retroactive legislation, courts have applied the Clause only to penal
laws. In Calder v. Bull, 3 U.S. 386 (1798), Justice Samuel
Chase stated that the Clause applies to any law that renders criminal an
action that was legal when it was taken, aggravates the severity of a
crime, increases the resulting punishment, or alters the applicable
rules of evidence after the crime was committed. In Johannessen v.
United States, 225 U.S. 227 (1912), the Supreme Court declared that the
Ex Post Facto Clause’s “prohibition is confined to laws respecting
criminal punishments, and has no relation to retrospective legislation
of any other description.”
It goes on to note that retroactive tax legislation is not at all unusual:
Congress routinely passes tax laws that apply to the full calendar
year in which they are enacted, and has at times passed tax laws
applicable to an entire calendar year that ended before enactment. The
courts have upheld those laws against due process challenges,
expressing approval of statutes that establish “only a modest period of
retroactivity .. confined to short and limited periods required by
the practicalities of producing national legislation.” United States v.
Carlton, 512 U.S. 26 (1994).
Retroactive tax laws have been struck down by the Supreme Court in the past, but only in cases where tax increases would give "civil form to that which is essentially criminal". The example the explainer gives is Burgess v. Salmon, 97 U.S. 381 (1878), in which legislation sought to increase tobacco stamp tax while also imposing criminal penalties on the sale of tobacco without the correct stamp. This was found to violate the ex post facto clause.