Commit d0f97bd6 authored by Claudio Saavedra's avatar Claudio Saavedra

HSTS: Add API to retrieve a list of policies in an enforcer

Add soup_hsts_enforcer_get_policies(). This API is needed for WebKit's
website data manager to handle HSTS policies.
parent 6c06d600
Pipeline #101794 passed with stage
in 1 minute and 3 seconds
......@@ -1403,6 +1403,7 @@ soup_hsts_enforcer_has_valid_policy
soup_hsts_enforcer_set_policy
soup_hsts_enforcer_set_session_policy
soup_hsts_enforcer_get_domains
soup_hsts_enforcer_get_policies
<SUBSECTION>
SoupHSTSPolicy
soup_hsts_policy_new
......
......@@ -686,3 +686,41 @@ soup_hsts_enforcer_get_domains (SoupHSTSEnforcer *hsts_enforcer,
return domains;
}
static void
add_policy_to_list (gpointer key,
gpointer value,
gpointer data)
{
GList **policies = (GList **) data;
*policies = g_list_prepend (*policies, soup_hsts_policy_copy ((SoupHSTSPolicy*)value));
}
/**
* soup_hsts_enforcer_all_policies:
* @hsts_enforcer: a #SoupHSTSEnforcer
* @session_policies: whether to include session policies
*
* Gets a list with the policies in @enforcer.
*
* Returns: (element-type SoupHSTSPolicy) (transfer full): a newly
* allocated list of policies. Use g_list_free_full() and
* soup_hsts_policy_free() to free the list.
*
* Since: 2.68
*
**/
GList*
soup_hsts_enforcer_get_policies (SoupHSTSEnforcer *hsts_enforcer,
gboolean session_policies)
{
GList *policies = NULL;
g_return_val_if_fail (SOUP_IS_HSTS_ENFORCER (hsts_enforcer), NULL);
g_hash_table_foreach (hsts_enforcer->priv->host_policies, add_policy_to_list, &policies);
if (session_policies)
g_hash_table_foreach (hsts_enforcer->priv->session_policies, add_policy_to_list, &policies);
return policies;
}
......@@ -80,6 +80,10 @@ SOUP_AVAILABLE_IN_2_68
GList *soup_hsts_enforcer_get_domains (SoupHSTSEnforcer *hsts_enforcer,
gboolean session_policies);
SOUP_AVAILABLE_IN_2_68
GList *soup_hsts_enforcer_get_policies (SoupHSTSEnforcer *hsts_enforcer,
gboolean session_policies);
G_END_DECLS
#endif /* __SOUP_HSTS_ENFORCER_H__ */
......@@ -503,6 +503,50 @@ do_hsts_get_domains_test (void)
g_object_unref (enforcer);
}
static void
do_hsts_get_policies_test (void)
{
SoupHSTSEnforcer *enforcer = soup_hsts_enforcer_new ();
SoupHSTSPolicy *policy;
GList* policies;
g_assert_null (soup_hsts_enforcer_get_policies (enforcer, TRUE));
g_assert_null (soup_hsts_enforcer_get_policies (enforcer, FALSE));
policy = soup_hsts_policy_new ("gnome.org", 3600, FALSE);
g_assert_nonnull (policy);
soup_hsts_enforcer_set_policy (enforcer, policy);
soup_hsts_policy_free (policy);
policy = soup_hsts_policy_new_session_policy ("freedesktop.org", FALSE);
g_assert_nonnull (policy);
soup_hsts_enforcer_set_policy (enforcer, policy);
soup_hsts_policy_free (policy);
policies = soup_hsts_enforcer_get_policies (enforcer, TRUE);
g_assert_nonnull (policies);
g_assert_cmpint (g_list_length (policies), ==, 2);
g_list_free_full (policies, (GDestroyNotify)soup_hsts_policy_free);
policies = soup_hsts_enforcer_get_policies (enforcer, FALSE);
g_assert_nonnull (policies);
g_assert_cmpint (g_list_length (policies), ==, 1);
policy = (SoupHSTSPolicy*)policies->data;
g_assert_cmpstr (policy->domain, ==, "gnome.org");
g_list_free_full (policies, (GDestroyNotify)soup_hsts_policy_free);
policy = soup_hsts_policy_new ("gnome.org", SOUP_HSTS_POLICY_MAX_AGE_PAST, FALSE);
soup_hsts_enforcer_set_policy (enforcer, policy);
soup_hsts_policy_free (policy);
policies = soup_hsts_enforcer_get_policies (enforcer, TRUE);
g_assert_cmpint (g_list_length (policies), ==, 1);
policy = (SoupHSTSPolicy*)policies->data;
g_assert_cmpstr (policy->domain, ==, "freedesktop.org");
g_list_free_full (policies, (GDestroyNotify)soup_hsts_policy_free);
g_object_unref(enforcer);
}
int
main (int argc, char **argv)
{
......@@ -549,6 +593,7 @@ main (int argc, char **argv)
g_test_add_func ("/hsts/session-policy", do_hsts_session_policy_test);
g_test_add_func ("/hsts/idna-addresses", do_hsts_idna_addresses_test);
g_test_add_func ("/hsts/get-domains", do_hsts_get_domains_test);
g_test_add_func ("/hsts/get-policies", do_hsts_get_policies_test);
ret = g_test_run ();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment