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

Fix: Unauthorized users can be allowed to view the private pipeline if the logged-in user's scmContext and the pipeline's scmContext are different (scm-github) #194

Merged
merged 1 commit into from
Nov 13, 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
5 changes: 5 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,11 @@ class GithubScm extends Scm {
privateRepo = scmRepo.privateRepo || false;
} else {
try {
if (scmHost !== this.config.gheHost) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is a bug if we're using open source (and gheHost is not set)

{"level":"error","message":"Failed to getPermissions:  Pipeline's scmHost github.com does not match with user's scmHost undefined","stack":"Error: Pipeline's scmHost github.com does not match with user's scmHost undefined\n    at GithubScm.lookupScmUri (/Users/tkyi/repos/opensource/screwdriver/node_modules/screwdriver-scm-github/index.js:231:27)\n    at GithubScm._getPermissions (/Users/tkyi/repos/opensource/screwdriver/node_modules/screwdriver-scm-github/index.js:865:40)\n    at /Users/tkyi/repos/opensource/screwdriver/node_modules/screwdriver-scm-base/index.js:355:25","timestamp":"2021-11-17T00:32:35.390Z"}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I will add a PR.

throw new Error(
`Pipeline's scmHost ${scmHost} does not match with user's scmHost ${this.config.gheHost}`
);
}
// https://github.com/octokit/rest.js/issues/163
const repo = await this.breaker.runCommand({
scopeType: 'request',
Expand Down
75 changes: 74 additions & 1 deletion test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ describe('index', function() {
oauthClientId: 'abcdefg',
oauthClientSecret: 'hijklmno',
secret: 'somesecret',
commentUserToken: 'sometoken'
commentUserToken: 'sometoken',
gheHost: 'github.com'
});
});

Expand Down Expand Up @@ -723,6 +724,33 @@ describe('index', function() {
}
);
});

it('rejects when scm settings is mismatch', () => {
const scmUriNotMatch = 'notmatching.com:23498:targetBranch';
const [scmHost] = scmUriNotMatch.split(':');
const loginContext = scm.getScmContexts();
const loginHost = loginContext[0].split(':')[1];

const testError = new Error(
`Pipeline's scmHost ${scmHost} does not match with user's scmHost ${loginHost}`
);

githubMock.request.rejects(testError);

return scm
.lookupScmUri({
scmUri: scmUriNotMatch,
token: 'sometoken'
})
.then(
() => {
assert.fail('This should not fail the test');
},
error => {
assert.strictEqual(error.message, testError.message);
}
);
});
});

describe('updateCommitStatus', () => {
Expand Down Expand Up @@ -1859,6 +1887,20 @@ jobs:
}
});

scm = new GithubScm({
fusebox: {
retry: {
minTimeout: 1
}
},
readOnly: {},
oauthClientId: 'abcdefg',
oauthClientSecret: 'hijklmno',
secret: 'somesecret',
commentUserToken: 'sometoken',
gheHost: 'internal-ghe.mycompany.com'
});

return scm
.decorateCommit({
scmUri,
Expand Down Expand Up @@ -1907,6 +1949,20 @@ jobs:
});
githubMock.users.getByUsername.resolves();

scm = new GithubScm({
fusebox: {
retry: {
minTimeout: 1
}
},
readOnly: {},
oauthClientId: 'abcdefg',
oauthClientSecret: 'hijklmno',
secret: 'somesecret',
commentUserToken: 'sometoken',
gheHost: 'internal-ghe.mycompany.com'
});

return scm
.decorateCommit({
scmUri,
Expand Down Expand Up @@ -1946,6 +2002,20 @@ jobs:

githubMock.repos.getCommit.rejects(testError);

scm = new GithubScm({
fusebox: {
retry: {
minTimeout: 1
}
},
readOnly: {},
oauthClientId: 'abcdefg',
oauthClientSecret: 'hijklmno',
secret: 'somesecret',
commentUserToken: 'sometoken',
gheHost: 'internal-ghe.mycompany.com'
});

return scm
.decorateCommit({
scmUri,
Expand Down Expand Up @@ -2119,6 +2189,9 @@ jobs:
'github:github.com': {
clientId: 'abcdefg',
clientSecret: 'hijklmno',
config: {
uri: 'https://github.com'
},
forceHttps: false,
isSecure: false,
provider: 'github',
Expand Down