Aziz Sattar plays Ali Baba, a poor man who struggles to make ends meet. He keeps sending his wife to his brother's house to borrow flour so they can eat, but S. Shamsudin's Kassim Baba is notoriously stingy and needles his brother, while K. Fatimah as Norsiah—Kassim's wife—suffers at his snide remarks. When Norsiah comes to ask for flour for the umpteenth time, Kassim loses his temper and sends her home in tears. She blames Ali Baba for their plight, accusing him of not finding work or contributing enough.
One day, while Ali Baba is out gathering firewood, he stumbles upon a band of forty thieves marching through the woods, loaded with loot. He hides in a tree and watches their chief, P. Ramlee, stand before a cave and chant a verse that seems nonsensical—a snippet of a Javanese poem—but that seemingly opens the entrance to the cave. When the thieves depart, Ali Baba slips down, repeats the magical words to enter the cave himself, and discovers a treasure yard beyond his wildest dreams, though he takes only a single box of gold coins.
With the gold, Ali Baba is able to repay Kassim and lift his family into clearer comfort. Kassim grows curious about the sudden windfall and pesters Ali Baba to reveal how he became rich. Ali Baba eventually relents and tells Kassim about the cave and the magical verse to open it, but refuses to share the word that closes the cave or any details about the thieves who guard it. Kassim, driven by greed, hurries off to find the cave himself.
Kassim, consumed by greed, attempts to seize everything inside the cave. The thieves return, discover the door is open, and quickly seal it again. Kassim forgets the reopening chant, is trapped, and is eventually killed by the vengeful intruders. When Kassim does not come home, Ali Baba sneaks to the cave and finds his brother’s remains; he has Kassim sewn back together through the help of the town cobbler, Leng Hussein as Apek, bribed to perform the macabre task.
The thieves catch wind of Apek’s involvement and decide to raid Ali Baba’s house, which is now under his protection. The plan fails twice thanks to the swift intervention of Marjina, Sarimah Ahmad as Marjina, Ali Baba’s newest servant. The enraged thief leader tracks down the house and disguises himself as an oil merchant while his crew hides inside oil jars in the courtyard. Marjina discovers the hidden thieves and, with the help of Ali Baba’s wife and his widowed sister-in-law, pours boiling oil into every jar. After the thieves are defeated, Marjina confronts and kills the leader himself, and Ali Baba, grateful for her loyalty, grants her freedom.