[libsoup/hsts-policies-api: 2/2] HSTS: Add API to retrieve all policies in an enforcer



commit 9a8ee6fe333c2cd85504e9cf57168ec11fb711e4
Author: Claudio Saavedra <csaavedra igalia com>
Date:   Fri Aug 2 18:56:21 2019 +0300

    HSTS: Add API to retrieve all policies in an enforcer
    
    Add soup_hsts_enforcer_all_policies(). This API is needed for WebKit's
    website data manager to handle HSTS policies.

 docs/reference/libsoup-2.4-sections.txt |  1 +
 libsoup/soup-hsts-enforcer.c            | 37 +++++++++++++++++++++++++++++++++
 libsoup/soup-hsts-enforcer.h            |  3 +++
 tests/hsts-test.c                       | 28 +++++++++++++++++++++++++
 4 files changed, 69 insertions(+)
---
diff --git a/docs/reference/libsoup-2.4-sections.txt b/docs/reference/libsoup-2.4-sections.txt
index be87fb9d..51d02d05 100644
--- a/docs/reference/libsoup-2.4-sections.txt
+++ b/docs/reference/libsoup-2.4-sections.txt
@@ -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_all_policies
 <SUBSECTION>
 SoupHSTSPolicy
 soup_hsts_policy_new
diff --git a/libsoup/soup-hsts-enforcer.c b/libsoup/soup-hsts-enforcer.c
index 0d99fad2..61dd8446 100644
--- a/libsoup/soup-hsts-enforcer.c
+++ b/libsoup/soup-hsts-enforcer.c
@@ -685,3 +685,40 @@ 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
+ *
+ * Gets a list with the policies in @enforcer. Note that you own the
+ * returned policies and that the list includes both session and
+ * non-session policies.
+ *
+ * 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_all_policies (SoupHSTSEnforcer *hsts_enforcer)
+{
+       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);
+       g_hash_table_foreach (hsts_enforcer->priv->session_policies, add_policy_to_list, &policies);
+
+       return policies;
+}
diff --git a/libsoup/soup-hsts-enforcer.h b/libsoup/soup-hsts-enforcer.h
index 5bbe5049..d1b1517c 100644
--- a/libsoup/soup-hsts-enforcer.h
+++ b/libsoup/soup-hsts-enforcer.h
@@ -79,6 +79,9 @@ void            soup_hsts_enforcer_set_policy                    (SoupHSTSEnforcer 
*hsts_enforcer,
 SOUP_AVAILABLE_IN_2_68
 GList            *soup_hsts_enforcer_get_domains                   (SoupHSTSEnforcer *hsts_enforcer);
 
+SOUP_AVAILABLE_IN_2_68
+GList            *soup_hsts_enforcer_all_policies                  (SoupHSTSEnforcer *hsts_enforcer);
+
 G_END_DECLS
 
 #endif /* __SOUP_HSTS_ENFORCER_H__ */
diff --git a/tests/hsts-test.c b/tests/hsts-test.c
index de5e85d1..92f2bc90 100644
--- a/tests/hsts-test.c
+++ b/tests/hsts-test.c
@@ -487,6 +487,33 @@ 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 = soup_hsts_policy_new ("gnome.org", 3600, FALSE);
+       GList* policies;
+
+       g_assert_nonnull (policy);
+       g_assert_null (soup_hsts_enforcer_get_domains (enforcer));
+       soup_hsts_enforcer_set_policy (enforcer, policy);
+       soup_hsts_policy_free (policy);
+
+       policies = soup_hsts_enforcer_all_policies (enforcer);
+       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);
+
+       g_assert_null (soup_hsts_enforcer_all_policies (enforcer));
+       g_object_unref(enforcer);
+}
+
 int
 main (int argc, char **argv)
 {
@@ -533,6 +560,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 ();
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]