Skip to content

Conversation

@IvanVas
Copy link

@IvanVas IvanVas commented May 29, 2025

No description provided.

@pgr0ss
Copy link
Owner

pgr0ss commented May 29, 2025

This is a cool idea. I'm wondering about the best way to include it, though. We can leave it as a comment, but then there's no guarantee it's correct. We could uncomment it and make it work with the default postgres from docker compose. But then it's a bit more work to port the sql to your own project.

Just brainstorming, but one idea would be to put it in its own file. That way, we can run it and write tests against it. But it would still be optional for someone using pgledger.sql in their own project.

@IvanVas
Copy link
Author

IvanVas commented May 30, 2025

no guarantee it's correct

True. I've tested it manually, but that's a one-time.

but one idea would be to put it in its own file

Yup, I like it! I was thinking about the same.
Smth like "security-hardening.sql", parameterised perhaps?

@pgr0ss
Copy link
Owner

pgr0ss commented Jun 1, 2025

Smth like "security-hardening.sql", parameterised perhaps?

Sure, let's try it and see how it goes. Thanks!

Copy link

@Godwottery Godwottery left a comment

Choose a reason for hiding this comment

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

Generally good idea. Some minor comments.

-- $$ LANGUAGE plpgsql;
-- CREATE TRIGGER pgledger_entries_nochange
-- BEFORE UPDATE OR DELETE ON pgledger_entries
-- FOR EACH ROW EXECUTE FUNCTION prevent_mutation_on_entries();

Choose a reason for hiding this comment

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

Is there any particular reason why you choose the non-default "FOR EACH ROW" here? In my opinion it should be enough to fire it once per statement. If you try to UPDATE multiple rows in a single statement, you will unnecessarily raise many exceptions instead of only one.

CREATE INDEX ON pgledger_entries(transfer_id);

-- It's recommended to make pgledger_entries an append-only table
-- REVOKE UPDATE, DELETE, INSERT ON pgledger_entries FROM PUBLIC;

Choose a reason for hiding this comment

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

Minor nit-pick only. For a full hardening, these should never be granted to PUBLIC. They should only be granted as necessary.

-- CREATE OR REPLACE FUNCTION prevent_mutation_on_entries()
-- RETURNS trigger AS $$
-- BEGIN
-- RAISE EXCEPTION

Choose a reason for hiding this comment

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

By always raising the exception, you prevent the option of having an admin that can update the row. Perhaps it would be useful to catch a missing permission error, and then raise this exception.

Copy link

Choose a reason for hiding this comment

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

Nobody should ever update any entries. If the admin wants to make a change, they must append a new entry instead

@aabbdev
Copy link

aabbdev commented Jul 7, 2025

It's good idea and mandatory for a ledger

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants