The government acted unlawfully when it awarded a £560,000 contract to a firm run by former colleagues of Michael Gove and the PM's adviser Dominic Cummings, the High Court has ruled.
Mr Gove, the Cabinet Office minister, denied any favouritism had been shown to market research agency Public First.
But the judge said a failure to consider other firms could be seen as suggesting a "real danger" of bias.
Public First looked into public understanding of coronavirus.
Campaigners took legal action against Mr Gove over the Cabinet Office's decision to use the company following the start of the pandemic last year.
They also questioned the involvement of Mr Cummings, who worked in Downing Street until he quit as the prime minister's chief adviser last autumn.
Lawyers for the Good Law Project argued that Mr Cummings wanted focus group and communications support services work to be given to Public First, whose founding partners were Rachel Wolf and James Frayne.
In 2011, Mr Frayne became director of communications at the Department of Education, where he worked alongside Mr Cummings, who was a special adviser to Mr Gove, the then education secretary.
Ms Wolf formerly worked as an adviser to Mr Gove and for Mr Cummings, and co-wrote the Conservatives' 2019 general election manifesto.
The government and Mr Cummings disputed the Good Law Project's claim.
Delivering her ruling, Mrs Justice O'Farrell said: "The claimant is entitled to a declaration that the decision of 5 June 2020 to award the contract to Public First gave rise to apparent bias and was unlawful."
She said: "The defendant's failure to consider any other research agency, by reference to experience, expertise, availability or capacity, would lead a fair-minded and informed observer to conclude that there was a real possibility, or a real danger, that the decision-maker was biased."
But she also said: "The fair-minded and informed observer would have appreciated that there was an urgent need or research through focus groups on effective communications in response to the Covid-19 crisis and that those research services were required immediately."
Mr Cummings had been "uniquely placed, given his experience and expertise, to form a rapid view on which organisation might best be able to deliver those urgent requirements", the judge said.
The High Court rejected two other claims made against the government by the Good Law Project.
The first was that the "direct" award of the contract to Public First was "unnecessary" because there were other firms who could potentially have done the work.
The second was that giving it a six-month contract was "disproportionate" for the government's "immediate" needs - and that a shorter arrangement could have been followed by a "competitive process" for a longer contract.
'Not the normal world'
In his witness statement, Mr Cummings told the court: "The country was facing an unprecedented national emergency. Thousands of lives were at stake, hundreds of billions of pounds were at stake.
"The extra money spent on doubling the focus groups and polling was essentially irrelevant in those circumstances.
"A few people did raise the question of how we could justify this as value for money. I responded that this was not the normal world, we were in a once-a-century pandemic and many thousands of pounds here was trivial if it helped us save lives and minimise economic destruction."
After the High Court ruling, he tweeted: "When it became clear [the Department of Health, Public Health England and the Cabinet Office] weren't gathering vital data, I acted urgently. This data immediately helped key decisions and saved lives."
But Good Law Project director Jo Maugham said: "This is not government for the public good - it is government for the good of friends of the Conservative Party."
Labour's deputy leader Angela Rayner accused the government of acting "immorally and unethically".
"In the middle of a deadly pandemic not only were those at the heart of government giving out taxpayers' money to their friends and associates, they have wasted even more trying to cover it up," she added.
However, a Public First spokesman said the company was "deeply proud of the work we did in the early stages of the pandemic, which helped save lives".
He added: "The judge rejected most of the Good Law Project's claims, not finding actual bias in the awarding of this work, nor any problems with the pace or scale of the award."
And the Cabinet Office said: "The judgement makes clear that there was no suggestion of actual bias and that the decision to award the contract was not due to any personal or professional connections."