Anti-Brexit campaigners have launched a legal bid to stop the UK government from passing its proposed EU withdrawal agreement.
They believe it contravenes legislation preventing Northern Ireland forming part of a separate customs territory to Great Britain.
MPs are due to debate the agreement at a special Commons sitting on Saturday.
Campaigner Jo Maugham QC confirmed the petition had been lodged at Scotland's highest civil court.
He expects the legal challenge to be heard on Friday.
The move followed the earlier announcement by Prime Minister Boris Johnson and European Commission President Jean-Claude Juncker that the two sides had come to an agreement on a "great new deal" for Brexit, ahead of a crucial EU summit in Brussels.
Mr Johnson tweeted that the new withdrawal agreement "takes back control" and removes the "anti-democratic" Irish backstop, although the Tories' Northern Irish allies in the DUP have indicated they cannot support the deal.
'Protecting the integrity'
EU chief negotiator Michel Barnier revealed the deal means Northern Ireland will remain in the UK's customs territory but that the island of Ireland will be aligned to some EU rules, meaning goods must be checked on entry to the island rather than border checks between Northern Ireland and the Republic.
Mr Barnier said: "We are fully committed to protecting peace, to protect stability on the island of Ireland," adding that a hard border would be avoided while protecting the integrity of the single market.
We have lodged our petition in the section 55 case which can be seen (in an unsigned form) here. https://t.co/9uW9r5crHh— Jo Maugham QC (@JolyonMaugham) October 17, 2019
Explaining his legal objections to the agreement, Mr Maugham said it contravenes legislation stating it is "unlawful for Her Majesty's Government to enter into arrangements under which Northern Ireland forms part of a separate customs territory to Great Britain".
Mr Maugham claims that if the court finds the proposed agreement is unlawful the government would be obliged to request an extension to Brexit negotiations, under the terms of the Benn Act.
That legislation stipulates the prime minister must ask the EU for a delay if Parliament does not agree a deal by Saturday.
Under the current law, Section 55 of the Taxation (Cross-border Trade) Act 2018 prevents Northern Ireland from having different customs rules than the rest of the UK.
This is purportedly to "uphold the constitutional integrity of the United Kingdom and safeguard the Union for the future", according to a government briefing on the Bill.
After details emerged of the Brexit deal, Mr Maugham said: "We do not understand how the government might have come to negotiate a withdrawal agreement in terms that breach amendments tabled by its own European Research Group.
"Unless and until Section 55 is repealed by the UK Parliament, it is simply not open, as a matter of law, for the United Kingdom to enter into such an agreement.
"If the proposed withdrawal agreement is unlawful, the government will be obliged to request an extension as mandated by the Benn Act and in accordance with undertakings given to the Court of Session in Vince, Maugham, Cherry v Boris Johnson."
Mr Maugham was involved in the legal fight against Boris Johnson's decision to suspend parliament for five weeks - a move that was ruled unlawful by the Supreme Court.
He was also part of the team that wants the Court of Session to rule on whether it could use a special power called nobile officium to sign letter requesting an extension to the Brexit process on behalf of the government.