Skip to content

Updates to security definer functions#537

Open
cymed wants to merge 52 commits intoteksi:mainfrom
cymed:set-search_path-for-materialized-views
Open

Updates to security definer functions#537
cymed wants to merge 52 commits intoteksi:mainfrom
cymed:set-search_path-for-materialized-views

Conversation

@cymed
Copy link
Contributor

@cymed cymed commented Jan 20, 2025

General

  • Fix a bug
  • Maintenance / sustainability
  • Add Documentation

Describe your changes

Security definer functions are a potential point of attack and should therefore be managed carefully. When the search_path is not defined, any user that can CREATE on public can gain access to SUPERUSER privileges through them.

This PR

  • Removes the security definer flag where possible
  • Truncates tww_od on plugin test instead of drop/create (easier grants management)
  • revokes CREATE ON DATABASE from PUBLIC
  • revokes CONNECT ON DATABASE from PUBLIC
  • grants CONNECT ON DATABASE to tww_viewer
  • Alters the OWNER of the materialized views that are to be updated by tww_user to tww_user (for Postgres<=16)
  • -GRANTs MAINTAIN onthe materialized views to tww_user` (for Postgres>=17) see Postgres 17 docs

To-Do

  • When introducing database-specific roles, we must also tackle how to grant OWNER to the corresponding to the database specific role

Screenshots

Issue ticket number and link

Checklist before requesting a review

  • I have performed a self-review of my code
  • If it is a core feature, I have added thorough tests.
  • CI Tests are green
  • The documentation is up to date with the proposed change.
  • My work is ready for review

Checklist before merge

  • A review has been performed
  • Comments are resolved
  • Documentation is ready

Security definer functions are a potential point of attack and should therefore be managed carefully. This PR

- Removes the security definer flag where possible
- defines the search_path for security definer functions
- revokes create statements on public from PUBLIC
@cymed cymed added datamodel Concerns the datamodel security labels Jan 20, 2025
@cymed cymed self-assigned this Jan 20, 2025
@cymed cymed added the help wanted Extra attention is needed label Jan 20, 2025
@cymed
Copy link
Contributor Author

cymed commented Jan 20, 2025

Does anyone know why hstore is not loaded?

@ponceta ponceta closed this Jan 20, 2025
@ponceta ponceta reopened this Jan 20, 2025
@ponceta
Copy link
Member

ponceta commented Jan 20, 2025

db-1  | psql:/src/datamodel/scripts/../changelogs/0001/90_audit.sql:22: NOTICE:  table "logged_actions" does not exist, skipping

I don't understand how master is fine and your branch is not.

Do you need a rebase of your branch?

Looks like the hstore extension is not pushed even if it is pushed on other branches like : #528

@cymed cymed removed the help wanted Extra attention is needed label Jan 21, 2025
@cymed
Copy link
Contributor Author

cymed commented Jan 21, 2025

The errors stem from me not including public in the search_path for SECURITY DEFINER functions, but installing the extensions on public. What is best practice @3nids?

@ponceta
Copy link
Member

ponceta commented Jan 21, 2025

As discussed with @cymed, take a common look with @3nids on TEKSI support to check what is the best approach on that matter, knowing that hstore should probably be removed in favor of jsonb.

Other extensions like postgis and uuid-ossp will probably use this search path too.

Many applications still provide all their tables in the public schema for simplicity reasons, security can be put at another level (for example avoiding any direct connection to the database from an untrusted network) could be a "physical security".

Copy link
Contributor

@3nids 3nids left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The modifications of the changelogs are fine, good catch.

Regarding the addition of the 99_post_all.sql, I would think this does not belong here, but rather in the role script.

@3nids
Copy link
Contributor

3nids commented Jan 21, 2025

Regarding the revokation, I would simply remove this right from the roles we are actually creating.

@cymed
Copy link
Contributor Author

cymed commented Jan 21, 2025

Regarding the revokation, I would simply remove this right from the roles we are actually creating.

As long as CONNECT and CREATE ON SCHEMA public is open to PUBLIC, removing the rights from the roles we are actually creating changes nothing, as everyone is member of PUBLIC and therefore has access

@cymed
Copy link
Contributor Author

cymed commented Jan 21, 2025

What do you think about revoking connect from PUBLIC per default and granting CONNECT to tww_viewer?

@3nids
Copy link
Contributor

3nids commented Jan 21, 2025

Interesting, I'll try to grab some expertise on the topic and come back.

@cymed
Copy link
Contributor Author

cymed commented Jan 28, 2025

I suggest GRANTing CREATE ON DATABASE to tww_sysadmin

@ponceta ponceta added review Waiting for review feature feature regarding issue or new feature request labels Mar 3, 2025
@cymed

This comment was marked as off-topic.

@cymed cymed mentioned this pull request Jun 16, 2025
@cymed cymed closed this Jun 23, 2025
@sjib
Copy link
Contributor

sjib commented Sep 9, 2025

@cymed Is this something that should be included in the next Release? If yes what are the stumbling blocks?

@cymed
Copy link
Contributor Author

cymed commented Sep 9, 2025

The problem is the CI: Even after granting tww_user to postgres, refreshing the materialized view fails

@cymed
Copy link
Contributor Author

cymed commented Nov 21, 2025

did we alter the plugin CI tests @ponceta ?

@cymed cymed added this to the TEKSI Wastewater 2026.0.1 milestone Feb 25, 2026
@cymed
Copy link
Contributor Author

cymed commented Feb 25, 2026

@ponceta ready for review

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

datamodel Concerns the datamodel feature feature regarding issue or new feature request review Waiting for review security

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants