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

Quote and escape job string fields #11

Merged
merged 1 commit into from
Nov 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions scheduler/job_configurator.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ import (
"github.com/ag-computational-bio/bakta-web-backend/objectStorage"
)

func quote(s string) string {
return fmt.Sprintf("\"%v\"", strings.ReplaceAll(s, "\"", "\\\""))
}

//createDownloadConf Creates the configuration string for the download part of a bakta job
//The job has to be provided along with two bools that indicate if a prodigal training file and/or a replicon file are present
func createDownloadConf(job *database.Job, prodigaltf bool, replicontsv bool) (string, error) {
Expand Down Expand Up @@ -64,31 +68,31 @@ func createBaktaConf(job *database.Job, conf *api.JobConfig) (string, error) {
}

if conf.Locus != "" {
confStringElements = append(confStringElements, fmt.Sprintf("--locus %v", conf.Locus))
confStringElements = append(confStringElements, fmt.Sprintf("--locus %v", quote(conf.Locus)))
}

if conf.LocusTag != "" {
confStringElements = append(confStringElements, fmt.Sprintf("--locus-tag %v", conf.LocusTag))
confStringElements = append(confStringElements, fmt.Sprintf("--locus-tag %v", quote(conf.LocusTag)))
}

if conf.KeepContigHeaders {
confStringElements = append(confStringElements, "--keep-contig-headers")
}

if conf.Genus != "" {
confStringElements = append(confStringElements, fmt.Sprintf("--genus %v", conf.Genus))
confStringElements = append(confStringElements, fmt.Sprintf("--genus %v", quote(conf.Genus)))
}

if conf.Species != "" {
confStringElements = append(confStringElements, fmt.Sprintf("--species %v", conf.Species))
confStringElements = append(confStringElements, fmt.Sprintf("--species %v", quote(conf.Species)))
}

if conf.Strain != "" {
confStringElements = append(confStringElements, fmt.Sprintf("--strain %v", conf.Strain))
confStringElements = append(confStringElements, fmt.Sprintf("--strain %v", quote(conf.Strain)))
}

if conf.Plasmid != "" {
confStringElements = append(confStringElements, fmt.Sprintf("--plasmid %v", conf.Plasmid))
confStringElements = append(confStringElements, fmt.Sprintf("--plasmid %v", quote(conf.Plasmid)))
}

if conf.Compliant {
Expand Down
106 changes: 106 additions & 0 deletions scheduler/job_configurator_test.go
Original file line number Diff line number Diff line change
@@ -1 +1,107 @@
package scheduler

import (
"testing"

api "github.com/ag-computational-bio/bakta-web-api-go/bakta/web/api/proto/v1"
db "github.com/ag-computational-bio/bakta-web-backend/database"
)

func Test_species_should_be_quoted(t *testing.T) {

job := db.Job{}
job.FastaKey = "irrelevant"

config := api.JobConfig{}
config.Species = "test\"; rm -rf /"

jobstring, _ := createBaktaConf(&job, &config)
expected := "--tmp-dir /cache --threads 12 --prefix result -o /output --db /db/db --species \"test\\\"; rm -rf /\" --gram ? /data/irrelevant"
if jobstring != expected {
t.Errorf("Expected '%v', Got '%v'", expected, jobstring)
}
}
func Test_genus_should_be_quoted(t *testing.T) {
job := db.Job{}
job.FastaKey = "irrelevant"

config := api.JobConfig{}
config.Genus = "test\"; rm -rf /"

jobstring, _ := createBaktaConf(&job, &config)
expected := "--tmp-dir /cache --threads 12 --prefix result -o /output --db /db/db --genus \"test\\\"; rm -rf /\" --gram ? /data/irrelevant"
if jobstring != expected {
t.Errorf("Expected '%v', Got '%v'", expected, jobstring)
}
}
func Test_strain_should_be_quoted(t *testing.T) {
job := db.Job{}
job.FastaKey = "irrelevant"

config := api.JobConfig{}
config.Strain = "test\"; rm -rf /"

jobstring, _ := createBaktaConf(&job, &config)
expected := "--tmp-dir /cache --threads 12 --prefix result -o /output --db /db/db --strain \"test\\\"; rm -rf /\" --gram ? /data/irrelevant"
if jobstring != expected {
t.Errorf("Expected '%v', Got '%v'", expected, jobstring)
}
}

func Test_locus_should_be_quoted(t *testing.T) {
job := db.Job{}
job.FastaKey = "irrelevant"

config := api.JobConfig{}
config.Locus = "test\"; rm -rf /"

jobstring, _ := createBaktaConf(&job, &config)
expected := "--tmp-dir /cache --threads 12 --prefix result -o /output --db /db/db --locus \"test\\\"; rm -rf /\" --gram ? /data/irrelevant"
if jobstring != expected {
t.Errorf("Expected '%v', Got '%v'", expected, jobstring)
}
}
func Test_locus_tag_should_be_quoted(t *testing.T) {
job := db.Job{}
job.FastaKey = "irrelevant"

config := api.JobConfig{}
config.LocusTag = "test\"; rm -rf /"

jobstring, _ := createBaktaConf(&job, &config)
expected := "--tmp-dir /cache --threads 12 --prefix result -o /output --db /db/db --locus-tag \"test\\\"; rm -rf /\" --gram ? /data/irrelevant"
if jobstring != expected {
t.Errorf("Expected '%v', Got '%v'", expected, jobstring)
}
}

func Test_plasmid_tag_should_be_quoted(t *testing.T) {
job := db.Job{}
job.FastaKey = "irrelevant"

config := api.JobConfig{}
config.Plasmid = "test\"; rm -rf /"

jobstring, _ := createBaktaConf(&job, &config)
expected := "--tmp-dir /cache --threads 12 --prefix result -o /output --db /db/db --plasmid \"test\\\"; rm -rf /\" --gram ? /data/irrelevant"
if jobstring != expected {
t.Errorf("Expected '%v', Got '%v'", expected, jobstring)
}
}

func TestQuote(t *testing.T) {
testData := []struct {
input string
expected string
}{
{"", "\"\""},
{"test", "\"test\""},
{"test\" rm -rf /", "\"test\\\" rm -rf /\""},
}
for _, data := range testData {
quoted := quote(data.input)
if quoted != data.expected {
t.Errorf("Expected '%v', Got '%v'", data.expected, quoted)
}
}
}