Kenya's High Court has suspended some sections of the controversial new security law, which was enacted two weeks ago amid fierce opposition.
The court blocked eight clauses until a legal challenge mounted by the opposition and rights groups is heard.
MPs had exchanged blows while debating the law, which the opposition said would turn Kenya into a "police state".
The government says the measures are necessary following a wave of attacks by Somalia-based Islamist militants.
Some 500,000 Somali refugees live in Kenya and the government suspects some of them have links to al-Shabab militants.
High Court Judge George Odunga said: "We cannot limit the freedoms and inalienable rights in the pretext of fighting terrorism, which must be done in the confines of the law," AP news agency reports.
The court suspended measures which could jail journalists who "undermine investigations or security operations relating to terrorism", and limit the number of refugees in the country.
The security law prescribes long jail terms for anyone convicted of terror-related offences, in particular anyone found with weapons inside a place of worship.
Owners of buildings or institutions, or persons in charge of public places who fail to prevent entry of weapons into such areas can also be liable to 30 years in jail.
Terror suspects can be detained for 24 hours before being presented to court. They can then be held for up to 30 days. Earlier reports had suggested suspects could be held up for up to a year.
Analysis: Emmanuel Igunza, BBC News, Nairobi
The High Court ruling is a big blow to the government, which has constantly insisted that the laws are not only good, but badly needed to counter the threat of al-Shabab.
It took less than two weeks for the amendments to be drafted and approved in a chaotic parliamentary session. This showed how urgently the government wanted the law.
The court ruling is a victory for the opposition but it is only the first step - the real test will be once the case proper begins later this month.
The judiciary will be making a ruling of huge significance to the country's constitution, which was passed only four years ago, amid huge fanfare.
It was hailed for guaranteeing human rights and liberties, which critics of the security law say will now be curtailed.
Opposition leader Raila Odinga and his supporters cheered in court as the judge handed down his ruling.
The government would appeal against the ruling, interior ministry spokesman Mwenda Njoka told the BBC's Focus on Africa programme.
He downplayed the significance of the ruling, noting that only eight clauses had been suspended, out of more than 100.
The suspended sections include:
- Allowing security agents to intercept communications for purposes of "deterring, detecting and disrupting terrorism"
- Allowing security agents to seize any equipment that can be used to commit a crime
- Banning the publication or broadcasting of "insulting, threatening, or inciting material", images of dead or injured people "likely to cause fear" and information that undermines security operations, including on social media. Punishable by a fine of $55,000, a three-year jail term or both
- Limiting number of refugees and asylum-seekers to 150,000 - those applying for refugee status are not allowed to leave camps