From 7b7f84ba45097a82f05cc007d4d6c146c39ed37e Mon Sep 17 00:00:00 2001 From: Sebastian Beltran Date: Thu, 7 Nov 2024 10:45:35 -0500 Subject: [PATCH 1/2] ci: add CodeQL (SAST) (#204) --- .github/workflows/codeql.yml | 66 ++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 .github/workflows/codeql.yml diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml new file mode 100644 index 0000000..cd41192 --- /dev/null +++ b/.github/workflows/codeql.yml @@ -0,0 +1,66 @@ +# For most projects, this workflow file will not need changing; you simply need +# to commit it to your repository. +# +# You may wish to alter this file to override the set of languages analyzed, +# or to provide custom queries or build logic. +# +# ******** NOTE ******** +# We have attempted to detect the languages in your repository. Please check +# the `language` matrix defined below to confirm you have the correct set of +# supported CodeQL languages. +# +name: "CodeQL" + +on: + push: + branches: ["master"] + pull_request: + # The branches below must be a subset of the branches above + branches: ["master"] + schedule: + - cron: "0 0 * * 1" + +permissions: + contents: read + +jobs: + analyze: + name: Analyze + runs-on: ubuntu-latest + permissions: + actions: read + contents: read + security-events: write + + steps: + - name: Checkout repository + uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + + # Initializes the CodeQL tools for scanning. + - name: Initialize CodeQL + uses: github/codeql-action/init@662472033e021d55d94146f66f6058822b0b39fd # v3.27.0 + with: + languages: javascript + # If you wish to specify custom queries, you can do so here or in a config file. + # By default, queries listed here will override any specified in a config file. + # Prefix the list here with "+" to use these queries and those in the config file. + + # Autobuild attempts to build any compiled languages (C/C++, C#, or Java). + # If this step fails, then you should remove it and run the build manually (see below) + # - name: Autobuild + # uses: github/codeql-action/autobuild@662472033e021d55d94146f66f6058822b0b39fd # v3.27.0 + + # ℹī¸ Command-line programs to run using the OS shell. + # 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun + + # If the Autobuild fails above, remove it and uncomment the following three lines. + # modify them (or add more) to build your code if your project, please refer to the EXAMPLE below for guidance. + + # - run: | + # echo "Run, Build Application using script" + # ./location_of_script_within_repo/buildscript.sh + + - name: Perform CodeQL Analysis + uses: github/codeql-action/analyze@662472033e021d55d94146f66f6058822b0b39fd # v3.27.0 + with: + category: "/language:javascript" \ No newline at end of file From 8e5641c188f766750900858da96bf596851f77ca Mon Sep 17 00:00:00 2001 From: maritz Date: Sat, 9 Nov 2024 18:28:00 +0100 Subject: [PATCH 2/2] Use headersSent instead of _header (#129) * fix: change usage of _header to headersSernt * improve logic * update history --------- Co-authored-by: Sebastian Beltran --- HISTORY.md | 4 ++++ index.js | 18 ++++++++++++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index 7f625d0..3d224ed 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,3 +1,7 @@ +unreleased +========== + * Use `res.headersSent` when available + 1.7.5 / 2024-10-31 ========== diff --git a/index.js b/index.js index c7494e7..332e4ab 100644 --- a/index.js +++ b/index.js @@ -80,7 +80,7 @@ function compression (options) { return false } - if (!this._header) { + if (!headersSent(res)) { this._implicitHeader() } @@ -94,7 +94,7 @@ function compression (options) { return false } - if (!this._header) { + if (!headersSent(res)) { // estimate the length if (!this.getHeader('Content-Length')) { length = chunkLength(chunk, encoding) @@ -281,3 +281,17 @@ function toBuffer (chunk, encoding) { ? chunk : Buffer.from(chunk, encoding) } + +/** + * Determine if the response headers have been sent. + * + * @param {object} res + * @returns {boolean} + * @private + */ + +function headersSent (res) { + return typeof res.headersSent !== 'boolean' + ? Boolean(res._header) + : res.headersSent +}