When in 2018 Donald Trump suggested to Theresa May that she should sue rather than negotiate with the EU, he was rightly ridiculed.

However, although this advice was, characteristically, based on ignorant bluster, the idea that the Court of Justice could be used by those opposed to elements of the EU’s approach to Brexit is not fanciful.

Indeed, a number of EU law experts, including a former judge of the European Court, are increasingly concerned that the recent agreement between the UK and EU may contain elements that are illegal under EU law.

Their worry is that the new agreement includes provisions that establish a permanent framework for trade between the EU and Northern Ireland. It is not clear that an agreement under Article 50 of the Treaty can validly be used for this purpose.

Article 50 provides the Union with the power to conclude an agreement governing the conditions of departure of a Member State. It provides a flexible procedure that allows the EU Council to conclude such an agreement with the support of a qualified majority of Member States voting in the Council, followed by ratification by the European Parliament.

Under EU law it is difficult for anyone other than a state or EU institution to directly challenge the legality of EU measures

The power to conclude trade agreements with non-members is contained in different articles (Articles 207 and 217). Article 218 requires that trade agreements are concluded following a specific procedure in which the Commission and Parliament play different roles from those they play under Article 50.

In addition, depending on the content of the trade agreement, the agreement may have to be approved by all Member States rather than a weighted majority in the Council.

Indeed, in the course of the Brexit negotiations, the EU itself has stressed the limits on what can validly be included under agreements done under Article 50. Back in 2017 the Union argued that there could be no negotiations on the UK’s future trading relationship with the EU under Article 50. Those talks, the Union insisted, had to wait until after the UK had left the EU.

BREXIT: The Facts Read them here

The reasons for this stance were partly political (the Union wanted to avoid the UK using the money it owes the EU as leverage in trade negotiations) but also legal (an agreement under Article 50 cannot legally be the basis of a permanent trade agreement).

However, the problem was that the only way to ensure that the UK’s departure did not result in a hardening of the Irish border was to include provisions that set down rules around trade between the two parts of the island.

Thus, the deal concluded with Theresa May under Article 50 did include a backstop that established rules for trade between Northern Ireland and the EU that would come into operation if the negotiations on the future trading relationship between the UK and the EU failed.

Although this did mean that Article 50 was being used to cover matters of trade, the fact that these provisions took the form of an insurance policy provided some legal cover. As they were intended to be temporary and to be superseded by a future trade agreement, it could be argued that they were measures intended as a temporary fix for problems arising from the UK’s departure rather than provisions of a trade agreement.

However, the new agreement between the Johnson government and the EU turns the backstop into a “frontstop”. Under this agreement, the arrangements around Northern Ireland are not an insurance policy that applies only if the negotiations fail. They are intended to be the permanent features of Northern Ireland-EU trading arrangements.

That means that, in effect, the EU has sought to use the procedures of Article 50 to conclude a permanent trade agreement in relation to Northern Ireland when such an agreement should have been done using the different procedures of Article 218.

Normally when the Union concludes an agreement using the incorrect Article of the Treaty, that agreement will be annulled by the European Court of Justice.

The Brexit process has seen an endless series of false dawns followed by the frustrating emergence of new obstacles

Under EU law it is difficult for anyone other than a state or EU institution to directly challenge the legality of EU measures so a challenge may not immediately arise.

However, national measures implementing the deal can be challenged by individuals in national courts who could then refer the legality of the agreement to the Court of Justice under Article 267 of the Treaty. Given the degree of unhappiness amongst DUP supporters, such as challenge is almost inevitable.

A literal reading of the treaties suggests that the Court should find the agreement to be in breach of EU law. However, the Court would be aware of the political earthquake this would cause. It has in the past been willing to be flexible with the limits imposed by the Treaty.

In the highly sensitive cases around the Eurozone crisis, for example, the Court was willing to adopt very flexible interpretations of the Treaty to avoid finding that the European Central Bank’s bond-buying programme violated EU law.

The Brexit process has seen an endless series of false dawns followed by the frustrating emergence of new obstacles. With Boris Johnson apparently close to getting his agreement through Parliament, the Irish government is tantalizingly close to an astonishing diplomatic triumph.

However, given the apparent disregard of the limits of Article 50 involved in concluding a permanent trade deal for Northern Ireland in a departure agreement, there is a significant risk of yet another dramatic twist in which the long-awaited agreement is found to violate EU law thus sending wearing politicians back to the negotiating table once more.

Ronan McCrea is professor of constitutional and European law at University College London