The are two Persian brothers, Ali Baba and Cassim. Cassim is married to a rich wife, while Ali Baba works hard to support himself and his own family.
One day, Ali Baba is cutting firewood in the forest, cursing his poor fortune, he caught sight of an approaching band of thieves, saddled with loot. Frightened, he hides in a tree, and watches as the Captain of the Thieves opens a magical portal in a boulder by reciting the words "Open Sesame!" (The actual phrase differs depending on translation.) They entered, and the portal closes behind them. Ali Baba assumes they are hiding their goods in the cavern.
After they leave the cavern, no longer saddled with their loot, the Captain uses the same words to close the portal before the band departs. Once they disappear, Ali Baba raids the cave himself, and discovers it packed wall to wall with valuables. He collects as much gold as his mules can carry, and brings it back home to his wife. It is enough wealth to last a lifetime.
Ali Baba's wife borrows a scale from Cassim's wife to measure the gold before Ali Baba hides it, and the latter woman uses a trick (she puts wax on the scale to capture the gold) to determine what they are measuring.
Cassim and his wife insist Ali Baba tell them of his secret, and he complies. Cassim sets out for the cavern himself, and is equally amazed. Unfortunately, he forgets the magic words, so is stuck inside the cavern. When the 40 thieves arrive again, they kill him, cut his body into quarters, and hang him up in the cave to scare anyone else who may trespass.
Concerned when Cassim does not return, Ali Baba investigates to discover the corpse, which he takes home. He works with Cassim's wife - promising to marry her himself after a period of mourning - and Cassim's maid, Morgiana, to bury Cassim without drawing any attention to the situation. To this end, Morgiana blindfolds a cobbler named Baba Mustapha, and brings him to a non-descript room so he can sew Cassim's body back together without recognizing the house or family.
Meanwhile, the thieves find the body missing, and realize someone else knows about the cave. The Captain dispatches one of them to infiltrate the town and listen for news of some strange deaths. That thief eventually meets Baba Mustapha, and learns that he had recently stitched up a body. The thief blindfolds the cobbler, who is able to reproduce his path to the house, which the thief then marks with white chalk so he can find it again with the rest of the band.
Morgiana, however, spots the mark and senses that something is wrong. To foil the plan, she marks a handful of neighboring houses with chalk as well.
When the thieves return to find they have been duped, the Captain kills the man who had found the house, and sends another thief to find it. A blind-folded Baba Mustapha again leads to Ali Baba's house, and the thief marks it this time with red chalk. However, Morgiana catches on and does the same thing to other houses.
The robbers are confused again that night, and the Captain murders the second man before deciding to handle the task himself. He repeats the same process with Baba Mustapha, but this time memorizes the location himself rather than using chalk.
Then he returns to the house disguised as an oil merchant in need of lodging for the night. The other thieves are hidden in oil jars carried by mules; only one jar actually has oil in it. They plan to sneak out once Ali Baba is asleep, and kill him.
Once again, Morgiana is not fooled; when she goes out to borrow some oil, she discovers the men in the jars, and boils oil from the final jar, killing them with it. The Captain investigates later that night, and escapes before he can be killed.
Morgiana tells Ali Baba what happened, and they bury the corpses. In gratitude, Ali Baba grants Morgiana her freedom. (In some versions, this does not happen until later.)
The danger is not over yet, though; the captain wants revenge. He disguises himself as a merchant and befriends Ali Baba's son so that he is invited for dinner. Morgiana senses something fishy, and hatches a plan. She and another servant perform for the men; during her dance, she stabs him.
When Ali Baba learns the truth, he is so grateful that he gives Morgiana his son's hand in marriage. Ali Baba is now the only living soul who knows the cave's secret words, so he passes it along only to his sons and they live happily in prosperity.
This story was also added to the collection by Antoine Galland in the 18th century, and remains one of the most popular of the tales. It is widely retold, and frequently performed for children (with the more violent parts amended, of course). Many people do not realize the popular, lighthearted phrase "Open Sesame" comes from this story, and that it connects to a tale a lot more grave and violent than we may have expected. Regardless, though, "Ali Baba and the Forty Thieves" is undoubtedly a classic tale.
Morgiana's role is the most interesting one to examine in this story. Socially, Morgiana is effectively invisible - she is not only a slave but also a woman. Women in these stories often cause messes through their foolishness; Morgiana, however, is the stark opposite. In fact, she is story's true hero, not only because of her cleverness but also because of her loyalty. She is strong, resourceful, and calm in the face of danger, performing all the clever feats we would expect a male hero to.
Analyzed in the context of the frame story, the characterization provides an interesting comment on Scheherazade's mission. The narrative involves a woman earning respect through her natural abilities, in the same way Scheherazade wants to do for herself and her peers through her storytelling. Even more intriguing is the performative aspect of the story, almost cinematic in the way Morgiana integrates a sensual dance into the scheme that finally solves the story's central conflict. Scheherazade, too, is attempting to bring stasis to a troubling situation through art.
On the other hand from Morgiana: Ali Baba, the namesake of this story, is an utter fool. His first minor slip comes in inadvertently allowing his brother and his wife to discover his possession of gold, which leads to the whole issue in the first place. (Of course, the story presents his flaw as trusting his wife, an interesting contradiction.) Worse, he is fooled twice by the robber captain, in two different disguises. Both times, Ali Baba ignorantly invites the impostor into his house, taking no precautions and completely missing the warning signs that Morgiana catches. She is left to clean up the messes he makes, and does so masterfully. Though we would expect Ali Baba to be our hero, he does not act in such a way, but is instead reliant on his slave's vigilance.
This story, just like "Aladdin", offers a great example of the common Arabian Nights motif, whereby a poor man rises to riches by means of a lucky break. Ali Baba is simply in the right place at the right time to learn the thieves' secret; he discovers the riches because of fate, not because of anyone else's actions. This supports the idea that even the poorest of men can come across good fortune. Of course, the story then suggests that one must capitalize on that good fortune through vigilance and cleverness.
Once again, we finish with a happy ending. It is interesting to note that Ali Baba is rewarded with prosperity and happiness for doing exactly what made the captain and the forty thieves reprehensible in the first place: stealing. Ali Baba consistently steals from the cave, and allows the greed of others to threaten his safety. At first, the story suggests he will suffer because of greed - but then cleverness saves the day.
An interesting parallel can be drawn between Ali Baba and the robber captain; are they really that different? In this story, the line between hero and villain can become blurred if you look closely enough, or unless you realize you ought to be looking not at the male namesake but at the slave girl in the background.