Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: DebugBar block by CSP #8411

Merged
merged 6 commits into from
Feb 12, 2024
Merged

fix: DebugBar block by CSP #8411

merged 6 commits into from
Feb 12, 2024

Conversation

YapsBridging
Copy link
Contributor

@YapsBridging YapsBridging commented Jan 12, 2024

Description
Fixes #8405

  • Remove inline style : javascrip:void ,onclick,display none
  • Update CSS for show tab
  • Add switchClass func to switch class CSS
  • Add eventListener

BEFORE
Screenshot 2024-01-11 202320

AFTER
Screenshot 2024-01-11 110823

Steps to Reproduce
set CSPEnabled = true,
set ENVIRONMENT to development

Checklist:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

@YapsBridging YapsBridging changed the title Fix: [DebugBar] block by CSP #8405 fix: DebugBar block by CSP Jan 12, 2024
@kenjis kenjis added the bug Verified issues on the current code behavior or pull requests that will fix them label Jan 12, 2024
Copy link
Member

@michalsn michalsn left a comment

Choose a reason for hiding this comment

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

Look nice, but please fix the styling of the CSS and JS files.

admin/css/debug-toolbar/toolbar.scss Outdated Show resolved Hide resolved
admin/css/debug-toolbar/toolbar.scss Outdated Show resolved Hide resolved
admin/css/debug-toolbar/toolbar.scss Outdated Show resolved Hide resolved
admin/css/debug-toolbar/toolbar.scss Outdated Show resolved Hide resolved
system/Debug/Toolbar/Views/toolbar.js Outdated Show resolved Hide resolved
system/Debug/Toolbar/Views/toolbar.js Outdated Show resolved Hide resolved
system/Debug/Toolbar/Views/toolbar.js Outdated Show resolved Hide resolved
system/Debug/Toolbar/Views/toolbar.js Outdated Show resolved Hide resolved
system/Debug/Toolbar/Views/toolbar.js Outdated Show resolved Hide resolved
system/Debug/Toolbar/Views/toolbar.js Outdated Show resolved Hide resolved
@YapsBridging
Copy link
Contributor Author

Thank you @michalsn for the explanation,
previously I was confused about what was wrong,...I'll try it

@kenjis
Copy link
Member

kenjis commented Jan 22, 2024

We don't use merge commits in PR branch.
If you can use git, please use git rebase.
See https://github.com/codeigniter4/CodeIgniter4/blob/develop/contributing/workflow.md#updating-your-branch

@@ -309,7 +331,8 @@

<!-- SCRIPTS -->

<script>
<script {csp-script-nonce}>
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
<script {csp-script-nonce}>
<script <?= csp_script_nonce() ?>>

Copy link
Contributor Author

Choose a reason for hiding this comment

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

thanks @kenjis , but why dont use {csp-script-nonce} ?
like line 12 welcome_message.php, use {csp-style-nonce}

Copy link
Member

Choose a reason for hiding this comment

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

Okay, you are correct. The line 12 uses <style {csp-style-nonce}>.
So using it here is okay.

I personally think we don't need to use {csp-style-nonce} if we can use the csp_script_nonce() function. It is a CI4 global function and we can always use it in PHP scripts.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i think so too..
if csp is enable ,it must following change the placeholder tagNonce , cause If not change and if an attacker injects a string like <script {csp-script-nonce}> it might become the real nonce attribute with functionality.
https://www.codeigniter.com/user_guide/outgoing/csp.html

so the best we can use csp_script_nonce()

@kenjis
Copy link
Member

kenjis commented Jan 22, 2024

@YapsBridging
Copy link
Contributor Author

We don't use merge commits in PR branch. If you can use git, please use git rebase. See https://github.com/codeigniter4/CodeIgniter4/blob/develop/contributing/workflow.md#updating-your-branch

thanks @kenjis , This is my first time making a PR,
sorry if there are any mistakes,
so what can I do to fix this with a rebase? can you tell me the steps?

I have read the link above, but I don't understand how to fix it, can you help me

and sorry my bad english.

@kenjis
Copy link
Member

kenjis commented Jan 23, 2024

@YapsBridging Welcome to CodeIgniter4!

First, I recommend you set up your git to sign all commits (in this repository).

To configure your Git client to sign commits by default for a local repository, in Git versions 2.0.0 and above, run git config commit.gpgsign true. To sign all commits by default in any local repository on your computer, run git config --global commit.gpgsign true.
https://docs.github.com/en/authentication/managing-commit-signature-verification/signing-commits

See also https://github.com/codeigniter4/CodeIgniter4/blob/develop/contributing/signing.md

@kenjis
Copy link
Member

kenjis commented Jan 23, 2024

Set upstream repository:

git remote add upstream git@github.com:codeigniter4/CodeIgniter4.git

Fetch the latest upstream:

git fetch upstream

Rebase your PR branch:

git switch csp
git rebase upstream/develop

Force push:

git push --force-with-lease origin HEAD

@YapsBridging
Copy link
Contributor Author

Set upstream repository:

git remote add upstream git@github.com:codeigniter4/CodeIgniter4.git

Fetch the latest upstream:

git fetch upstream

Rebase your PR branch:

git switch csp
git rebase upstream/develop

Force push:

git push --force-with-lease origin HEAD

thank @kenjis
I have tried the steps above, and I got a big problem..haha, sorry im a newbe..

so can i create a new PR with new Branch and close/delete this one?

@kenjis
Copy link
Member

kenjis commented Jan 24, 2024

Oh, you don't use git. Creating a new PR is no problem!

But if you could use git, you can change your commits easily without creating new PRs.
If you want to learn git, I would help you.
Or if you help to improve our contribution guide, I would appreciate it.

@YapsBridging
Copy link
Contributor Author

Oh, you don't use git. Creating a new PR is no problem!

But if you could use git, you can change your commits easily without creating new PRs. If you want to learn git, I would help you. Or if you help to improve our contribution guide, I would appreciate it.

thanks @kenjis , of course i want,

i'm glad ,if u want to help me to,

basically i use git with command prompt but just standard command pull, push ,commit create branch,switch, check log,etc,,

one of the problems is when i try to signing to all old commit with

git rebase -i --root --exec 'git commit --amend --no-edit --no-verify -S'

https://github.com/codeigniter4/CodeIgniter4/blob/develop/contributing/workflow.md#gpg-signing-old-commits

its also overwrites other people's commits with my signature
i dont understand how this work..LOL

maybe because of the merge.

Screenshot 2024-01-25 194106

LoL..
so can I go back to the old commit before I merged, before all these problems happened?

@kenjis
Copy link
Member

kenjis commented Jan 25, 2024

You did not push your local branch to the remote branch in GitHub.
So you still have the remote branch (copy) in your local repository.

$ git switch develop
$ git branch -D csp # delete local csp branch
$ git checkout csp
branch 'csp' set up to track 'origin/csp'.
Switched to a new branch 'csp'

@kenjis
Copy link
Member

kenjis commented Jan 25, 2024

Or you can clone your repository in GitHub again.

git clone git@github.com:YapsBridging/CodeIgniter4.git
git checkout csp

@kenjis
Copy link
Member

kenjis commented Jan 25, 2024

I recommend:

As a faster alternative, you can still securely sign commits without the -S option in git commit by setting git config --global commit.gpgsign true and git config --global user.signingkey 3AC5C34371567BD2 to all local repositories. Without the --global option, the change is applied to one local repository only.
https://github.com/codeigniter4/CodeIgniter4/blob/develop/contributing/workflow.md#gpg-signing-old-commits

and if you set it, git rebase also automatically signs all rebased commits.

If you follow the instruction, the commit log would be following:

a7a441716c (HEAD -> csp) update style & js
795de34a07 update style & js
539c8b3c02 fix changes SCSS files did not match the expected CSS output
915b3251f1 remove inline style debugbar,welcome_message for CSP
7c426fa1dd update css for show tab debugbar
219011070a (upstream/develop) Merge pull request #8453 from kenjis/test-postgre-upsertBatch

@YapsBridging
Copy link
Contributor Author

@kenjis , Ok I'll try it

@YapsBridging
Copy link
Contributor Author

greate !..

Screenshot 2024-01-25 224501

one step again
i try to Force push:
git push --force-with-lease origin HEAD ,
and i get this message..what happen?

Screenshot 2024-01-25 224936

@kenjis
Copy link
Member

kenjis commented Jan 25, 2024

You did git rebase successfully!

The error on push seems an issue about your token permission.
See https://stackoverflow.com/questions/64059610/how-to-resolve-refusing-to-allow-an-oauth-app-to-create-or-update-workflow-on

I use SSH auth, so I have never seen that error.

$ git remote -v
origin	git@github.com:YapsBridging/CodeIgniter4.git (fetch)
origin	git@github.com:YapsBridging/CodeIgniter4.git (push)
upstream	git@github.com:codeigniter4/CodeIgniter4.git (fetch)
upstream	git@github.com:codeigniter4/CodeIgniter4.git (push)

@YapsBridging
Copy link
Contributor Author

hi @kenjis , I have completed the permission token permission,
i try again
git push --force-with-lease origin HEAD ,

now i get message
Screenshot 2024-02-02 114209

can u tell me how to fix it ?

@kenjis
Copy link
Member

kenjis commented Feb 2, 2024

It shows someone (probably you?) added commit(s) that is not included in your local branch to the remote branch on GitHub.

If you are sure the local branch is no problem, try:

# Update your local branch, because the upstream develop advanced.
git fetch upstream
git switch csp
git rebase upstream/develop
# Force push with no check.
git push -f origin HEAD

@kenjis
Copy link
Member

kenjis commented Feb 10, 2024

@YapsBridging Can't you push to your PR branch?

@YapsBridging
Copy link
Contributor Author

@YapsBridging Can't you push to your PR branch?

@kenjis , I thought I had completed the token permission. but apparently not, I need some more time to push again..., maybe with a new branch, I'm also afraid of lots of conflicts when I push using the same branch

@kenjis
Copy link
Member

kenjis commented Feb 10, 2024

The steps in #8411 (comment) did not work?

Or pushing as a new branch and creating a new PR would be fine.

@YapsBridging
Copy link
Contributor Author

The steps in #8411 (comment) did not work?

Or pushing as a new branch and creating a new PR would be fine.

I haven't tried it yet,
I'm afraid that when I push there will be lots of conflicts, I have already committed to the remote branch, and also the merge that I did is already on the remote branch. CSP's remote branch contains merge and commit, and my local branch doesn't

@kenjis
Copy link
Member

kenjis commented Feb 10, 2024

I don't understand the conflicts you are afraid of.
Force push pushes your local branch as it is. It can remove the commits in the remote branch on GitHub.

@YapsBridging
Copy link
Contributor Author

I don't understand the conflicts you are afraid of. Force push pushes your local branch as it is. It can remove the commits in the remote branch on GitHub.

ow, i dont know about that,,,thanks @kenjis , i will try push again

@kenjis kenjis changed the title fix: DebugBar block by CSP fix: DebugBar block by CSP Feb 10, 2024
@kenjis kenjis requested a review from michalsn February 10, 2024 08:55
Comment on lines 471 to 478
#ci-database.debug-bar-dblock,
#ci-files.debug-bar-dblock,
#ci-routes.debug-bar-dblock,
#ci-events.debug-bar-dblock,
#ci-history.debug-bar-dblock,
#ci-vars.debug-bar-dblock,
#ci-config.debug-bar-dblock,
#ci-timeline.debug-bar-dblock {
Copy link
Member

Choose a reason for hiding this comment

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

I believe this can be simplified to one CSS class, .debug-bar-dblock, instead of individually showing all uses in #ci-* ids. If there would be a new id, say #ci-dump, it would need to be added here again.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're right..thanks @paulbalandan

@@ -150,6 +150,26 @@
.further h2:first-of-type {
padding-top: 0;
}
.f1 {
Copy link
Member

Choose a reason for hiding this comment

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

What is f?

Copy link
Contributor Author

@YapsBridging YapsBridging Feb 10, 2024

Choose a reason for hiding this comment

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

@kenjis its style for font

Copy link
Member

Choose a reason for hiding this comment

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

It seems we don't use names like f1.
Can you change to meaningful names?
font-something?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok @kenjis i'll change it

@kenjis
Copy link
Member

kenjis commented Feb 12, 2024

macOS and Firefox.

Before:
Screenshot 2024-02-12 14 53 36

After:
Screenshot 2024-02-12 14 54 00

Copy link
Member

@michalsn michalsn left a comment

Choose a reason for hiding this comment

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

LGTM!

Copy link
Member

@kenjis kenjis left a comment

Choose a reason for hiding this comment

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

Thank you!

@kenjis kenjis merged commit 3b1d8e3 into codeigniter4:develop Feb 12, 2024
63 checks passed
@crustamet
Copy link
Contributor

crustamet commented Feb 12, 2024

This is great, but there is one little issue if only one case exist... while having

for example
<base href="localhost:8080">
in your main html the buttons on debugbar always redirect to base if you click the buttons "Database", "Views", "Logs", "Files"

I always get redirected to "homepage" using this new debugbar. i think this happened before but without the CSP Fix while clicking on them nothing happened...

The fix is as following remove the href="#" from the button links

	<a href="#" data-toggle="datatable" data-table="<?= strtolower(str_replace(' ', '-', $heading)) ?>">
		<h2><?= $heading ?></h2>
	</a>

	TO

	<a data-toggle="datatable" data-table="<?= strtolower(str_replace(' ', '-', $heading)) ?>">
		<h2><?= $heading ?></h2>
	</a>

@kenjis
Copy link
Member

kenjis commented Feb 12, 2024

What do you mean?

It works with no problem with the following settings.

diff --git a/app/Config/App.php b/app/Config/App.php
index 21b4df2052..aa433bf847 100644
--- a/app/Config/App.php
+++ b/app/Config/App.php
@@ -174,5 +174,5 @@ class App extends BaseConfig
      * @see http://www.html5rocks.com/en/tutorials/security/content-security-policy/
      * @see http://www.w3.org/TR/CSP/
      */
-    public bool $CSPEnabled = false;
+    public bool $CSPEnabled = true;
 }
