Skip to content

Commit

Permalink
feat: add Ruby SAST stage (#126)
Browse files Browse the repository at this point in the history
  • Loading branch information
lilianeconstantino committed Jan 4, 2022
1 parent 5a7e654 commit 82be558
Show file tree
Hide file tree
Showing 13 changed files with 155 additions and 2 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ pipelinit
<td rowspan="1">Shell script</td>
<td>Lint</td>
<td>✔️</td>
<td rowspan="6">Coming soon</td>
<td rowspan="7">Coming soon</td>
</tr>
<tr>
<td rowspan="2">Java</td>
Expand All @@ -170,14 +170,18 @@ pipelinit
<td>✔️</td>
</tr>
<tr>
<td rowspan="2">Ruby</td>
<td rowspan="3">Ruby</td>
<td>Lint</td>
<td>✔️</td>
</tr>
<tr>
<td>Format</td>
<td>✔️</td>
</tr>
<tr>
<td>SAST (Semgrep)</td>
<td>✔️</td>
</tr>
<tr>
<td rowspan="1">Markdown</td>
<td>Lint</td>
Expand Down
8 changes: 8 additions & 0 deletions core/plugins/stack/ruby/dependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,11 @@ export const hasRubyDependency = async (
const dependencies = await readDependencyFile(context);
return dependencies.some((dep) => dep === dependencyName);
};

export const hasRubyDependencyAny = async (
context: Context,
dependencyList: Set<string>,
): Promise<boolean> => {
const dependencies = await readDependencyFile(context);
return dependencies.some((dep) => dependencyList.has(dep));
};
9 changes: 9 additions & 0 deletions core/plugins/stack/ruby/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
Formatters,
introspect as introspectFormatters,
} from "./formatters.ts";
import { introspect as introspectType } from "./types.ts";

/**
* Introspected information about a project with Ruby
Expand All @@ -19,6 +20,10 @@ export default interface RubyProject {
*/
linters: Linters;
formatters: Formatters;
/**
* Identified type of project
*/
type?: string | null;
}

export const introspector: Introspector<RubyProject | undefined> = {
Expand All @@ -42,10 +47,14 @@ export const introspector: Introspector<RubyProject | undefined> = {
const linters = await introspectLinters(context);
const formatters = await introspectFormatters(context);

//Project type
const projectType = await introspectType(context);

return {
version: version,
linters: linters,
formatters: formatters,
type: projectType,
};
},
};
18 changes: 18 additions & 0 deletions core/plugins/stack/ruby/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { IntrospectFn } from "../../../types.ts";
import { hasRubyDependencyAny } from "./dependencies.ts";

const webApps = new Set([
"rails",
"rack",
"sinatra",
"hanami",
"padrino",
"roda",
]);

export const introspect: IntrospectFn<string | null> = async (context) => {
if (await hasRubyDependencyAny(context, webApps)) {
return "webApp";
}
return null;
};
19 changes: 19 additions & 0 deletions core/templates/github/ruby/sast.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: SAST Ruby
on:
pull_request:
paths:
- "**.rb"
jobs:
semgrep:
name: Scan
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: returntocorp/semgrep-action@v1
with:
config: >-
<% if (it.type === "webApp") { -%>
p/owasp-top-ten
<% } else {-%>
p/ci
<% } -%>
9 changes: 9 additions & 0 deletions tests/default_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,15 @@ test(
},
);

test(
{ fixture: "ruby/sinatra", args: ["--no-strict"] },
async (stdout, _stderr, code, assertExpectedFiles) => {
assertStringIncludes(stdout, "Detected stack: ruby");
assertEquals(code, 0);
await assertExpectedFiles();
},
);

test(
{ fixture: "docker/docker-lint-build", args: ["--no-strict"] },
async (stdout, _stderr, code, assertExpectedFiles) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated with pipelinit 0.3.0
# https://pipelinit.com/
name: SAST Ruby
on:
pull_request:
paths:
- "**.rb"
jobs:
semgrep:
name: Scan
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: returntocorp/semgrep-action@v1
with:
config: >-
p/ci
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Generated with pipelinit 0.3.0
# https://pipelinit.com/
name: Format Ruby
on:
pull_request:
paths:
- "**.rb"
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: "3.0.2"
bundler-cache: true
- run: gem install
- run: gem install rubocop
- run: bundle exec rubocop --format .
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Generated with pipelinit 0.3.0
# https://pipelinit.com/
name: Lint Ruby
on:
pull_request:
paths:
- "**.rb"
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: "3.0.2"
bundler-cache: true
- run: gem install
- run: gem install rubocop
- run: bundle exec rubocop --lint .
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated with pipelinit 0.3.0
# https://pipelinit.com/
name: SAST Ruby
on:
pull_request:
paths:
- "**.rb"
jobs:
semgrep:
name: Scan
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: returntocorp/semgrep-action@v1
with:
config: >-
p/owasp-top-ten
1 change: 1 addition & 0 deletions tests/fixtures/ruby/sinatra/project/.pipelinit.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
platforms = ["github"]
5 changes: 5 additions & 0 deletions tests/fixtures/ruby/sinatra/project/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
source 'https://rubygems.org'
gem 'sinatra', :github => 'sinatra/sinatra'

# other dependencies
gem 'haml' # for instance, if you use haml
6 changes: 6 additions & 0 deletions tests/fixtures/ruby/sinatra/project/main.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# myapp.rb
require 'sinatra'

get '/' do
'Hello world!'
end

0 comments on commit 82be558

Please sign in to comment.