Labour will try to attach a customs union and second referendum to Boris Johnson’s Brexit deal, setting up a parliamentary battle over the legislation this week.

Keir Starmer, the shadow Brexit secretary, suggested Labour could vote for Johnson’s deal if a second referendum was added to the withdrawal agreement bill, despite the party’s fundamental objections to the terms of the UK’s proposed departure from the EU.

He went much further than his party leader, Jeremy Corbyn, who has previously said Labour could not vote for Johnson’s deal even with a second referendum attached.

Johnson will try to get MPs to approve the principles of his Brexit deal on Monday and then move to votes on related legislation on Tuesday. The prime minister believes he has enough support for his deal, but it is possible the bill will be amended to add changes suggested by the opposition.

Starmer said Johnson’s deal had a “trapdoor to no deal” contained within it, but he suggested Labour could back it if it meant achieving a second referendum and the option of remaining in the EU.

Speaking to BBC One’s The Andrew Marr Show, he said: “We will see what that looks like but it makes sense to say that by whatever means we get that referendum. The spirit of this is clear. We offered this to Theresa May. We said: we don’t think your deal is very good but if it’s up against the safeguard of being able to remain then we will allow it to proceed in that way.”

He added: “The position we have adopted is whatever the outcome, whether it’s Boris Johnson’s bad deal or a better one which could be secured, it has got to go to a referendum up against remain.”

Starmer said Labour would have to look at the specific circumstances before deciding how to vote and the party would try to amend Johnson’s deal to prevent a no-deal departure and allow the UK to have a closer relationship with the EU in future.

“We will put down amendments to make sure the future destination is a close economic relationship with the EU,” he said. “We’ve been arguing for a very long time for a customs union with the EU and single market alignment.”

Starmer added: “There are other amendments that are important because there is a trapdoor to no deal at the end of 2020 that we need to deal with and close and we can do that in the legislation. And of course we need an amendment saying whatever deal gets through should be subject to a referendum.”

John McDonnell, the shadow chancellor, confirmed to Sky News’s Sophy Ridge that Labour would back a second referendum amendment, probably proposed from the backbenches. He said Johnson “may well be in contempt of parliament or the courts” for having sent an additional letter to the EU arguing against a Brexit extension.

“Not signing the [extension request] letter? He’s behaving a bit like a spoiled brat,” he said.

Dominic Raab, the foreign secretary, was optimistic that Johnson had enough support to get his deal passed.

“We seem to have the numbers in the House of Commons. Why hasn’t parliament pushed this through? That’s what we’re going to do this week,” he told the Marr show. “We’ve got a deal. Why would we have a second referendum?”

Michael Gove, the Cabinet Office minister in charge of no-deal planning, insisted the UK would be leaving on 31 October and an extension would not be necessary.