diff --git a/app/Views/welcome_message.php b/app/Views/welcome_message.php
index 86a6e39de3..88001613ca 100644
--- a/app/Views/welcome_message.php
+++ b/app/Views/welcome_message.php
@@ -2,6 +2,7 @@
 <html lang="en">
 <head>
     <meta charset="UTF-8">
+    <base href="localhost:8080">
     <title>Welcome to CodeIgniter 4!</title>
     <meta name="description" content="The small framework with powerful features">
     <meta name="viewport" content="width=device-width, initial-scale=1.0">

@YapsBridging
Copy link
Contributor Author

@crustamet i see, cause tag base add value to All relative link in the page
@kenjis this will happen if the url page different from the tag base

ex in
head tag base http://localhost:8080
but page url http://localhost:8080/dashboard

so the tag/button debugbar will be http://localhost:8080/#

before i change javascript:void(0) to # , because javascript:void(0) was blocked by CSP

@YapsBridging
Copy link
Contributor Author

but in my opinion, if you use tag base, maybe other plugins will also be affected. If you use other plugins, you don't know whether the plugin you are using contains relative links.

the tag Base will add value to All relative link in the page

So in my opinion using the base tag is not appropriate, it can cause unexpected problems.

that's just my opinion

@crustamet
Copy link
Contributor

but in my opinion, if you use tag base, maybe other plugins will also be affected. If you use other plugins, you don't know whether the plugin you are using contains relative links.
the tag Base will add value to All relative link in the page
So in my opinion using the base tag is not appropriate, it can cause unexpected problems.
that's just my opinion

It only causes problems if you have already create the project that is not using this base option and then you use it when the project is finished :)) it doesnt work like that, you use it from the beginning of the project creation so it helps you organize everything better

I don't think this matters for is appropriate or not, it does not break any functionalities for me, and its better because i load all my links with this base link.

And i don't think it matters in this situation either, because what ? i added a valid base meta tag and the debugbar is not working ? :))) the debugbar should work whatever you do with your code.

@YapsBridging
Copy link
Contributor Author

ok @crustamet, you're right, maybe this is the reason why the debugbar button uses javascript:void(0) not #

@crustamet
Copy link
Contributor

why in the world would you want to have a hashtag in your links without wanting them there ?
the hashtag was before used in the old times to remember what you opened..

Here in the new debugbar it does that by adding data-(*attrs) to the links so now there is no need for href="#" at this point
It should be removed completely from the links

It would be better to have them as buttons instead of links but yeah there is bug with <base> tag the debugbar not working..
It only works on the homepage of the project

@YapsBridging
Copy link
Contributor Author

ow..i dont know about history of the hastag.. i know the single hastag, href="#"
one of the ways to create anchors that don't link to anything. so i use that for alternative change javascript:void..
however you're right we can remove href to create anchors that don't link to anything

@crustamet
Copy link
Contributor

crustamet commented Feb 13, 2024

you should really make them with buttons 😆 but a tags without href so weird because now you will loose pointer cursor xD

@YapsBridging
Copy link
Contributor Author

ow i forgot...Thank you @kenjis , for teaching Git....:))

@kenjis
Copy link
Member

kenjis commented Feb 13, 2024

@kenjis this will happen if the url page different from the tag base

ex in
head tag base http://localhost:8080
but page url http://localhost:8080/dashboard

so the tag/button debugbar will be http://localhost:8080/#

Cannot reproduce with <base href="localhost:8080"> on Firefox.
The tag/button debugbar is http://localhost:8080/dashboard#

@kenjis
Copy link
Member

kenjis commented Feb 13, 2024

Oh, I was able to reproduce with <base href="http://localhost:8080"> and http://localhost:8080/dashboard.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Verified issues on the current code behavior or pull requests that will fix them
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: [DebugBar] block by CSP
5 participants