Skip to content
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
5 changes: 3 additions & 2 deletions internal/cmd/sanitize/sanitize.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ relative paths. That folder is cleared on every run. Original files are never ch
The sanitized files are printed in the same format as ` + "`debricked files find`" + `, with the paths of
the copies instead of the originals. Skipped and failed files, and a summary, go to stderr.

A map from each placeholder to the real name and path is written to .debricked/` + sanitize.MapFileName + `.
It stays on this machine: it is outside the folder of copies and must not be uploaded.
A local map is written to .debricked/` + sanitize.MapFileName + `. It lists every sanitized file with the path
of its copy, and every placeholder with the real name and path it replaced. It stays on this machine:
it is outside the folder of copies and must not be uploaded.

File types that sanitize doesn't handle are skipped and reported, and no copy of them is written.
` + policy.DefaultFileName + ` files are copied unchanged, after checking that they are valid JSON, and listed
Expand Down
64 changes: 64 additions & 0 deletions internal/sanitize/pm/composer/composer_json.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package composer

import "github.com/debricked/cli/internal/sanitize/pm/util"

// composerManifestLists are the lists the backend reads from composer.json: their names decide
// which packages are direct dependencies.
var composerManifestLists = []string{"require", "require-dev"}

// sanitizeComposerJSON keeps only require and require-dev, with their names and version
// constraints as they are. Everything else is dropped: repositories, scripts and config often
// hold internal hosts and credentials.
func sanitizeComposerJSON(content []byte, _ *util.FileContext) ([]byte, error) {
manifest, err := util.ParseJSONObject(content)
if err != nil {
return nil, util.ErrInvalidFormat
}

sanitized := util.NewOrderedObject()
for _, listName := range composerManifestLists {
value, present := manifest.Values[listName]
if !present {
continue
}
requirements, isObject := value.(*util.OrderedObject)
if !isObject {
return nil, util.ErrInvalidFormat
}
constraints, err := keepConstraints(requirements)
if err != nil {
return nil, err
}
if len(constraints.Keys) > 0 {
sanitized.Set(listName, constraints)
}
}

return util.EncodeJSON(sanitized)
}

// keepConstraints copies a require list. Every constraint must be a string, so nothing nested can
// be copied.
func keepConstraints(requirements *util.OrderedObject) (*util.OrderedObject, error) {
sanitized := util.NewOrderedObject()
for _, name := range requirements.Keys {
constraint, isString := requirements.Values[name].(string)
if !isString {
return nil, util.ErrInvalidFormat
}
sanitized.Set(name, constraint)
}

return sanitized, nil
}

// anyVersion keeps the names of a require list in composer.lock and sets every constraint to "*".
// The backend reads only the names; each version comes from the package's own entry.
func anyVersion(requirements *util.OrderedObject) *util.OrderedObject {
sanitized := util.NewOrderedObject()
for _, name := range requirements.Keys {
sanitized.Set(name, "*")
}

return sanitized
}
81 changes: 81 additions & 0 deletions internal/sanitize/pm/composer/composer_json_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package composer

import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/debricked/cli/internal/sanitize/pm/testdata"
"github.com/debricked/cli/internal/sanitize/pm/util"
)

const composerJSONBefore = `{
"name": "acme/payments-portal",
"description": "Internal card payments portal",
"type": "project",
"repositories": [
{"type": "composer", "url": "https://satis.acme.internal"}
],
"require": {
"php": ">=8.2",
"ext-json": "*",
"guzzlehttp/guzzle": "^7.8",
"acme/auth-client": "dev-feature/PROJ-123-new-login"
},
"require-dev": {
"phpunit/phpunit": "^10.5"
},
"scripts": {
"deploy": "s3cret"
}
}
`

const composerJSONAfter = `{
"require": {
"php": ">=8.2",
"ext-json": "*",
"guzzlehttp/guzzle": "^7.8",
"acme/auth-client": "dev-feature/PROJ-123-new-login"
},
"require-dev": {
"phpunit/phpunit": "^10.5"
}
}
`

func TestSanitizeComposerJSON(t *testing.T) {
sanitized, err := sanitizeComposerJSON([]byte(composerJSONBefore), newTestContext("composer.json"))

assert.NoError(t, err)
assert.Equal(t, composerJSONAfter, string(sanitized))
assert.NotContains(t, string(sanitized), "acme.internal")
assert.NotContains(t, string(sanitized), "s3cret")
}

func TestSanitizeComposerJSONMissingLists(t *testing.T) {
sanitized, err := sanitizeComposerJSON([]byte(`{"name":"portal"}`), newTestContext("composer.json"))

assert.NoError(t, err)
assert.Equal(t, "{}\n", string(sanitized))
}

func TestSanitizeComposerJSONOmitsEmptySections(t *testing.T) {
sanitized, err := sanitizeComposerJSON([]byte(`{"require":{"php":">=8.2"},"require-dev":{}}`), newTestContext("composer.json"))

assert.NoError(t, err)
assert.Equal(t, "{\n \"require\": {\n \"php\": \">=8.2\"\n }\n}\n", string(sanitized))
}

func TestSanitizeComposerJSONInvalid(t *testing.T) {
for _, content := range []string{`{not-json`, `{"require": []}`, `{"require-dev": "x"}`, `{"require": {"a/b": {"nested": "x"}}}`, `{"require": {"a/b": 1}}`} {
_, err := sanitizeComposerJSON([]byte(content), newTestContext("composer.json"))
assert.ErrorIs(t, err, util.ErrInvalidFormat, content)
}
}

func newTestContext(path string) *util.FileContext {
context, _ := testdata.NewContext(path)

return context
}
93 changes: 93 additions & 0 deletions internal/sanitize/pm/composer/composer_lock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package composer

