Skip to content

Commit f1dde69

Browse files
authored
RUBY-3357 Add static analysis (#2869)
* let's try out codeql * .evergreen/tools.rb is not referenced anywhere we can remove it, to remove the warnings it generates * this is not well-documented; hopefully this works to exclude those paths * action docs say this should be a "YAML string" * static analysis reported "Polynomial regular expression used on uncontrolled data"
1 parent c6c63a7 commit f1dde69

File tree

3 files changed

+99
-68
lines changed

3 files changed

+99
-68
lines changed

Diff for: .evergreen/tools.rb

-67
This file was deleted.

Diff for: .github/workflows/codeql.yml

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
name: "CodeQL"
2+
3+
on:
4+
push:
5+
branches: [ "master" ]
6+
pull_request:
7+
branches: [ "master" ]
8+
schedule:
9+
- cron: '20 0 * * 0'
10+
11+
jobs:
12+
analyze:
13+
name: Analyze (${{ matrix.language }})
14+
# Runner size impacts CodeQL analysis time. To learn more, please see:
15+
# - https://gh.io/recommended-hardware-resources-for-running-codeql
16+
# - https://gh.io/supported-runners-and-hardware-resources
17+
# - https://gh.io/using-larger-runners (GitHub.com only)
18+
# Consider using larger runners or machines with greater resources for possible analysis time improvements.
19+
runs-on: 'ubuntu-latest'
20+
timeout-minutes: 360
21+
permissions:
22+
# required for all workflows
23+
security-events: write
24+
25+
# required to fetch internal or private CodeQL packs
26+
packages: read
27+
28+
# only required for workflows in private repositories
29+
actions: read
30+
contents: read
31+
32+
strategy:
33+
fail-fast: false
34+
matrix:
35+
include:
36+
- language: ruby
37+
build-mode: none
38+
steps:
39+
- name: Checkout repository
40+
uses: actions/checkout@v4
41+
42+
# Initializes the CodeQL tools for scanning.
43+
- name: Initialize CodeQL
44+
uses: github/codeql-action/init@v3
45+
with:
46+
languages: ${{ matrix.language }}
47+
build-mode: ${{ matrix.build-mode }}
48+
config: |
49+
paths-ignore:
50+
- .evergreen
51+
- spec
52+
# If you wish to specify custom queries, you can do so here or in a config file.
53+
# By default, queries listed here will override any specified in a config file.
54+
# Prefix the list here with "+" to use these queries and those in the config file.
55+
56+
# For more details on CodeQL's query packs, refer to: https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-code-scanning#using-queries-in-ql-packs
57+
# queries: security-extended,security-and-quality
58+
59+
# If the analyze step fails for one of the languages you are analyzing with
60+
# "We were unable to automatically build your code", modify the matrix above
61+
# to set the build mode to "manual" for that language. Then modify this step
62+
# to build your code.
63+
# ℹ️ Command-line programs to run using the OS shell.
64+
# 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun
65+
- if: matrix.build-mode == 'manual'
66+
run: |
67+
echo 'If you are using a "manual" build mode for one or more of the' \
68+
'languages you are analyzing, replace this with the commands to build' \
69+
'your code, for example:'
70+
echo ' make bootstrap'
71+
echo ' make release'
72+
exit 1
73+
74+
- name: Perform CodeQL Analysis
75+
uses: github/codeql-action/analyze@v3
76+
with:
77+
category: "/language:${{matrix.language}}"

Diff for: lib/mongo/socket/ssl.rb

+22-1
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ def set_cert(context, options)
287287
# for instance, if there is no newline between two certificates
288288
# this code will extract them both but OpenSSL fails in this situation.
289289
if cert_text
290-
certs = cert_text.scan(/-----BEGIN CERTIFICATE-----(?:.|\n)+?-----END CERTIFICATE-----/)
290+
certs = extract_certs(cert_text)
291291
if certs.length > 1
292292
context.cert = OpenSSL::X509::Certificate.new(certs.shift)
293293
context.extra_chain_cert = certs.map do |cert|
@@ -390,6 +390,27 @@ def run_tls_context_hooks
390390
hook.call(@context)
391391
end
392392
end
393+
394+
BEGIN_CERT = "-----BEGIN CERTIFICATE-----"
395+
END_CERT = "-----END CERTIFICATE-----"
396+
397+
# This was originally a scan + regex, but the regex was particularly
398+
# inefficient and was flagged as a concern by static analysis.
399+
def extract_certs(text)
400+
[].tap do |list|
401+
pos = 0
402+
403+
while (begin_idx = text.index(BEGIN_CERT, pos))
404+
end_idx = text.index(END_CERT, begin_idx)
405+
break unless end_idx
406+
407+
end_idx += END_CERT.length
408+
list.push(text[begin_idx...end_idx])
409+
410+
pos = end_idx
411+
end
412+
end
413+
end
393414
end
394415
end
395416
end

0 commit comments

Comments
 (0)