CVE-2019-16097 (Go, published 2019-09-08T16:15Z)
Description: value: core/api/user.go in Harbor 1.7.0 through 1.8.2 allows non-admin users to create admin accounts via the POST /api/users API, when Harbor is setup with DB as authentication backend and allow user to do self-registration. Fixed version: v1.7.6 v1.8.3. v.1.9.0. Workaround without applying the fix: configure Harbor to use non-DB authentication backend such as LDAP.
Severity: MEDIUM (CVSS v3 base score: 6.5)
Weakness: CWE-862 \u2014 Missing Authorization
The product does not perform an authorization check when an actor attempts to access a resource or perform an action.
Repository: https://github.com/goharbor/harbor
Files changed:
src/core/api/user.go
src/core/api/user_test.go
Commit message: Disallow creating an admin user when registration

This commit enhance the `POST /api/users` API to block request from non-admin to create
admin user.

Signed-off-by: Daniel Jiang <jiangd@vmware.com>


Vulnerable code:
	fmt.Println("Register user without admin auth")
		assert.Equal(400, code, "Add user status should be 400")
	fmt.Println("Register user with admin auth, but username is empty")
		assert.Equal(400, code, "Add user status should be 400")
	fmt.Println("Register user with admin auth, but bad username format")
		assert.Equal(400, code, "Add user status should be 400")
	fmt.Println("Register user with admin auth, but empty password.")
		assert.Equal(400, code, "Add user status should be 400")
	fmt.Println("Register user with admin auth, but email is empty")
		assert.Equal(400, code, "Add user status should be 400")
	fmt.Println("Register user with admin auth, but bad email format")
		assert.Equal(400, code, "Add user status should be 400")
	fmt.Println("Register user with admin auth, but bad user realname format")
		assert.Equal(400, code, "Add user status should be 400")
	fmt.Println("Register user with admin auth, but user comment length is illegal")
		assert.Equal(400, code, "Add user status should be 400")

	// case 10: register a new user with admin auth, expect 201
	fmt.Println("Register user with admin auth, right parameters")
		assert.Equal(201, code, "Add user status should be 201")
	// case 11: register duplicate user with admin auth, expect 409
	fmt.Println("Register duplicate user with admin auth")
		assert.Equal(409, code, "Add user status should be 409")
	// case 12: register a new user with admin auth, but duplicate email, expect 409
	fmt.Println("Register user with admin auth, but duplicate email")
		assert.Equal(409, code, "Add user status should be 409")

Fixed code:

	if !ua.IsAdmin && user.HasAdminRole {
		msg := "Non-admin cannot create an admin user."
		log.Errorf(msg)
		ua.SendForbiddenError(errors.New(msg))
		return
	}


	t.Log("case 1: Register user without admin auth")
		assert.Equal(400, code, "case 1: Add user status should be 400")
	t.Log("case 2: Register user with admin auth, but username is empty")
		assert.Equal(400, code, "case 2: Add user status should be 400")
	t.Log("case 3: Register user with admin auth, but bad username format")
		assert.Equal(400, code, "case 3: Add user status should be 400")
	t.Log("case 4: Register user with admin auth, but empty password.")
		assert.Equal(400, code, "case 4: Add user status should be 400")
	t.Log("case 5: Register user with admin auth, but email is empty")
		assert.Equal(400, code, "case 5: Add user status should be 400")
	t.Log("case 6: Register user with admin auth, but bad email format")
		assert.Equal(400, code, "case 6: Add user status should be 400")
	t.Log("case 8: Register user with admin auth, but bad user realname format")
		assert.Equal(400, code, "case 8: Add user status should be 400")
	t.Log("case 9: Register user with admin auth, but user comment length is illegal")
		assert.Equal(400, code, "case 9: Add user status should be 400")

	// case 10: register an admin using non-admin user, expect 403
	t.Log("case 10: Register admin user with non admin auth")
	testUser0002.HasAdminRole = true
	code, err = apiTest.UsersPost(testUser0002)
	if err != nil {
		t.Error("Error occurred while add a user", err.Error())
		t.Log(err)
	} else {
		assert.Equal(http.StatusForbidden, code, "case 10: Add user status should be 403")
	}
	testUser0002.HasAdminRole = false

	// case 11: register a new user with admin auth, expect 201
	t.Log("case 11: Register user with admin auth, right parameters")
		assert.Equal(201, code, "case 11: Add user status should be 201")
	// case 12: register duplicate user with admin auth, expect 409
	t.Log("case 12: Register duplicate user with admin auth")
		assert.Equal(409, code, "case 12: Add user status should be 409")
	// case 13: register a new user with admin auth, but duplicate email, expect 409
	t.Log("case 13: Register user with admin auth, but duplicate email")
		assert.Equal(409, code, "case 13: Add user status should be 409")