import (
"strings"

"github.com/debricked/cli/internal/sanitize/pm/util"
)

// sanitizeComposerLock keeps only the dependency data read by the backend parser.
func sanitizeComposerLock(content []byte, _ *util.FileContext) ([]byte, error) {
lockFile, err := util.ParseJSONObject(content)
if err != nil {
return nil, util.ErrInvalidFormat
}

sanitized := util.NewOrderedObject()
handled := false
for _, key := range lockFile.Keys {
if key != "packages" && key != "packages-dev" {
continue
}
packages, isArray := lockFile.Values[key].([]any)
if !isArray {
return nil, util.ErrInvalidFormat
}
handled = true
sanitizedPackages, sanitizeErr := sanitizeComposerPackages(packages)
if sanitizeErr != nil {
return nil, sanitizeErr
}
sanitized.Set(key, sanitizedPackages)
}
if !handled {
return nil, util.ErrInvalidFormat
}

return util.EncodeJSON(sanitized)
}

func sanitizeComposerPackages(packages []any) ([]any, error) {
sanitized := []any{}
for _, pkg := range packages {
entry, isObject := pkg.(*util.OrderedObject)
if !isObject {
return nil, util.ErrInvalidFormat
}
sanitizedEntry, err := sanitizeComposerPackage(entry)
if err != nil {
return nil, err
}
sanitized = append(sanitized, sanitizedEntry)
}

return sanitized, nil
}

// sanitizeComposerPackage keeps name and version, which make the component and its purl, and the
// names in require, which make the edges. Their constraints are set to "*".
// source and dist are dropped: they hold private URLs and paths.
func sanitizeComposerPackage(pkg *util.OrderedObject) (*util.OrderedObject, error) {
name, isName := pkg.Values["name"].(string)
version, isVersion := pkg.Values["version"].(string)
if !isName || !isVersion {
return nil, util.ErrInvalidFormat
}
sanitized := util.NewOrderedObject()
sanitized.Set("name", name)
sanitized.Set("version", sanitizeComposerVersion(version))

value, present := pkg.Values["require"]
if !present {
return sanitized, nil
}
requirements, isObject := value.(*util.OrderedObject)
if !isObject {
return nil, util.ErrInvalidFormat
}
if len(requirements.Keys) > 0 {
sanitized.Set("require", anyVersion(requirements))
}

return sanitized, nil
}

// sanitizeComposerVersion replaces branch versions. The parser reports every dev- version as
// empty, and branch names can hold ticket numbers and feature names.
func sanitizeComposerVersion(version string) string {
if strings.HasPrefix(version, "dev-") {
return "dev-redacted"
}

return version
}
119 changes: 119 additions & 0 deletions internal/sanitize/pm/composer/composer_lock_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package composer

import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/debricked/cli/internal/sanitize/pm/util"
)

const composerLockBefore = `{
"_readme": ["readme"],
"content-hash": "aaaa",
"packages": [
{
"name": "acme/auth-client",
"version": "dev-feature/PROJ-123-new-login",
"source": {"url": "git@git.acme.internal:platform/auth-client.git"},
"require": {
"guzzlehttp/guzzle": "^7.0",
"php": ">=8.1"
},
"authors": [{"name": "Jane Doe"}]
},
{
"name": "acme/shared-utils",
"version": "1.0.0",
"dist": {"url": "../shared-utils"}
}
],
"packages-dev": [
{
"name": "phpunit/phpunit",
"version": "10.5.10",
"source": {"url": "https://github.com/sebastianbergmann/phpunit.git"}
}
],
"minimum-stability": "dev"
}
`

const composerLockAfter = `{
"packages": [
{
"name": "acme/auth-client",
"version": "dev-redacted",
"require": {
"guzzlehttp/guzzle": "*",
"php": "*"
}
},
{
"name": "acme/shared-utils",
"version": "1.0.0"
}
],
"packages-dev": [
{
"name": "phpunit/phpunit",
"version": "10.5.10"
}
]
}
`

func TestSanitizeComposerLock(t *testing.T) {
sanitized, err := sanitizeComposerLock([]byte(composerLockBefore), newTestContext("composer.lock"))

assert.NoError(t, err)
assert.Equal(t, composerLockAfter, string(sanitized))
assert.NotContains(t, string(sanitized), "PROJ-123")
assert.NotContains(t, string(sanitized), "acme.internal")
}

func TestSanitizeComposerLockInvalid(t *testing.T) {
invalid := []string{
`{not-json`,
`[]`,
`{"packages": {}}`,
`{"packages": ["bad-entry"]}`,
`{"name":"not-lock"}`,
`{"packages": [{"version": "1.0.0"}]}`,
`{"packages": [{"name": "a/b"}]}`,
`{"packages": [{"name": "a/b", "version": 1}]}`,
`{"packages": [{"name": "a/b", "version": "1.0.0", "require": ["c/d"]}]}`,
}

for _, content := range invalid {
_, err := sanitizeComposerLock([]byte(content), newTestContext("composer.lock"))
assert.ErrorIs(t, err, util.ErrInvalidFormat, content)
}
}

func TestSanitizeComposerLockOmitsEmptyRequire(t *testing.T) {
before := `{
"packages": [
{
"name": "acme/shared-utils",
"version": "1.0.0",
"require": {}
}
]
}
`
after := `{
"packages": [
{
"name": "acme/shared-utils",
"version": "1.0.0"
}
]
}
`

sanitized, err := sanitizeComposerLock([]byte(before), newTestContext("composer.lock"))

assert.NoError(t, err)
assert.Equal(t, after, string(sanitized))
}
Loading
Loading