President Hassan Sheikh Mohamud said the law voided the "illegal" pact giving landlocked Ethiopia long-sought access to the Red Sea through Somaliland, a separatist northwestern region over which Somalia exercises little real authority.
The passage of the bill on Saturday evening "is an illustration of our commitment to safeguard our unity, sovereignty & territorial integrity as per international law," the president wrote on X, formerly Twitter.
The central government in Mogadishu has vowed to strenuously oppose by any legal means the port agreement between regional power Ethiopia and Somaliland, whose 1991 claim of independence from Somalia is not recognised internationally.
Somalia called the surprise pact signed on Monday an act of "aggression" and a violation of its sovereignty, and appealed for international support.
It staunchly opposes Somaliland's claim to independence but in reality has little say over the affairs of the de facto state, which has its own government, security forces and currency and a long coastline on one of the world's busiest shipping routes.
Somaliland's leadership has said Ethiopia would "formally recognise the Republic of Somaliland" under the deal, but this has not been confirmed by the government in Addis Ababa.
The agreement has raised tensions in the Horn of Africa and the African Union, United States, European Union and the Arab League have called for calm and for Somalia's sovereignty to be respected.
The memorandum of understanding gives Ethiopia access to commercial maritime services and a military base, with Somaliland leasing it 20 kilometers (12 miles) of coastline for 50 years.
Ethiopia, the second most populous country in Africa and one of the biggest landlocked nations in the world, was cut off from the coast after Eritrea seceded and declared independence in 1993 following a three-decade war.
Addis Ababa had maintained access to a port in Eritrea until the two countries went to war in 1998-2000, and since then Ethiopia funnels most of its trade through Djibouti.
NAIROBI - -Somalia's president has signed a law "nullifying" a contentious agreement between Ethiopia and Somaliland in a largely symbolic gesture of his government's displeasure over the deal to grant port access.
Forum