A goalkeeper cannot handle the ball if a teammate passes ball to them.
But can the goalkeeper take the ball into their hands if it is deflected from the leg? Also, can they take the ball in their hands from a teammate's throw-in?
Law 12 - Fouls and Misconduct, Section 2 - Indirect Free Kick outlines when a goalkeeper may not deliberately handle the ball1 in their own penalty area.
An indirect free kick is awarded if a goalkeeper, inside their penalty area, commits any of the following offences:
touches the ball with the hands after:
- releasing it and before it has touched another player
- it has been deliberately kicked to the goalkeeper by a team-mate
- receiving it directly from a throw-in taken by a team-mate
Your question begins with the assumption that the goalkeeper may not deliberately handle the ball after it is passed to them by a teammate.
This is not entirely correct, as the goalkeeper may in fact deliberately handle the ball after it is passed to them by a teammate. The only time they may not do so is when that pass is a kick (i.e. played with the foot). Headed passes, and passes with the chest, knee, thigh etc. are able to be handled legally.
A deflection from the leg is legal for the goalkeeper to handle for two reasons:
Finally, as seen above, deliberately handling the ball after receiving it directly from a throw-in taken by a teammate is also an indirect free kick offence.
It is also worth noting that the goalkeeper can never be cautioned or sent-off for handling the ball in their own penalty area - an indirect free kick is the maximum punishment. Law 12 - Fouls and Misconduct, Section 1 - Direct Free Kick:
Inside their penalty area, the goalkeeper cannot be guilty of a handling offence incurring a direct free kick or any related sanction but can be guilty of handling offences that incur an indirect free kick.
For completeness, when the ball is wholly outside the penalty area, the goalkeeper, like any other player, may not deliberately handle the ball as also noted in 12.1:
The goalkeeper has the same restrictions on handling the ball as any other player outside the penalty area.
1 - although the Law says "touches the ball with hands", not "deliberately handles", it is understood that to breach this Law, it must be a deliberate handling as implied in both the first sentence and final paragraph of Law 12, Section 1.
2 - the goalkeeper is taking a big risk by handling a ball that has been deliberately played to them from a teammate's lower leg, as the referee may (wrongly) believe it has come off the foot and award an indirect free kick to the opposing team.
Your two questions will be answered by the IFAB laws of the game Law 12 - Fouls and Misconduct.
Concerned rule: Goalkeeper touches the ball with his hands after it has been deliberately kicked to him by a team-mate.
Deliberate being the key word. Since, the ball deflected from the leg is not deliberate. He can take the ball.
Concerned rule: Goalkeeper touches the ball with his hands after he has received it directly from a throw-in taken by a team-mate.
In this case he can't take the ball.
Doing the things mentioned in rules will result in an indirect free kick for opposite team, when goalkeeper is inside his own penalty area.
Other sources: Quora
The goalkeeper cannot pick the ball up when it has been intentionally passed back to them, except via the head or chest. They also cannot drop the ball from their hands and then handle it again before anyone else has touched it.
Reference: Fouls and misconduct (thefa.com)
If the ball is deflected from the leg truly accidentally (and is seen as so by the official) then it is okay for the keeper to use his/her hands. A keeper can only use his/her hands to pick up the ball if the the ball has been passed by a teammate via their head or chest.
The keeper cannot pick up the ball directly off of a friendly throw in.